Skip to content

Commit

Permalink
jspromise_create only promise, espruino resolve fake prombox
Browse files Browse the repository at this point in the history
  • Loading branch information
d3nd3 committed Apr 24, 2024
1 parent 8859819 commit abbf754
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/jswrap_promise.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
*
* ES6 Promise implementation
* ----------------------------------------------------------------------------
* See https://github.com/espruino/Espruino/pull/2454 for better understanding.
*/

//#define PROMISE_DEBUG

#include "jsutils.h"

#if ESPR_NO_PROMISES!=1
Expand Down Expand Up @@ -236,16 +237,33 @@ void _jswrap_promise_queuereject(JsVar *prombox, JsVar *data) {
jsvUnLock(fn);
}

void jspromise_resolve(JsVar *prombox, JsVar *data) {
void jspromise_resolve(JsVar *promise, JsVar *data) {
// give the promise a prombox - ensure resolve once only in c.
// allows us to accept a promise instead of a prombox.
JsVar *prombox = jsvNewObject();
if (!prombox) {
jsvUnLock(promise);
return 0;
}
jsvObjectSetChildAndUnLock(prombox, JS_PROMISE_PROM_NAME, promise);
jsvObjectSetChildAndUnLock(prombox,JS_PROMISE_ISRESOLVED_NAME,jsvNewFromBool(false));
_jswrap_promise_queueresolve(prombox, data);
}

void jspromise_reject(JsVar *prombox, JsVar *data) {
void jspromise_reject(JsVar *promise, JsVar *data) {
// give the promise a prombox - ensure resolve once only in c.
// allows us to accept a promise instead of a prombox.
JsVar *prombox = jsvNewObject();
if (!prombox) {
jsvUnLock(promise);
return 0;
}
jsvObjectSetChildAndUnLock(prombox, JS_PROMISE_PROM_NAME, promise);
jsvObjectSetChildAndUnLock(prombox,JS_PROMISE_ISRESOLVED_NAME,jsvNewFromBool(false));
_jswrap_promise_queuereject(prombox, data);
}

//Now returns a promise box.
JsVar *jspromise_create() {
JsVar *jspromise_create_prombox() {
JsVar *p = jspNewObject(0, "Promise");
if (!p) return 0;
JsVar *box = jsvNewObject();
Expand All @@ -259,6 +277,10 @@ JsVar *jspromise_create() {
jsvObjectSetChildAndUnLock(box,JS_PROMISE_ISRESOLVED_NAME,jsvNewFromBool(false));
return box;
}
/// Create a new promise
JsVar *jspromise_create() {
return jspNewObject(0, "Promise");
}

/*JSON{
"type" : "constructor",
Expand All @@ -280,7 +302,7 @@ JsVar *jswrap_promise_constructor(JsVar *executor) {
jsExceptionHere(JSET_ERROR,"Executor function required in promise constructor");
return 0;
}
JsVar *promBox = jspromise_create();
JsVar *promBox = jspromise_create_prombox();
if (!promBox) return 0;
JsVar * promise = jsvObjectGetChildIfExists(promBox, JS_PROMISE_PROM_NAME);
if (promise) {
Expand Down Expand Up @@ -365,7 +387,7 @@ JsVar *jswrap_promise_all(JsVar *arr) {
jsExceptionHere(JSET_TYPEERROR, "Expecting something iterable, got %t", arr);
return 0;
}
JsVar *promBox = jspromise_create();
JsVar *promBox = jspromise_create_prombox();
if (!promBox) return 0;
JsVar * promise = jsvObjectGetChildIfExists(promBox, JS_PROMISE_PROM_NAME);
if (promise) {
Expand Down Expand Up @@ -439,7 +461,7 @@ JsVar *jswrap_promise_resolve(JsVar *data) {
if (promise) return promise;
}
// otherwise the returned promise will be fulfilled with the value.
JsVar *promBox = jspromise_create();
JsVar *promBox = jspromise_create_prombox();
if (!promBox) return 0;
promise = jsvObjectGetChildIfExists(promBox, JS_PROMISE_PROM_NAME);
if (promise) {
Expand Down Expand Up @@ -467,7 +489,7 @@ Return a new promise that is already rejected (at idle it'll call `.catch`)
*/
JsVar *jswrap_promise_reject(JsVar *data) {
// otherwise the returned promise will be fulfilled with the value.
JsVar *promBox = jspromise_create();
JsVar *promBox = jspromise_create_prombox();
if (!promBox) return 0;
JsVar * promise = jsvObjectGetChildIfExists(promBox, JS_PROMISE_PROM_NAME);
if (promise) {
Expand Down Expand Up @@ -524,7 +546,7 @@ void _jswrap_promise_add_reaction(JsVar *parent, JsVar * nextPromBox, JsVar *cal
}
*/
JsVar *jswrap_promise_then(JsVar *parent, JsVar *onFulfilled, JsVar *onRejected) {
JsVar *nextPromBox = jspromise_create();
JsVar *nextPromBox = jspromise_create_prombox();
if (!nextPromBox) return 0;
JsVar * nextProm = jsvObjectGetChildIfExists(nextPromBox, JS_PROMISE_PROM_NAME);
if (nextProm) {
Expand Down
2 changes: 2 additions & 0 deletions src/jswrap_promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

/// Create a new promise
JsVar *jspromise_create();
/// Create a promise and put it inside a promise box
JsVar *jspromise_create_prombox();
/// Resolve the given promise
void jspromise_resolve(JsVar *promise, JsVar *data);
/// Reject the given promise
Expand Down

0 comments on commit abbf754

Please sign in to comment.