Skip to content

Commit

Permalink
Implement Promise.withResolvers
Browse files Browse the repository at this point in the history
Signed-off-by: Seonghyun Kim <[email protected]>
  • Loading branch information
ksh8281 committed Jul 2, 2024
1 parent c5b711b commit c695385
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/builtins/BuiltinPromise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,34 @@ static Value builtinPromiseAny(ExecutionState& state, Value thisValue, size_t ar
return result;
}

// https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-promise.withResolvers
static Value builtinPromiseWithResolvers(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
// Let C be the this value.
const Value& C = thisValue;
// Let promiseCapability be ? NewPromiseCapability(C).
if (!C.isObject()) {
// NOTE: isConstructor will be checked on PromiseObject::newPromiseCapability function
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, ErrorObject::Messages::Not_Constructor);
}
auto promiseCapability = PromiseObject::newPromiseCapability(state, C.asObject());
// Let obj be OrdinaryObjectCreate(%Object.prototype%).
Object* obj = new Object(state);

StaticStrings* strings = &state.context()->staticStrings();
// Perform ! CreateDataPropertyOrThrow(obj, "promise", promiseCapability.[[Promise]]).
obj->defineOwnPropertyThrowsException(state, ObjectPropertyName(strings->lazyPromise()),
ObjectPropertyDescriptor(promiseCapability.m_promise, ObjectPropertyDescriptor::AllPresent));
// Perform ! CreateDataPropertyOrThrow(obj, "resolve", promiseCapability.[[Resolve]]).
obj->defineOwnPropertyThrowsException(state, ObjectPropertyName(strings->resolve),
ObjectPropertyDescriptor(promiseCapability.m_resolveFunction, ObjectPropertyDescriptor::AllPresent));
// Perform ! CreateDataPropertyOrThrow(obj, "reject", promiseCapability.[[Reject]]).
obj->defineOwnPropertyThrowsException(state, ObjectPropertyName(strings->reject),
ObjectPropertyDescriptor(promiseCapability.m_rejectFunction, ObjectPropertyDescriptor::AllPresent));
// Return obj.
return obj;
}

void GlobalObject::initializePromise(ExecutionState& state)
{
ObjectPropertyNativeGetterSetterData* nativeData = new ObjectPropertyNativeGetterSetterData(true, false, true,
Expand Down Expand Up @@ -819,6 +847,11 @@ void GlobalObject::installPromise(ExecutionState& state)
ObjectPropertyDescriptor(m_promiseAny,
(ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));

// Promise.withResolvers()
m_promise->directDefineOwnProperty(state, ObjectPropertyName(strings->withResolvers),
ObjectPropertyDescriptor(new NativeFunctionObject(state, NativeFunctionInfo(strings->withResolvers, builtinPromiseWithResolvers, 0, NativeFunctionInfo::Strict)),
(ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));


redefineOwnProperty(state, ObjectPropertyName(strings->Promise),
ObjectPropertyDescriptor(m_promise, (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/StaticStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ namespace Escargot {
F(values) \
F(var) \
F(with) \
F(withResolvers) \
F(writable) \
F(yield)

Expand Down Expand Up @@ -683,6 +684,7 @@ namespace Escargot {
F(ObjectRegExpToString, "[object RegExp]") \
F(ObjectStringToString, "[object String]") \
F(ObjectUndefinedToString, "[object Undefined]") \
F(Promise, "promise") \
F(Reason, "reason") \
F(Rejected, "rejected") \
F(Status, "status") \
Expand Down
4 changes: 0 additions & 4 deletions tools/test/test262/excludelist.orig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,6 @@
<test id="built-ins/Object/prototype/toString/symbol-tag-set-builtin"><reason>TODO</reason></test>
<test id="built-ins/Object/prototype/toString/symbol-tag-string-builtin"><reason>TODO</reason></test>
<test id="built-ins/Object/seal/proxy-with-defineProperty-handler"><reason>TODO</reason></test>
<test id="built-ins/Promise/withResolvers/ctx-ctor"><reason>TODO</reason></test>
<test id="built-ins/Promise/withResolvers/promise"><reason>TODO</reason></test>
<test id="built-ins/Promise/withResolvers/resolvers"><reason>TODO</reason></test>
<test id="built-ins/Promise/withResolvers/result"><reason>TODO</reason></test>
<test id="built-ins/RegExp/duplicate-flags"><reason>TODO</reason></test>
<test id="built-ins/RegExp/duplicate-named-capturing-groups-syntax"><reason>TODO</reason></test>
<test id="built-ins/RegExp/lookBehind/alternations"><reason>TODO</reason></test>
Expand Down

0 comments on commit c695385

Please sign in to comment.