forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExposeNodeModuleGlobals.cpp
105 lines (92 loc) · 5.19 KB
/
ExposeNodeModuleGlobals.cpp
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
// clang-format off
#include "root.h"
#include <JavaScriptCore/PropertySlot.h>
#include <JavaScriptCore/JSMap.h>
#include <JavaScriptCore/JSString.h>
#include "ZigGlobalObject.h"
#include "InternalModuleRegistry.h"
#undef assert
#define FOREACH_EXPOSED_BUILTIN_IMR(v) \
v(ffi, Bun::InternalModuleRegistry::BunFFI) \
v(assert, Bun::InternalModuleRegistry::NodeAssert) \
v(async_hooks, Bun::InternalModuleRegistry::NodeAsyncHooks) \
v(child_process, Bun::InternalModuleRegistry::NodeChildProcess) \
v(cluster, Bun::InternalModuleRegistry::NodeCluster) \
v(dgram, Bun::InternalModuleRegistry::NodeDgram) \
v(diagnostics_channel, Bun::InternalModuleRegistry::NodeDiagnosticsChannel) \
v(dns, Bun::InternalModuleRegistry::NodeDNS) \
v(domain, Bun::InternalModuleRegistry::NodeDomain) \
v(events, Bun::InternalModuleRegistry::NodeEvents) \
v(fs, Bun::InternalModuleRegistry::NodeFS) \
v(http, Bun::InternalModuleRegistry::NodeHttp) \
v(http2, Bun::InternalModuleRegistry::NodeHttp2) \
v(https, Bun::InternalModuleRegistry::NodeHttps) \
v(inspector, Bun::InternalModuleRegistry::NodeInspector) \
v(net, Bun::InternalModuleRegistry::NodeNet) \
v(os, Bun::InternalModuleRegistry::NodeOS) \
v(path, Bun::InternalModuleRegistry::NodePath) \
v(perf_hooks, Bun::InternalModuleRegistry::NodePerfHooks) \
v(punycode, Bun::InternalModuleRegistry::NodePunycode) \
v(querystring, Bun::InternalModuleRegistry::NodeQuerystring) \
v(readline, Bun::InternalModuleRegistry::NodeReadline) \
v(stream, Bun::InternalModuleRegistry::NodeStream) \
v(sys, Bun::InternalModuleRegistry::NodeUtil) \
v(timers, Bun::InternalModuleRegistry::NodeTimers) \
v(tls, Bun::InternalModuleRegistry::NodeTLS) \
v(trace_events, Bun::InternalModuleRegistry::NodeTraceEvents) \
v(tty, Bun::InternalModuleRegistry::NodeTty) \
v(url, Bun::InternalModuleRegistry::NodeUrl) \
v(util, Bun::InternalModuleRegistry::NodeUtil) \
v(v8, Bun::InternalModuleRegistry::NodeV8) \
v(vm, Bun::InternalModuleRegistry::NodeVM) \
v(wasi, Bun::InternalModuleRegistry::NodeWasi) \
v(sqlite, Bun::InternalModuleRegistry::BunSqlite) \
v(worker_threads, Bun::InternalModuleRegistry::NodeWorkerThreads) \
v(zlib, Bun::InternalModuleRegistry::NodeZlib) \
#define FOREACH_EXPOSED_BUILTIN_NATIVE(v) \
v(constants, SyntheticModuleType::NodeConstants) \
v(string_decoder, SyntheticModuleType::NodeStringDecoder) \
v(buffer, SyntheticModuleType::NodeBuffer) \
v(jsc, SyntheticModuleType::BunJSC) \
namespace ExposeNodeModuleGlobalGetters {
#define DECL_GETTER(id, field) \
JSC_DEFINE_CUSTOM_GETTER(id, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) \
{ \
Zig::GlobalObject* thisObject = JSC::jsCast<Zig::GlobalObject*>(lexicalGlobalObject); \
JSC::VM& vm = thisObject->vm(); \
return JSC::JSValue::encode(thisObject->internalModuleRegistry()->requireId(thisObject, vm, field)); \
}
FOREACH_EXPOSED_BUILTIN_IMR(DECL_GETTER)
#undef DECL_GETTER
#define DECL_GETTER(id, field) \
JSC_DEFINE_CUSTOM_GETTER(id, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) \
{ \
Zig::GlobalObject* globalObject = jsCast<Zig::GlobalObject*>(lexicalGlobalObject); \
JSC::VM& vm = globalObject->vm(); \
auto& builtinNames = WebCore::builtinNames(vm); \
JSC::JSFunction* function = jsCast<JSC::JSFunction*>(globalObject->getDirect(vm, builtinNames.requireNativeModulePrivateName())); \
JSC::MarkedArgumentBuffer arguments = JSC::MarkedArgumentBuffer(); \
arguments.append(JSC::jsString(vm, WTF::String(#id##_s))); \
auto callData = JSC::getCallData(function); \
return JSC::JSValue::encode(call(globalObject, function, callData, JSC::jsUndefined(), arguments)); \
}
FOREACH_EXPOSED_BUILTIN_NATIVE(DECL_GETTER)
#undef DECL_GETTER
} // namespace ExposeNodeModuleGlobalGetters
extern "C" void Bun__ExposeNodeModuleGlobals(Zig::GlobalObject* globalObject)
{
JSC::VM& vm = globalObject->vm();
#define PUT_CUSTOM_GETTER_SETTER(id, field) \
globalObject->putDirectCustomAccessor( \
vm, \
JSC::Identifier::fromString(vm, #id##_s), \
JSC::CustomGetterSetter::create( \
vm, \
ExposeNodeModuleGlobalGetters::id, \
nullptr), \
0 | JSC::PropertyAttribute::CustomAccessorOrValue \
);
FOREACH_EXPOSED_BUILTIN_IMR(PUT_CUSTOM_GETTER_SETTER)
// FOREACH_EXPOSED_BUILTIN_NATIVE(PUT_CUSTOM_GETTER_SETTER)
#undef PUT_CUSTOM_GETTER_SETTER
}