Skip to content

Commit

Permalink
Use JSObject instead of JSFunction in Bun.plugin (oven-sh#15968)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored and probably-neb committed Jan 7, 2025
1 parent de3cd52 commit a01b7c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
26 changes: 16 additions & 10 deletions src/bun.js/bindings/BunPlugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "BunPlugin.h"

#include "JavaScriptCore/ExceptionScope.h"
#include "JavaScriptCore/JSCast.h"
#include "headers-handwritten.h"
#include "helpers.h"
Expand Down Expand Up @@ -57,9 +58,11 @@ static JSC::EncodedJSValue jsFunctionAppendOnLoadPluginBody(JSC::JSGlobalObject*
RETURN_IF_EXCEPTION(scope, {});
JSC::RegExpObject* filter = nullptr;
if (JSValue filterValue = filterObject->getIfPropertyExists(globalObject, Identifier::fromString(vm, "filter"_s))) {
RETURN_IF_EXCEPTION(scope, {});
if (filterValue.isCell() && filterValue.asCell()->inherits<JSC::RegExpObject>())
filter = jsCast<JSC::RegExpObject*>(filterValue);
}
RETURN_IF_EXCEPTION(scope, {});

if (!filter) {
throwException(globalObject, scope, createError(globalObject, "onLoad() expects first argument to be an object with a filter RegExp"_s));
Expand All @@ -76,8 +79,8 @@ static JSC::EncodedJSValue jsFunctionAppendOnLoadPluginBody(JSC::JSGlobalObject*
return {};
}
}
RETURN_IF_EXCEPTION(scope, {});
}
RETURN_IF_EXCEPTION(scope, {});

auto func = callframe->uncheckedArgument(1);
RETURN_IF_EXCEPTION(scope, {});
Expand All @@ -87,7 +90,7 @@ static JSC::EncodedJSValue jsFunctionAppendOnLoadPluginBody(JSC::JSGlobalObject*
return {};
}

plugin.append(vm, filter->regExp(), jsCast<JSFunction*>(func), namespaceString);
plugin.append(vm, filter->regExp(), func.getObject(), namespaceString);
callback(ctx, globalObject);

return JSValue::encode(jsUndefined());
Expand Down Expand Up @@ -163,9 +166,11 @@ static JSC::EncodedJSValue jsFunctionAppendOnResolvePluginBody(JSC::JSGlobalObje
RETURN_IF_EXCEPTION(scope, {});
JSC::RegExpObject* filter = nullptr;
if (JSValue filterValue = filterObject->getIfPropertyExists(globalObject, Identifier::fromString(vm, "filter"_s))) {
RETURN_IF_EXCEPTION(scope, {});
if (filterValue.isCell() && filterValue.asCell()->inherits<JSC::RegExpObject>())
filter = jsCast<JSC::RegExpObject*>(filterValue);
}
RETURN_IF_EXCEPTION(scope, {});

if (!filter) {
throwException(globalObject, scope, createError(globalObject, "onResolve() expects first argument to be an object with a filter RegExp"_s));
Expand Down Expand Up @@ -194,7 +199,8 @@ static JSC::EncodedJSValue jsFunctionAppendOnResolvePluginBody(JSC::JSGlobalObje
return {};
}

plugin.append(vm, filter->regExp(), jsCast<JSFunction*>(func), namespaceString);
RETURN_IF_EXCEPTION(scope, {});
plugin.append(vm, filter->regExp(), jsCast<JSObject*>(func), namespaceString);
callback(ctx, globalObject);

return JSValue::encode(jsUndefined());
Expand Down Expand Up @@ -321,7 +327,7 @@ static inline JSC::EncodedJSValue setupBunPlugin(JSC::JSGlobalObject* globalObje
JSC::MarkedArgumentBuffer args;
args.append(builderObject);

JSFunction* function = jsCast<JSFunction*>(setupFunctionValue);
JSObject* function = jsCast<JSObject*>(setupFunctionValue);
JSC::CallData callData = JSC::getCallData(function);
JSValue result = call(globalObject, function, callData, JSC::jsUndefined(), args);

Expand All @@ -334,13 +340,13 @@ static inline JSC::EncodedJSValue setupBunPlugin(JSC::JSGlobalObject* globalObje
RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined()));
}

void BunPlugin::Group::append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func)
void BunPlugin::Group::append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSObject* func)
{
filters.append(JSC::Strong<JSC::RegExp> { vm, filter });
callbacks.append(JSC::Strong<JSC::JSFunction> { vm, func });
callbacks.append(JSC::Strong<JSC::JSObject> { vm, func });
}

void BunPlugin::Base::append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func, String& namespaceString)
void BunPlugin::Base::append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSObject* func, String& namespaceString)
{
if (namespaceString.isEmpty() || namespaceString == "file"_s) {
this->fileNamespace.append(vm, filter, func);
Expand All @@ -354,7 +360,7 @@ void BunPlugin::Base::append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction*
}
}

JSFunction* BunPlugin::Group::find(JSC::JSGlobalObject* globalObject, String& path)
JSC::JSObject* BunPlugin::Group::find(JSC::JSGlobalObject* globalObject, String& path)
{
size_t count = filters.size();
for (size_t i = 0; i < count; i++) {
Expand Down Expand Up @@ -691,7 +697,7 @@ EncodedJSValue BunPlugin::OnLoad::run(JSC::JSGlobalObject* globalObject, BunStri

auto pathString = path->toWTFString(BunString::ZeroCopy);

JSC::JSFunction* function = group.find(globalObject, pathString);
auto* function = group.find(globalObject, pathString);
if (!function) {
return JSValue::encode(JSC::jsUndefined());
}
Expand Down Expand Up @@ -777,7 +783,7 @@ EncodedJSValue BunPlugin::OnResolve::run(JSC::JSGlobalObject* globalObject, BunS
if (!filters[i].get()->match(globalObject, pathString, 0)) {
continue;
}
JSC::JSFunction* function = callbacks[i].get();
auto* function = callbacks[i].get();
if (UNLIKELY(!function)) {
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/bun.js/bindings/BunPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class BunPlugin {
// We want JIT!
// TODO: evaluate if using JSInternalFieldImpl(2) is faster
Vector<JSC::Strong<JSC::RegExp>> filters = {};
Vector<JSC::Strong<JSC::JSFunction>> callbacks = {};
Vector<JSC::Strong<JSC::JSObject>> callbacks = {};
BunPluginTarget target { BunPluginTargetBun };

void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func);
JSFunction* find(JSC::JSGlobalObject* globalObj, String& path);
void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSObject* func);
JSObject* find(JSC::JSGlobalObject* globalObj, String& path);
void clear()
{
filters.clear();
Expand Down Expand Up @@ -58,7 +58,7 @@ class BunPlugin {
return nullptr;
}

void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func, String& namespaceString);
void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSObject* func, String& namespaceString);
};

class OnLoad final : public Base {
Expand Down

0 comments on commit a01b7c5

Please sign in to comment.