]> git.kaiwu.me - quickjs.git/commitdiff
Add C API function JS_GetClassID()
authorTyler Rockwood <rockwotj@gmail.com>
Tue, 20 Feb 2024 08:29:08 +0000 (09:29 +0100)
committerCharlie Gordon <github@chqrlie.org>
Wed, 21 Feb 2024 14:33:37 +0000 (15:33 +0100)
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>
quickjs.c
quickjs.h

index 678df93bfa531f8df19927fab95da54ad551880c..216b12035422d68bbdfa8c4cad6d24145fa2f996 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -3391,6 +3391,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 &&
index 59148f704e87737d246e209fea0725e2ad36797d..003af2fb57297adbf84ef1ec5a3528af4e2c4fa3 100644 (file)
--- a/quickjs.h
+++ b/quickjs.h
@@ -499,7 +499,10 @@ typedef struct JSClassDef {
     JSClassExoticMethods *exotic;
 } 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);