Skip to content

Commit

Permalink
Add C API function JS_GetClassID()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rockwotj authored and chqrlie committed Feb 21, 2024
1 parent 12c91df commit b91a2ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
3 changes: 3 additions & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b91a2ae

Please sign in to comment.