From 57a06745a48093c25d0f4729ccea41a918d6427d Mon Sep 17 00:00:00 2001 From: dave caruso Date: Thu, 7 Sep 2023 04:58:44 -0700 Subject: Progress for Next.js (#4468) * L * ipc * asdfghjkl * dfghjk * it works! * types * patches for next.js * sdfghj * wsdfgn,./ * this * yolo * okay loser * asdfghjk * add some more APIs * MESS * sdfghjkl * remove native events from streams * stuff * remove lazy(primordials) test * debugging * okay * less fake extensions object * fix `Buffer.toString()` args logic * fix deserialize * make tests work * add test for `Buffer.toString` args * Update server.zig * remove test * update test * Update spawn-streaming-stdin.test.ts * fix linux build * Update fs.test.ts * cli message improvements * dfshaj * Fix fs.watch bug maybe? * remove --------- Co-authored-by: Dylan Conway --- src/bun.js/modules/NodeModuleModule.h | 44 +++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/bun.js/modules/NodeModuleModule.h') diff --git a/src/bun.js/modules/NodeModuleModule.h b/src/bun.js/modules/NodeModuleModule.h index 7c8f6ee14..b3c34eb5e 100644 --- a/src/bun.js/modules/NodeModuleModule.h +++ b/src/bun.js/modules/NodeModuleModule.h @@ -270,6 +270,40 @@ template consteval std::size_t countof(T (&)[N]) { return N; } +JSC_DEFINE_CUSTOM_GETTER(get_resolveFilename, (JSGlobalObject * globalObject, + EncodedJSValue thisValue, + PropertyName propertyName)) { + auto override = static_cast(globalObject) + ->m_nodeModuleOverriddenResolveFilename.get(); + if (override) { + return JSValue::encode(override); + } + // Instead of storing the original function on the global object and have + // those extra bytes, just have it be a property alias. + JSObject *thisObject = JSValue::decode(thisValue).getObject(); + if (!thisObject) + return JSValue::encode(jsUndefined()); + auto &vm = globalObject->vm(); + return JSValue::encode(thisObject->getDirect( + vm, Identifier::fromString(vm, "__resolveFilename"_s))); +} + +JSC_DEFINE_CUSTOM_SETTER(set_resolveFilename, + (JSGlobalObject * globalObject, + EncodedJSValue thisValue, EncodedJSValue value, + PropertyName propertyName)) { + auto valueJS = JSValue::decode(value); + if (valueJS.isCell()) { + if (auto fn = jsDynamicCast(valueJS.asCell())) { + static_cast(globalObject) + ->m_nodeModuleOverriddenResolveFilename.set(globalObject->vm(), + globalObject, fn); + return true; + } + } + return false; +} + namespace Zig { DEFINE_NATIVE_MODULE(NodeModule) { @@ -303,6 +337,14 @@ DEFINE_NATIVE_MODULE(NodeModule) { put(Identifier::fromString(vm, "Module"_s), defaultObject); + defaultObject->putDirectCustomAccessor( + vm, JSC::Identifier::fromString(vm, "_resolveFilename"_s), + 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), @@ -314,8 +356,6 @@ DEFINE_NATIVE_MODULE(NodeModule) { putNativeFn(Identifier::fromString(vm, "SourceMap"_s), jsFunctionSourceMap); putNativeFn(Identifier::fromString(vm, "isBuiltin"_s), jsFunctionIsBuiltinModule); - putNativeFn(Identifier::fromString(vm, "_resolveFilename"_s), - jsFunctionResolveFileName); putNativeFn(Identifier::fromString(vm, "_nodeModulePaths"_s), Resolver__nodeModulePathsForJS); putNativeFn(Identifier::fromString(vm, "wrap"_s), jsFunctionWrap); -- cgit v1.2.3