diff options
author | 2022-09-05 23:05:22 -0700 | |
---|---|---|
committer | 2022-09-05 23:05:22 -0700 | |
commit | 11aa17a57cc679d34e8e6f6f7aa665f565cb7305 (patch) | |
tree | ccb9a605cb4bcc42c6b2665ddbf8d04af72d94c7 /src/bun.js/bindings/bindings.cpp | |
parent | d2397b60e79e4386c6a7b7a9783a6f8e379a5ae0 (diff) | |
download | bun-11aa17a57cc679d34e8e6f6f7aa665f565cb7305.tar.gz bun-11aa17a57cc679d34e8e6f6f7aa665f565cb7305.tar.zst bun-11aa17a57cc679d34e8e6f6f7aa665f565cb7305.zip |
Support async `onLoad` callbacks in `Bun.plugin`
Diffstat (limited to 'src/bun.js/bindings/bindings.cpp')
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 81e9c602b..bb0718ea9 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -873,27 +873,33 @@ bool JSC__JSModuleLoader__checkSyntax(JSC__JSGlobalObject* arg0, const JSC__Sour return result; } -JSC__JSValue JSC__JSModuleLoader__evaluate(JSC__JSGlobalObject* arg0, const unsigned char* arg1, +JSC__JSValue JSC__JSModuleLoader__evaluate(JSC__JSGlobalObject* globalObject, const unsigned char* arg1, size_t arg2, const unsigned char* arg3, size_t arg4, JSC__JSValue JSValue5, JSC__JSValue* arg6) { - WTF::String src = WTF::String(WTF::StringImpl::createWithoutCopying(arg1, arg2)); - WTF::URL origin = WTF::URL::fileURLWithFileSystemPath(WTF::StringView(arg3, arg4)); + WTF::String src = WTF::String::fromUTF8(arg1, arg2).isolatedCopy(); + WTF::URL origin = WTF::URL::fileURLWithFileSystemPath(WTF::String(WTF::StringImpl::createWithoutCopying(arg3, arg4))).isolatedCopy(); - JSC::VM& vm = arg0->vm(); - JSC::JSLockHolder locker(vm); + JSC::VM& vm = globalObject->vm(); JSC::SourceCode sourceCode = JSC::makeSource( - src, JSC::SourceOrigin { origin }, origin.lastPathComponent().toStringWithoutCopying(), + src, JSC::SourceOrigin { origin }, origin.fileSystemPath(), WTF::TextPosition(), JSC::SourceProviderSourceType::Module); - WTF::NakedPtr<JSC::Exception> exception; - auto val = JSC::evaluate(arg0, sourceCode, JSC::JSValue(), exception); - if (exception.get()) { - *arg6 = JSC::JSValue::encode(JSC::JSValue(exception.get())); + globalObject->moduleLoader()->provideFetch(globalObject, jsString(vm, origin.fileSystemPath()), WTFMove(sourceCode)); + auto* promise = JSC::importModule(globalObject, JSC::Identifier::fromString(vm, origin.fileSystemPath()), JSValue(), JSValue()); + + if (promise->status(vm) == JSC::JSPromise::Status::Pending) { + vm.drainMicrotasks(); } - vm.drainMicrotasks(); - return JSC::JSValue::encode(val); + if (promise->status(vm) == JSC::JSPromise::Status::Fulfilled) { + return JSC::JSValue::encode(promise->result(vm)); + } else if (promise->status(vm) == JSC::JSPromise::Status::Rejected) { + *arg6 = JSC::JSValue::encode(promise->result(vm)); + return JSC::JSValue::encode(JSC::jsUndefined()); + } else { + return JSC::JSValue::encode(promise); + } } JSC__JSInternalPromise* JSC__JSModuleLoader__importModule(JSC__JSGlobalObject* arg0, const JSC__Identifier* arg1) |