mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:12:43 +00:00 
			
		
		
		
	 7327a28ccc
			
		
	
	
		7327a28ccc
		
	
	
	
	
		
			
			As mentioned on Discord earlier, we'll add these to all new functions going forward - this is the backfill. Reasons: - It makes you look at the spec, implementing based on MDN or V8 behavior is a no-go - It makes finding the various functions that are non-compliant easier, in the future everything should either have such a comment or, if it's not from the spec at all, a comment explaining why that is the case - It makes it easier to check whether a certain abstract operation is implemented in LibJS, not all of them use the same name as the spec. E.g. RejectPromise() is Promise::reject() - It makes it easier to reason about vm.arguments(), e.g. when the function has a rest parameter - It makes it easier to see whether a certain function is from a proposal or Annex B Also: - Add arguments to all functions and abstract operations that already had a comment - Fix some outdated section numbers - Replace some ecma-international.org URLs with tc39.es
		
			
				
	
	
		
			120 lines
		
	
	
	
		
			5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
	
		
			5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <AK/Debug.h>
 | |
| #include <LibJS/Runtime/GlobalObject.h>
 | |
| #include <LibJS/Runtime/JobCallback.h>
 | |
| #include <LibJS/Runtime/Promise.h>
 | |
| #include <LibJS/Runtime/PromiseJobs.h>
 | |
| #include <LibJS/Runtime/PromiseReaction.h>
 | |
| 
 | |
| namespace JS {
 | |
| 
 | |
| PromiseReactionJob* PromiseReactionJob::create(GlobalObject& global_object, PromiseReaction& reaction, Value argument)
 | |
| {
 | |
|     return global_object.heap().allocate<PromiseReactionJob>(global_object, reaction, argument, *global_object.function_prototype());
 | |
| }
 | |
| 
 | |
| PromiseReactionJob::PromiseReactionJob(PromiseReaction& reaction, Value argument, Object& prototype)
 | |
|     : NativeFunction(prototype)
 | |
|     , m_reaction(reaction)
 | |
|     , m_argument(argument)
 | |
| {
 | |
| }
 | |
| 
 | |
| // 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
 | |
| Value PromiseReactionJob::call()
 | |
| {
 | |
|     auto& vm = this->vm();
 | |
|     auto& promise_capability = m_reaction.capability();
 | |
|     auto type = m_reaction.type();
 | |
|     auto handler = m_reaction.handler();
 | |
|     Value handler_result;
 | |
|     if (!handler.has_value()) {
 | |
|         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Handler is empty", this);
 | |
|         switch (type) {
 | |
|         case PromiseReaction::Type::Fulfill:
 | |
|             dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument);
 | |
|             handler_result = m_argument;
 | |
|             break;
 | |
|         case PromiseReaction::Type::Reject:
 | |
|             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 is set to exception value further below
 | |
|             break;
 | |
|         }
 | |
|     } else {
 | |
|         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);
 | |
|     }
 | |
| 
 | |
|     if (!promise_capability.has_value()) {
 | |
|         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction has no PromiseCapability, returning empty value", this);
 | |
|         return {};
 | |
|     }
 | |
| 
 | |
|     if (vm.exception()) {
 | |
|         handler_result = vm.exception()->value();
 | |
|         vm.clear_exception();
 | |
|         vm.stop_unwind();
 | |
|         auto* reject_function = promise_capability.value().reject;
 | |
|         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function);
 | |
|         return vm.call(*reject_function, js_undefined(), handler_result);
 | |
|     } else {
 | |
|         auto* resolve_function = promise_capability.value().resolve;
 | |
|         dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function);
 | |
|         return vm.call(*resolve_function, js_undefined(), handler_result);
 | |
|     }
 | |
| }
 | |
| 
 | |
| void PromiseReactionJob::visit_edges(Visitor& visitor)
 | |
| {
 | |
|     Base::visit_edges(visitor);
 | |
|     visitor.visit(&m_reaction);
 | |
|     visitor.visit(m_argument);
 | |
| }
 | |
| 
 | |
| PromiseResolveThenableJob* PromiseResolveThenableJob::create(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
 | |
| {
 | |
|     // FIXME: A bunch of stuff regarding realms, see step 2-5 in the spec linked below
 | |
|     return global_object.heap().allocate<PromiseResolveThenableJob>(global_object, promise_to_resolve, thenable, then, *global_object.function_prototype());
 | |
| }
 | |
| 
 | |
| PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve, Value thenable, JobCallback then, Object& prototype)
 | |
|     : NativeFunction(prototype)
 | |
|     , m_promise_to_resolve(promise_to_resolve)
 | |
|     , m_thenable(thenable)
 | |
|     , m_then(then)
 | |
| {
 | |
| }
 | |
| 
 | |
| // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
 | |
| Value PromiseResolveThenableJob::call()
 | |
| {
 | |
|     auto& vm = this->vm();
 | |
|     auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions();
 | |
|     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);
 | |
|     if (vm.exception()) {
 | |
|         auto error = vm.exception()->value();
 | |
|         vm.clear_exception();
 | |
|         vm.stop_unwind();
 | |
|         dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: An exception was thrown, returning error {}", this, error);
 | |
|         return error;
 | |
|     }
 | |
|     dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result);
 | |
|     return then_call_result;
 | |
| }
 | |
| 
 | |
| void PromiseResolveThenableJob::visit_edges(Visitor& visitor)
 | |
| {
 | |
|     Base::visit_edges(visitor);
 | |
|     visitor.visit(&m_promise_to_resolve);
 | |
|     visitor.visit(m_thenable);
 | |
|     visitor.visit(m_then.callback);
 | |
| }
 | |
| 
 | |
| }
 |