From 76626ac54b83a5a9a24abee1bc35f13a2196504d Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 25 Jun 2023 19:48:56 -0700 Subject: Fix test failures in import.meta (#3403) * Fix test failures in import.meta * regenerate * Use bound functions for `import.meta.require` and `import.meta.require.resolve` and `Module.createRequire` --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/modules/NodeModuleModule.cpp | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'src/bun.js/modules/NodeModuleModule.cpp') diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index f11277709..8b278ddd8 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -19,24 +19,12 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleCreateRequire, return JSC::JSValue::encode(JSC::jsUndefined()); } - auto str = callFrame->uncheckedArgument(0).toStringOrNull(globalObject); + auto val = callFrame->uncheckedArgument(0).toWTFString(globalObject); RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); - WTF::String val = str->value(globalObject); - auto *meta = Zig::ImportMetaObject::create(globalObject, str); auto clientData = WebCore::clientData(vm); - auto requireFunction = - Zig::ImportMetaObject::createRequireFunction(vm, globalObject, val); - auto nameStr = jsCast(requireFunction)->name(vm); - JSC::JSBoundFunction *boundRequireFunction = - JSC::JSBoundFunction::create(vm, globalObject, requireFunction, meta, - ArgList(), 0, jsString(vm, nameStr)); - boundRequireFunction->putDirect( - vm, clientData->builtinNames().resolvePublicName(), - requireFunction->getDirect( - vm, clientData->builtinNames().resolvePublicName()), - 0); - - RELEASE_AND_RETURN(scope, JSValue::encode(boundRequireFunction)); + RELEASE_AND_RETURN( + scope, JSValue::encode(Zig::ImportMetaObject::createRequireFunction( + vm, globalObject, val))); } JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModulePaths, (JSC::JSGlobalObject * globalObject, @@ -158,7 +146,8 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, jsFunctionNodeModulePaths, ImplementationVisibility::Public)); exportNames.append(JSC::Identifier::fromString(vm, "_cache"_s)); - exportValues.append(JSC::constructEmptyObject(globalObject)); + exportValues.append( + jsCast(globalObject)->lazyRequireCacheObject()); exportNames.append(JSC::Identifier::fromString(vm, "builtinModules"_s)); -- cgit v1.2.3 From ec3ed67bc9ad8cbb0e59234564d57265d5423fce Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Mon, 26 Jun 2023 08:12:37 -0700 Subject: implement `_nodeModulePaths` and `require.main.paths` (#3411) * tests in progress * add `require.main.paths`, add every dir up to root * remove imports --- src/bun.js/bindings/CommonJSModuleRecord.cpp | 31 +++++++++ src/bun.js/bindings/CommonJSModuleRecord.h | 1 + src/bun.js/modules/NodeModuleModule.cpp | 15 ++-- src/resolver/resolver.zig | 95 ++++++++++++++++++++++++++ test/js/node/module/node-module-module.test.js | 19 ++++++ 5 files changed, 150 insertions(+), 11 deletions(-) (limited to 'src/bun.js/modules/NodeModuleModule.cpp') diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index 8d4fe0a1e..3615db774 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -295,6 +295,35 @@ JSC_DEFINE_CUSTOM_SETTER(setterPath, return true; } +extern "C" EncodedJSValue Resolver__propForRequireMainPaths(JSGlobalObject*); + +JSC_DEFINE_CUSTOM_GETTER(getterPaths, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (UNLIKELY(!thisObject)) { + return JSValue::encode(jsUndefined()); + } + + if (!thisObject->m_paths) { + JSValue paths = JSValue::decode(Resolver__propForRequireMainPaths(globalObject)); + thisObject->m_paths.set(globalObject->vm(), thisObject, paths); + } + + return JSValue::encode(thisObject->m_paths.get()); +} + +JSC_DEFINE_CUSTOM_SETTER(setterPaths, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, JSC::PropertyName propertyName)) +{ + JSCommonJSModule* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (!thisObject) + return false; + + thisObject->m_paths.set(globalObject->vm(), thisObject, JSValue::decode(value)); + return true; +} + JSC_DEFINE_CUSTOM_SETTER(setterFilename, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName propertyName)) @@ -340,6 +369,7 @@ static const struct HashTableValue JSCommonJSModulePrototypeTableValues[] = { { "loaded"_s, static_cast(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createLoaded } }, { "parent"_s, static_cast(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createParent } }, { "path"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterPath, setterPath } }, + { "paths"_s, static_cast(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterPaths, setterPaths } }, }; class JSCommonJSModulePrototype final : public JSC::JSNonFinalObject { @@ -675,6 +705,7 @@ void JSCommonJSModule::visitChildrenImpl(JSCell* cell, Visitor& visitor) visitor.append(thisObject->sourceCode); visitor.append(thisObject->m_filename); visitor.append(thisObject->m_dirname); + visitor.append(thisObject->m_paths); } DEFINE_VISIT_CHILDREN(JSCommonJSModule); diff --git a/src/bun.js/bindings/CommonJSModuleRecord.h b/src/bun.js/bindings/CommonJSModuleRecord.h index 48f14b39c..a96ab5f75 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.h +++ b/src/bun.js/bindings/CommonJSModuleRecord.h @@ -24,6 +24,7 @@ public: mutable JSC::WriteBarrier m_id; mutable JSC::WriteBarrier m_filename; mutable JSC::WriteBarrier m_dirname; + mutable JSC::WriteBarrier m_paths; mutable JSC::WriteBarrier sourceCode; static void destroy(JSC::JSCell*); diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index 8b278ddd8..34d45698f 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -26,15 +26,8 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleCreateRequire, scope, JSValue::encode(Zig::ImportMetaObject::createRequireFunction( vm, globalObject, val))); } -JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModulePaths, - (JSC::JSGlobalObject * globalObject, - JSC::CallFrame *callFrame)) { - return JSC::JSValue::encode(JSC::JSArray::create( - globalObject->vm(), - globalObject->arrayStructureForIndexingTypeDuringAllocation( - ArrayWithContiguous), - 0)); -} +extern "C" EncodedJSValue Resolver__nodeModulePathsForJS(JSGlobalObject *, + CallFrame *); JSC_DEFINE_HOST_FUNCTION(jsFunctionFindSourceMap, (JSGlobalObject * globalObject, @@ -114,7 +107,7 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, vm, globalObject, 1, String("createRequire"_s), jsFunctionNodeModuleCreateRequire, ImplementationVisibility::Public)); exportValues.append(JSFunction::create(vm, globalObject, 1, String("paths"_s), - jsFunctionNodeModulePaths, + Resolver__nodeModulePathsForJS, ImplementationVisibility::Public)); exportValues.append(JSFunction::create( vm, globalObject, 1, String("findSourceMap"_s), jsFunctionFindSourceMap, @@ -143,7 +136,7 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, exportNames.append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s)); exportValues.append(JSFunction::create( vm, globalObject, 0, String("_nodeModulePaths"_s), - jsFunctionNodeModulePaths, ImplementationVisibility::Public)); + Resolver__nodeModulePathsForJS, ImplementationVisibility::Public)); exportNames.append(JSC::Identifier::fromString(vm, "_cache"_s)); exportValues.append( diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 14bc358d0..40b106f3a 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -94,6 +94,7 @@ const bufs = struct { threadlocal var remap_path_trailing_slash: [bun.MAX_PATH_BYTES]u8 = undefined; threadlocal var path_in_global_disk_cache: [bun.MAX_PATH_BYTES]u8 = undefined; threadlocal var abs_to_rel: [bun.MAX_PATH_BYTES]u8 = undefined; + threadlocal var node_modules_paths_buf: [bun.MAX_PATH_BYTES]u8 = undefined; pub inline fn bufs(comptime field: std.meta.DeclEnum(@This())) *@TypeOf(@field(@This(), @tagName(field))) { return &@field(@This(), @tagName(field)); @@ -3107,6 +3108,93 @@ pub const Resolver = struct { }; } + pub export fn Resolver__nodeModulePathsForJS(globalThis: *bun.JSC.JSGlobalObject, callframe: *bun.JSC.CallFrame) callconv(.C) bun.JSC.JSValue { + bun.JSC.markBinding(@src()); + const argument: bun.JSC.JSValue = callframe.argument(0); + + if (argument.isEmpty() or !argument.isString()) { + globalThis.throwInvalidArgumentType("nodeModulePaths", "path", "string"); + return .zero; + } + + const in_str = argument.toBunString(globalThis); + var r = &globalThis.bunVM().bundler.resolver; + return nodeModulePathsJSValue(r, in_str, globalThis); + } + + pub export fn Resolver__propForRequireMainPaths(globalThis: *bun.JSC.JSGlobalObject) callconv(.C) bun.JSC.JSValue { + bun.JSC.markBinding(@src()); + + const in_str = bun.String.create("."); + var r = &globalThis.bunVM().bundler.resolver; + return nodeModulePathsJSValue(r, in_str, globalThis); + } + + pub fn nodeModulePathsJSValue( + r: *ThisResolver, + in_str: bun.String, + globalObject: *bun.JSC.JSGlobalObject, + ) bun.JSC.JSValue { + var list = std.ArrayList(bun.String).init(bun.default_allocator); + defer list.deinit(); + + const sliced = in_str.toUTF8(bun.default_allocator); + defer sliced.deinit(); + + const str = brk: { + if (std.fs.path.isAbsolute(sliced.slice())) break :brk sliced.slice(); + var dir_path_buf = bufs(.node_modules_paths_buf); + break :brk r.fs.joinBuf(&[_]string{ r.fs.top_level_dir, sliced.slice() }, dir_path_buf); + }; + var arena = std.heap.ArenaAllocator.init(bun.default_allocator); + defer arena.deinit(); + var stack_fallback_allocator = std.heap.stackFallback(1024, arena.allocator()); + + if (r.readDirInfo(strings.withoutTrailingSlash(str)) catch null) |result| { + var dir_info = result; + + while (true) { + const path_without_trailing_slash = strings.withoutTrailingSlash(dir_info.abs_path); + const path_parts = brk: { + if (path_without_trailing_slash.len == 1 and path_without_trailing_slash[0] == '/') { + break :brk [2]string{ "", "/node_modules" }; + } + + break :brk [2]string{ path_without_trailing_slash, "/node_modules" }; + }; + list.append( + bun.String.create( + bun.strings.concat(stack_fallback_allocator.get(), &path_parts) catch unreachable, + ), + ) catch unreachable; + dir_info = (r.readDirInfo(std.fs.path.dirname(path_without_trailing_slash) orelse break) catch null) orelse break; + } + } else { + // does not exist + const full_path = std.fs.path.resolve(r.allocator, &[1][]const u8{str}) catch unreachable; + var path = full_path; + while (true) { + const path_without_trailing_slash = strings.withoutTrailingSlash(path); + + list.append( + bun.String.create( + bun.strings.concat( + stack_fallback_allocator.get(), + &[_]string{ + path_without_trailing_slash, + "/node_modules", + }, + ) catch unreachable, + ), + ) catch unreachable; + + path = path[0 .. strings.lastIndexOfChar(path, '/') orelse break]; + } + } + + return bun.String.toJSArray(globalObject, list.items); + } + pub fn loadAsIndex(r: *ThisResolver, dir_info: *DirInfo, extension_order: []const string) ?MatchResult { var rfs = &r.fs.fs; // Try the "index" file with extensions @@ -3892,3 +3980,10 @@ pub const GlobalCache = enum { }; } }; + +comptime { + if (!bun.JSC.is_bindgen) { + _ = Resolver.Resolver__nodeModulePathsForJS; + _ = Resolver.Resolver__propForRequireMainPaths; + } +} diff --git a/test/js/node/module/node-module-module.test.js b/test/js/node/module/node-module-module.test.js index 549b5e085..3ced63da1 100644 --- a/test/js/node/module/node-module-module.test.js +++ b/test/js/node/module/node-module-module.test.js @@ -1,5 +1,24 @@ import { expect, test } from "bun:test"; +import { _nodeModulePaths } from "module"; test("module.globalPaths exists", () => { expect(Array.isArray(require("module").globalPaths)).toBe(true); }); + +test("_nodeModulePaths() works", () => { + expect(() => { + _nodeModulePaths(); + }).toThrow(); + expect(_nodeModulePaths(".").length).toBeGreaterThan(0); + expect(_nodeModulePaths(".").pop()).toBe("/node_modules"); + expect(_nodeModulePaths("")).toEqual(_nodeModulePaths(".")); + expect(_nodeModulePaths("/")).toEqual(["/node_modules"]); + expect(_nodeModulePaths("/a/b/c/d")).toEqual([ + "/a/b/c/d/node_modules", + "/a/b/c/node_modules", + "/a/b/node_modules", + "/a/node_modules", + "/node_modules", + ]); + expect(_nodeModulePaths("/a/b/../d")).toEqual(["/a/d/node_modules", "/a/node_modules", "/node_modules"]); +}); -- cgit v1.2.3 From 4e4cae0fc308c448fb73d59228fb30bd994453ed Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:16:59 -0700 Subject: webpack test and add empty `prototype` prop to module (#3421) --- src/bun.js/modules/NodeModuleModule.cpp | 9 ++++++--- test/js/third_party/webpack/package.json | 7 +++++++ test/js/third_party/webpack/test.js | 11 +++++++++++ test/js/third_party/webpack/webpack.test.ts | 27 +++++++++++++++++++++++++++ test/js/third_party/webpack/world.js | 3 +++ test/package.json | 4 +++- 6 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 test/js/third_party/webpack/package.json create mode 100644 test/js/third_party/webpack/test.js create mode 100644 test/js/third_party/webpack/webpack.test.ts create mode 100644 test/js/third_party/webpack/world.js (limited to 'src/bun.js/modules/NodeModuleModule.cpp') diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index 34d45698f..88e08a3ac 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -142,10 +142,10 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, exportValues.append( jsCast(globalObject)->lazyRequireCacheObject()); - exportNames.append(JSC::Identifier::fromString(vm, "builtinModules"_s)); + exportNames.append(JSC::Identifier::fromString(vm, "prototype"_s)); + exportValues.append(constructEmptyObject(globalObject)); - exportNames.append(JSC::Identifier::fromString(vm, "globalPaths"_s)); - exportValues.append(JSC::constructEmptyArray(globalObject, 0)); + exportNames.append(JSC::Identifier::fromString(vm, "builtinModules"_s)); JSC::JSArray *builtinModules = JSC::JSArray::create( vm, @@ -167,5 +167,8 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, builtinModules->putDirectIndex(globalObject, 6, JSC::jsString(vm, String("bun:sqlite"_s))); exportValues.append(builtinModules); + + exportNames.append(JSC::Identifier::fromString(vm, "globalPaths"_s)); + exportValues.append(JSC::constructEmptyArray(globalObject, 0)); } } // namespace Zig diff --git a/test/js/third_party/webpack/package.json b/test/js/third_party/webpack/package.json new file mode 100644 index 000000000..fb08bfdc5 --- /dev/null +++ b/test/js/third_party/webpack/package.json @@ -0,0 +1,7 @@ +{ + "name": "webpack-test", + "version": "2.0.0", + "dependencies": { + "webpack": "5.88.0" + } +} \ No newline at end of file diff --git a/test/js/third_party/webpack/test.js b/test/js/third_party/webpack/test.js new file mode 100644 index 000000000..bfd3e80a1 --- /dev/null +++ b/test/js/third_party/webpack/test.js @@ -0,0 +1,11 @@ +import { world } from "./world.js"; + +function component() { + const element = document.createElement("div"); + + element.innerHTML = "hello " + world(); + + return element; +} + +document.body.appendChild(component()); diff --git a/test/js/third_party/webpack/webpack.test.ts b/test/js/third_party/webpack/webpack.test.ts new file mode 100644 index 000000000..ffc8195c6 --- /dev/null +++ b/test/js/third_party/webpack/webpack.test.ts @@ -0,0 +1,27 @@ +import { bunExe, bunEnv } from "harness"; +import { existsSync, rmdirSync } from "fs"; +import { join } from "path"; + +afterEach(() => { + rmdirSync(join(import.meta.dir, "dist"), { recursive: true }); +}); + +test("webpack works", () => { + Bun.spawnSync({ + cmd: [bunExe(), "-b", "webpack", "--entry", "./test.js", "-o", "./dist/test1/main.js"], + cwd: import.meta.dir, + env: bunEnv, + }); + + expect(existsSync(join(import.meta.dir, "dist", "test1/main.js"))).toBe(true); +}); + +test("webpack --watch works", async () => { + Bun.spawnSync({ + cmd: ["timeout", "3", bunExe(), "-b", "webpack", "--entry", "./test.js", "-o", "./dist/test2/main.js", "--watch"], + cwd: import.meta.dir, + env: bunEnv, + }); + + expect(existsSync(join(import.meta.dir, "dist", "test2/main.js"))).toBe(true); +}); diff --git a/test/js/third_party/webpack/world.js b/test/js/third_party/webpack/world.js new file mode 100644 index 000000000..3fa21bf67 --- /dev/null +++ b/test/js/third_party/webpack/world.js @@ -0,0 +1,3 @@ +export function world() { + return "world"; +} diff --git a/test/package.json b/test/package.json index b9aa55ae2..d4246ea43 100644 --- a/test/package.json +++ b/test/package.json @@ -23,7 +23,9 @@ "svelte": "^3.55.1", "typescript": "^5.0.2", "undici": "^5.20.0", - "vitest": "^0.32.2" + "vitest": "^0.32.2", + "webpack": "5.88.0", + "webpack-cli": "4.7.2" }, "private": true, "scripts": { -- cgit v1.2.3 From c96f3b303b07312d320f3f07299aac0f93d4640f Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Mon, 26 Jun 2023 20:37:09 -0700 Subject: Fix default export of `"module"` module --- src/bun.js/modules/NodeModuleModule.cpp | 102 ++++++++++++++----------- test/js/node/module/node-module-module.test.js | 5 ++ 2 files changed, 61 insertions(+), 46 deletions(-) (limited to 'src/bun.js/modules/NodeModuleModule.cpp') diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index 88e08a3ac..10853ae32 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -103,49 +103,53 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, JSC::MarkedArgumentBuffer &exportValues) { JSC::VM &vm = globalObject->vm(); - exportValues.append(JSFunction::create( - vm, globalObject, 1, String("createRequire"_s), - jsFunctionNodeModuleCreateRequire, ImplementationVisibility::Public)); - exportValues.append(JSFunction::create(vm, globalObject, 1, String("paths"_s), - Resolver__nodeModulePathsForJS, - ImplementationVisibility::Public)); - exportValues.append(JSFunction::create( - vm, globalObject, 1, String("findSourceMap"_s), jsFunctionFindSourceMap, - ImplementationVisibility::Public)); - exportValues.append(JSFunction::create( - vm, globalObject, 0, String("syncBuiltinExports"_s), - jsFunctionSyncBuiltinExports, ImplementationVisibility::Public)); - exportValues.append( - JSFunction::create(vm, globalObject, 1, String("SourceMap"_s), - jsFunctionSourceMap, ImplementationVisibility::Public, - NoIntrinsic, jsFunctionSourceMap, nullptr)); - - exportNames.append(JSC::Identifier::fromString(vm, "createRequire"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "paths"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "findSourceMap"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "syncBuiltinExports"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "SourceMap"_s)); - - // note: this is not technically correct - // it doesn't set process.mainModule - exportNames.append(JSC::Identifier::fromString(vm, "_resolveFileName"_s)); - exportValues.append(JSFunction::create( - vm, globalObject, 3, String("_resolveFileName"_s), - jsFunctionResolveFileName, ImplementationVisibility::Public)); - - exportNames.append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s)); - exportValues.append(JSFunction::create( - vm, globalObject, 0, String("_nodeModulePaths"_s), - Resolver__nodeModulePathsForJS, ImplementationVisibility::Public)); - - exportNames.append(JSC::Identifier::fromString(vm, "_cache"_s)); - exportValues.append( - jsCast(globalObject)->lazyRequireCacheObject()); - - exportNames.append(JSC::Identifier::fromString(vm, "prototype"_s)); - exportValues.append(constructEmptyObject(globalObject)); - - exportNames.append(JSC::Identifier::fromString(vm, "builtinModules"_s)); + JSObject *defaultObject = JSC::constructEmptyObject( + vm, globalObject->nullPrototypeObjectStructure()); + auto append = [&](Identifier name, JSValue value) { + defaultObject->putDirect(vm, name, value); + exportNames.append(name); + exportValues.append(value); + }; + + append(Identifier::fromString(vm, "createRequire"_s), + JSFunction::create(vm, globalObject, 1, String("createRequire"_s), + jsFunctionNodeModuleCreateRequire, + ImplementationVisibility::Public)); + + append(Identifier::fromString(vm, "paths"_s), + JSFunction::create(vm, globalObject, 1, String("paths"_s), + Resolver__nodeModulePathsForJS, + ImplementationVisibility::Public)); + + append(Identifier::fromString(vm, "findSourceMap"_s), + JSFunction::create(vm, globalObject, 1, String("findSourceMap"_s), + jsFunctionFindSourceMap, + ImplementationVisibility::Public)); + append(Identifier::fromString(vm, "syncBuiltinExports"_s), + JSFunction::create(vm, globalObject, 0, String("syncBuiltinExports"_s), + jsFunctionSyncBuiltinExports, + ImplementationVisibility::Public)); + append(Identifier::fromString(vm, "SourceMap"_s), + JSFunction::create(vm, globalObject, 1, String("SourceMap"_s), + jsFunctionSourceMap, + ImplementationVisibility::Public, NoIntrinsic, + jsFunctionSourceMap, nullptr)); + + append(JSC::Identifier::fromString(vm, "_resolveFileName"_s), + JSFunction::create(vm, globalObject, 3, String("_resolveFileName"_s), + jsFunctionResolveFileName, + ImplementationVisibility::Public)); + + append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s), + JSFunction::create(vm, globalObject, 0, String("_nodeModulePaths"_s), + Resolver__nodeModulePathsForJS, + ImplementationVisibility::Public)); + + append(JSC::Identifier::fromString(vm, "_cache"_s), + jsCast(globalObject)->lazyRequireCacheObject()); + + append(JSC::Identifier::fromString(vm, "globalPaths"_s), + JSC::constructEmptyArray(globalObject, nullptr, 0)); JSC::JSArray *builtinModules = JSC::JSArray::create( vm, @@ -166,9 +170,15 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, JSC::jsString(vm, String("bun:ffi"_s))); builtinModules->putDirectIndex(globalObject, 6, JSC::jsString(vm, String("bun:sqlite"_s))); - exportValues.append(builtinModules); - exportNames.append(JSC::Identifier::fromString(vm, "globalPaths"_s)); - exportValues.append(JSC::constructEmptyArray(globalObject, 0)); + append(JSC::Identifier::fromString(vm, "builtinModules"_s), builtinModules); + + defaultObject->putDirect(vm, + JSC::PropertyName(Identifier::fromUid( + vm.symbolRegistry().symbolForKey("CommonJS"_s))), + jsNumber(0), 0); + + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(defaultObject); } } // namespace Zig diff --git a/test/js/node/module/node-module-module.test.js b/test/js/node/module/node-module-module.test.js index 3ced63da1..434bac829 100644 --- a/test/js/node/module/node-module-module.test.js +++ b/test/js/node/module/node-module-module.test.js @@ -1,10 +1,15 @@ import { expect, test } from "bun:test"; import { _nodeModulePaths } from "module"; +import Module from "module"; test("module.globalPaths exists", () => { expect(Array.isArray(require("module").globalPaths)).toBe(true); }); +test("Module exists", () => { + expect(Module).toBeDefined(); +}); + test("_nodeModulePaths() works", () => { expect(() => { _nodeModulePaths(); -- cgit v1.2.3 From d220d9ee5ab7d45c5480ad9a278807eb434f4276 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Tue, 27 Jun 2023 08:21:04 -0700 Subject: Update NodeModuleModule.cpp --- src/bun.js/modules/NodeModuleModule.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/bun.js/modules/NodeModuleModule.cpp') diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index 10853ae32..3019ea95c 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -135,8 +135,8 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, ImplementationVisibility::Public, NoIntrinsic, jsFunctionSourceMap, nullptr)); - append(JSC::Identifier::fromString(vm, "_resolveFileName"_s), - JSFunction::create(vm, globalObject, 3, String("_resolveFileName"_s), + append(JSC::Identifier::fromString(vm, "_resolveFilename"_s), + JSFunction::create(vm, globalObject, 3, String("_resolveFilename"_s), jsFunctionResolveFileName, ImplementationVisibility::Public)); @@ -151,6 +151,9 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, append(JSC::Identifier::fromString(vm, "globalPaths"_s), JSC::constructEmptyArray(globalObject, nullptr, 0)); + append(JSC::Identifier::fromString(vm, "prototype"_s), + JSC::constructEmptyObject(globalObject)); + JSC::JSArray *builtinModules = JSC::JSArray::create( vm, globalObject->arrayStructureForIndexingTypeDuringAllocation( -- cgit v1.2.3