From 33f72491a909117e4ac3c86b67c479c00cf95919 Mon Sep 17 00:00:00 2001 From: Tyler Rockwood Date: Tue, 20 Feb 2024 02:29:08 -0600 Subject: [PATCH] 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 * introduce constant for invalid class ID Signed-off-by: Tyler Rockwood --------- Signed-off-by: Tyler Rockwood --- quickjs.c | 9 +++++++++ quickjs.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/quickjs.c b/quickjs.c index 3feacd9..39dbfd0 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3344,6 +3344,15 @@ JSClassID JS_NewClassID(JSRuntime *rt, 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/quickjs.h b/quickjs.h index 34bcaef..453e03d 100644 --- a/quickjs.h +++ b/quickjs.h @@ -455,7 +455,10 @@ typedef struct JSClassDef { JSClassExoticMethods *exotic; } JSClassDef; +#define JS_INVALID_CLASS_ID 0 JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id); +/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */ +JS_EXTERN JSClassID JS_GetClassID(JSValue v); JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def); JS_EXTERN int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);