about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTyler Rockwood <rockwotj@gmail.com>2024-02-20 09:29:08 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-02 18:12:24 +0100
commit951fa118f79460c1aaa284d0a9e605a8d5780406 (patch)
treed15d777828ca7bfd829f66983b37127f49a9cabd /lib
parentd4885cdedbe2d2bb366a178e54622893898a4793 (diff)
downloadchawan-951fa118f79460c1aaa284d0a9e605a8d5780406.tar.gz
Add C API function JS_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.

* add JS_INVALID_CLASS_ID constant for invalid class ID.

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/quickjs/quickjs.c9
-rw-r--r--lib/quickjs/quickjs.h3
2 files changed, 12 insertions, 0 deletions
diff --git a/lib/quickjs/quickjs.c b/lib/quickjs/quickjs.c
index 4e5f9fb7..ae882138 100644
--- a/lib/quickjs/quickjs.c
+++ b/lib/quickjs/quickjs.c
@@ -3395,6 +3395,15 @@ JSClassID JS_NewClassID(JSClassID *pclass_id)
     return class_id;
 }
 
+JSClassID JS_GetClassID(JSValue v)
+{
+    JSObject *p;
+    if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT)
+        return JS_INVALID_CLASS_ID;
+    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/lib/quickjs/quickjs.h b/lib/quickjs/quickjs.h
index b2b52ed9..9505aed3 100644
--- a/lib/quickjs/quickjs.h
+++ b/lib/quickjs/quickjs.h
@@ -501,7 +501,10 @@ typedef struct JSClassDef {
     JSClassCanDestroy *can_destroy;
 } JSClassDef;
 
+#define JS_INVALID_CLASS_ID 0
 JSClassID JS_NewClassID(JSClassID *pclass_id);
+/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
+JSClassID JS_GetClassID(JSValue v);
 int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
 int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);