From ecf55237e85ac164985640dbbd9df658de7a76f9 Mon Sep 17 00:00:00 2001 From: caturria Date: Tue, 28 Oct 2025 19:08:02 -0400 Subject: [PATCH 1/3] Make the definition of JSClass public and add JS_GetClass. --- quickjs.c | 17 +++++++---------- quickjs.h | 12 ++++++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/quickjs.c b/quickjs.c index a6099dc3c..87c265cb2 100644 --- a/quickjs.c +++ b/quickjs.c @@ -327,16 +327,6 @@ struct JSRuntime { JSRuntimeFinalizerState *finalizers; }; -struct JSClass { - uint32_t class_id; /* 0 means free entry */ - JSAtom class_name; - JSClassFinalizer *finalizer; - JSClassGCMark *gc_mark; - JSClassCall *call; - /* pointers for exotic behavior, can be NULL if none are present */ - const JSClassExoticMethods *exotic; -}; - typedef struct JSStackFrame { struct JSStackFrame *prev_frame; /* NULL if first stack frame */ JSValue cur_func; /* current function, JS_UNDEFINED if the frame is detached */ @@ -3610,6 +3600,13 @@ bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id) rt->class_array[class_id].class_id != 0); } +const JSClass *JS_GetClass(JSRuntime *rt, JSClassID class_id) +{ + if(!JS_IsRegisteredClass(rt, class_id)) { + return NULL; + } + return &rt->class_array[class_id]; +} /* create a new object internal class. Return -1 if error, 0 if OK. The finalizer can be NULL if none is needed. */ static int JS_NewClass1(JSRuntime *rt, JSClassID class_id, diff --git a/quickjs.h b/quickjs.h index ab07706d9..60f4daec3 100644 --- a/quickjs.h +++ b/quickjs.h @@ -618,6 +618,16 @@ typedef struct JSClassDef { JSClassExoticMethods *exotic; } JSClassDef; +struct JSClass { + uint32_t class_id; /* 0 means free entry */ + JSAtom class_name; + JSClassFinalizer *finalizer; + JSClassGCMark *gc_mark; + JSClassCall *call; + /* pointers for exotic behavior, can be NULL if none are present */ + const JSClassExoticMethods *exotic; +}; + #define JS_EVAL_OPTIONS_VERSION 1 typedef struct JSEvalOptions { @@ -634,6 +644,8 @@ JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id); JS_EXTERN JSClassID JS_GetClassID(JSValueConst v); JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def); JS_EXTERN bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id); +/* Returns a class definition if `class_id` is a registered class, otherwise returns NULL. */ +const JSClass *JS_GetClass(JSRuntime *rt, JSClassID class_id); /* value handling */ From 6a9f3a59ad5591514355fbbb0a20104e2d515e09 Mon Sep 17 00:00:00 2001 From: caturria Date: Thu, 30 Oct 2025 16:39:52 -0400 Subject: [PATCH 2/3] Replace JS_GetClass with JS_GetClassName. --- quickjs.c | 22 +++++++++++++++++----- quickjs.h | 14 ++------------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/quickjs.c b/quickjs.c index 87c265cb2..ba3fc91de 100644 --- a/quickjs.c +++ b/quickjs.c @@ -327,6 +327,16 @@ struct JSRuntime { JSRuntimeFinalizerState *finalizers; }; +struct JSClass { + uint32_t class_id; /* 0 means free entry */ + JSAtom class_name; + JSClassFinalizer *finalizer; + JSClassGCMark *gc_mark; + JSClassCall *call; + /* pointers for exotic behavior, can be NULL if none are present */ + const JSClassExoticMethods *exotic; +}; + typedef struct JSStackFrame { struct JSStackFrame *prev_frame; /* NULL if first stack frame */ JSValue cur_func; /* current function, JS_UNDEFINED if the frame is detached */ @@ -3599,14 +3609,16 @@ bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id) return (class_id < rt->class_count && rt->class_array[class_id].class_id != 0); } - -const JSClass *JS_GetClass(JSRuntime *rt, JSClassID class_id) +JSAtom JS_GetClassName(JSRuntime *rt, JSClassID id) { - if(!JS_IsRegisteredClass(rt, class_id)) { - return NULL; + if(!JS_IsRegisteredClass(rt, id)) { + return JS_ATOM_NULL; } - return &rt->class_array[class_id]; + JSAtom ret = rt->class_array[id].class_name; + JS_DupAtomRT(rt, ret); + return ret; } + /* create a new object internal class. Return -1 if error, 0 if OK. The finalizer can be NULL if none is needed. */ static int JS_NewClass1(JSRuntime *rt, JSClassID class_id, diff --git a/quickjs.h b/quickjs.h index 60f4daec3..15f3d77ea 100644 --- a/quickjs.h +++ b/quickjs.h @@ -618,16 +618,6 @@ typedef struct JSClassDef { JSClassExoticMethods *exotic; } JSClassDef; -struct JSClass { - uint32_t class_id; /* 0 means free entry */ - JSAtom class_name; - JSClassFinalizer *finalizer; - JSClassGCMark *gc_mark; - JSClassCall *call; - /* pointers for exotic behavior, can be NULL if none are present */ - const JSClassExoticMethods *exotic; -}; - #define JS_EVAL_OPTIONS_VERSION 1 typedef struct JSEvalOptions { @@ -644,8 +634,8 @@ JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id); JS_EXTERN JSClassID JS_GetClassID(JSValueConst v); JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def); JS_EXTERN bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id); -/* Returns a class definition if `class_id` is a registered class, otherwise returns NULL. */ -const JSClass *JS_GetClass(JSRuntime *rt, JSClassID class_id); +/* Returns the class name or JS_ATOM_NULL if `id` is not a registered class. Must be freed with JS_FreeAtom. */ +JS_EXTERN JSAtom JS_GetClassName(JSRuntime *rt, JSClassID id); /* value handling */ From 59e77cf315789b4fd66b85fe0b39ecea799c54d2 Mon Sep 17 00:00:00 2001 From: caturria Date: Thu, 30 Oct 2025 19:54:57 -0400 Subject: [PATCH 3/3] Implement stylistic suggestions. --- quickjs.c | 11 +++++------ quickjs.h | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/quickjs.c b/quickjs.c index ba3fc91de..e4328a107 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3609,15 +3609,14 @@ bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id) return (class_id < rt->class_count && rt->class_array[class_id].class_id != 0); } -JSAtom JS_GetClassName(JSRuntime *rt, JSClassID id) +JSAtom JS_GetClassName(JSRuntime *rt, JSClassID class_id) { - if(!JS_IsRegisteredClass(rt, id)) { + if (JS_IsRegisteredClass(rt, class_id)) { + return JS_DupAtomRT(rt, rt->class_array[class_id].class_id); + } else { return JS_ATOM_NULL; } - JSAtom ret = rt->class_array[id].class_name; - JS_DupAtomRT(rt, ret); - return ret; -} + } /* create a new object internal class. Return -1 if error, 0 if OK. The finalizer can be NULL if none is needed. */ diff --git a/quickjs.h b/quickjs.h index 15f3d77ea..6ba21237c 100644 --- a/quickjs.h +++ b/quickjs.h @@ -635,7 +635,7 @@ JS_EXTERN JSClassID JS_GetClassID(JSValueConst v); JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def); JS_EXTERN bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id); /* Returns the class name or JS_ATOM_NULL if `id` is not a registered class. Must be freed with JS_FreeAtom. */ -JS_EXTERN JSAtom JS_GetClassName(JSRuntime *rt, JSClassID id); +JS_EXTERN JSAtom JS_GetClassName(JSRuntime *rt, JSClassID class_id); /* value handling */