diff options
author | 2023-07-19 17:20:00 -0700 | |
---|---|---|
committer | 2023-07-19 17:20:00 -0700 | |
commit | 9b6dc49575df5fb953918c284505f24741138130 (patch) | |
tree | 3a052876fa8c6524e0c8d18479aabe38e2d5a52a /src/bun.js/bindings/bindings.cpp | |
parent | 723e9d1ea7c7fdb424ecedd0fb023524366322c4 (diff) | |
download | bun-9b6dc49575df5fb953918c284505f24741138130.tar.gz bun-9b6dc49575df5fb953918c284505f24741138130.tar.zst bun-9b6dc49575df5fb953918c284505f24741138130.zip |
Implement `AsyncLocalStorage` (#3089)
* work to get async local storage working.
* a
* a
* everything but queueMicrotask
* sdfghj
* .
* finish
* tests
* test
* ok
* done
* im so stupid
* Upgrade WebKit
* refactor
* refactor
* changes requested
* oops
* cool
* fix runInAsyncScope
Diffstat (limited to 'src/bun.js/bindings/bindings.cpp')
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 1c97a57a4..55f718207 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -95,6 +95,9 @@ #include <JavaScriptCore/JSWeakMap.h> #include "JSURLSearchParams.h" +#include "AsyncContextFrame.h" +#include "JavaScriptCore/InternalFieldTuple.h" + template<typename UWSResponse> static void copyToUWS(WebCore::FetchHeaders* headers, UWSResponse* res) { @@ -1734,22 +1737,27 @@ bool JSC__JSValue__jestDeepMatch(JSC__JSValue JSValue0, JSC__JSValue JSValue1, J // This is the same as the C API version, except it returns a JSValue which may be a *Exception // We want that so we can return stack traces. -JSC__JSValue JSObjectCallAsFunctionReturnValue(JSContextRef ctx, JSObjectRef object, - JSObjectRef thisObject, size_t argumentCount, - const JSValueRef* arguments); - -JSC__JSValue JSObjectCallAsFunctionReturnValue(JSContextRef ctx, JSObjectRef object, - JSObjectRef thisObject, size_t argumentCount, +extern "C" JSC__JSValue JSObjectCallAsFunctionReturnValue(JSContextRef ctx, JSC__JSValue object, + JSC__JSValue thisObject, size_t argumentCount, const JSValueRef* arguments) { JSC::JSGlobalObject* globalObject = toJS(ctx); JSC::VM& vm = globalObject->vm(); - if (!object) + if (UNLIKELY(!object)) return JSC::JSValue::encode(JSC::JSValue()); - JSC::JSObject* jsObject = toJS(object); - JSC::JSObject* jsThisObject = toJS(thisObject); + JSC::JSValue jsObject = JSValue::decode(object); + JSC::JSValue jsThisObject = JSValue::decode(thisObject); + + JSValue restoreAsyncContext; + InternalFieldTuple* asyncContextData = nullptr; + if (auto* wrapper = jsDynamicCast<AsyncContextFrame*>(jsObject)) { + jsObject = jsCast<JSC::JSObject*>(wrapper->callback.get()); + asyncContextData = globalObject->m_asyncContextData.get(); + restoreAsyncContext = asyncContextData->getInternalField(0); + asyncContextData->putInternalField(vm, 0, wrapper->context.get()); + } if (!jsThisObject) jsThisObject = globalObject->globalThis(); @@ -1765,6 +1773,10 @@ JSC__JSValue JSObjectCallAsFunctionReturnValue(JSContextRef ctx, JSObjectRef obj NakedPtr<JSC::Exception> returnedException = nullptr; auto result = JSC::call(globalObject, jsObject, callData, jsThisObject, argList, returnedException); + if (asyncContextData) { + asyncContextData->putInternalField(vm, 0, restoreAsyncContext); + } + if (returnedException.get()) { return JSC::JSValue::encode(JSC::JSValue(returnedException.get())); } @@ -1775,11 +1787,6 @@ JSC__JSValue JSObjectCallAsFunctionReturnValue(JSContextRef ctx, JSObjectRef obj JSC__JSValue JSObjectCallAsFunctionReturnValueHoldingAPILock(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, - const JSValueRef* arguments); - -JSC__JSValue JSObjectCallAsFunctionReturnValueHoldingAPILock(JSContextRef ctx, JSObjectRef object, - JSObjectRef thisObject, - size_t argumentCount, const JSValueRef* arguments) { JSC::JSGlobalObject* globalObject = toJS(ctx); @@ -2543,9 +2550,9 @@ void JSC__JSPromise__rejectOnNextTickWithHandled(JSC__JSPromise* promise, JSC__J globalObject->queueMicrotask( globalObject->performMicrotaskFunction(), globalObject->rejectPromiseFunction(), + globalObject->m_asyncContextData.get()->getInternalField(0), promise, - value, - JSValue {}); + value); RETURN_IF_EXCEPTION(scope, void()); } } @@ -4331,14 +4338,14 @@ extern "C" size_t JSC__VM__externalMemorySize(JSC__VM* vm) #endif } -extern "C" void JSC__JSGlobalObject__queueMicrotaskJob(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1, JSC__JSValue JSValue2, JSC__JSValue JSValue3, JSC__JSValue JSValue4) +extern "C" void JSC__JSGlobalObject__queueMicrotaskJob(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1, JSC__JSValue JSValue3, JSC__JSValue JSValue4) { Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(arg0); JSC::VM& vm = globalObject->vm(); globalObject->queueMicrotask( JSValue(globalObject->performMicrotaskFunction()), JSC::JSValue::decode(JSValue1), - JSC::JSValue::decode(JSValue2), + globalObject->m_asyncContextData.get()->getInternalField(0), JSC::JSValue::decode(JSValue3), JSC::JSValue::decode(JSValue4)); } |