From 29b22175bf6fc726d0b028c7bf5619ab89fca09a Mon Sep 17 00:00:00 2001 From: dave caruso Date: Fri, 15 Sep 2023 11:22:06 -0400 Subject: feat(runtime): add `process.binding` `uv`/`natives`/`config` + make global object properties lazy (#5355) * binding uv * we did that * some more bindings * fix doc * fix uv * yo * static hash table nonsense <3 * huge refactor to the global object i am not ready for merge conflicts * it works part 3 * lose --------- Co-authored-by: Jarred Sumner --- src/bun.js/bindings/ZigGlobalObject.h | 144 ++++++++++------------------------ 1 file changed, 43 insertions(+), 101 deletions(-) (limited to 'src/bun.js/bindings/ZigGlobalObject.h') diff --git a/src/bun.js/bindings/ZigGlobalObject.h b/src/bun.js/bindings/ZigGlobalObject.h index 29c1cd09c..ad5138527 100644 --- a/src/bun.js/bindings/ZigGlobalObject.h +++ b/src/bun.js/bindings/ZigGlobalObject.h @@ -13,21 +13,22 @@ namespace JSC { class Structure; class Identifier; class LazyClassStructure; - -} // namespace JSC - -namespace JSC { - enum class JSPromiseRejectionOperation : unsigned; - -} +} // namespace JSC namespace WebCore { class ScriptExecutionContext; class DOMGuardedObject; class EventLoopTask; class DOMWrapperWorld; -} +class GlobalScope; +class SubtleCrypto; +class EventTarget; +} // namespace WebCore + +namespace Bun { +class InternalModuleRegistry; +} // namespace Bun #include "root.h" @@ -43,37 +44,9 @@ class DOMWrapperWorld; #include "BunPlugin.h" #include "JSMockFunction.h" #include "InternalModuleRegistry.h" -#include "ProcessBindingConstants.h" - -namespace WebCore { -class GlobalScope; -class SubtleCrypto; -class EventTarget; -} extern "C" void Bun__reportError(JSC__JSGlobalObject*, JSC__JSValue); extern "C" void Bun__reportUnhandledError(JSC__JSGlobalObject*, JSC::EncodedJSValue); -// defined in ModuleLoader.cpp -extern "C" JSC::EncodedJSValue jsFunctionOnLoadObjectResultResolve(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callFrame); -extern "C" JSC::EncodedJSValue jsFunctionOnLoadObjectResultReject(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callFrame); - -// #include "EventTarget.h" - -// namespace WebCore { -// class GlobalEventTarget : public EventTargetWithInlineData, public ContextDestructionObserver { -// WTF_MAKE_ISO_ALLOCATED(GlobalEventTarget); - -// public: -// static Ref create(ScriptExecutionContext&); - -// EventTargetInterface eventTargetInterface() const final { return DOMWindowEventTargetInterfaceType; } -// ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); } -// void refEventTarget() final {} -// void derefEventTarget() final {} -// void eventListenersDidChange() final; -// }; - -// } namespace Zig { @@ -184,22 +157,12 @@ public: static void reportUncaughtExceptionAtEventLoop(JSGlobalObject*, JSC::Exception*); static JSGlobalObject* deriveShadowRealmGlobalObject(JSGlobalObject* globalObject); - static JSC::JSInternalPromise* moduleLoaderImportModule(JSGlobalObject*, JSC::JSModuleLoader*, - JSC::JSString* moduleNameValue, - JSC::JSValue parameters, - const JSC::SourceOrigin&); - static JSC::Identifier moduleLoaderResolve(JSGlobalObject*, JSC::JSModuleLoader*, - JSC::JSValue keyValue, JSC::JSValue referrerValue, - JSC::JSValue); - static JSC::JSInternalPromise* moduleLoaderFetch(JSGlobalObject*, JSC::JSModuleLoader*, - JSC::JSValue, JSC::JSValue, JSC::JSValue); - static JSC::JSObject* moduleLoaderCreateImportMetaProperties(JSGlobalObject*, - JSC::JSModuleLoader*, JSC::JSValue, - JSC::JSModuleRecord*, JSC::JSValue); - static JSC::JSValue moduleLoaderEvaluate(JSGlobalObject*, JSC::JSModuleLoader*, JSC::JSValue, - JSC::JSValue, JSC::JSValue, JSC::JSValue, JSC::JSValue); - static void promiseRejectionTracker(JSGlobalObject*, JSC::JSPromise*, - JSC::JSPromiseRejectionOperation); + static JSC::JSInternalPromise* moduleLoaderImportModule(JSGlobalObject*, JSC::JSModuleLoader*, JSC::JSString* moduleNameValue, JSC::JSValue parameters, const JSC::SourceOrigin&); + static JSC::Identifier moduleLoaderResolve(JSGlobalObject*, JSC::JSModuleLoader*, JSC::JSValue keyValue, JSC::JSValue referrerValue, JSC::JSValue); + static JSC::JSInternalPromise* moduleLoaderFetch(JSGlobalObject*, JSC::JSModuleLoader*, JSC::JSValue, JSC::JSValue, JSC::JSValue); + static JSC::JSObject* moduleLoaderCreateImportMetaProperties(JSGlobalObject*, JSC::JSModuleLoader*, JSC::JSValue, JSC::JSModuleRecord*, JSC::JSValue); + static JSC::JSValue moduleLoaderEvaluate(JSGlobalObject*, JSC::JSModuleLoader*, JSC::JSValue, JSC::JSValue, JSC::JSValue, JSC::JSValue, JSC::JSValue); + static void promiseRejectionTracker(JSGlobalObject*, JSC::JSPromise*, JSC::JSPromiseRejectionOperation); void setConsole(void* console); WebCore::JSBuiltinInternalFunctions& builtinInternalFunctions() { return m_builtinInternalFunctions; } JSC::Structure* FFIFunctionStructure() { return m_JSFFIFunctionStructure.getInitializedOnMainThread(this); } @@ -266,7 +229,8 @@ public: JSObject* requireFunctionUnbound() { return m_requireFunctionUnbound.getInitializedOnMainThread(this); } JSObject* requireResolveFunctionUnbound() { return m_requireResolveFunctionUnbound.getInitializedOnMainThread(this); } Bun::InternalModuleRegistry* internalModuleRegistry() { return m_internalModuleRegistry.getInitializedOnMainThread(this); } - Bun::ProcessBindingConstants* processBindingConstants() { return m_processBindingConstants.getInitializedOnMainThread(this); } + + JSObject* processBindingConstants() { return m_processBindingConstants.getInitializedOnMainThread(this); } JSObject* lazyRequireCacheObject() { return m_lazyRequireCacheObject.getInitializedOnMainThread(this); } @@ -286,31 +250,22 @@ public: bool hasProcessObject() const { return m_processObject.isInitialized(); } - JSC::JSObject* processObject() - { - return m_processObject.getInitializedOnMainThread(this); - } - - JSC::JSObject* processEnvObject() - { - return m_processEnvObject.getInitializedOnMainThread(this); - } + JSC::JSObject* processObject() { return m_processObject.getInitializedOnMainThread(this); } + JSC::JSObject* processEnvObject() { return m_processEnvObject.getInitializedOnMainThread(this); } + JSC::JSObject* bunObject() { return m_bunObject.getInitializedOnMainThread(this); } void drainMicrotasks(); void handleRejectedPromises(); - void initGeneratedLazyClasses(); + ALWAYS_INLINE void initGeneratedLazyClasses(); template void visitGeneratedLazyClasses(GlobalObject*, Visitor&); - void* bunVM() { return m_bunVM; } + ALWAYS_INLINE void* bunVM() { return m_bunVM; } bool isThreadLocalDefaultGlobalObject = false; - JSObject* subtleCrypto() - { - return m_subtleCryptoObject.getInitializedOnMainThread(this); - } + JSObject* subtleCrypto() { return m_subtleCryptoObject.getInitializedOnMainThread(this); } EncodedJSValue assignToStream(JSValue stream, JSValue controller); @@ -377,6 +332,7 @@ public: * For example, if you don't add the queueMicrotask functions to visitChildrenImpl(), * those callbacks will eventually never be called anymore. But it'll work the first time! */ + // TODO: these should use LazyProperty mutable WriteBarrier m_assignToStream; mutable WriteBarrier m_readableStreamToArrayBuffer; mutable WriteBarrier m_readableStreamToArrayBufferResolve; @@ -391,25 +347,8 @@ public: mutable WriteBarrier m_nextTickQueue; mutable WriteBarrier m_BunCommonJSModuleValue; - mutable WriteBarrier m_JSBroadcastChannelSetterValue; - mutable WriteBarrier m_JSBufferSetterValue; - mutable WriteBarrier m_JSCloseEventSetterValue; - mutable WriteBarrier m_JSCustomEventSetterValue; - mutable WriteBarrier m_JSDOMExceptionSetterValue; - mutable WriteBarrier m_JSDOMFormDataSetterValue; - mutable WriteBarrier m_JSErrorEventSetterValue; - mutable WriteBarrier m_JSEventSetterValue; - mutable WriteBarrier m_JSEventTargetSetterValue; - mutable WriteBarrier m_JSFetchHeadersSetterValue; - mutable WriteBarrier m_JSMessageChannelSetterValue; - mutable WriteBarrier m_JSMessageEventSetterValue; - mutable WriteBarrier m_JSMessagePortSetterValue; - mutable WriteBarrier m_JSTextEncoderSetterValue; - mutable WriteBarrier m_JSURLSearchParamsSetterValue; - mutable WriteBarrier m_JSWebSocketSetterValue; - mutable WriteBarrier m_JSWorkerSetterValue; - - mutable WriteBarrier m_JSBunDebuggerValue; + + // mutable WriteBarrier m_JSBunDebuggerValue; mutable WriteBarrier m_thenables[promiseFunctionsSize + 1]; Structure* memoryFootprintStructure() @@ -490,6 +429,9 @@ private: WebCore::ScriptExecutionContext* m_scriptExecutionContext; Ref m_world; + // JSC's hashtable code-generator tries to access these properties, so we make them public. + // However, we'd like it better if they could be protected. +public: /** * WARNING: You must update visitChildrenImpl() if you add a new field. * @@ -523,7 +465,7 @@ private: * For example, if you don't add the queueMicrotask functions to visitChildrenImpl(), * those callbacks will eventually never be called anymore. But it'll work the first time! */ - LazyProperty m_pendingVirtualModuleResultStructure; + LazyProperty m_pendingVirtualModuleResultStructure; LazyProperty m_performMicrotaskFunction; LazyProperty m_nativeMicrotaskTrampoline; LazyProperty m_performMicrotaskVariadicFunction; @@ -537,34 +479,34 @@ private: LazyProperty m_JSArrayBufferControllerPrototype; LazyProperty m_JSFileSinkControllerPrototype; LazyProperty m_JSHTTPSResponseControllerPrototype; - LazyProperty m_navigatorObject; - LazyProperty m_performanceObject; - LazyProperty m_processObject; LazyProperty m_subtleCryptoObject; LazyProperty m_JSHTTPResponseController; - LazyProperty m_JSBufferSubclassStructure; + LazyProperty m_JSBufferSubclassStructure; LazyProperty m_vmModuleContextMap; LazyProperty m_lazyRequireCacheObject; LazyProperty m_lazyTestModuleObject; LazyProperty m_lazyPreloadTestModuleObject; - LazyProperty m_bunSleepThenCallback; LazyProperty m_cachedGlobalObjectStructure; LazyProperty m_cachedGlobalProxyStructure; LazyProperty m_commonJSModuleObjectStructure; LazyProperty m_commonJSFunctionArgumentsStructure; LazyProperty m_memoryFootprintStructure; - LazyProperty m_cryptoObject; - - LazyProperty m_requireFunctionUnbound; - LazyProperty m_requireResolveFunctionUnbound; + LazyProperty m_requireFunctionUnbound; + LazyProperty m_requireResolveFunctionUnbound; LazyProperty m_internalModuleRegistry; - LazyProperty m_processBindingConstants; - LazyProperty m_importMetaObjectStructure; - LazyProperty m_asyncBoundFunctionStructure; - + LazyProperty m_processBindingConstants; + LazyProperty m_importMetaObjectStructure; + LazyProperty m_asyncBoundFunctionStructure; LazyProperty m_JSDOMFileConstructor; + LazyProperty m_bunObject; + LazyProperty m_cryptoObject; + LazyProperty m_navigatorObject; + LazyProperty m_performanceObject; + LazyProperty m_processObject; + +private: DOMGuardedObjectSet m_guardedObjects WTF_GUARDED_BY_LOCK(m_gcLock); void* m_bunVM; -- cgit v1.2.3