diff options
author | 2022-10-15 02:57:28 -0700 | |
---|---|---|
committer | 2022-10-15 02:57:28 -0700 | |
commit | 35cbfa63a6e54d37ad5edb14ef2c025acf7bbddd (patch) | |
tree | c168fe367766f1e5489647ed024f13401b8a44e8 /src/bun.js/scripts/generate-classes.ts | |
parent | dd84681d30b11f57fd090c6787f233666fda277a (diff) | |
download | bun-35cbfa63a6e54d37ad5edb14ef2c025acf7bbddd.tar.gz bun-35cbfa63a6e54d37ad5edb14ef2c025acf7bbddd.tar.zst bun-35cbfa63a6e54d37ad5edb14ef2c025acf7bbddd.zip |
Add a way to update cached values from Zig
Diffstat (limited to 'src/bun.js/scripts/generate-classes.ts')
-rw-r--r-- | src/bun.js/scripts/generate-classes.ts | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/src/bun.js/scripts/generate-classes.ts b/src/bun.js/scripts/generate-classes.ts index 2fe053441..30696dd45 100644 --- a/src/bun.js/scripts/generate-classes.ts +++ b/src/bun.js/scripts/generate-classes.ts @@ -40,7 +40,7 @@ function appendSymbols( symbolName: (name: string) => string, prop ) { - var { defaultValue, getter, setter, accesosr, fn } = prop; + var { defaultValue, getter, setter, accesosr, fn, cache } = prop; if (accesosr) { getter = accesosr.getter; @@ -612,6 +612,17 @@ JSC_DEFINE_CUSTOM_GETTER(${symbolName( thisObject->${cacheName}.set(vm, thisObject, result); RELEASE_AND_RETURN(throwScope, JSValue::encode(result)); } +extern "C" void ${symbolName( + typeName, + name + )}SetCachedValue(JSC::EncodedJSValue thisValue, JSC::JSGlobalObject *globalObject, JSC::EncodedJSValue value) +{ + auto& vm = globalObject->vm(); + auto* thisObject = jsCast<${className( + typeName + )}*>(JSValue::decode(thisValue)); + thisObject->${cacheName}.set(vm, thisObject, JSValue::decode(value)); +} `); } else if ( "getter" in proto[name] || @@ -964,6 +975,27 @@ function generateZig( appendSymbols(exports, (name) => protoSymbolName(typeName, name), a) ); + const externs = Object.entries(proto) + .filter(([name, { cache }]) => cache && typeof cache !== "string") + .map( + ([name, { cache }]) => + `extern fn ${protoSymbolName( + typeName, + name + )}SetCachedValue(JSC.JSValue, *JSC.JSGlobalObject, JSC.JSValue) void; + + /// Set the cached value for ${name} on ${typeName} + /// This value will be visited by the garbage collector. + pub fn ${name}SetCached(thisValue: JSC.JSValue, globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) void { + ${protoSymbolName( + typeName, + name + )}SetCachedValue(thisValue, globalObject, value); + } +`.trim() + "\n" + ) + .join("\n"); + function typeCheck() { var output = ""; @@ -991,37 +1023,39 @@ function generateZig( `; } - [...Object.values(proto)].forEach(({ getter, setter, accessor, fn }) => { - if (accessor) { - getter = accessor.getter; - setter = accessor.setter; - } + [...Object.values(proto)].forEach( + ({ getter, setter, accessor, fn, cache }) => { + if (accessor) { + getter = accessor.getter; + setter = accessor.setter; + } - if (getter) { - output += ` + if (getter) { + output += ` if (@TypeOf(${typeName}.${getter}) != GetterType) @compileLog( "Expected ${typeName}.${getter} to be a getter" ); `; - } + } - if (setter) { - output += ` + if (setter) { + output += ` if (@TypeOf(${typeName}.${setter}) != SetterType) @compileLog( "Expected ${typeName}.${setter} to be a setter" );`; - } + } - if (fn) { - output += ` + if (fn) { + output += ` if (@TypeOf(${typeName}.${fn}) != CallbackType) @compileLog( "Expected ${typeName}.${fn} to be a callback" );`; + } } - }); + ); [...Object.values(klass)].forEach(({ getter, setter, accessor, fn }) => { if (accessor) { @@ -1073,6 +1107,8 @@ pub const ${className(typeName)} = struct { return ${symbolName(typeName, "fromJS")}(value); } + ${externs} + ${ !noConstructor ? ` |