Add JSArrayIterator, don't call JSC::Options after JSC already loaded once (that causes a crash)

This commit is contained in:
Jarred Sumner
2021-09-26 20:04:26 -07:00
parent 018ba2c83b
commit 002d46d0c4
2 changed files with 41 additions and 9 deletions

View File

@@ -80,17 +80,21 @@ using JSObject = JSC::JSObject;
using JSNonFinalObject = JSC::JSNonFinalObject;
namespace JSCastingHelpers = JSC::JSCastingHelpers;
bool has_loaded_jsc = false;
extern "C" JSC__JSGlobalObject *Zig__GlobalObject__create(JSClassRef *globalObjectClass, int count,
void *console_client) {
JSC::Options::useSourceProviderCache() = true;
JSC::Options::useUnlinkedCodeBlockJettisoning() = false;
// JSC::Options::useTopLevelAwait() = true;
JSC::Options::exposeInternalModuleLoader() = true;
std::set_terminate([]() { Zig__GlobalObject__onCrash(); });
WTF::initializeMainThread();
JSC::initialize();
if (!has_loaded_jsc) {
JSC::Options::useSourceProviderCache() = true;
JSC::Options::useUnlinkedCodeBlockJettisoning() = false;
// JSC::Options::useTopLevelAwait() = true;
JSC::Options::exposeInternalModuleLoader() = true;
std::set_terminate([]() { Zig__GlobalObject__onCrash(); });
WTF::initializeMainThread();
JSC::initialize();
has_loaded_jsc = true;
}
// JSC::Options::useCodeCache() = false;
@@ -104,7 +108,7 @@ extern "C" JSC__JSGlobalObject *Zig__GlobalObject__create(JSClassRef *globalObje
JSC::JSLockHolder locker(vm);
Zig::GlobalObject *globalObject =
Zig::GlobalObject::create(vm, Zig::GlobalObject::createStructure(vm, JSC::jsNull()));
Zig::GlobalObject::create(vm, Zig::GlobalObject::createStructure(vm, JSC::jsNull()));
globalObject->setConsole(globalObject);
if (count > 0) { globalObject->installAPIGlobals(globalObjectClass, count); }

View File

@@ -100,6 +100,10 @@ pub const ZigString = extern struct {
return ZigString{ .ptr = slice_.ptr, .len = slice_.len };
}
pub inline fn toRef(slice_: []const u8, global: *JSGlobalObject) C_API.JSValueRef {
return init(slice_).toValue(global).asRef();
}
pub const Empty = ZigString{ .ptr = "", .len = 0 };
pub fn slice(this: *const ZigString) []const u8 {
@@ -1076,6 +1080,26 @@ pub const URL = extern struct {
pub const Extern = [_][]const u8{ "fromFileSystemPath", "fromString", "isEmpty", "isValid", "protocol", "encodedUser", "encodedPassword", "host", "path", "lastPathComponent", "query", "fragmentIdentifier", "queryWithLeadingQuestionMark", "fragmentIdentifierWithLeadingNumberSign", "stringWithoutQueryOrFragmentIdentifier", "stringWithoutFragmentIdentifier", "protocolHostAndPort", "hostAndPort", "user", "password", "fileSystemPath", "setProtocol", "setHost", "setHostAndPort", "setUser", "setPassword", "setPath", "setQuery", "truncatedForUseAsBase" };
};
pub const JSArrayIterator = struct {
i: u32 = 0,
len: u32 = 0,
array: JSValue,
global: *JSGlobalObject,
pub fn init(value: JSValue, global: *JSGlobalObject) JSArrayIterator {
return .{ .array = value, .global = global, .len = value.getLengthOfArray(global) };
}
pub fn next(this: *JSArrayIterator) ?JSValue {
if (this.i >= this.len) {
return null;
}
const i = this.i;
this.i += 1;
return JSObject.getIndex(this.array, this.global, i);
}
};
pub const String = extern struct {
pub const shim = Shimmer("WTF", "String", @This());
bytes: shim.Bytes,
@@ -1226,6 +1250,10 @@ pub const JSValue = enum(i64) {
});
}
pub inline fn arrayIterator(this: JSValue, global: *JSGlobalObject) JSArrayIterator {
return JSArrayIterator.init(this, global);
}
pub fn jsNumberFromDouble(i: f64) JSValue {
return cppFn("jsNumberFromDouble", .{i});
}