aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/ZigGlobalObject.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-09-03 03:57:43 -0700
committerGravatar GitHub <noreply@github.com> 2022-09-03 03:57:43 -0700
commit0ca42e81f31d31270c44f81d88fff33361817720 (patch)
tree27e27b32d1850a1fada1a002207e3254cbc597e8 /src/bun.js/bindings/ZigGlobalObject.cpp
parent8b2d07e82ee6249e0886109585b63429ccf3ab3a (diff)
downloadbun-0ca42e81f31d31270c44f81d88fff33361817720.tar.gz
bun-0ca42e81f31d31270c44f81d88fff33361817720.tar.zst
bun-0ca42e81f31d31270c44f81d88fff33361817720.zip
Plugin API (#1199)
* Plugin API * Fix the bugs * Implement `"object"` loader Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp')
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index 855e212f9..d378a0f4c 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -106,6 +106,8 @@
#include "ZigGeneratedClasses.h"
+#include "BunPlugin.h"
+
#if ENABLE(REMOTE_INSPECTOR)
#include "JavaScriptCore/RemoteInspectorServer.h"
#endif
@@ -162,6 +164,7 @@ using JSBuffer = WebCore::JSBuffer;
#include "../modules/EventsModule.h"
#include "../modules/ProcessModule.h"
#include "../modules/StringDecoderModule.h"
+#include "../modules/ObjectModule.h"
// #include <iostream>
static bool has_loaded_jsc = false;
@@ -2400,6 +2403,14 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm
JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0);
}
+ {
+ JSC::Identifier identifier = JSC::Identifier::fromString(vm, "plugin"_s);
+ JSFunction* pluginFunction = JSFunction::create(vm, this, 1, String("plugin"_s), jsFunctionBunPlugin, ImplementationVisibility::Public, NoIntrinsic);
+ pluginFunction->putDirectNativeFunction(vm, this, JSC::Identifier::fromString(vm, "clearAll"_s), 1, jsFunctionBunPluginClear, ImplementationVisibility::Public, NoIntrinsic,
+ JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0);
+ object->putDirect(vm, PropertyName(identifier), pluginFunction, JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0);
+ }
+
extraStaticGlobals.uncheckedAppend(
GlobalPropertyInfo { builtinNames.BunPublicName(),
JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 });
@@ -2655,6 +2666,22 @@ static JSC_DEFINE_HOST_FUNCTION(functionFulfillModuleSync,
RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined()));
RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsUndefined()));
}
+ case SyntheticModuleType::ObjectModule: {
+ JSC::EncodedJSValue encodedValue = reinterpret_cast<JSC::EncodedJSValue>(
+ bitwise_cast<int64_t>(reinterpret_cast<size_t>(res.result.value.source_code.ptr)));
+ JSC::JSObject* object = JSC::JSValue::decode(encodedValue).getObject();
+ auto function = generateObjectModuleSourceCode(
+ globalObject,
+ object);
+ auto source = JSC::SourceCode(
+ JSC::SyntheticSourceProvider::create(WTFMove(function),
+ JSC::SourceOrigin(), WTFMove(moduleKey)));
+
+ RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined()));
+ globalObject->moduleLoader()->provideFetch(globalObject, key, WTFMove(source));
+ RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined()));
+ RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsUndefined()));
+ }
case SyntheticModuleType::Process: {
auto source = JSC::SourceCode(
JSC::SyntheticSourceProvider::create(
@@ -2746,6 +2773,23 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderFetch(JSGlobalObject* globalOb
globalObject->vm().drainMicrotasks();
return promise;
}
+ case SyntheticModuleType::ObjectModule: {
+ JSC::EncodedJSValue encodedValue = reinterpret_cast<JSC::EncodedJSValue>(
+ bitwise_cast<int64_t>(reinterpret_cast<size_t>(res.result.value.source_code.ptr)));
+ JSC::JSObject* object = JSC::JSValue::decode(encodedValue).getObject();
+ auto source = JSC::SourceCode(
+ JSC::SyntheticSourceProvider::create(generateObjectModuleSourceCode(
+ globalObject,
+ object),
+ JSC::SourceOrigin(), WTFMove(moduleKey)));
+
+ auto sourceCode = JSSourceCode::create(vm, WTFMove(source));
+ RETURN_IF_EXCEPTION(scope, promise->rejectWithCaughtException(globalObject, scope));
+
+ promise->resolve(globalObject, sourceCode);
+ scope.release();
+ return promise;
+ }
case SyntheticModuleType::Buffer: {
auto source = JSC::SourceCode(
JSC::SyntheticSourceProvider::create(generateBufferSourceCode,