Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
shim: adding v8::Promise and v8::Promise::Resolver implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
MSLaguana committed Feb 12, 2018
1 parent 7a53fce commit 354e306
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 8 deletions.
1 change: 1 addition & 0 deletions deps/chakrashim/include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ class Local {
friend class PropertyDescriptor;
friend class Proxy;
friend class RegExp;
friend class Promise;
friend class Set;
friend class Signature;
friend class Script;
Expand Down
2 changes: 1 addition & 1 deletion deps/chakrashim/lib/chakra_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@
obj instanceof URIError;
};
utils.isPromise = function(obj) {
return compareType(obj, 'Object') && obj instanceof Promise;
return compareType(obj, 'Promise') && obj instanceof Promise;
};
utils.isRegExp = function(obj) {
return compareType(obj, 'RegExp');
Expand Down
6 changes: 6 additions & 0 deletions deps/chakrashim/src/jsrtcachedpropertyidref.inc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ DEF(Symbol_toStringTag)
DEF(add)
DEF(from)


DEF(promise)
DEF(resolve)
DEF(reject)
DEF(state)

DEFSYMBOL(self)
DEFSYMBOL(__external__)
DEFSYMBOL(__hiddenvalues__)
Expand Down
20 changes: 18 additions & 2 deletions deps/chakrashim/src/v8promise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,27 @@ namespace v8 {
Promise::Promise() { }

Local<Value> Promise::Result() {
return Local<Value>();
JsValueRef value;
if (jsrt::GetProperty(this, jsrt::CachedPropertyIdRef::value, &value) != JsNoError) {
return Local<Value>();
}
return Local<Value>::New(value);
}

Promise::PromiseState Promise::State() {
return PromiseState::kFulfilled;
JsValueRef state;
if (jsrt::GetProperty(this, jsrt::CachedPropertyIdRef::state, &state) != JsNoError) {
return PromiseState::kPending;
}
int stateNumber;
if (JsNumberToInt(state, &stateNumber) != JsNoError) {
return PromiseState::kPending;
}
switch(stateNumber) {
case 1: return PromiseState::kFulfilled;
case 2: return PromiseState::kRejected;
default: return PromiseState::kPending;
}
}

MaybeLocal<Promise> Promise::Then(Local<Context> context,
Expand Down
99 changes: 94 additions & 5 deletions deps/chakrashim/src/v8resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,84 @@ namespace v8 {
using Resolver = Promise::Resolver;
// CHAKRA-TODO: Unimplemented completely
MaybeLocal<Resolver> Resolver::New(Local<Context> context) {
return Local<Resolver>();
JsValueRef resolver;
if (JsCreateObject(&resolver) != JsNoError) {
return Local<Resolver>();
}

JsValueRef promise, resolve, reject;
if (JsCreatePromise(&promise, &resolve, &reject) != JsNoError) {
return Local<Resolver>();
}

if (jsrt::SetProperty(resolver, jsrt::CachedPropertyIdRef::promise, promise) != JsNoError) {
return Local<Resolver>();
}

if (jsrt::SetProperty(resolver, jsrt::CachedPropertyIdRef::resolve, resolve) != JsNoError) {
return Local<Resolver>();
}

if (jsrt::SetProperty(resolver, jsrt::CachedPropertyIdRef::reject, reject) != JsNoError) {
return Local<Resolver>();
}

JsValueRef state;
jsrt::UintToValue(0, &state);
if (jsrt::SetProperty(promise, jsrt::CachedPropertyIdRef::value, state) != JsNoError) {
return Local<Resolver>();
}

return Local<Resolver>::New(static_cast<Resolver*>(resolver));
}

Local<Resolver> Resolver::New(Isolate* isolate) {
return New(isolate->GetCurrentContext()).ToLocalChecked();
}

Local<Promise> Resolver::GetPromise() {
CHAKRA_ASSERT(false);
return Local<Promise>();
JsValueRef promise;
if (jsrt::GetProperty(this, jsrt::CachedPropertyIdRef::promise, &promise) != JsNoError) {
return Local<Promise>();
}
return Local<Promise>::New(static_cast<Promise*>(promise));
}

Resolver* Resolver::Cast(Value* obj) {
return static_cast<Resolver*>(obj);
}

Maybe<bool> Resolver::Resolve(Local<Context> context, Local<Value> value) {
return Just(false);
JsValueRef resolve;
if (jsrt::GetProperty(this, jsrt::CachedPropertyIdRef::resolve, &resolve) != JsNoError) {
return Nothing<bool>();
}

JsValueRef promise;
if (jsrt::GetProperty(this, jsrt::CachedPropertyIdRef::promise, &promise) != JsNoError) {
return Nothing<bool>();
}

if (jsrt::SetProperty(promise, jsrt::CachedPropertyIdRef::value, *value) != JsNoError) {
return Nothing<bool>();
}

JsValueRef state;
jsrt::UintToValue(1, &state);
if (jsrt::SetProperty(promise, jsrt::CachedPropertyIdRef::value, state) != JsNoError) {
return Nothing<bool>();
}

JsValueRef result;
JsValueRef args[2];
args[0] = this; // ? What is the "this" of the resolver here?
args[1] = reinterpret_cast<JsValueRef>(*value);

if (JsCallFunction(resolve, args, 2, &result) != JsNoError) {
return Nothing<bool>();
}

return Just(true);
}

void Resolver::Resolve(Local<Value> value) {
Expand All @@ -51,6 +111,35 @@ namespace v8 {
}

Maybe<bool> Resolver::Reject(Local<Context> context, Local<Value> value) {
return Just(false);
JsValueRef reject;
if (jsrt::GetProperty(this, jsrt::CachedPropertyIdRef::reject, &reject) != JsNoError) {
return Nothing<bool>();
}

JsValueRef promise;
if (jsrt::GetProperty(this, jsrt::CachedPropertyIdRef::promise, &promise) != JsNoError) {
return Nothing<bool>();
}

if (jsrt::SetProperty(promise, jsrt::CachedPropertyIdRef::value, *value) != JsNoError) {
return Nothing<bool>();
}

JsValueRef state;
jsrt::UintToValue(2, &state);
if (jsrt::SetProperty(promise, jsrt::CachedPropertyIdRef::value, state) != JsNoError) {
return Nothing<bool>();
}

JsValueRef result;
JsValueRef args[2];
args[0] = this; // ? What is the "this" of the resolver here?
args[1] = reinterpret_cast<JsValueRef>(*value);

if (JsCallFunction(reject, args, 2, &result) != JsNoError) {
return Nothing<bool>();
}

return Just(true);
}
} // namespace v8

0 comments on commit 354e306

Please sign in to comment.