Add a way to update cached values from Zig

This commit is contained in:
Jarred Sumner
2022-10-15 02:57:28 -07:00
parent dd84681d30
commit 35cbfa63a6

View File

@@ -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
? `