import bun (#4055)

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2023-08-08 01:42:44 -07:00
committed by GitHub
parent 38df5b146f
commit 320ee6b6b7
8 changed files with 61 additions and 18 deletions

View File

@@ -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" },

View File

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

View File

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

View File

@@ -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) \

View File

@@ -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"

View File

@@ -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,
};

View File

@@ -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,
};

View File

@@ -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);
});