Skip to content
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

GetClassID utility and SetHostUnhandledPromiseRejectionTracker #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ typedef struct JSClassDef {
} JSClassDef;

JSClassID JS_NewClassID(JSClassID *pclass_id);
JSClassID JS_GetClassID(JSValueConst v);

int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);

Expand Down Expand Up @@ -795,6 +797,7 @@ JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs);
/* is_handled = TRUE means that the rejection is handled */
typedef void JSHostPromiseRejectionTracker(JSContext* ctx, JSValueConst promise, JSValueConst reason, JS_BOOL is_handled, void* opaque);
void JS_SetHostPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque);
void JS_SetHostUnhandledPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque);

/* return != 0 if the JS code needs to be interrupted */
typedef int JSInterruptHandler(JSRuntime *rt, void *opaque);
Expand Down
16 changes: 16 additions & 0 deletions src/core/builtins/js-promise.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ void JS_SetHostPromiseRejectionTracker(JSRuntime *rt,
rt->host_promise_rejection_tracker_opaque = opaque;
}

void JS_SetHostUnhandledPromiseRejectionTracker(JSRuntime *rt,
JSHostPromiseRejectionTracker *cb,
void *opaque)
{
rt->host_unhandled_promise_rejection_tracker = cb;
rt->host_unhandled_promise_rejection_tracker_opaque = opaque;
}

void fulfill_or_reject_promise(JSContext *ctx, JSValueConst promise,
JSValueConst value, BOOL is_reject)
{
Expand Down Expand Up @@ -335,6 +343,14 @@ void js_promise_finalizer(JSRuntime *rt, JSValue val)

if (!s)
return;

if (s->promise_state == JS_PROMISE_REJECTED && !s->is_handled) {
if (rt->host_unhandled_promise_rejection_tracker) {
rt->host_unhandled_promise_rejection_tracker(rt, val, s->promise_result, FALSE,
rt->host_unhandled_promise_rejection_tracker_opaque);
}
}

for(i = 0; i < 2; i++) {
list_for_each_safe(el, el1, &s->promise_reactions[i]) {
JSPromiseReactionData *rd =
Expand Down
10 changes: 10 additions & 0 deletions src/core/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ JSClassID JS_NewClassID(JSClassID* pclass_id) {
return class_id;
}

JSClassID JS_GetClassID(JSValueConst v) {
JSObject *p;

if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT)
return 0;
p = JS_VALUE_GET_OBJ(v);
assert(p != 0);
return p->class_id;
}

BOOL JS_IsRegisteredClass(JSRuntime* rt, JSClassID class_id) {
return (class_id < rt->class_count && rt->class_array[class_id].class_id != 0);
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ struct JSRuntime {
JSHostPromiseRejectionTracker *host_promise_rejection_tracker;
void *host_promise_rejection_tracker_opaque;

JSHostPromiseRejectionTracker *host_unhandled_promise_rejection_tracker;
void *host_unhandled_promise_rejection_tracker_opaque;

struct list_head job_list; /* list of JSJobEntry.link */

JSModuleNormalizeFunc *module_normalize_func;
Expand Down