Bun.allocUnsafe

This commit is contained in:
Jarred Sumner
2022-03-19 07:47:30 -07:00
parent be99967d67
commit 8313f19eb8
3 changed files with 43 additions and 1 deletions

View File

@@ -938,6 +938,10 @@ pub const Class = NewClass(
.rfn = Bun.runGC,
.ts = d.ts{},
},
.allocUnsafe = .{
.rfn = Bun.allocUnsafe,
.ts = .{},
},
.generateHeapSnapshot = .{
.rfn = Bun.generateHeapSnapshot,
.ts = d.ts{},
@@ -979,6 +983,7 @@ pub const Class = NewClass(
.env = .{
.get = EnvironmentVariables.getter,
},
.enableANSIColors = .{
.get = enableANSIColors,
},
@@ -996,6 +1001,35 @@ pub const Class = NewClass(
},
);
pub fn allocUnsafe(
_: void,
ctx: js.JSContextRef,
_: js.JSObjectRef,
_: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSValueRef {
var args = JSC.Node.ArgumentsSlice.from(arguments);
const length = @intCast(
usize,
@minimum(
@maximum(1, (args.nextEat() orelse JSC.JSValue.jsNumber(@as(i32, 1))).toInt32()),
std.math.maxInt(i32),
),
);
var bytes = bun.default_allocator.alloc(u8, length) catch {
JSC.JSError(bun.default_allocator, "OOM! Out of memory", .{}, ctx, exception);
return null;
};
return JSC.MarkedArrayBuffer.fromBytes(
bytes,
bun.default_allocator,
.Uint8Array,
).toJSObjectRef(ctx, null);
}
pub fn getTranspilerConstructor(
_: void,
ctx: js.JSContextRef,

View File

@@ -2168,7 +2168,7 @@ const Arguments = struct {
};
};
const Constants = struct {
pub const Constants = struct {
// File Access Constants
/// Constant for fs.access(). File is visible to the calling process.
pub const F_OK = std.os.F_OK;