aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/ModuleLoader.h
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-05 23:05:22 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-05 23:05:22 -0700
commit11aa17a57cc679d34e8e6f6f7aa665f565cb7305 (patch)
treeccb9a605cb4bcc42c6b2665ddbf8d04af72d94c7 /src/bun.js/bindings/ModuleLoader.h
parentd2397b60e79e4386c6a7b7a9783a6f8e379a5ae0 (diff)
downloadbun-11aa17a57cc679d34e8e6f6f7aa665f565cb7305.tar.gz
bun-11aa17a57cc679d34e8e6f6f7aa665f565cb7305.tar.zst
bun-11aa17a57cc679d34e8e6f6f7aa665f565cb7305.zip
Support async `onLoad` callbacks in `Bun.plugin`
Diffstat (limited to 'src/bun.js/bindings/ModuleLoader.h')
-rw-r--r--src/bun.js/bindings/ModuleLoader.h94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/bun.js/bindings/ModuleLoader.h b/src/bun.js/bindings/ModuleLoader.h
new file mode 100644
index 000000000..98f8b7dbb
--- /dev/null
+++ b/src/bun.js/bindings/ModuleLoader.h
@@ -0,0 +1,94 @@
+#include "root.h"
+#include "headers-handwritten.h"
+
+#include "JavaScriptCore/JSCInlines.h"
+#include "BunClientData.h"
+
+namespace Zig {
+class GlobalObject;
+}
+
+namespace JSC {
+class JSInternalPromise;
+}
+
+namespace Bun {
+using namespace JSC;
+
+typedef uint8_t OnLoadResultType;
+const OnLoadResultType OnLoadResultTypeError = 0;
+const OnLoadResultType OnLoadResultTypeCode = 1;
+const OnLoadResultType OnLoadResultTypeObject = 2;
+const OnLoadResultType OnLoadResultTypePromise = 3;
+
+struct CodeString {
+ ZigString string;
+ JSC::JSValue value;
+ BunLoaderType loader;
+};
+
+union OnLoadResultValue {
+ CodeString sourceText;
+ JSC::JSValue object;
+ JSC::JSValue promise;
+ JSC::JSValue error;
+};
+
+struct OnLoadResult {
+ OnLoadResultValue value;
+ OnLoadResultType type;
+};
+
+class PendingVirtualModuleResult : public JSC::JSInternalFieldObjectImpl<3> {
+public:
+ using Base = JSC::JSInternalFieldObjectImpl<3>;
+
+ template<typename, JSC::SubspaceAccess mode> static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
+ {
+ if constexpr (mode == JSC::SubspaceAccess::Concurrently)
+ return nullptr;
+ return WebCore::subspaceForImpl<PendingVirtualModuleResult, WebCore::UseCustomHeapCellType::No>(
+ vm,
+ [](auto& spaces) { return spaces.m_clientSubspaceForPendingVirtualModuleResult.get(); },
+ [](auto& spaces, auto&& space) { spaces.m_clientSubspaceForPendingVirtualModuleResult = WTFMove(space); },
+ [](auto& spaces) { return spaces.m_subspaceForPendingVirtualModuleResult.get(); },
+ [](auto& spaces, auto&& space) { spaces.m_subspaceForPendingVirtualModuleResult = WTFMove(space); });
+ }
+
+ JS_EXPORT_PRIVATE static PendingVirtualModuleResult* create(VM&, Structure*);
+ static PendingVirtualModuleResult* create(JSC::JSGlobalObject* globalObject, const WTF::String& specifier, const WTF::String& referrer);
+ static PendingVirtualModuleResult* createWithInitialValues(VM&, Structure*);
+ static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
+
+ JSC::JSInternalPromise* internalPromise();
+
+ static std::array<JSValue, numberOfInternalFields> initialValues()
+ {
+ return { {
+ jsUndefined(),
+ jsUndefined(),
+ jsUndefined(),
+ } };
+ }
+
+ DECLARE_EXPORT_INFO;
+ DECLARE_VISIT_CHILDREN;
+
+ PendingVirtualModuleResult(JSC::VM&, JSC::Structure*);
+ void finishCreation(JSC::VM&, const WTF::String& specifier, const WTF::String& referrer);
+};
+
+OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::JSValue objectValue);
+JSValue fetchSourceCodeSync(
+ Zig::GlobalObject* globalObject,
+ ErrorableResolvedSource* res,
+ ZigString* specifier,
+ ZigString* referrer);
+
+JSValue fetchSourceCodeAsync(
+ Zig::GlobalObject* globalObject,
+ ErrorableResolvedSource* res,
+ ZigString* specifier,
+ ZigString* referrer);
+
+} // namespace Bun \ No newline at end of file