forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBunPlugin.h
109 lines (83 loc) · 2.94 KB
/
BunPlugin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once
#include "root.h"
#include "headers-handwritten.h"
#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/Strong.h>
#include "helpers.h"
extern "C" JSC_DECLARE_HOST_FUNCTION(jsFunctionBunPlugin);
extern "C" JSC_DECLARE_HOST_FUNCTION(jsFunctionBunPluginClear);
namespace Zig {
using namespace JSC;
class BunPlugin {
public:
using VirtualModuleMap = WTF::HashMap<String, JSC::Strong<JSC::JSObject>>;
// This is a list of pairs of regexps and functions to match against
class Group {
public:
// JavaScriptCore/RegularExpression does exist however it does not JIT
// We want JIT!
// TODO: evaluate if using JSInternalFieldImpl(2) is faster
Vector<JSC::Strong<JSC::RegExp>> filters = {};
Vector<JSC::Strong<JSC::JSFunction>> callbacks = {};
BunPluginTarget target { BunPluginTargetBun };
void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func);
JSFunction* find(JSC::JSGlobalObject* globalObj, String& path);
void clear()
{
filters.clear();
callbacks.clear();
}
};
class Base {
public:
Group fileNamespace = {};
Vector<String> namespaces = {};
Vector<Group> groups = {};
Group* group(const String& namespaceStr)
{
if (namespaceStr.isEmpty()) {
return &fileNamespace;
}
for (size_t i = 0; i < namespaces.size(); i++) {
if (namespaces[i] == namespaceStr) {
return &groups[i];
}
}
return nullptr;
}
void append(JSC::VM& vm, JSC::RegExp* filter, JSC::JSFunction* func, String& namespaceString);
};
class OnLoad final : public Base {
public:
OnLoad()
: Base()
{
}
VirtualModuleMap* virtualModules = nullptr;
bool mustDoExpensiveRelativeLookup = false;
JSC::EncodedJSValue run(JSC::JSGlobalObject* globalObject, BunString* namespaceString, BunString* path);
bool hasVirtualModules() const { return virtualModules != nullptr; }
void addModuleMock(JSC::VM& vm, const String& path, JSC::JSObject* mock);
std::optional<String> resolveVirtualModule(const String& path, const String& from);
~OnLoad()
{
if (virtualModules) {
delete virtualModules;
}
}
};
class OnResolve final : public Base {
public:
OnResolve()
: Base()
{
}
JSC::EncodedJSValue run(JSC::JSGlobalObject* globalObject, BunString* namespaceString, BunString* path, BunString* importer);
};
};
class GlobalObject;
} // namespace Zig
namespace Bun {
JSC::JSValue runVirtualModule(Zig::GlobalObject*, BunString* specifier, bool& wasModuleMock);
JSC::Structure* createModuleMockStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype);
}