diff options
author | 2022-08-13 18:16:32 -0700 | |
---|---|---|
committer | 2022-08-13 18:18:05 -0700 | |
commit | f90c09d7154f4b4ac3c9539739b33a445956cc33 (patch) | |
tree | 5d0bf1e09a1245a8d93d8f70d087de02b3f775ee | |
parent | 5644da1a3eff62ec4f5b7a72da36c1da390dc981 (diff) | |
download | bun-f90c09d7154f4b4ac3c9539739b33a445956cc33.tar.gz bun-f90c09d7154f4b4ac3c9539739b33a445956cc33.tar.zst bun-f90c09d7154f4b4ac3c9539739b33a445956cc33.zip |
wip SyntheticModule
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 41 | ||||
-rw-r--r-- | src/bun.js/bindings/exports.zig | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-handwritten.h | 5 | ||||
-rw-r--r-- | src/bun.js/bindings/headers.h | 8 | ||||
-rw-r--r-- | src/bun.js/bindings/sizes.zig | 29 | ||||
-rw-r--r-- | src/bun.js/headergen/sizegen.cpp | 7 | ||||
-rw-r--r-- | src/bun.js/javascript.zig | 21 | ||||
-rw-r--r-- | src/http/method.zig | 2 |
9 files changed, 91 insertions, 26 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 4136cce09..54f79af54 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -2460,6 +2460,28 @@ static JSC_DEFINE_HOST_FUNCTION(functionFulfillModuleSync, return JSValue::encode(JSC::jsUndefined()); } + if (res.result.value.tag == SyntheticModuleType::Buffer) { + auto generateSource = [](JSC::JSGlobalObject* lexicalGlobalObject, JSC::Identifier moduleKey, Vector<JSC::Identifier, 4>& exportNames, JSC::MarkedArgumentBuffer& exportValues) { + JSC::VM& vm = lexicalGlobalObject->vm(); + GlobalObject* globalObject = reinterpret_cast<GlobalObject*>(lexicalGlobalObject); + + exportNames.append(JSC::Identifier::fromString(vm, "Buffer"_s)); + exportValues.append(WebCore::JSBuffer::getConstructor(vm, globalObject)); + + exportNames.append(JSC::Identifier::fromString(vm, "Blob"_s)); + exportValues.append(globalObject->getDirect(vm, JSC::Identifier::fromString(vm, "Blob"_s))); + }; + + auto source = JSC::SourceCode( + JSC::SyntheticSourceProvider::create( + WTFMove(generateSource), + JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath("node:buffer"_s)), WTFMove(moduleKey))); + + globalObject->moduleLoader()->provideFetch(globalObject, key, WTFMove(source)); + RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); + RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsUndefined())); + } + auto provider = Zig::SourceProvider::create(res.result.value); globalObject->moduleLoader()->provideFetch(globalObject, key, JSC::SourceCode(provider)); RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); @@ -2482,6 +2504,7 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderFetch(JSGlobalObject* globalOb auto moduleKey = key.toWTFString(globalObject); RETURN_IF_EXCEPTION(scope, promise->rejectWithCaughtException(globalObject, scope)); + if (moduleKey.endsWith(".node"_s)) { return rejectWithError(createTypeError(globalObject, "To load Node-API modules, use require() or process.dlopen instead of import."_s)); } @@ -2515,6 +2538,24 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderFetch(JSGlobalObject* globalOb globalObject->vm().drainMicrotasks(); return promise; + } else if (res.result.value.tag == 1024) { + auto generateSource = [](JSC::JSGlobalObject* lexicalGlobalObject, JSC::Identifier moduleKey, Vector<JSC::Identifier, 4>& exportNames, JSC::MarkedArgumentBuffer& exportValues) { + JSC::VM& vm = lexicalGlobalObject->vm(); + GlobalObject* globalObject = reinterpret_cast<GlobalObject*>(lexicalGlobalObject); + exportNames.append(JSC::Identifier::fromString(globalObject->vm(), "Buffer"_s)); + exportValues.append(WebCore::JSBuffer::getConstructor(vm, globalObject)); + }; + + auto source = JSC::SourceCode( + JSC::SyntheticSourceProvider::create(WTFMove(generateSource), + JSC::SourceOrigin(), WTFMove(moduleKey))); + + auto sourceCode = JSSourceCode::create(vm, WTFMove(source)); + RETURN_IF_EXCEPTION(scope, promise->rejectWithCaughtException(globalObject, scope)); + + promise->resolve(globalObject, sourceCode); + scope.release(); + return promise; } else { auto provider = Zig::SourceProvider::create(res.result.value); auto jsSourceCode = JSC::JSSourceCode::create(vm, JSC::SourceCode(provider)); diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index a6439a263..e824c877f 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -243,6 +243,8 @@ pub const ResolvedSource = extern struct { pub const Tag = enum(u64) { javascript = 0, wasm = 1, + + @"node:buffer" = 1024, }; }; diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h index e19a15c6d..bef2b16e4 100644 --- a/src/bun.js/bindings/headers-cpp.h +++ b/src/bun.js/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1660348299 +//-- AUTOGENERATED FILE -- 1660434151 // clang-format off #pragma once diff --git a/src/bun.js/bindings/headers-handwritten.h b/src/bun.js/bindings/headers-handwritten.h index 3d5523a64..32133c798 100644 --- a/src/bun.js/bindings/headers-handwritten.h +++ b/src/bun.js/bindings/headers-handwritten.h @@ -174,6 +174,11 @@ typedef struct { bool shared; } Bun__ArrayBuffer; +enum SyntheticModuleType : uint64_t { + Buffer = 1024, + Process = 1025, +}; + extern "C" ZigErrorCode Zig_ErrorCodeParserError; extern "C" void ZigString__free(const unsigned char* ptr, size_t len, void* allocator); diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h index 68152ae15..85781699a 100644 --- a/src/bun.js/bindings/headers.h +++ b/src/bun.js/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format off -//-- AUTOGENERATED FILE -- 1660348299 +//-- AUTOGENERATED FILE -- 1660434151 #pragma once #include <stddef.h> @@ -30,7 +30,7 @@ typedef void* JSClassRef; typedef char* bJSC__SourceCode_buf; typedef struct bWTF__URL { unsigned char bytes[40]; } bWTF__URL; typedef char* bWTF__URL_buf; - typedef struct bJSC__JSModuleRecord { unsigned char bytes[216]; } bJSC__JSModuleRecord; + typedef struct bJSC__JSModuleRecord { unsigned char bytes[208]; } bJSC__JSModuleRecord; typedef char* bJSC__JSModuleRecord_buf; typedef struct bJSC__ThrowScope { unsigned char bytes[8]; } bJSC__ThrowScope; typedef char* bJSC__ThrowScope_buf; @@ -38,7 +38,7 @@ typedef void* JSClassRef; typedef char* bJSC__PropertyName_buf; typedef struct bJSC__JSFunction { unsigned char bytes[32]; } bJSC__JSFunction; typedef char* bJSC__JSFunction_buf; - typedef struct bJSC__JSGlobalObject { unsigned char bytes[2312]; } bJSC__JSGlobalObject; + typedef struct bJSC__JSGlobalObject { unsigned char bytes[2840]; } bJSC__JSGlobalObject; typedef char* bJSC__JSGlobalObject_buf; typedef struct bJSC__JSCell { unsigned char bytes[8]; } bJSC__JSCell; typedef char* bJSC__JSCell_buf; @@ -54,7 +54,7 @@ typedef void* JSClassRef; typedef char* bInspector__ScriptArguments_buf; typedef struct bJSC__Exception { unsigned char bytes[40]; } bJSC__Exception; typedef char* bJSC__Exception_buf; - typedef struct bJSC__VM { unsigned char bytes[52168]; } bJSC__VM; + typedef struct bJSC__VM { unsigned char bytes[52008]; } bJSC__VM; typedef char* bJSC__VM_buf; typedef struct bJSC__JSString { unsigned char bytes[16]; } bJSC__JSString; typedef char* bJSC__JSString_buf; diff --git a/src/bun.js/bindings/sizes.zig b/src/bun.js/bindings/sizes.zig index cebd07d7d..890d46ee4 100644 --- a/src/bun.js/bindings/sizes.zig +++ b/src/bun.js/bindings/sizes.zig @@ -1,4 +1,4 @@ -// Auto-generated by src/bun.js/headergen/sizegen.cpp at 2022-05-02 01:43:1651481039. +// Auto-generated by src/bun.js/headergen/sizegen.cpp at 2022-08-13 16:15:1660432547. // These are the byte sizes for the different object types with bindings in JavaScriptCore. // This allows us to safely return stack allocated C++ types to Zig. // It is only safe to do this when these sizes are correct. @@ -6,13 +6,13 @@ // 1. We can't dynamically link JavaScriptCore // 2. It's important that this is run whenever JavaScriptCore is updated or the bindings on the Zig side change. // Failure to do so will lead to undefined behavior and probably some frustrated people. -// --- Regenerate this: --- +// --- Regenerate this: --- // 1. "make headers" // 2. "make sizegen" // 3. "make headers" // ------------------------ // You can verify the numbers written in this file at runtime via the `extern`d types -// Run "headers" twice because it uses these values in the output. That's how all the bJSC__.* types are created - from these values. +// Run "headers" twice because it uses these values in the output. That's how all the bJSC__.* types are created - from these values. pub const JSC__JSObject = 16; pub const JSC__JSObject_align = 8; pub const WebCore__DOMURL = 112; @@ -29,7 +29,7 @@ pub const Inspector__ScriptArguments = 32; pub const Inspector__ScriptArguments_align = 8; pub const JSC__JSModuleLoader = 16; pub const JSC__JSModuleLoader_align = 8; -pub const JSC__JSModuleRecord = 216; +pub const JSC__JSModuleRecord = 208; pub const JSC__JSModuleRecord_align = 8; pub const JSC__JSPromise = 32; pub const JSC__JSPromise_align = 8; @@ -41,7 +41,7 @@ pub const JSC__SourceCode = 24; pub const JSC__SourceCode_align = 8; pub const JSC__JSFunction = 32; pub const JSC__JSFunction_align = 8; -pub const JSC__JSGlobalObject = 2312; +pub const JSC__JSGlobalObject = 2840; pub const JSC__JSGlobalObject_align = 8; pub const WTF__URL = 40; pub const WTF__URL_align = 8; @@ -53,14 +53,12 @@ pub const JSC__PropertyName = 8; pub const JSC__PropertyName_align = 8; pub const JSC__Exception = 40; pub const JSC__Exception_align = 8; -pub const JSC__VM = 52168; +pub const JSC__VM = 52008; pub const JSC__VM_align = 8; pub const JSC__ThrowScope = 8; pub const JSC__ThrowScope_align = 8; pub const JSC__CatchScope = 8; pub const JSC__CatchScope_align = 8; -pub const JSC__CallFrame = 8; -pub const JSC__CallFrame_align = 8; pub const JSC__Identifier = 8; pub const JSC__Identifier_align = 8; pub const WTF__StringImpl = 24; @@ -69,12 +67,13 @@ pub const WTF__ExternalStringImpl = 40; pub const WTF__ExternalStringImpl_align = 8; pub const WTF__StringView = 16; pub const WTF__StringView_align = 8; -pub const Zig__GlobalObject = 2384; +pub const Zig__GlobalObject = 4672; pub const Zig__GlobalObject_align = 8; -pub const Bun__Readable = 24; -pub const Bun__Readable_align = 4; -pub const Bun__Writable = 20; -pub const Bun__Writable_align = 4; -pub const Bun__Path = 8; -pub const Bun__Path_align = 8; +pub const ArrayBufferSink = 1; +pub const ArrayBufferSink_align = 1; +pub const HTTPSResponseSink = 1; +pub const HTTPSResponseSink_align = 1; +pub const HTTPResponseSink = 1; +pub const HTTPResponseSink_align = 1; pub const Bun_FFI_PointerOffsetToArgumentsList = 6; +pub const Bun_FFI_PointerOffsetToTypedArrayVector = 16; diff --git a/src/bun.js/headergen/sizegen.cpp b/src/bun.js/headergen/sizegen.cpp index 316caabc6..721f8e652 100644 --- a/src/bun.js/headergen/sizegen.cpp +++ b/src/bun.js/headergen/sizegen.cpp @@ -14,6 +14,7 @@ using namespace std; #include "headers-cpp.h" #include "JavaScriptCore/CallFrame.h" +#include "JavaScriptCore/JSArrayBufferViewInlines.h" int main() { time_t rawtime; @@ -49,12 +50,14 @@ int main() { "in the output. That's how all the bJSC__.* types are created - from " "these values. \n"; int i = 0; - int len = sizeof(sizes) / sizeof(sizes[0]); + int len = 31; for (i = 0; i < len; i++) { cout << "pub const " << names[i] << " = " << sizes[i] << ";\n"; cout << "pub const " << names[i] << "_align = " << aligns[i] << ";\n"; } - cout << "pub const Bun_FFI_PointerOffsetToArgumentsList = << " + cout << "pub const Bun_FFI_PointerOffsetToArgumentsList = " << JSC::CallFrame::argumentOffset(0) << ";\n"; + cout << "pub const Bun_FFI_PointerOffsetToTypedArrayVector = " + << JSC::JSArrayBufferView::offsetOfVector() << ";\n"; return 0; }
\ No newline at end of file diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index ee0e70bd7..71394bfbf 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -807,6 +807,16 @@ pub const VirtualMachine = struct { .hash = 0, }; }, + .@"node:buffer" => { + return ResolvedSource{ + .allocator = null, + .source_code = ZigString.init(""), + .specifier = ZigString.init("node:buffer"), + .source_url = ZigString.init("node:buffer"), + .hash = 0, + .tag = ResolvedSource.Tag.@"node:buffer", + }; + }, .@"node:fs/promises" => { return ResolvedSource{ .allocator = null, @@ -2731,9 +2741,10 @@ pub const HardcodedModule = enum { @"bun:sqlite", @"depd", @"detect-libc", + @"node:buffer", @"node:fs", - @"node:http", @"node:fs/promises", + @"node:http", @"node:module", @"node:path", @"node:perf_hooks", @@ -2758,9 +2769,10 @@ pub const HardcodedModule = enum { .{ "fs", HardcodedModule.@"node:fs" }, .{ "http", HardcodedModule.@"node:http" }, .{ "module", HardcodedModule.@"node:module" }, + .{ "node:buffer", HardcodedModule.@"node:buffer" }, .{ "node:fs", HardcodedModule.@"node:fs" }, - .{ "node:http", HardcodedModule.@"node:http" }, .{ "node:fs/promises", HardcodedModule.@"node:fs/promises" }, + .{ "node:http", HardcodedModule.@"node:http" }, .{ "node:module", HardcodedModule.@"node:module" }, .{ "node:path", HardcodedModule.@"node:path" }, .{ "node:path/posix", HardcodedModule.@"node:path" }, @@ -2789,12 +2801,13 @@ pub const HardcodedModule = enum { .{ "detect-libc/lib/detect-libc.js", "detect-libc" }, .{ "ffi", "bun:ffi" }, .{ "fs", "node:fs" }, - .{ "http", "node:http" }, .{ "fs/promises", "node:fs/promises" }, + .{ "http", "node:http" }, .{ "module", "node:module" }, + .{ "node:buffer", "node:buffer" }, .{ "node:fs", "node:fs" }, - .{ "node:http", "node:http" }, .{ "node:fs/promises", "node:fs/promises" }, + .{ "node:http", "node:http" }, .{ "node:module", "node:module" }, .{ "node:path", "node:path" }, .{ "node:path/posix", "node:path" }, diff --git a/src/http/method.zig b/src/http/method.zig index 62ea3ce7d..4f6c7827e 100644 --- a/src/http/method.zig +++ b/src/http/method.zig @@ -25,6 +25,8 @@ pub const Method = enum { var values = std.enums.EnumSet(Method).initFull(); values.remove(.HEAD); values.remove(.TRACE); + values.remove(.OPTIONS); + values.remove(.GET); break :brk values; }; |