diff options
-rw-r--r-- | src/bun.js/module_loader.zig | 3 | ||||
-rw-r--r-- | src/bun.js/modules/BunObjectModule.cpp | 24 | ||||
-rw-r--r-- | src/bun.js/modules/BunObjectModule.h | 8 | ||||
-rw-r--r-- | src/bun.js/modules/_NativeModule.h | 1 | ||||
-rw-r--r-- | src/js/out/NativeModuleImpl.h | 1 | ||||
-rw-r--r-- | src/js/out/ResolvedSourceTag.zig | 19 | ||||
-rw-r--r-- | src/js/out/SyntheticModuleType.h | 19 | ||||
-rw-r--r-- | test/js/bun/resolve/import-meta.test.js | 4 |
8 files changed, 61 insertions, 18 deletions
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index 765cf70a6..9e2af7e9f 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -2038,6 +2038,7 @@ pub const ModuleLoader = struct { }, // Native modules + .bun => return jsSyntheticModule(.bun, specifier), .@"node:buffer" => return jsSyntheticModule(.@"node:buffer", specifier), .@"node:string_decoder" => return jsSyntheticModule(.@"node:string_decoder", specifier), .@"node:module" => return jsSyntheticModule(.@"node:module", specifier), @@ -2217,6 +2218,7 @@ pub const FetchFlags = enum { const SavedSourceMap = JSC.SavedSourceMap; pub const HardcodedModule = enum { + bun, @"bun:ffi", @"bun:jsc", @"bun:main", @@ -2290,6 +2292,7 @@ pub const HardcodedModule = enum { HardcodedModule, .{ .{ "buffer", HardcodedModule.@"node:buffer" }, + .{ "bun", HardcodedModule.bun }, .{ "bun:ffi", HardcodedModule.@"bun:ffi" }, .{ "bun:jsc", HardcodedModule.@"bun:jsc" }, .{ "bun:main", HardcodedModule.@"bun:main" }, diff --git a/src/bun.js/modules/BunObjectModule.cpp b/src/bun.js/modules/BunObjectModule.cpp new file mode 100644 index 000000000..55f694fa1 --- /dev/null +++ b/src/bun.js/modules/BunObjectModule.cpp @@ -0,0 +1,24 @@ +#include "root.h" + +#include "ZigGlobalObject.h" + +#include "ObjectModule.h" + +namespace Zig { +void generateNativeModule_BunObject(JSC::JSGlobalObject *lexicalGlobalObject, + JSC::Identifier moduleKey, + Vector<JSC::Identifier, 4> &exportNames, + JSC::MarkedArgumentBuffer &exportValues) { + JSC::VM &vm = lexicalGlobalObject->vm(); + Zig::GlobalObject *globalObject = + reinterpret_cast<Zig::GlobalObject *>(lexicalGlobalObject); + + JSObject *object = + globalObject->get(globalObject, Identifier::fromString(vm, "Bun"_s)) + .getObject(); + + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(object); +} + +} // namespace Zig
\ No newline at end of file diff --git a/src/bun.js/modules/BunObjectModule.h b/src/bun.js/modules/BunObjectModule.h new file mode 100644 index 000000000..3d7ba1534 --- /dev/null +++ b/src/bun.js/modules/BunObjectModule.h @@ -0,0 +1,8 @@ + +namespace Zig { +void generateNativeModule_BunObject(JSC::JSGlobalObject *lexicalGlobalObject, + JSC::Identifier moduleKey, + Vector<JSC::Identifier, 4> &exportNames, + JSC::MarkedArgumentBuffer &exportValues); + +} // namespace Zig
\ No newline at end of file diff --git a/src/bun.js/modules/_NativeModule.h b/src/bun.js/modules/_NativeModule.h index 96eb9ad9d..485987e1c 100644 --- a/src/bun.js/modules/_NativeModule.h +++ b/src/bun.js/modules/_NativeModule.h @@ -24,6 +24,7 @@ // given is the default export #define BUN_FOREACH_NATIVE_MODULE(macro) \ + macro("bun"_s, BunObject) \ macro("bun:jsc"_s, BunJSC) \ macro("node:buffer"_s, NodeBuffer) \ macro("node:constants"_s, NodeConstants) \ diff --git a/src/js/out/NativeModuleImpl.h b/src/js/out/NativeModuleImpl.h index f519e7d7d..5a6881b7f 100644 --- a/src/js/out/NativeModuleImpl.h +++ b/src/js/out/NativeModuleImpl.h @@ -1,3 +1,4 @@ +#include "../../bun.js/modules/BunObjectModule.h" #include "../../bun.js/modules/BunJSCModule.h" #include "../../bun.js/modules/NodeBufferModule.h" #include "../../bun.js/modules/NodeConstantsModule.h" diff --git a/src/js/out/ResolvedSourceTag.zig b/src/js/out/ResolvedSourceTag.zig index f054d09d7..43dc24689 100644 --- a/src/js/out/ResolvedSourceTag.zig +++ b/src/js/out/ResolvedSourceTag.zig @@ -66,13 +66,14 @@ pub const ResolvedSourceTag = enum(u32) { @"vercel_fetch" = 566, @"ws" = 567, // Native modules run through a different system using ESM registry. - @"bun:jsc" = 1024, - @"node:buffer" = 1025, - @"node:constants" = 1026, - @"node:module" = 1027, - @"node:process" = 1028, - @"node:string_decoder" = 1029, - @"node:tty" = 1030, - @"node:util/types" = 1031, - @"utf-8-validate" = 1032, + @"bun" = 1024, + @"bun:jsc" = 1025, + @"node:buffer" = 1026, + @"node:constants" = 1027, + @"node:module" = 1028, + @"node:process" = 1029, + @"node:string_decoder" = 1030, + @"node:tty" = 1031, + @"node:util/types" = 1032, + @"utf-8-validate" = 1033, }; diff --git a/src/js/out/SyntheticModuleType.h b/src/js/out/SyntheticModuleType.h index 12ec073ad..26704a71d 100644 --- a/src/js/out/SyntheticModuleType.h +++ b/src/js/out/SyntheticModuleType.h @@ -69,14 +69,15 @@ enum SyntheticModuleType : uint32_t { // Native modules run through the same system, but with different underlying initializers. // They also have bit 10 set to differentiate them from JS builtins. NativeModuleFlag = (1 << 10) | (1 << 9), - BunJSC = 1024, - NodeBuffer = 1025, - NodeConstants = 1026, - NodeModule = 1027, - NodeProcess = 1028, - NodeStringDecoder = 1029, - NodeTTY = 1030, - NodeUtilTypes = 1031, - UTF8Validate = 1032, + BunObject = 1024, + BunJSC = 1025, + NodeBuffer = 1026, + NodeConstants = 1027, + NodeModule = 1028, + NodeProcess = 1029, + NodeStringDecoder = 1030, + NodeTTY = 1031, + NodeUtilTypes = 1032, + UTF8Validate = 1033, }; diff --git a/test/js/bun/resolve/import-meta.test.js b/test/js/bun/resolve/import-meta.test.js index e23b44446..613afe636 100644 --- a/test/js/bun/resolve/import-meta.test.js +++ b/test/js/bun/resolve/import-meta.test.js @@ -181,3 +181,7 @@ it('import("bun") works', async () => { it("require.resolve with empty options object", () => { expect(require.resolve(import.meta.path + String(""), {})).toBe(import.meta.path); }); + +it("dynamically import bun", async () => { + expect((await import(eval("'bun'"))).default).toBe(Bun); +}); |