aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-06-26 08:12:37 -0700
committerGravatar GitHub <noreply@github.com> 2023-06-26 08:12:37 -0700
commitec3ed67bc9ad8cbb0e59234564d57265d5423fce (patch)
tree4ed05a075e303c8f620aaee2854df419b1cb8ae8 /src/bun.js
parent76626ac54b83a5a9a24abee1bc35f13a2196504d (diff)
downloadbun-ec3ed67bc9ad8cbb0e59234564d57265d5423fce.tar.gz
bun-ec3ed67bc9ad8cbb0e59234564d57265d5423fce.tar.zst
bun-ec3ed67bc9ad8cbb0e59234564d57265d5423fce.zip
implement `_nodeModulePaths` and `require.main.paths` (#3411)
* tests in progress * add `require.main.paths`, add every dir up to root * remove imports
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/bindings/CommonJSModuleRecord.cpp31
-rw-r--r--src/bun.js/bindings/CommonJSModuleRecord.h1
-rw-r--r--src/bun.js/modules/NodeModuleModule.cpp15
3 files changed, 36 insertions, 11 deletions
diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp
index 8d4fe0a1e..3615db774 100644
--- a/src/bun.js/bindings/CommonJSModuleRecord.cpp
+++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp
@@ -295,6 +295,35 @@ JSC_DEFINE_CUSTOM_SETTER(setterPath,
return true;
}
+extern "C" EncodedJSValue Resolver__propForRequireMainPaths(JSGlobalObject*);
+
+JSC_DEFINE_CUSTOM_GETTER(getterPaths, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
+{
+ JSCommonJSModule* thisObject = jsDynamicCast<JSCommonJSModule*>(JSValue::decode(thisValue));
+ if (UNLIKELY(!thisObject)) {
+ return JSValue::encode(jsUndefined());
+ }
+
+ if (!thisObject->m_paths) {
+ JSValue paths = JSValue::decode(Resolver__propForRequireMainPaths(globalObject));
+ thisObject->m_paths.set(globalObject->vm(), thisObject, paths);
+ }
+
+ return JSValue::encode(thisObject->m_paths.get());
+}
+
+JSC_DEFINE_CUSTOM_SETTER(setterPaths,
+ (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
+ JSC::EncodedJSValue value, JSC::PropertyName propertyName))
+{
+ JSCommonJSModule* thisObject = jsDynamicCast<JSCommonJSModule*>(JSValue::decode(thisValue));
+ if (!thisObject)
+ return false;
+
+ thisObject->m_paths.set(globalObject->vm(), thisObject, JSValue::decode(value));
+ return true;
+}
+
JSC_DEFINE_CUSTOM_SETTER(setterFilename,
(JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
JSC::EncodedJSValue value, JSC::PropertyName propertyName))
@@ -340,6 +369,7 @@ static const struct HashTableValue JSCommonJSModulePrototypeTableValues[] = {
{ "loaded"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createLoaded } },
{ "parent"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback | PropertyAttribute::DontEnum | 0), NoIntrinsic, { HashTableValue::LazyPropertyType, createParent } },
{ "path"_s, static_cast<unsigned>(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterPath, setterPath } },
+ { "paths"_s, static_cast<unsigned>(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, getterPaths, setterPaths } },
};
class JSCommonJSModulePrototype final : public JSC::JSNonFinalObject {
@@ -675,6 +705,7 @@ void JSCommonJSModule::visitChildrenImpl(JSCell* cell, Visitor& visitor)
visitor.append(thisObject->sourceCode);
visitor.append(thisObject->m_filename);
visitor.append(thisObject->m_dirname);
+ visitor.append(thisObject->m_paths);
}
DEFINE_VISIT_CHILDREN(JSCommonJSModule);
diff --git a/src/bun.js/bindings/CommonJSModuleRecord.h b/src/bun.js/bindings/CommonJSModuleRecord.h
index 48f14b39c..a96ab5f75 100644
--- a/src/bun.js/bindings/CommonJSModuleRecord.h
+++ b/src/bun.js/bindings/CommonJSModuleRecord.h
@@ -24,6 +24,7 @@ public:
mutable JSC::WriteBarrier<JSC::JSString> m_id;
mutable JSC::WriteBarrier<JSC::JSString> m_filename;
mutable JSC::WriteBarrier<JSC::JSString> m_dirname;
+ mutable JSC::WriteBarrier<Unknown> m_paths;
mutable JSC::WriteBarrier<JSC::JSSourceCode> sourceCode;
static void destroy(JSC::JSCell*);
diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp
index 8b278ddd8..34d45698f 100644
--- a/src/bun.js/modules/NodeModuleModule.cpp
+++ b/src/bun.js/modules/NodeModuleModule.cpp
@@ -26,15 +26,8 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleCreateRequire,
scope, JSValue::encode(Zig::ImportMetaObject::createRequireFunction(
vm, globalObject, val)));
}
-JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModulePaths,
- (JSC::JSGlobalObject * globalObject,
- JSC::CallFrame *callFrame)) {
- return JSC::JSValue::encode(JSC::JSArray::create(
- globalObject->vm(),
- globalObject->arrayStructureForIndexingTypeDuringAllocation(
- ArrayWithContiguous),
- 0));
-}
+extern "C" EncodedJSValue Resolver__nodeModulePathsForJS(JSGlobalObject *,
+ CallFrame *);
JSC_DEFINE_HOST_FUNCTION(jsFunctionFindSourceMap,
(JSGlobalObject * globalObject,
@@ -114,7 +107,7 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject,
vm, globalObject, 1, String("createRequire"_s),
jsFunctionNodeModuleCreateRequire, ImplementationVisibility::Public));
exportValues.append(JSFunction::create(vm, globalObject, 1, String("paths"_s),
- jsFunctionNodeModulePaths,
+ Resolver__nodeModulePathsForJS,
ImplementationVisibility::Public));
exportValues.append(JSFunction::create(
vm, globalObject, 1, String("findSourceMap"_s), jsFunctionFindSourceMap,
@@ -143,7 +136,7 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject,
exportNames.append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s));
exportValues.append(JSFunction::create(
vm, globalObject, 0, String("_nodeModulePaths"_s),
- jsFunctionNodeModulePaths, ImplementationVisibility::Public));
+ Resolver__nodeModulePathsForJS, ImplementationVisibility::Public));
exportNames.append(JSC::Identifier::fromString(vm, "_cache"_s));
exportValues.append(