Use BunString in JSBundlerPlugin (#3557)

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2023-07-07 21:20:30 -07:00
committed by GitHub
parent 386d8b7836
commit 52b7962dae
3 changed files with 67 additions and 41 deletions

View File

@@ -26,7 +26,7 @@ const strings = bun.strings;
const NewClass = Base.NewClass;
const To = Base.To;
const Request = WebCore.Request;
const String = bun.String;
const FetchEvent = WebCore.FetchEvent;
const MacroMap = @import("../../resolver/package_json.zig").MacroMap;
const TSConfigJSON = @import("../../resolver/tsconfig_json.zig").TSConfigJSON;
@@ -871,16 +871,16 @@ pub const JSBundler = struct {
extern fn JSBundlerPlugin__anyMatches(
*Plugin,
namespaceString: *const ZigString,
path: *const ZigString,
namespaceString: *const String,
path: *const String,
bool,
) bool;
extern fn JSBundlerPlugin__matchOnLoad(
*JSC.JSGlobalObject,
*Plugin,
namespaceString: *const ZigString,
path: *const ZigString,
namespaceString: *const String,
path: *const String,
context: *anyopaque,
u8,
) void;
@@ -888,9 +888,9 @@ pub const JSBundler = struct {
extern fn JSBundlerPlugin__matchOnResolve(
*JSC.JSGlobalObject,
*Plugin,
namespaceString: *const ZigString,
path: *const ZigString,
importer: *const ZigString,
namespaceString: *const String,
path: *const String,
importer: *const String,
context: *anyopaque,
u8,
) void;
@@ -905,10 +905,10 @@ pub const JSBundler = struct {
defer tracer.end();
const namespace_string = if (path.isFile())
ZigString.Empty
bun.String.empty
else
ZigString.fromUTF8(path.namespace);
const path_string = ZigString.fromUTF8(path.text);
bun.String.create(path.namespace);
const path_string = bun.String.create(path.text);
return JSBundlerPlugin__anyMatches(this, &namespace_string, &path_string, is_onLoad);
}
@@ -924,10 +924,12 @@ pub const JSBundler = struct {
const tracer = bun.tracy.traceNamed(@src(), "JSBundler.matchOnLoad");
defer tracer.end();
const namespace_string = if (namespace.len == 0)
ZigString.init("file")
bun.String.static("file")
else
ZigString.fromUTF8(namespace);
const path_string = ZigString.fromUTF8(path);
bun.String.create(namespace);
const path_string = bun.String.create(path);
defer namespace_string.deref();
defer path_string.deref();
JSBundlerPlugin__matchOnLoad(globalThis, this, &namespace_string, &path_string, context, @intFromEnum(default_loader));
}
@@ -944,11 +946,14 @@ pub const JSBundler = struct {
const tracer = bun.tracy.traceNamed(@src(), "JSBundler.matchOnResolve");
defer tracer.end();
const namespace_string = if (strings.eqlComptime(namespace, "file"))
ZigString.Empty
bun.String.empty
else
ZigString.fromUTF8(namespace);
const path_string = ZigString.fromUTF8(path);
const importer_string = ZigString.fromUTF8(importer);
bun.String.create(namespace);
const path_string = bun.String.create(path);
const importer_string = bun.String.create(importer);
defer namespace_string.deref();
defer path_string.deref();
defer importer_string.deref();
JSBundlerPlugin__matchOnResolve(globalThis, this, &namespace_string, &path_string, &importer_string, context, @intFromEnum(import_record_kind));
}

View File

@@ -54,7 +54,7 @@ void BundlerPlugin::NamespaceList::append(JSC::VM& vm, JSC::RegExp* filter, Stri
nsGroup->append(WTFMove(regex));
}
bool BundlerPlugin::anyMatchesCrossThread(JSC::VM& vm, const ZigString* namespaceStr, const ZigString* path, bool isOnLoad)
bool BundlerPlugin::anyMatchesCrossThread(JSC::VM& vm, const BunString* namespaceStr, const BunString* path, bool isOnLoad)
{
constexpr bool usesPatternContextBuffer = false;
if (isOnLoad) {
@@ -62,7 +62,7 @@ bool BundlerPlugin::anyMatchesCrossThread(JSC::VM& vm, const ZigString* namespac
return false;
// Avoid unnecessary string copies
auto namespaceString = namespaceStr ? Zig::toString(*namespaceStr) : String();
auto namespaceString = namespaceStr ? Bun::toWTFString(*namespaceStr) : String();
auto* group = this->onLoad.group(namespaceString);
if (group == nullptr) {
@@ -70,7 +70,7 @@ bool BundlerPlugin::anyMatchesCrossThread(JSC::VM& vm, const ZigString* namespac
}
auto& filters = *group;
auto pathString = Zig::toString(*path);
auto pathString = Bun::toWTFString(*path);
for (auto& filter : filters) {
Yarr::MatchingContextHolder regExpContext(vm, usesPatternContextBuffer, nullptr, Yarr::MatchFrom::CompilerThread);
@@ -84,14 +84,14 @@ bool BundlerPlugin::anyMatchesCrossThread(JSC::VM& vm, const ZigString* namespac
return false;
// Avoid unnecessary string copies
auto namespaceString = namespaceStr ? Zig::toString(*namespaceStr) : String();
auto namespaceString = namespaceStr ? Bun::toWTFString(*namespaceStr) : String();
auto* group = this->onResolve.group(namespaceString);
if (group == nullptr) {
return false;
}
auto pathString = Zig::toString(*path);
auto pathString = Bun::toWTFString(*path);
auto& filters = *group;
for (auto& filter : filters) {
@@ -115,9 +115,19 @@ static const HashTableValue JSBundlerPluginHashTable[] = {
class JSBundlerPlugin final : public JSC::JSNonFinalObject {
public:
using Base = JSC::JSNonFinalObject;
static JSBundlerPlugin* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, void* config, BunPluginTarget target)
static JSBundlerPlugin* create(JSC::VM& vm,
JSC::JSGlobalObject* globalObject,
JSC::Structure* structure,
void* config,
BunPluginTarget target,
JSBundlerPluginAddErrorCallback addError = JSBundlerPlugin__addError,
JSBundlerPluginOnLoadAsyncCallback onLoadAsync = JSBundlerPlugin__onLoadAsync,
JSBundlerPluginOnResolveAsyncCallback onResolveAsync = JSBundlerPlugin__onResolveAsync)
{
JSBundlerPlugin* ptr = new (NotNull, JSC::allocateCell<JSBundlerPlugin>(vm)) JSBundlerPlugin(vm, globalObject, structure, config, target);
JSBundlerPlugin* ptr = new (NotNull, JSC::allocateCell<JSBundlerPlugin>(vm)) JSBundlerPlugin(vm, globalObject, structure, config, target,
addError,
onLoadAsync,
onResolveAsync);
ptr->finishCreation(vm);
return ptr;
}
@@ -147,9 +157,9 @@ public:
JSC::LazyProperty<JSBundlerPlugin, JSC::JSFunction> setupFunction;
private:
JSBundlerPlugin(JSC::VM& vm, JSC::JSGlobalObject*, JSC::Structure* structure, void* config, BunPluginTarget target)
JSBundlerPlugin(JSC::VM& vm, JSC::JSGlobalObject*, JSC::Structure* structure, void* config, BunPluginTarget target, JSBundlerPluginAddErrorCallback addError, JSBundlerPluginOnLoadAsyncCallback onLoadAsync, JSBundlerPluginOnResolveAsyncCallback onResolveAsync)
: JSC::JSNonFinalObject(vm, structure)
, plugin(BundlerPlugin(config, target))
, plugin(BundlerPlugin(config, target, addError, onLoadAsync, onResolveAsync))
{
}
@@ -199,7 +209,7 @@ JSC_DEFINE_HOST_FUNCTION(jsBundlerPluginFunction_addError, (JSC::JSGlobalObject
{
JSBundlerPlugin* thisObject = jsCast<JSBundlerPlugin*>(callFrame->thisValue());
if (!thisObject->plugin.tombstoned) {
JSBundlerPlugin__addError(
thisObject->plugin.addError(
UNWRAP_BUNDLER_PLUGIN(callFrame),
thisObject->plugin.config,
JSValue::encode(callFrame->argument(1)),
@@ -212,7 +222,7 @@ JSC_DEFINE_HOST_FUNCTION(jsBundlerPluginFunction_onLoadAsync, (JSC::JSGlobalObje
{
JSBundlerPlugin* thisObject = jsCast<JSBundlerPlugin*>(callFrame->thisValue());
if (!thisObject->plugin.tombstoned) {
JSBundlerPlugin__onLoadAsync(
thisObject->plugin.onLoadAsync(
UNWRAP_BUNDLER_PLUGIN(callFrame),
thisObject->plugin.config,
JSValue::encode(callFrame->argument(1)),
@@ -225,7 +235,7 @@ JSC_DEFINE_HOST_FUNCTION(jsBundlerPluginFunction_onResolveAsync, (JSC::JSGlobalO
{
JSBundlerPlugin* thisObject = jsCast<JSBundlerPlugin*>(callFrame->thisValue());
if (!thisObject->plugin.tombstoned) {
JSBundlerPlugin__onResolveAsync(
thisObject->plugin.onResolveAsync(
UNWRAP_BUNDLER_PLUGIN(callFrame),
thisObject->plugin.config,
JSValue::encode(callFrame->argument(1)),
@@ -274,15 +284,15 @@ void JSBundlerPlugin::finishCreation(JSC::VM& vm)
reifyStaticProperties(vm, JSBundlerPlugin::info(), JSBundlerPluginHashTable, *this);
}
extern "C" bool JSBundlerPlugin__anyMatches(Bun::JSBundlerPlugin* pluginObject, const ZigString* namespaceString, const ZigString* path, bool isOnLoad)
extern "C" bool JSBundlerPlugin__anyMatches(Bun::JSBundlerPlugin* pluginObject, const BunString* namespaceString, const BunString* path, bool isOnLoad)
{
return pluginObject->plugin.anyMatchesCrossThread(pluginObject->vm(), namespaceString, path, isOnLoad);
}
extern "C" void JSBundlerPlugin__matchOnLoad(JSC::JSGlobalObject* globalObject, Bun::JSBundlerPlugin* plugin, const ZigString* namespaceString, const ZigString* path, void* context, uint8_t defaultLoaderId)
extern "C" void JSBundlerPlugin__matchOnLoad(JSC::JSGlobalObject* globalObject, Bun::JSBundlerPlugin* plugin, const BunString* namespaceString, const BunString* path, void* context, uint8_t defaultLoaderId)
{
WTF::String namespaceStringStr = namespaceString ? Zig::toStringCopy(*namespaceString) : WTF::String();
WTF::String pathStr = path ? Zig::toStringCopy(*path) : WTF::String();
WTF::String namespaceStringStr = namespaceString ? Bun::toWTFString(*namespaceString) : WTF::String();
WTF::String pathStr = path ? Bun::toWTFString(*path) : WTF::String();
JSFunction* function = plugin->onLoadFunction.get(plugin);
if (UNLIKELY(!function))
@@ -306,7 +316,7 @@ extern "C" void JSBundlerPlugin__matchOnLoad(JSC::JSGlobalObject* globalObject,
auto exception = scope.exception();
scope.clearException();
if (!plugin->plugin.tombstoned) {
JSBundlerPlugin__addError(
plugin->plugin.addError(
context,
plugin->plugin.config,
JSC::JSValue::encode(exception),
@@ -315,14 +325,14 @@ extern "C" void JSBundlerPlugin__matchOnLoad(JSC::JSGlobalObject* globalObject,
}
}
extern "C" void JSBundlerPlugin__matchOnResolve(JSC::JSGlobalObject* globalObject, Bun::JSBundlerPlugin* plugin, const ZigString* namespaceString, const ZigString* path, const ZigString* importer, void* context, uint8_t kindId)
extern "C" void JSBundlerPlugin__matchOnResolve(JSC::JSGlobalObject* globalObject, Bun::JSBundlerPlugin* plugin, const BunString* namespaceString, const BunString* path, const BunString* importer, void* context, uint8_t kindId)
{
WTF::String namespaceStringStr = namespaceString ? Zig::toStringCopy(*namespaceString) : WTF::String("file"_s);
WTF::String namespaceStringStr = namespaceString ? Bun::toWTFString(*namespaceString) : WTF::String("file"_s);
if (namespaceStringStr.length() == 0) {
namespaceStringStr = WTF::String("file"_s);
}
WTF::String pathStr = path ? Zig::toStringCopy(*path) : WTF::String();
WTF::String importerStr = importer ? Zig::toStringCopy(*importer) : WTF::String();
WTF::String pathStr = path ? Bun::toWTFString(*path) : WTF::String();
WTF::String importerStr = importer ? Bun::toWTFString(*importer) : WTF::String();
auto& vm = globalObject->vm();
JSFunction* function = plugin->onResolveFunction.get(plugin);

View File

@@ -9,6 +9,10 @@
#include <JavaScriptCore/Yarr.h>
#include <JavaScriptCore/Strong.h>
typedef void (*JSBundlerPluginAddErrorCallback)(void*, void*, JSC::EncodedJSValue, JSC::EncodedJSValue);
typedef void (*JSBundlerPluginOnLoadAsyncCallback)(void*, void*, JSC::EncodedJSValue, JSC::EncodedJSValue);
typedef void (*JSBundlerPluginOnResolveAsyncCallback)(void*, void*, JSC::EncodedJSValue, JSC::EncodedJSValue, JSC::EncodedJSValue);
namespace Bun {
using namespace JSC;
@@ -42,10 +46,13 @@ public:
};
public:
bool anyMatchesCrossThread(JSC::VM&, const ZigString* namespaceStr, const ZigString* path, bool isOnLoad);
bool anyMatchesCrossThread(JSC::VM&, const BunString* namespaceStr, const BunString* path, bool isOnLoad);
void tombstone() { tombstoned = true; }
BundlerPlugin(void* config, BunPluginTarget target)
BundlerPlugin(void* config, BunPluginTarget target, JSBundlerPluginAddErrorCallback addError, JSBundlerPluginOnLoadAsyncCallback onLoadAsync, JSBundlerPluginOnResolveAsyncCallback onResolveAsync)
: addError(addError)
, onLoadAsync(onLoadAsync)
, onResolveAsync(onResolveAsync)
{
this->target = target;
this->config = config;
@@ -54,6 +61,10 @@ public:
NamespaceList onLoad = {};
NamespaceList onResolve = {};
BunPluginTarget target { BunPluginTargetBrowser };
JSBundlerPluginAddErrorCallback addError;
JSBundlerPluginOnLoadAsyncCallback onLoadAsync;
JSBundlerPluginOnResolveAsyncCallback onResolveAsync;
void* config { nullptr };
bool tombstoned { false };
};