Skip to content

Commit b91a2ae

Browse files
rockwotjchqrlie
authored andcommitted
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 <[email protected]>
1 parent 12c91df commit b91a2ae

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

quickjs.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,6 +3391,15 @@ JSClassID JS_NewClassID(JSClassID *pclass_id)
33913391
return class_id;
33923392
}
33933393

3394+
JSClassID JS_GetClassID(JSValue v)
3395+
{
3396+
JSObject *p;
3397+
if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT)
3398+
return JS_INVALID_CLASS_ID;
3399+
p = JS_VALUE_GET_OBJ(v);
3400+
return p->class_id;
3401+
}
3402+
33943403
BOOL JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id)
33953404
{
33963405
return (class_id < rt->class_count &&

quickjs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,10 @@ typedef struct JSClassDef {
499499
JSClassExoticMethods *exotic;
500500
} JSClassDef;
501501

502+
#define JS_INVALID_CLASS_ID 0
502503
JSClassID JS_NewClassID(JSClassID *pclass_id);
504+
/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
505+
JSClassID JS_GetClassID(JSValue v);
503506
int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
504507
int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);
505508

0 commit comments

Comments
 (0)