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/modules/NodeModuleModule.h | |
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/modules/NodeModuleModule.h')
-rw-r--r-- | src/bun.js/modules/NodeModuleModule.h | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/bun.js/modules/NodeModuleModule.h b/src/bun.js/modules/NodeModuleModule.h index 6d8654024..ddb273de4 100644 --- a/src/bun.js/modules/NodeModuleModule.h +++ b/src/bun.js/modules/NodeModuleModule.h @@ -304,6 +304,39 @@ JSC_DEFINE_CUSTOM_SETTER(set_resolveFilename, return false; } +// These two setters are only used if you directly hit +// `Module.prototype.require` or `module.require`. When accessing the cjs +// require argument, this is a bound version of `require`, which calls into the +// overridden one. +// +// This require function also intentionally does not have .resolve on it, nor +// does it have any of the other properties. +// +// Note: allowing require to be overridable at all is only needed for Next.js to +// work (they do Module.prototype.require = ...) + +JSC_DEFINE_CUSTOM_GETTER(getterRequireFunction, + (JSC::JSGlobalObject * globalObject, + JSC::EncodedJSValue thisValue, JSC::PropertyName)) { + return JSValue::encode(globalObject->getDirect( + globalObject->vm(), WebCore::clientData(globalObject->vm()) + ->builtinNames() + .overridableRequirePrivateName())); +} + +JSC_DEFINE_CUSTOM_SETTER(setterRequireFunction, + (JSC::JSGlobalObject * globalObject, + JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, + JSC::PropertyName propertyName)) { + globalObject->putDirect(globalObject->vm(), + WebCore::clientData(globalObject->vm()) + ->builtinNames() + .overridableRequirePrivateName(), + JSValue::decode(value), 0); + return true; +} + namespace Zig { DEFINE_NATIVE_MODULE(NodeModule) { @@ -371,8 +404,15 @@ DEFINE_NATIVE_MODULE(NodeModule) { put(Identifier::fromString(vm, "globalPaths"_s), constructEmptyArray(globalObject, nullptr, 0)); - put(Identifier::fromString(vm, "prototype"_s), - constructEmptyObject(globalObject)); + auto prototype = + constructEmptyObject(globalObject, globalObject->objectPrototype(), 1); + prototype->putDirectCustomAccessor( + vm, JSC::Identifier::fromString(vm, "require"_s), + JSC::CustomGetterSetter::create(vm, getterRequireFunction, + setterRequireFunction), + 0); + + defaultObject->putDirect(vm, vm.propertyNames->prototype, prototype); JSC::JSArray *builtinModules = JSC::JSArray::create( vm, |