Introduce high-performance native addon API in Bun.build, starting with build.onBeforeParse hook (#14971)

Co-authored-by: Zack Radisic <56137411+zackradisic@users.noreply.github.com>
Co-authored-by: zackradisic <zackradisic@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2024-12-04 22:35:43 -08:00
committed by GitHub
parent dd5c40dab7
commit 784bc4e012
61 changed files with 4234 additions and 123 deletions

View File

@@ -40,6 +40,7 @@
#include "ErrorCode.h"
#include "napi_handle_scope.h"
#include "napi_external.h"
#ifndef WIN32
#include <errno.h>
@@ -359,6 +360,8 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen,
void* handle = dlopen(utf8.data(), RTLD_LAZY);
#endif
globalObject->m_pendingNapiModuleDlopenHandle = handle;
Bun__process_dlopen_count++;
if (!handle) {
@@ -425,10 +428,18 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen,
EncodedJSValue exportsValue = JSC::JSValue::encode(exports);
JSC::JSValue resultValue = JSValue::decode(napi_register_module_v1(globalObject, exportsValue));
// TODO: think about the finalizer here
// currently we do not dealloc napi modules so we don't have to worry about it right now
auto* meta = new Bun::NapiModuleMeta(globalObject->m_pendingNapiModuleDlopenHandle);
Bun::NapiExternal* napi_external = Bun::NapiExternal::create(vm, globalObject->NapiExternalStructure(), meta, nullptr, nullptr);
bool success = resultValue.getObject()->putDirect(vm, WebCore::builtinNames(vm).napiDlopenHandlePrivateName(), napi_external, JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
ASSERT(success);
RETURN_IF_EXCEPTION(scope, {});
globalObject->m_pendingNapiModuleAndExports[0].clear();
globalObject->m_pendingNapiModuleAndExports[1].clear();
globalObject->m_pendingNapiModuleDlopenHandle = nullptr;
// https://github.com/nodejs/node/blob/2eff28fb7a93d3f672f80b582f664a7c701569fb/src/node_api.cc#L734-L742
// https://github.com/oven-sh/bun/issues/1288