diff options
author | 2023-09-28 03:53:24 -0700 | |
---|---|---|
committer | 2023-09-28 03:53:24 -0700 | |
commit | 387f1260c9dc0cea667b44ec0152fff0cd4def25 (patch) | |
tree | 24dde83cf812481b6d1c8de316a30ece7b745a54 /src/bun.js/bindings/CommonJSModuleRecord.cpp | |
parent | e60b3607c12c91959ec795228cc299703d5b09d0 (diff) | |
download | bun-387f1260c9dc0cea667b44ec0152fff0cd4def25.tar.gz bun-387f1260c9dc0cea667b44ec0152fff0cd4def25.tar.zst bun-387f1260c9dc0cea667b44ec0152fff0cd4def25.zip |
Get Next.js Pages Router to work (#6095)
* hell
* make it so bun-debug-src
* teag
* wild
* yippee
* fas
* fix async hooks assertions
* yap
* yeah that's wild
* aa
* a
* increase time allowed
* so trivial
Diffstat (limited to 'src/bun.js/bindings/CommonJSModuleRecord.cpp')
-rw-r--r-- | src/bun.js/bindings/CommonJSModuleRecord.cpp | 69 |
1 files changed, 27 insertions, 42 deletions
diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index eea3b2a6f..38b55ba4d 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -8,7 +8,7 @@ * Then, at runtime, we create a JSCommonJSModule object. * * On this special object, we override the setter for the "exports" property in - * a non-observable way (`static bool put ...`) + * a non-observable way using a CustomGetterSetter. * * When the setter is called, we set the internal "exports" property to the * value passed in and we also update the requireMap with the new value. @@ -20,17 +20,13 @@ * * If an exception occurs, we remove the entry from the requireMap. * - * We tried using a CustomGetterSetter instead of overriding `put`, but it led - * to returning the getter itself - * - * How cyclical dependencies are handled + * How cyclical dependencies are handled: * * Before executing the CommonJS module, we set the exports object in the * requireMap to an empty object. When the CommonJS module is required again, we * return the exports object from the requireMap. The values should be in sync * while the module is being executed, unless module.exports is re-assigned to a * different value. In that case, it will have a stale value. - * */ #include "root.h" @@ -98,29 +94,35 @@ static bool canPerformFastEnumeration(Structure* s) static bool evaluateCommonJSModuleOnce(JSC::VM& vm, Zig::GlobalObject* globalObject, JSCommonJSModule* moduleObject, JSString* dirname, JSValue filename, WTF::NakedPtr<Exception>& exception) { JSC::Structure* thisObjectStructure = globalObject->commonJSFunctionArgumentsStructure(); - JSC::JSObject* thisObject = JSC::constructEmptyObject( - vm, - thisObjectStructure); - thisObject->putDirectOffset( - vm, - 0, - moduleObject); - thisObject->putDirectOffset( - vm, - 1, - dirname); + JSFunction* resolveFunction = JSC::JSBoundFunction::create(vm, + globalObject, + globalObject->requireResolveFunctionUnbound(), + moduleObject->id(), + ArgList(), 1, jsString(vm, String("resolve"_s))); + JSFunction* requireFunction = JSC::JSBoundFunction::create(vm, + globalObject, + globalObject->requireFunctionUnbound(), + moduleObject, + ArgList(), 1, jsString(vm, String("require"_s))); + requireFunction->putDirect(vm, vm.propertyNames->resolve, resolveFunction, 0); + moduleObject->putDirect(vm, WebCore::clientData(vm)->builtinNames().requirePublicName(), requireFunction, 0); - thisObject->putDirectOffset( - vm, - 2, - filename); + JSC::JSObject* thisObject = JSC::constructEmptyObject(vm, thisObjectStructure); + thisObject->putDirectOffset(vm, 0, moduleObject); + thisObject->putDirectOffset(vm, 1, requireFunction); + thisObject->putDirectOffset(vm, 2, resolveFunction); + thisObject->putDirectOffset(vm, 3, dirname); + thisObject->putDirectOffset(vm, 4, filename); moduleObject->hasEvaluated = true; + // TODO: try to not use this write barrier. it needs some extensive testing. + // there is some possible GC issue where `thisObject` is gc'd before it should be globalObject->m_BunCommonJSModuleValue.set(vm, globalObject, thisObject); JSValue empty = JSC::evaluate(globalObject, moduleObject->sourceCode.get()->sourceCode(), thisObject, exception); + ensureStillAliveHere(thisObject); globalObject->m_BunCommonJSModuleValue.clear(); moduleObject->sourceCode.clear(); @@ -398,9 +400,9 @@ JSC_DEFINE_HOST_FUNCTION(functionCommonJSModuleRecord_compile, (JSGlobalObject * RETURN_IF_EXCEPTION(throwScope, JSValue::encode({})); String wrappedString = makeString( - "(function(module,exports,require,__dirname,__filename){"_s, + "(function(exports,require,module,__filename,__dirname){"_s, sourceString, - "\n}).call($_BunCommonJSModule_$.module.exports, $_BunCommonJSModule_$.module, $_BunCommonJSModule_$.module.exports, ($_BunCommonJSModule_$.module.require = $_BunCommonJSModule_$.module.require.bind($_BunCommonJSModule_$.module), $_BunCommonJSModule_$.module.require.path = $_BunCommonJSModule_$.module.id, $_BunCommonJSModule_$.module.require.resolve = $_BunCommonJSModule_$.module.require.resolve.bind($_BunCommonJSModule_$.module.id), $_BunCommonJSModule_$.module.require), $_BunCommonJSModule_$.__dirname, $_BunCommonJSModule_$.__filename);"_s); + "\n}).call(this.module.exports,this.module.exports,this.require,this.module,this.__filename,this.__dirname)"_s); SourceCode sourceCode = makeSource( WTFMove(wrappedString), @@ -483,14 +485,12 @@ public: ASSERT(inherits(vm, info())); reifyStaticProperties(vm, JSCommonJSModule::info(), JSCommonJSModulePrototypeTableValues, *this); - this->putDirect(vm, clientData(vm)->builtinNames().requirePublicName(), (static_cast<Zig::GlobalObject*>(globalObject))->requireFunctionUnbound(), PropertyAttribute::Builtin | PropertyAttribute::Function | 0); - this->putDirectNativeFunction( vm, globalObject, clientData(vm)->builtinNames().requirePrivateName(), 2, - jsFunctionRequireCommonJS, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | 0); + jsFunctionRequireCommonJS, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete); } }; @@ -774,21 +774,6 @@ JSValue JSCommonJSModule::id() return m_id.get(); } -bool JSCommonJSModule::put( - JSC::JSCell* cell, - JSC::JSGlobalObject* globalObject, - JSC::PropertyName propertyName, - JSC::JSValue value, - JSC::PutPropertySlot& slot) -{ - - auto& vm = globalObject->vm(); - auto* clientData = WebCore::clientData(vm); - auto throwScope = DECLARE_THROW_SCOPE(vm); - - RELEASE_AND_RETURN(throwScope, Base::put(cell, globalObject, propertyName, value, slot)); -} - template<typename, SubspaceAccess mode> JSC::GCClient::IsoSubspace* JSCommonJSModule::subspaceFor(JSC::VM& vm) { if constexpr (mode == JSC::SubspaceAccess::Concurrently) @@ -1022,7 +1007,7 @@ JSObject* JSCommonJSModule::createBoundRequireFunction(VM& vm, JSGlobalObject* l globalObject, globalObject->requireResolveFunctionUnbound(), moduleObject, - ArgList(), 1, jsString(vm, String("require"_s))); + ArgList(), 1, jsString(vm, String("resolve"_s))); requireFunction->putDirect(vm, builtinNames.resolvePublicName(), resolveFunction, PropertyAttribute::Function | 0); |