-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added JsCreatePromise to JsRT #2581
Conversation
+@curtisman , this is the context #2541 |
lib/Jsrt/Jsrt.cpp
Outdated
|
||
return JsNoError; | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: indentation level in the rest of this file is 4 spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, VS doesn't seem to auto-detect indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, should confirm we want the JsCreatePromise API to be a common export.
lib/Jsrt/ChakraCommon.h
Outdated
/// Requires an active script context. | ||
/// </para> | ||
/// </remarks> | ||
/// <param name="promise">The new Promise object.</param> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a nitpick, this param tag is all one line while the others are split up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're inconsistent about this elsewhere in this file as well. I wonder if it has to do with line length and VS doing something automatically with formatting?
lib/Jsrt/Jsrt.cpp
Outdated
@@ -2866,6 +2867,32 @@ CHAKRA_API JsSetPromiseContinuationCallback(_In_ JsPromiseContinuationCallback p | |||
/*allowInObjectBeforeCollectCallback*/true); | |||
} | |||
|
|||
CHAKRA_API JsCreatePromise(_Out_ JsValueRef *promise, _Out_ JsValueRef *resolve, _Out_ JsValueRef *reject) | |||
{ | |||
return ContextAPIWrapper<true>([&](Js::ScriptContext *scriptContext, TTDRecorder& _actionEntryPopper) -> JsErrorCode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, indentation here.
d946fce
to
a7ed8f7
Compare
I'm a little confused about what Let's say JsCreatePromise is equivalent to
|
These are essentially the same functions passed to the executor. Another method to fetch them might be like this: var resolve, reject;
function executor(res, rej) { resolve = res; reject = rej; };
var promise = new Promise(executor); Goal would be to allow our native code to resolve the promise whenever it wants to. One use might be an API which reads a file from the network and returns a promise resolved with the contents of that file. Native code can construct the promise using JsCreatePromise and hold-on to resolveFunction for that promise. Assume the native-side performs some processing to fetch the result data and when that is done it can call the resolveFunction with that result data which will handle resolving the promise in the engine. I guess the key thing here is that the native code can resolve the promise in an async way without having to create an executor function which would manually suck out the resolve/reject functions. |
Thanks @boingoing ! Makes much sense now. |
a7ed8f7
to
90dd972
Compare
PARAM_NOT_NULL(resolve); | ||
PARAM_NOT_NULL(reject); | ||
|
||
*promise = nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this needed? (same applies to two below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not mandatory to have either resolve() or reject() methods FYI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@obastemur I was just following the pattern of other methods. The idea was to ensure that regardless of how the flow changed we would always have set some value. I realize that in this flow we will always set a value, so I'm fine removing it if others agree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a reason to remove this. In this case the compiler will be able to figure out that this operation is not needed -- if the flow ever changes, we are protected.
LGTM. I just would like to mention that it is not required to have both reject() and response() methods. And if possible, it would be good to accept multiple methods accepted because people can catch multiple types of errors on diff methods. For example:
|
@galvesribeiro I think you might have it backwards, the https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise For continuations you would just use the JsRT functions to get the property ( In C#:
Since it's just a JavaScript function, you can choose to leave out the second parameter when calling it. |
Oh! Right, I understood it completely wrong, sorry. You are right! Good work! |
90dd972
to
8d1bb5b
Compare
CHAKRA_API JsCreatePromise(_Out_ JsValueRef *promise, _Out_ JsValueRef *resolve, _Out_ JsValueRef *reject) | ||
{ | ||
return ContextAPIWrapper<true>([&](Js::ScriptContext *scriptContext, TTDRecorder& _actionEntryPopper) -> JsErrorCode { | ||
PERFORM_JSRT_TTD_RECORD_ACTION_NOT_IMPLEMENTED(scriptContext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to implement this for TTD? @mrkmarron
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spoke with @mrkmarron and this was his recommendation. If the user is running TTD and runs into this we'll fail fast and deal with it at that time.
Closing this one and retargeting to release/2.0 in #2594. |
No description provided.