diff options
Diffstat (limited to 'src/bun.js/scripts/generate-classes.ts')
-rw-r--r-- | src/bun.js/scripts/generate-classes.ts | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/src/bun.js/scripts/generate-classes.ts b/src/bun.js/scripts/generate-classes.ts index ec0021637..0b88a3eb1 100644 --- a/src/bun.js/scripts/generate-classes.ts +++ b/src/bun.js/scripts/generate-classes.ts @@ -739,7 +739,7 @@ JSC_DEFINE_CUSTOM_GETTER(js${typeName}Constructor, (JSGlobalObject * lexicalGlob auto* prototype = jsDynamicCast<${prototypeName(typeName)}*>(JSValue::decode(thisValue)); if (UNLIKELY(!prototype)) - return throwVMTypeError(lexicalGlobalObject, throwScope); + return throwVMTypeError(lexicalGlobalObject, throwScope, "Cannot get constructor for ${typeName}"_s); return JSValue::encode(globalObject->${className(typeName)}Constructor()); } @@ -832,7 +832,8 @@ JSC_DEFINE_CUSTOM_SETTER(${symbolName( if (UNLIKELY(!thisObject)) { auto throwScope = DECLARE_THROW_SCOPE(vm); - return throwVMTypeError(lexicalGlobalObject, throwScope); + throwVMTypeError(lexicalGlobalObject, throwScope, "Expected 'this' to be instanceof ${typeName}"_s); + return JSValue::encode({}); } JSC::EnsureStillAliveScope thisArg = JSC::EnsureStillAliveScope(thisObject); @@ -910,6 +911,11 @@ function generateClassHeader(typeName, obj: ClassDefinition) { } `; } + var suffix = ""; + + if (obj.getInternalProperties) { + suffix += `JSC::JSValue getInternalProperties(JSC::VM &vm, JSC::JSGlobalObject *globalObject, ${name}*);`; + } return ` class ${name} final : public JSC::JSDestructibleObject { @@ -992,11 +998,20 @@ function generateClassHeader(typeName, obj: ClassDefinition) { ${renderCachedFieldsHeader(typeName, klass, proto, values)} }; + ${suffix} `; } function generateClassImpl(typeName, obj: ClassDefinition) { - const { klass: fields, finalize, proto, construct, estimatedSize, hasPendingActivity = false } = obj; + const { + klass: fields, + finalize, + proto, + construct, + estimatedSize, + hasPendingActivity = false, + getInternalProperties = false, + } = obj; const name = className(typeName); const DEFINE_VISIT_CHILDREN_LIST = [...Object.entries(fields), ...Object.entries(proto)] @@ -1076,6 +1091,24 @@ DEFINE_VISIT_OUTPUT_CONSTRAINTS(${name}); `; } + if (getInternalProperties) { + output += ` + extern "C" EncodedJSValue ${symbolName( + typeName, + "getInternalProperties", + )}(void* ptr, JSC::JSGlobalObject *globalObject, EncodedJSValue thisValue); + + JSC::JSValue getInternalProperties(JSC::VM &, JSC::JSGlobalObject *globalObject, ${name}* castedThis) + { + return JSValue::decode(${symbolName( + typeName, + "getInternalProperties", + )}(castedThis->impl(), globalObject, JSValue::encode(castedThis))); + } + + `; + } + if (finalize) { output += ` ${name}::~${name}() @@ -1220,6 +1253,7 @@ function generateZig( values = [], hasPendingActivity = false, structuredClone = false, + getInternalProperties = false, } = {} as ClassDefinition, ) { const exports = new Map<string, string>(); @@ -1246,6 +1280,10 @@ function generateZig( Object.values(klass).map(a => appendSymbols(exports, name => classSymbolName(typeName, name), a)); Object.values(proto).map(a => appendSymbols(exports, name => protoSymbolName(typeName, name), a)); + if (getInternalProperties) { + exports.set("getInternalProperties", symbolName(typeName, "getInternalProperties")); + } + if (structuredClone) { exports.set("onStructuredCloneSerialize", symbolName(typeName, "onStructuredCloneSerialize")); @@ -1306,6 +1344,14 @@ function generateZig( } `; + if (getInternalProperties) { + output += ` + if (@TypeOf(${typeName}.getInternalProperties) != (fn(*${typeName}, globalThis: *JSC.JSGlobalObject, JSC.JSValue thisValue) callconv(.C) JSC.JSValue { + @compileLog("${typeName}.getInternalProperties is not a getInternalProperties function"); + } + `; + } + if (structuredClone === "transferable") { exports.set("structuredClone", symbolName(typeName, "onTransferableStructuredClone")); output += ` |