From 273f5c543661685edef7a0e7f4443d940be2e92e Mon Sep 17 00:00:00 2001 From: Tyler Rockwood Date: Mon, 19 Feb 2024 04:58:08 -0600 Subject: [PATCH 1/2] Add method to GetClassID If you want to extend a built-in class you need it's class ID and there is no robust way to get that without this accessor. Signed-off-by: Tyler Rockwood --- quickjs.c | 9 +++++++++ quickjs.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/quickjs.c b/quickjs.c index e6166ee05..e71cbf207 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3344,6 +3344,15 @@ JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id) return class_id; } +JSClassID JS_GetClassID(JSValue v) +{ + JSObject *p; + if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT) + return 0; + p = JS_VALUE_GET_OBJ(v); + return p->class_id; +} + BOOL JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id) { return (class_id < rt->class_count && diff --git a/quickjs.h b/quickjs.h index 34bcaefcb..c87feb28b 100644 --- a/quickjs.h +++ b/quickjs.h @@ -456,6 +456,8 @@ typedef struct JSClassDef { } JSClassDef; JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id); +/* Returns the class ID if `v` is an object, otherwise returns 0. */ +JS_EXTERN JSClassID JS_GetClassID(JSValue v); JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def); JS_EXTERN int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id); From 3a54f95ad4dd7eae12a9fb88ce4b214a2dabcbfc Mon Sep 17 00:00:00 2001 From: Tyler Rockwood Date: Mon, 19 Feb 2024 19:57:35 -0600 Subject: [PATCH 2/2] introduce constant for invalid class ID Signed-off-by: Tyler Rockwood --- quickjs.c | 2 +- quickjs.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/quickjs.c b/quickjs.c index e71cbf207..c8c80d462 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3348,7 +3348,7 @@ JSClassID JS_GetClassID(JSValue v) { JSObject *p; if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT) - return 0; + return JS_INVALID_CLASS_ID; p = JS_VALUE_GET_OBJ(v); return p->class_id; } diff --git a/quickjs.h b/quickjs.h index c87feb28b..453e03d69 100644 --- a/quickjs.h +++ b/quickjs.h @@ -455,8 +455,9 @@ typedef struct JSClassDef { JSClassExoticMethods *exotic; } JSClassDef; +#define JS_INVALID_CLASS_ID 0 JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id); -/* Returns the class ID if `v` is an object, otherwise returns 0. */ +/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */ JS_EXTERN JSClassID JS_GetClassID(JSValue v); JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def); JS_EXTERN int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);