Skip to content

Commit 33f7249

Browse files
authored
Add method to GetClassID (#275)
* 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 <rockwood@redpanda.com> * introduce constant for invalid class ID Signed-off-by: Tyler Rockwood <rockwood@redpanda.com> --------- Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
1 parent b257545 commit 33f7249

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

quickjs.c

+9
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,15 @@ JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id)
33443344
return class_id;
33453345
}
33463346

3347+
JSClassID JS_GetClassID(JSValue v)
3348+
{
3349+
JSObject *p;
3350+
if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT)
3351+
return JS_INVALID_CLASS_ID;
3352+
p = JS_VALUE_GET_OBJ(v);
3353+
return p->class_id;
3354+
}
3355+
33473356
BOOL JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id)
33483357
{
33493358
return (class_id < rt->class_count &&

quickjs.h

+3
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ typedef struct JSClassDef {
455455
JSClassExoticMethods *exotic;
456456
} JSClassDef;
457457

458+
#define JS_INVALID_CLASS_ID 0
458459
JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id);
460+
/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
461+
JS_EXTERN JSClassID JS_GetClassID(JSValue v);
459462
JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
460463
JS_EXTERN int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);
461464

0 commit comments

Comments
 (0)