Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): implement the rest of node:module #6188

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/runtime/nodejs-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This page is updated regularly to reflect compatibility status of the latest ver

### [`node:events`](https://nodejs.org/api/events.html)

🟡 Missing `require('node:events').on`.
🟡 Missing `events.on`.

### [`node:fs`](https://nodejs.org/api/fs.html)

Expand All @@ -74,7 +74,7 @@ This page is updated regularly to reflect compatibility status of the latest ver

### [`node:module`](https://nodejs.org/api/module.html)

🟢 Fully implemented.
🟡 Missing `module.register`, `module.syncBuiltinESMExports`, `module.findSourceMap`, `module.SourceMap`.

### [`node:net`](https://nodejs.org/api/net.html)

Expand Down
14 changes: 8 additions & 6 deletions src/bun.js/bindings/CommonJSModuleRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,35 +810,37 @@ const JSC::ClassInfo JSCommonJSModule::s_info = { "Module"_s, &Base::s_info, nul
const JSC::ClassInfo RequireResolveFunctionPrototype::s_info = { "resolve"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(RequireResolveFunctionPrototype) };
const JSC::ClassInfo RequireFunctionPrototype::s_info = { "require"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(RequireFunctionPrototype) };

// This is .$require on a CommonJSModuleRecord. It is used by the CJS module loader internals in `Module.ts`
JSC_DEFINE_HOST_FUNCTION(jsFunctionRequireCommonJS, (JSGlobalObject * lexicalGlobalObject, CallFrame* callframe))
{
auto* globalObject = jsCast<Zig::GlobalObject*>(lexicalGlobalObject);
auto& vm = globalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);

ASSERT(callframe->argumentCount() == 2);

JSCommonJSModule* thisObject = jsDynamicCast<JSCommonJSModule*>(callframe->thisValue());
if (!thisObject)
return throwVMTypeError(globalObject, throwScope);
RELEASE_ASSERT(thisObject);

JSValue specifierValue = callframe->argument(0);
WTF::String specifier = specifierValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(throwScope, {});

// Special-case for "process" to just return the process object directly.
if (UNLIKELY(specifier == "process"_s || specifier == "node:process"_s)) {
jsCast<JSCommonJSModule*>(callframe->argument(1))->putDirect(vm, builtinNames(vm).exportsPublicName(), globalObject->processObject(), 0);
thisObject->putDirect(vm, builtinNames(vm).exportsPublicName(), globalObject->processObject(), 0);
return JSValue::encode(globalObject->processObject());
}

WTF::String referrer = thisObject->id().toWTFString(globalObject);
RETURN_IF_EXCEPTION(throwScope, {});
JSValue referrerModule = callframe->argument(1);
WTF::String referrer = referrerModule.isString() ? referrerModule.toWTFString(globalObject) : MAKE_STATIC_STRING_IMPL(".");

BunString specifierStr = Bun::toString(specifier);
BunString referrerStr = Bun::toString(referrer);

JSValue fetchResult = Bun::fetchCommonJSModule(
globalObject,
jsCast<JSCommonJSModule*>(callframe->argument(1)),
thisObject,
specifierValue,
&specifierStr,
&referrerStr);
Expand Down
139 changes: 109 additions & 30 deletions src/bun.js/modules/NodeModuleModule.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// clang-format off
#pragma once

#include "CommonJSModuleRecord.h"
Expand Down Expand Up @@ -152,6 +153,72 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionIsBuiltinModule,
return JSValue::encode(jsBoolean(Bun::isBuiltinModule(moduleStr)));
}

JSC_DEFINE_HOST_FUNCTION(jsFunctionDebugNoop,
(JSC::JSGlobalObject * globalObject,
JSC::CallFrame *callFrame)) {
return JSValue::encode(jsUndefined());
}

JSC_DEFINE_HOST_FUNCTION(jsFunctionFindPath,
(JSC::JSGlobalObject * globalObject,
JSC::CallFrame *callFrame)) {
JSC::VM &vm = globalObject->vm();

auto specifier = callFrame->argument(0);
auto paths = jsDynamicCast<JSArray*>(callFrame->argument(1));

if (!specifier.isString()) {
return JSValue::encode(jsBoolean(false));
}
if (!paths) {
return JSValue::encode(jsBoolean(false));
}

auto result = Bun__resolveSync(globalObject, JSC::JSValue::encode(moduleName), from, isESM);

if (JSC::JSValue::decode(result).isString()) {
return result;
}

// TODO: iterate

return JSValue::encode(jsBoolean(false));
}

// Might be faster as a JS builtin
JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModulePreloadModules,
(JSC::JSGlobalObject * globalObject,
JSC::CallFrame *callFrame)) {
JSC::VM &vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);

JSValue requests = callFrame->argument(0);

JSArray *requestsArray = jsDynamicCast<JSArray *>(requests.asCell());
if (!requestsArray)
return JSValue::encode(jsUndefined());

auto length = requestsArray->length();
auto requireFn = jsCast<Zig::GlobalObject *>(globalObject)
->getDirect(vm, WebCore::clientData(vm)
->builtinNames()
.overridableRequirePrivateName());

if (!requireFn.isCallable())
return JSValue::encode(jsUndefined());

JSC::CallData callData = JSC::getCallData(requireFn);
WTF::NakedPtr<JSC::Exception> exception;
for (unsigned i = 0; i < length; ++i) {
MarkedArgumentBuffer args;
args.append(requestsArray->getIndex(globalObject, i));
JSValue result = JSC::call(globalObject, requireFn, callData,
JSC::jsUndefined(), args, exception);
if (exception)
return JSValue::encode(jsUndefined());
}
}

JSC_DEFINE_HOST_FUNCTION(jsFunctionWrap, (JSC::JSGlobalObject * globalObject,
JSC::CallFrame *callFrame)) {
auto &vm = globalObject->vm();
Expand Down Expand Up @@ -196,8 +263,20 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionFindSourceMap,
CallFrame *callFrame)) {
auto &vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
throwException(globalObject, scope,
createError(globalObject, "Not implemented"_s));
throwException(
globalObject, scope,
createError(globalObject,
"module.findSourceMap is not implemented in Bun"_s));
return JSValue::encode(jsUndefined());
}

JSC_DEFINE_HOST_FUNCTION(jsFunctionRegister, (JSGlobalObject * globalObject,
CallFrame *callFrame)) {
auto &vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
throwException(
globalObject, scope,
createError(globalObject, "Bun does not support Node.js loaders"_s));
return JSValue::encode(jsUndefined());
}

Expand All @@ -211,8 +290,10 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionSourceMap, (JSGlobalObject * globalObject,
CallFrame *callFrame)) {
auto &vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
throwException(globalObject, scope,
createError(globalObject, "Not implemented"_s));
throwException(
globalObject, scope,
createError(globalObject,
"module.SourceMap is not yet implemented in Bun"_s));
return JSValue::encode(jsUndefined());
}

Expand Down Expand Up @@ -354,46 +435,44 @@ DEFINE_NATIVE_MODULE(NodeModule) {
exportNames.append(name);
exportValues.append(value);
};
exportNames.reserveCapacity(16);
exportValues.ensureCapacity(16);
exportNames.reserveCapacity(17);
exportValues.ensureCapacity(17);
exportNames.append(vm.propertyNames->defaultKeyword);
exportValues.append(defaultObject);

put(Identifier::fromString(vm, "Module"_s), defaultObject);

// Module._extensions === require.extensions
put(Identifier::fromString(vm, "_extensions"_s),
globalObject->requireFunctionUnbound()->get(
globalObject, Identifier::fromString(vm, "extensions"_s)));
put(
Identifier::fromString(vm, "_extensions"_s),
globalObject->requireFunctionUnbound()->get(globalObject, Identifier::fromString(vm, "extensions"_s))
);

put(Identifier::fromString(vm, "_pathCache"_s), JSC::constructEmptyObject(globalObject));

putNativeFn(Identifier::fromString(vm, "__resolveFilename"_s), jsFunctionResolveFileName);
defaultObject->putDirectCustomAccessor(
vm, JSC::Identifier::fromString(vm, "_resolveFilename"_s),
JSC::CustomGetterSetter::create(vm, get_resolveFilename,
set_resolveFilename),
JSC::CustomGetterSetter::create(vm, get_resolveFilename, set_resolveFilename),
JSC::PropertyAttribute::CustomAccessor | 0);
putNativeFn(Identifier::fromString(vm, "__resolveFilename"_s),
jsFunctionResolveFileName);

putNativeFn(Identifier::fromString(vm, "createRequire"_s),
jsFunctionNodeModuleCreateRequire);
putNativeFn(Identifier::fromString(vm, "paths"_s),
Resolver__nodeModulePathsForJS);
putNativeFn(Identifier::fromString(vm, "findSourceMap"_s),
jsFunctionFindSourceMap);
putNativeFn(Identifier::fromString(vm, "syncBuiltinExports"_s),
jsFunctionSyncBuiltinExports);

putNativeFn(Identifier::fromString(vm, "_preloadModules"_s), jsFunctionNodeModulePreloadModules);
putNativeFn(Identifier::fromString(vm, "createRequire"_s), jsFunctionNodeModuleCreateRequire);
putNativeFn(Identifier::fromString(vm, "paths"_s), Resolver__nodeModulePathsForJS);
putNativeFn(Identifier::fromString(vm, "findSourceMap"_s), jsFunctionFindSourceMap);
putNativeFn(Identifier::fromString(vm, "syncBuiltinExports"_s), jsFunctionSyncBuiltinExports);
putNativeFn(Identifier::fromString(vm, "SourceMap"_s), jsFunctionSourceMap);
putNativeFn(Identifier::fromString(vm, "isBuiltin"_s),
jsFunctionIsBuiltinModule);
putNativeFn(Identifier::fromString(vm, "_nodeModulePaths"_s),
Resolver__nodeModulePathsForJS);
putNativeFn(Identifier::fromString(vm, "isBuiltin"_s), jsFunctionIsBuiltinModule);
putNativeFn(Identifier::fromString(vm, "_nodeModulePaths"_s), Resolver__nodeModulePathsForJS);
putNativeFn(Identifier::fromString(vm, "wrap"_s), jsFunctionWrap);

put(Identifier::fromString(vm, "_cache"_s),
jsCast<Zig::GlobalObject *>(globalObject)->lazyRequireCacheObject());
putNativeFn(Identifier::fromString(vm, "_debug"_s), jsFunctionDebugNoop);

put(Identifier::fromString(vm, "_load"_s), JSFunction::create(vm, moduleModuleLoadCodeGenerator(vm), globalObject));

put(Identifier::fromString(vm, "_cache"_s), jsCast<Zig::GlobalObject *>(globalObject)->lazyRequireCacheObject());

put(Identifier::fromString(vm, "globalPaths"_s),
constructEmptyArray(globalObject, nullptr, 0));
put(Identifier::fromString(vm, "globalPaths"_s), constructEmptyArray(globalObject, nullptr, 0));

auto prototype =
constructEmptyObject(globalObject, globalObject->objectPrototype(), 1);
Expand Down
2 changes: 1 addition & 1 deletion src/js/builtins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ declare function $createCommonJSModule(
id: string,
exports: any,
hasEvaluated: boolean,
parent: CommonJSModuleRecord,
parent: CommonJSModuleRecord | undefined,
): CommonJSModuleRecord;

declare function $overridableRequire(this: CommonJSModuleRecord, id: string): any;
Expand Down
15 changes: 12 additions & 3 deletions src/js/builtins/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function require(this: CommonJSModuleRecord, id: string) {

// overridableRequire can be overridden by setting `Module.prototype.require`
export function overridableRequire(this: CommonJSModuleRecord, id: string) {
const existing = $requireMap.$get(id) || $requireMap.$get((id = $resolveSync(id, this.path, false)));
const existing = $requireMap.$get(id) || $requireMap.$get((id = $resolveSync(id, this?.path ?? ".", false)));
if (existing) {
// Scenario where this is necessary:
//
Expand Down Expand Up @@ -46,8 +46,7 @@ export function overridableRequire(this: CommonJSModuleRecord, id: string) {
//
// Note: we do not need to wrap this in a try/catch, if it throws the C++ code will
// clear the module from the map.
//
var out = this.$require(id, mod);
var out = mod.$require(id, this?.id);

// -1 means we need to lookup the module from the ESM registry.
if (out === -1) {
Expand Down Expand Up @@ -86,3 +85,13 @@ export function requireNativeModule(id: string) {
}
return $requireESM(id).default;
}

/** require('node:module')._load */
export function moduleLoad(request: string, parentPath: CommonJSModuleRecord, isMain: string) {
// TODO: `isMain` does four things in node
// - sets `process.mainModule`
// - sets `module.require.main`
// - sets `module.id` to "."
// - would pass true to the third argument of _resolveFilename if overridden
return $overridableRequire.$call(parentPath, request);
}
12 changes: 10 additions & 2 deletions src/js/out/WebCoreJSBuiltins.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/js/out/WebCoreJSBuiltins.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/js/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ interface BunLazyModules {
declare var $exports: any;

interface CommonJSModuleRecord {
$require(id: string, mod: any): any;
$require(this: CommonJSModuleRecord, id: string, parent: string | undefined): any;
children: CommonJSModuleRecord[];
exports: any;
id: string;
Expand Down