mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 04:32:44 +00:00 
			
		
		
		
	LibJS: Convert call_job_callback() to ThrowCompletionOr
This commit is contained in:
		
							parent
							
								
									68ac13a192
								
							
						
					
					
						commit
						cd391aa8ef
					
				
					 2 changed files with 21 additions and 17 deletions
				
			
		|  | @ -6,8 +6,9 @@ | ||||||
| 
 | 
 | ||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
|  | #include <LibJS/Runtime/AbstractOperations.h> | ||||||
|  | #include <LibJS/Runtime/Completion.h> | ||||||
| #include <LibJS/Runtime/FunctionObject.h> | #include <LibJS/Runtime/FunctionObject.h> | ||||||
| #include <LibJS/Runtime/VM.h> |  | ||||||
| 
 | 
 | ||||||
| namespace JS { | namespace JS { | ||||||
| 
 | 
 | ||||||
|  | @ -19,16 +20,19 @@ struct JobCallback { | ||||||
| // 9.5.2 HostMakeJobCallback ( callback ), https://tc39.es/ecma262/#sec-hostmakejobcallback
 | // 9.5.2 HostMakeJobCallback ( callback ), https://tc39.es/ecma262/#sec-hostmakejobcallback
 | ||||||
| inline JobCallback make_job_callback(FunctionObject& callback) | inline JobCallback make_job_callback(FunctionObject& callback) | ||||||
| { | { | ||||||
|  |     // 1. Return the JobCallback Record { [[Callback]]: callback, [[HostDefined]]: empty }.
 | ||||||
|     return { &callback }; |     return { &callback }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // 9.5.3 HostCallJobCallback ( jobCallback, V, argumentsList ), https://tc39.es/ecma262/#sec-hostcalljobcallback
 | // 9.5.3 HostCallJobCallback ( jobCallback, V, argumentsList ), https://tc39.es/ecma262/#sec-hostcalljobcallback
 | ||||||
| template<typename... Args> | template<typename... Args> | ||||||
| [[nodiscard]] inline Value call_job_callback(VM& vm, JobCallback& job_callback, Value this_value, Args... args) | inline ThrowCompletionOr<Value> call_job_callback(GlobalObject& global_object, JobCallback& job_callback, Value this_value, Args... args) | ||||||
| { | { | ||||||
|  |     // 1. Assert: IsCallable(jobCallback.[[Callback]]) is true.
 | ||||||
|     VERIFY(job_callback.callback); |     VERIFY(job_callback.callback); | ||||||
|     auto& callback = *job_callback.callback; | 
 | ||||||
|     return TRY_OR_DISCARD(vm.call(callback, this_value, args...)); |     // 2. Return ? Call(jobCallback.[[Callback]], V, argumentsList).
 | ||||||
|  |     return call(global_object, job_callback.callback, this_value, args...); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -29,6 +29,7 @@ PromiseReactionJob::PromiseReactionJob(PromiseReaction& reaction, Value argument | ||||||
| ThrowCompletionOr<Value> PromiseReactionJob::call() | ThrowCompletionOr<Value> PromiseReactionJob::call() | ||||||
| { | { | ||||||
|     auto& vm = this->vm(); |     auto& vm = this->vm(); | ||||||
|  |     auto& global_object = this->global_object(); | ||||||
| 
 | 
 | ||||||
|     // a. Let promiseCapability be reaction.[[Capability]].
 |     // a. Let promiseCapability be reaction.[[Capability]].
 | ||||||
|     auto& promise_capability = m_reaction.capability(); |     auto& promise_capability = m_reaction.capability(); | ||||||
|  | @ -39,7 +40,7 @@ ThrowCompletionOr<Value> PromiseReactionJob::call() | ||||||
|     // c. Let handler be reaction.[[Handler]].
 |     // c. Let handler be reaction.[[Handler]].
 | ||||||
|     auto handler = m_reaction.handler(); |     auto handler = m_reaction.handler(); | ||||||
| 
 | 
 | ||||||
|     Value handler_result; |     Completion handler_result; | ||||||
| 
 | 
 | ||||||
|     // d. If handler is empty, then
 |     // d. If handler is empty, then
 | ||||||
|     if (!handler.has_value()) { |     if (!handler.has_value()) { | ||||||
|  | @ -48,7 +49,7 @@ ThrowCompletionOr<Value> PromiseReactionJob::call() | ||||||
|         // i. If type is Fulfill, let handlerResult be NormalCompletion(argument).
 |         // i. If type is Fulfill, let handlerResult be NormalCompletion(argument).
 | ||||||
|         if (type == PromiseReaction::Type::Fulfill) { |         if (type == PromiseReaction::Type::Fulfill) { | ||||||
|             dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument); |             dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument); | ||||||
|             handler_result = m_argument; |             handler_result = normal_completion(m_argument); | ||||||
|         } |         } | ||||||
|         // ii. Else,
 |         // ii. Else,
 | ||||||
|         else { |         else { | ||||||
|  | @ -56,15 +57,14 @@ ThrowCompletionOr<Value> PromiseReactionJob::call() | ||||||
|             VERIFY(type == PromiseReaction::Type::Reject); |             VERIFY(type == PromiseReaction::Type::Reject); | ||||||
| 
 | 
 | ||||||
|             // 2. Let handlerResult be ThrowCompletion(argument).
 |             // 2. Let handlerResult be ThrowCompletion(argument).
 | ||||||
|             // NOTE: handler_result is set to exception value further below
 |  | ||||||
|             dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Reject, throwing exception with argument {}", this, m_argument); |             dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Reject, throwing exception with argument {}", this, m_argument); | ||||||
|             vm.throw_exception(global_object(), m_argument); |             handler_result = throw_completion(m_argument); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     // e. Else, let handlerResult be HostCallJobCallback(handler, undefined, « argument »).
 |     // e. Else, let handlerResult be HostCallJobCallback(handler, undefined, « argument »).
 | ||||||
|     else { |     else { | ||||||
|         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling handler callback {} @ {} with argument {}", this, handler.value().callback->class_name(), handler.value().callback, m_argument); |         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling handler callback {} @ {} with argument {}", this, handler.value().callback->class_name(), handler.value().callback, m_argument); | ||||||
|         handler_result = call_job_callback(vm, handler.value(), js_undefined(), m_argument); |         handler_result = call_job_callback(global_object, handler.value(), js_undefined(), m_argument); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // f. If promiseCapability is undefined, then
 |     // f. If promiseCapability is undefined, then
 | ||||||
|  | @ -82,22 +82,21 @@ ThrowCompletionOr<Value> PromiseReactionJob::call() | ||||||
|     // g. Assert: promiseCapability is a PromiseCapability Record.
 |     // g. Assert: promiseCapability is a PromiseCapability Record.
 | ||||||
| 
 | 
 | ||||||
|     // h. If handlerResult is an abrupt completion, then
 |     // h. If handlerResult is an abrupt completion, then
 | ||||||
|     if (vm.exception()) { |     if (handler_result.is_abrupt()) { | ||||||
|         handler_result = vm.exception()->value(); |  | ||||||
|         vm.clear_exception(); |         vm.clear_exception(); | ||||||
|         vm.stop_unwind(); |         vm.stop_unwind(); | ||||||
| 
 | 
 | ||||||
|         // i. Let status be Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).
 |         // i. Let status be Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).
 | ||||||
|         auto* reject_function = promise_capability.value().reject; |         auto* reject_function = promise_capability.value().reject; | ||||||
|         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function); |         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function); | ||||||
|         return vm.call(*reject_function, js_undefined(), handler_result); |         return vm.call(*reject_function, js_undefined(), handler_result.value()); | ||||||
|     } |     } | ||||||
|     // i. Else,
 |     // i. Else,
 | ||||||
|     else { |     else { | ||||||
|         // i. Let status be Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »).
 |         // i. Let status be Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »).
 | ||||||
|         auto* resolve_function = promise_capability.value().resolve; |         auto* resolve_function = promise_capability.value().resolve; | ||||||
|         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function); |         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function); | ||||||
|         return vm.call(*resolve_function, js_undefined(), handler_result); |         return vm.call(*resolve_function, js_undefined(), handler_result.value()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // j. Return Completion(status).
 |     // j. Return Completion(status).
 | ||||||
|  | @ -128,17 +127,18 @@ PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve | ||||||
| ThrowCompletionOr<Value> PromiseResolveThenableJob::call() | ThrowCompletionOr<Value> PromiseResolveThenableJob::call() | ||||||
| { | { | ||||||
|     auto& vm = this->vm(); |     auto& vm = this->vm(); | ||||||
|  |     auto& global_object = this->global_object(); | ||||||
| 
 | 
 | ||||||
|     // a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).
 |     // a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).
 | ||||||
|     auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions(); |     auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions(); | ||||||
| 
 | 
 | ||||||
|     // b. Let thenCallResult be HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »).
 |     // b. Let thenCallResult be HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »).
 | ||||||
|     dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Calling then job callback for thenable {}", this, &m_thenable); |     dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Calling then job callback for thenable {}", this, &m_thenable); | ||||||
|     auto then_call_result = call_job_callback(vm, m_then, m_thenable, &resolve_function, &reject_function); |     auto then_call_result = call_job_callback(global_object, m_then, m_thenable, &resolve_function, &reject_function); | ||||||
| 
 | 
 | ||||||
|     // c. If thenCallResult is an abrupt completion, then
 |     // c. If thenCallResult is an abrupt completion, then
 | ||||||
|     if (vm.exception()) { |     if (then_call_result.is_error()) { | ||||||
|         auto error = vm.exception()->value(); |         auto error = then_call_result.throw_completion().value(); | ||||||
|         vm.clear_exception(); |         vm.clear_exception(); | ||||||
|         vm.stop_unwind(); |         vm.stop_unwind(); | ||||||
| 
 | 
 | ||||||
|  | @ -151,7 +151,7 @@ ThrowCompletionOr<Value> PromiseResolveThenableJob::call() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // d. Return Completion(thenCallResult).
 |     // d. Return Completion(thenCallResult).
 | ||||||
|     dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result); |     dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result.value()); | ||||||
|     return then_call_result; |     return then_call_result; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus Groh
						Linus Groh