Skip to content

Commit

Permalink
Reduce microtask count in import(path) (expressions)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Sep 15, 2024
1 parent 20e270d commit 8a5ce39
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,23 +845,25 @@ JSC_DEFINE_HOST_FUNCTION(globalFuncImportMapStatus, (JSGlobalObject* globalObjec
JSC_DEFINE_HOST_FUNCTION(globalFuncImportModule, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();

auto* promise = JSPromise::create(vm, globalObject->promiseStructure());

auto scope = DECLARE_THROW_SCOPE(vm);

auto sourceOrigin = callFrame->callerSourceOrigin(vm);
RELEASE_ASSERT(callFrame->argumentCount() >= 1);
auto* specifier = callFrame->uncheckedArgument(0).toString(globalObject);
RETURN_IF_EXCEPTION(scope, JSValue::encode(promise->rejectWithCaughtException(globalObject, scope)));
RETURN_IF_EXCEPTION(scope, JSValue::encode(JSPromise::rejectedPromiseWithCaughtException(globalObject, scope)));

// We always specify parameters as undefined. Once dynamic import() starts accepting fetching parameters,
// we should retrieve this from the arguments.
JSValue parameters = callFrame->argument(1);
auto* internalPromise = globalObject->moduleLoader()->importModule(globalObject, specifier, parameters, sourceOrigin);
RETURN_IF_EXCEPTION(scope, JSValue::encode(promise->rejectWithCaughtException(globalObject, scope)));
RETURN_IF_EXCEPTION(scope, JSValue::encode(JSPromise::rejectedPromiseWithCaughtException(globalObject, scope)));

scope.release();
if (internalPromise->status(vm) == JSPromise::Status::Fulfilled) {
return JSPromise::resolvedPromise(globalObject, internalPromise->result(vm));
}

auto* promise = JSPromise::create(vm, globalObject->promiseStructure());
promise->resolve(globalObject, internalPromise);
return JSValue::encode(promise);
}
Expand Down
5 changes: 5 additions & 0 deletions Source/JavaScriptCore/runtime/JSInternalPromise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ JSInternalPromise* JSInternalPromise::rejectWithCaughtException(JSGlobalObject*
return jsCast<JSInternalPromise*>(JSPromise::rejectWithCaughtException(globalObject, scope));
}

JSInternalPromise* JSInternalPromise::rejectedPromiseWithCaughtException(JSGlobalObject* globalObject, ThrowScope& scope)
{
return jsCast<JSInternalPromise*>(JSPromise::rejectedPromiseWithCaughtException(globalObject, scope));
}

} // namespace JSC
1 change: 1 addition & 0 deletions Source/JavaScriptCore/runtime/JSInternalPromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class JSInternalPromise final : public JSPromise {
JS_EXPORT_PRIVATE JSInternalPromise* then(JSGlobalObject*, JSFunction* = nullptr, JSFunction* = nullptr);

JS_EXPORT_PRIVATE JSInternalPromise* rejectWithCaughtException(JSGlobalObject*, ThrowScope&);
JS_EXPORT_PRIVATE static JSInternalPromise* rejectedPromiseWithCaughtException(JSGlobalObject*, ThrowScope&);

private:
JSInternalPromise(VM&, Structure*);
Expand Down
14 changes: 14 additions & 0 deletions Source/JavaScriptCore/runtime/JSPromise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ JSPromise* JSPromise::rejectWithCaughtException(JSGlobalObject* globalObject, Th
return this;
}

JSPromise* JSPromise::rejectedPromiseWithCaughtException(JSGlobalObject* globalObject, ThrowScope& scope)
{
VM& vm = globalObject->vm();
Exception* exception = scope.exception();
ASSERT(exception);
if (UNLIKELY(vm.isTerminationException(exception))) {
scope.release();
return nullptr;
}
scope.clearException();
scope.release();
return rejectedPromise(globalObject, exception->value());
}

void JSPromise::performPromiseThen(JSGlobalObject* globalObject, JSFunction* onFulFilled, JSFunction* onRejected, JSValue resultCapability)
{
JSFunction* performPromiseThenFunction = globalObject->performPromiseThenFunction();
Expand Down
1 change: 1 addition & 0 deletions Source/JavaScriptCore/runtime/JSPromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class JSPromise : public JSInternalFieldObjectImpl<2> {

JS_EXPORT_PRIVATE static JSPromise* resolvedPromise(JSGlobalObject*, JSValue);
JS_EXPORT_PRIVATE static JSPromise* rejectedPromise(JSGlobalObject*, JSValue);
JS_EXPORT_PRIVATE static JSPromise* rejectedPromiseWithCaughtException(JSGlobalObject*, ThrowScope&);

JS_EXPORT_PRIVATE void resolve(JSGlobalObject*, JSValue);
JS_EXPORT_PRIVATE void reject(JSGlobalObject*, JSValue);
Expand Down

0 comments on commit 8a5ce39

Please sign in to comment.