Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
81de8147d7 [internal] Start to make ZigString support either UTF-16 or LATIN1 instead of assuming latin1 2021-10-31 17:16:25 -07:00
5 changed files with 97 additions and 26 deletions

View File

@@ -1564,6 +1564,7 @@ pub const JSPrivateDataPtr = TaggedPointerUnion(.{
JSNode,
LazyPropertiesObject,
ModuleNamespace,
TextEncoder,
});
pub inline fn GetJSPrivateData(comptime Type: type, ref: js.JSObjectRef) ?*Type {

View File

@@ -101,10 +101,10 @@ extern "C" void JSCInitialize() {
extern "C" JSC__JSGlobalObject *Zig__GlobalObject__create(JSClassRef *globalObjectClass, int count,
void *console_client) {
auto heapSize = JSC::LargeHeap;
JSC::VM &vm = JSC::VM::create(heapSize).leakRef();
vm.heap.acquireAccess();
#if ENABLE(WEBASSEMBLY)
JSC::Wasm::enableFastMemory();
#endif

View File

@@ -81,9 +81,15 @@ pub const JSObject = extern struct {
};
};
pub const Encoding = enum(u8) {
latin1 = 0,
utf16 = 1,
};
pub const ZigString = extern struct {
ptr: [*]const u8,
len: usize,
ptr: ?*const c_void,
len: u32,
encoding: Encoding,
pub const shim = Shimmer("", "ZigString", @This());
pub const name = "ZigString";
@@ -97,7 +103,7 @@ pub const ZigString = extern struct {
}
pub fn init(slice_: []const u8) ZigString {
return ZigString{ .ptr = slice_.ptr, .len = slice_.len };
return ZigString{ .ptr = slice_.ptr, .len = @truncate(u32, slice_.len), .encoding = Encoding.latin1 };
}
pub inline fn toRef(slice_: []const u8, global: *JSGlobalObject) C_API.JSValueRef {
@@ -115,7 +121,7 @@ pub const ZigString = extern struct {
}
pub fn trimmedSlice(this: *const ZigString) []const u8 {
return std.mem.trim(u8, this.ptr[0..std.math.min(this.len, 4096)], " \r\n");
return std.mem.trim(u8, @as([*]const u8, this.ptr)[0..std.math.min(this.len, 4096)], " \r\n");
}
pub fn toValue(this: ZigString, global: *JSGlobalObject) JSValue {

View File

@@ -754,6 +754,7 @@ pub const ZigConsoleClient = struct {
Boolean,
const CellType = CAPI.CellType;
threadlocal var name_buf: [512]u8 = undefined;
pub fn format(comptime Writer: type, writer: Writer, value: JSValue, globalThis: *JSGlobalObject, comptime enable_ansi_colors: bool) anyerror!void {
if (comptime @hasDecl(@import("root"), "bindgen")) {
return;
@@ -816,7 +817,6 @@ pub const ZigConsoleClient = struct {
value.getNameProperty(globalThis, &printable);
try writer.print("[Function {s}]", .{printable.slice()});
} else {
var str = value.toWTFString(JS.VirtualMachine.vm.global);
_ = try writer.write(str.slice());
}
}

View File

@@ -42,6 +42,7 @@ pub const GlobalClasses = [_]type{
Fetch.Class,
js_ast.Macro.JSNode.BunJSXCallbackFunction,
Performance.Class,
TextEncoder.Class,
// The last item in this array becomes "process.env"
Bun.EnvironmentVariables.Class,
@@ -566,26 +567,34 @@ pub const Bun = struct {
},
},
},
.{ .main = .{
.get = getMain,
.ts = d.ts{ .name = "main", .@"return" = "string" },
}, .cwd = .{
.get = getCWD,
.ts = d.ts{ .name = "cwd", .@"return" = "string" },
}, .origin = .{
.get = getOrigin,
.ts = d.ts{ .name = "origin", .@"return" = "string" },
}, .routesDir = .{
.get = getRoutesDir,
.ts = d.ts{ .name = "routesDir", .@"return" = "string" },
}, .assetPrefix = .{
.get = getAssetPrefix,
.ts = d.ts{ .name = "assetPrefix", .@"return" = "string" },
}, .env = .{
.get = EnvironmentVariables.getter,
}, .enableANSIColors = .{
.get = enableANSIColors,
} },
.{
.main = .{
.get = getMain,
.ts = d.ts{ .name = "main", .@"return" = "string" },
},
.cwd = .{
.get = getCWD,
.ts = d.ts{ .name = "cwd", .@"return" = "string" },
},
.origin = .{
.get = getOrigin,
.ts = d.ts{ .name = "origin", .@"return" = "string" },
},
.routesDir = .{
.get = getRoutesDir,
.ts = d.ts{ .name = "routesDir", .@"return" = "string" },
},
.assetPrefix = .{
.get = getAssetPrefix,
.ts = d.ts{ .name = "assetPrefix", .@"return" = "string" },
},
.env = .{
.get = EnvironmentVariables.getter,
},
.enableANSIColors = .{
.get = enableANSIColors,
},
},
);
/// EnvironmentVariables is runtime defined.
@@ -1979,6 +1988,61 @@ pub const EventListenerMixin = struct {
}
};
pub const TextEncoder = struct {
not_a_zero_bit_type: bool = true,
pub const Class = NewClass(
TextEncoder,
.{
.name = "TextEncoder",
.read_only = true,
},
.{
.constructor = .{
.rfn = TextEncoder.constructor,
.ts = d.ts{},
},
// .encode = .{
// .rfn = TextEncoder.encode,
// .ts = d.ts{},
// },
// .encodeInto = .{
// .rfn = TextEncoder.encodeInto,
// .ts = d.ts{},
// },
},
.{},
);
pub fn constructor(
ctx: js.JSContextRef,
function: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSObjectRef {
var text_encoder = getAllocator(ctx).create(TextEncoder) catch unreachable;
text_encoder.* = TextEncoder{};
return Class.make(ctx, text_encoder);
}
// pub fn encode(
// this: *TextEncoder,
// ctx: js.JSContextRef,
// function: js.JSObjectRef,
// arguments: []const js.JSValueRef,
// exception: js.ExceptionRef,
// ) js.JSObjectRef {}
// pub fn encodeInto(
// this: *TextEncoder,
// ctx: js.JSContextRef,
// function: js.JSObjectRef,
// arguments: []const js.JSValueRef,
// exception: js.ExceptionRef,
// ) js.JSObjectRef {}
};
pub const ResolveError = struct {
msg: logger.Msg,
allocator: *std.mem.Allocator,