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

Refactor node:module #14227

Merged
merged 9 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ set(BUN_OBJECT_LUT_SOURCES
${CWD}/src/bun.js/bindings/BunProcess.cpp
${CWD}/src/bun.js/bindings/ProcessBindingConstants.cpp
${CWD}/src/bun.js/bindings/ProcessBindingNatives.cpp
${CWD}/src/bun.js/modules/NodeModuleModule.cpp
)

set(BUN_OBJECT_LUT_OUTPUTS
Expand All @@ -407,8 +408,10 @@ set(BUN_OBJECT_LUT_OUTPUTS
${CODEGEN_PATH}/BunProcess.lut.h
${CODEGEN_PATH}/ProcessBindingConstants.lut.h
${CODEGEN_PATH}/ProcessBindingNatives.lut.h
${CODEGEN_PATH}/NodeModuleModule.lut.h
)


macro(WEBKIT_ADD_SOURCE_DEPENDENCIES _source _deps)
set(_tmp)
get_source_file_property(_tmp ${_source} OBJECT_DEPENDS)
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/CommonJSModuleRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class RequireResolveFunctionPrototype final : public JSC::JSNonFinalObject {
using Base = JSC::JSNonFinalObject;

static RequireResolveFunctionPrototype* create(JSC::JSGlobalObject* globalObject);
static Structure* createStructure(VM& vm, JSC::JSGlobalObject* globalObject);
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject);

DECLARE_INFO;

Expand All @@ -180,7 +180,7 @@ class RequireFunctionPrototype final : public JSC::JSNonFinalObject {
using Base = JSC::JSNonFinalObject;

static RequireFunctionPrototype* create(JSC::JSGlobalObject* globalObject);
static Structure* createStructure(VM& vm, JSC::JSGlobalObject* globalObject);
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject);

DECLARE_INFO;

Expand Down
38 changes: 20 additions & 18 deletions src/bun.js/bindings/ImportMetaObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,26 +303,28 @@ extern "C" JSC::EncodedJSValue functionImportMeta__resolveSyncPrivate(JSC::JSGlo

if (!isESM) {
if (LIKELY(globalObject)) {
auto overrideHandler = globalObject->m_nodeModuleOverriddenResolveFilename.get();
if (UNLIKELY(overrideHandler)) {
ASSERT(overrideHandler->isCallable());
JSValue parentModuleObject = globalObject->requireMap()->get(globalObject, from);

JSValue parentID = jsUndefined();
if (auto* parent = jsDynamicCast<Bun::JSCommonJSModule*>(parentModuleObject)) {
parentID = parent->id();
} else {
parentID = from;
}
if (UNLIKELY(globalObject->hasOverridenModuleResolveFilenameFunction)) {
auto overrideHandler = jsCast<JSObject*>(globalObject->m_moduleResolveFilenameFunction.getInitializedOnMainThread(globalObject));
if (UNLIKELY(overrideHandler)) {
ASSERT(overrideHandler->isCallable());
JSValue parentModuleObject = globalObject->requireMap()->get(globalObject, from);

JSValue parentID = jsUndefined();
if (auto* parent = jsDynamicCast<Bun::JSCommonJSModule*>(parentModuleObject)) {
parentID = parent->id();
} else {
parentID = from;
}

MarkedArgumentBuffer args;
args.append(moduleName);
args.append(parentModuleObject);
auto parentIdStr = parentID.toWTFString(globalObject);
auto bunStr = Bun::toString(parentIdStr);
args.append(jsBoolean(Bun__isBunMain(lexicalGlobalObject, &bunStr)));
MarkedArgumentBuffer args;
args.append(moduleName);
args.append(parentModuleObject);
auto parentIdStr = parentID.toWTFString(globalObject);
auto bunStr = Bun::toString(parentIdStr);
args.append(jsBoolean(Bun__isBunMain(lexicalGlobalObject, &bunStr)));

return JSValue::encode(JSC::call(lexicalGlobalObject, overrideHandler, JSC::getCallData(overrideHandler), parentModuleObject, args));
return JSValue::encode(JSC::call(lexicalGlobalObject, overrideHandler, JSC::getCallData(overrideHandler), parentModuleObject, args));
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/bun.js/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
#include "ErrorCode.h"
#include "v8/shim/GlobalInternals.h"
#include "EventLoopTask.h"
#include "NodeModuleModule.h"
#include <JavaScriptCore/JSCBytecodeCacheVersion.h>

#if ENABLE(REMOTE_INSPECTOR)
Expand Down Expand Up @@ -2685,6 +2686,8 @@ void GlobalObject::finishCreation(VM& vm)

m_commonStrings.initialize();

Bun::addNodeModuleConstructorProperties(vm, this);

m_JSDOMFileConstructor.initLater(
[](const Initializer<JSObject>& init) {
JSObject* fileConstructor = Bun::createJSDOMFileConstructor(init.vm, init.owner);
Expand Down Expand Up @@ -3602,7 +3605,6 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor)
visitor.append(thisObject->m_readableStreamToJSON);
visitor.append(thisObject->m_readableStreamToText);
visitor.append(thisObject->m_readableStreamToFormData);
visitor.append(thisObject->m_nodeModuleOverriddenResolveFilename);

visitor.append(thisObject->m_nextTickQueue);
visitor.append(thisObject->m_errorConstructorPrepareStackTraceValue);
Expand All @@ -3612,6 +3614,8 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor)

visitor.append(thisObject->m_currentNapiHandleScopeImpl);

thisObject->m_moduleResolveFilenameFunction.visit(visitor);
thisObject->m_nodeModuleConstructor.visit(visitor);
thisObject->m_asyncBoundFunctionStructure.visit(visitor);
thisObject->m_bunObject.visit(visitor);
thisObject->m_cachedNodeVMGlobalObjectStructure.visit(visitor);
Expand Down
7 changes: 4 additions & 3 deletions src/bun.js/bindings/ZigGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,8 @@ class GlobalObject : public Bun::GlobalScope {
mutable WriteBarrier<JSFunction> m_readableStreamToText;
mutable WriteBarrier<JSFunction> m_readableStreamToFormData;

// This is set when doing `require('module')._resolveFilename = ...`
// a hack used by Next.js to inject their versions of webpack and react
mutable WriteBarrier<JSFunction> m_nodeModuleOverriddenResolveFilename;
LazyProperty<JSGlobalObject, JSCell> m_moduleResolveFilenameFunction;
LazyProperty<JSGlobalObject, JSObject> m_nodeModuleConstructor;

mutable WriteBarrier<Unknown> m_nextTickQueue;

Expand Down Expand Up @@ -586,6 +585,8 @@ class GlobalObject : public Bun::GlobalScope {
LazyProperty<JSGlobalObject, JSObject> m_performanceObject;
LazyProperty<JSGlobalObject, JSObject> m_processObject;

bool hasOverridenModuleResolveFilenameFunction = false;

private:
DOMGuardedObjectSet m_guardedObjects WTF_GUARDED_BY_LOCK(m_gcLock);
WebCore::SubtleCrypto* m_subtleCrypto = nullptr;
Expand Down
Loading
Loading