aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/bindings/Process.cpp15
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp2
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.h6
-rw-r--r--src/bun.js/bindings/exports.zig1
-rw-r--r--src/bun.js/bindings/napi.cpp43
-rw-r--r--src/bun.js/javascript.zig12
6 files changed, 59 insertions, 20 deletions
diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp
index f1900da9a..c3a98d876 100644
--- a/src/bun.js/bindings/Process.cpp
+++ b/src/bun.js/bindings/Process.cpp
@@ -111,8 +111,9 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionNextTick,
static JSC_DECLARE_HOST_FUNCTION(Process_functionDlopen);
static JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen,
- (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
+ (JSC::JSGlobalObject * globalObject_, JSC::CallFrame* callFrame))
{
+ Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(globalObject_);
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
JSC::VM& vm = globalObject->vm();
@@ -133,6 +134,8 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen,
WTF::String filename = callFrame->uncheckedArgument(1).toWTFString(globalObject);
CString utf8 = filename.utf8();
+
+ globalObject->pendingNapiModule = exports;
void* handle = dlopen(utf8.data(), RTLD_LAZY);
if (!handle) {
@@ -141,6 +144,16 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen,
return JSC::JSValue::encode(JSC::JSValue {});
}
+ if (JSValue pendingModule = globalObject->pendingNapiModule) {
+ globalObject->pendingNapiModule = JSValue {};
+ if (pendingModule.isCell() && pendingModule.getObject()->isErrorInstance()) {
+ JSC::throwException(globalObject, scope, pendingModule);
+ return JSC::JSValue::encode(JSC::JSValue {});
+ }
+ return JSC::JSValue::encode(pendingModule);
+ }
+ globalObject->pendingNapiModule = JSValue {};
+
JSC::EncodedJSValue (*napi_register_module_v1)(JSC::JSGlobalObject * globalObject,
JSC::EncodedJSValue exports);
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index 31abac3c3..8e8608833 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -284,8 +284,6 @@ extern "C" bool Zig__GlobalObject__resetModuleRegistryMap(JSC__JSGlobalObject* g
return true;
}
-extern "C" void Bun__reportError(JSC__JSGlobalObject*, JSC__JSValue);
-
#define GENERATED_CONSTRUCTOR_GETTER(ConstructorName) \
JSC_DECLARE_CUSTOM_GETTER(ConstructorName##_getter); \
JSC_DEFINE_CUSTOM_GETTER(ConstructorName##_getter, \
diff --git a/src/bun.js/bindings/ZigGlobalObject.h b/src/bun.js/bindings/ZigGlobalObject.h
index 144e43bd8..6c5c23b3a 100644
--- a/src/bun.js/bindings/ZigGlobalObject.h
+++ b/src/bun.js/bindings/ZigGlobalObject.h
@@ -33,6 +33,9 @@ class EventLoopTask;
#include "DOMWrapperWorld-class.h"
#include "DOMIsoSubspaces.h"
#include "BunPlugin.h"
+
+extern "C" void Bun__reportError(JSC__JSGlobalObject*, JSC__JSValue);
+
// #include "EventTarget.h"
// namespace WebCore {
@@ -308,6 +311,9 @@ public:
BunPlugin::OnResolve onResolvePlugins[BunPluginTargetMax + 1] {};
BunPluginTarget defaultBunPluginTarget = BunPluginTargetBun;
+ // When a napi module initializes on dlopen, we need to know what the value is
+ JSValue pendingNapiModule = JSValue {};
+
#include "ZigGeneratedClasses+lazyStructureHeader.h"
private:
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig
index 20bc1195b..2186177ab 100644
--- a/src/bun.js/bindings/exports.zig
+++ b/src/bun.js/bindings/exports.zig
@@ -250,6 +250,7 @@ pub const ResolvedSource = extern struct {
@"node:process" = 1025,
@"node:events" = 1026,
@"node:string_decoder" = 1027,
+ @"node:module" = 1028,
};
};
diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp
index 3029ff102..b6deb3a56 100644
--- a/src/bun.js/bindings/napi.cpp
+++ b/src/bun.js/bindings/napi.cpp
@@ -47,6 +47,10 @@
#include "JavaScriptCore/JSSourceCode.h"
#include "JavaScriptCore/JSNativeStdFunction.h"
+#include "../modules/ObjectModule.h"
+
+#include "JavaScriptCore/JSSourceCode.h"
+
// #include <iostream>
using namespace JSC;
using namespace Zig;
@@ -474,18 +478,43 @@ extern "C" void napi_module_register(napi_module* mod)
{
auto* globalObject = Bun__getDefaultGlobal();
JSC::VM& vm = globalObject->vm();
- JSC::JSObject* object = JSC::constructEmptyObject(globalObject);
+ JSC::JSObject* object = globalObject->pendingNapiModule.getObject();
+ if (!object) {
+ object = JSC::constructEmptyObject(globalObject);
+ } else {
+ globalObject->pendingNapiModule = JSC::JSValue();
+ }
+
+ EnsureStillAliveScope ensureAlive(object);
auto result = reinterpret_cast<JSC::EncodedJSValue>(
mod->nm_register_func(reinterpret_cast<napi_env>(globalObject), reinterpret_cast<napi_value>(JSC::JSValue::encode(JSC::JSValue(object)))));
- // std::cout << "loaded " << mod->nm_modname << std::endl;
auto keyStr = WTF::String::fromUTF8(mod->nm_modname);
- auto key = JSC::jsString(vm, keyStr);
- auto sourceCode = Napi::generateSourceCode(keyStr, vm, object, globalObject);
+ JSC::JSValue resultValue = JSC::JSValue::decode(result);
+ EnsureStillAliveScope ensureAlive2(resultValue);
+ if (resultValue.isEmpty()) {
+ globalObject->pendingNapiModule = createError(globalObject, makeString("Node-API module \""_s, keyStr, "\" returned an error"_s));
+ EnsureStillAliveScope ensureAlive(globalObject->pendingNapiModule);
+ return;
+ }
+
+ if (!resultValue.isObject()) {
+ globalObject->pendingNapiModule = createError(globalObject, makeString("Expected Node-API module \""_s, keyStr, "\" to return an exports object"_s));
+ EnsureStillAliveScope ensureAlive(globalObject->pendingNapiModule);
+ return;
+ }
+
+ // std::cout << "loaded " << mod->nm_modname << std::endl;
+
+ auto source = JSC::SourceCode(
+ JSC::SyntheticSourceProvider::create(generateObjectModuleSourceCode(
+ globalObject,
+ object),
+ JSC::SourceOrigin(), keyStr));
- globalObject->moduleLoader()->provideFetch(globalObject, key, WTFMove(sourceCode));
- auto promise = globalObject->moduleLoader()->loadAndEvaluateModule(globalObject, key, jsUndefined(), jsUndefined());
- vm.drainMicrotasks();
+ // Add it to the ESM registry
+ globalObject->moduleLoader()->provideFetch(globalObject, JSC::jsString(vm, WTFMove(keyStr)), WTFMove(source));
+ globalObject->pendingNapiModule = object;
}
extern "C" napi_status napi_wrap(napi_env env,
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 54486e38a..5fc440ee7 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -823,6 +823,7 @@ pub const VirtualMachine = struct {
.@"node:string_decoder" => return jsSyntheticModule(.@"node:string_decoder"),
.@"node:module" => return jsSyntheticModule(.@"node:module"),
.@"node:events" => return jsSyntheticModule(.@"node:events"),
+ .@"node:process" => return jsSyntheticModule(.@"node:process"),
.@"node:stream" => {
return ResolvedSource{
.allocator = null,
@@ -851,16 +852,7 @@ pub const VirtualMachine = struct {
.hash = 0,
};
},
- .@"node:process" => {
- return ResolvedSource{
- .allocator = null,
- .source_code = ZigString.init(""),
- .specifier = ZigString.init("node:process"),
- .source_url = ZigString.init("node:process"),
- .hash = 0,
- .tag = ResolvedSource.Tag.@"node:process",
- };
- },
+
.@"node:os" => {
return ResolvedSource{
.allocator = null,