From be92adcc6659b678e8a45093a249b0d4f42c67cc Mon Sep 17 00:00:00 2001 From: Haruma <114157573+Haruma-VN@users.noreply.github.com> Date: Sun, 25 May 2025 15:25:57 +0700 Subject: [PATCH 1/3] Add JS_AtomToCStringLen and change JS_AtomToCString method to use JS_AtomToCStringLen instead --- quickjs.c | 7 ++++--- quickjs.h | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/quickjs.c b/quickjs.c index 5e9e1a1d0..ad1cf207f 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3459,15 +3459,16 @@ static __maybe_unused void print_atom(JSContext *ctx, JSAtom atom) } /* free with JS_FreeCString() */ -const char *JS_AtomToCString(JSContext *ctx, JSAtom atom) +const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom) { JSValue str; const char *cstr; str = JS_AtomToString(ctx, atom); if (JS_IsException(str)) - return NULL; - cstr = JS_ToCString(ctx, str); + if (plen) + *plen = 0; + cstr = JS_ToCStringLen(ctx, plen, str); JS_FreeValue(ctx, str); return cstr; } diff --git a/quickjs.h b/quickjs.h index 3d9b8dec4..ba0718eb3 100644 --- a/quickjs.h +++ b/quickjs.h @@ -544,7 +544,11 @@ JS_EXTERN void JS_FreeAtom(JSContext *ctx, JSAtom v); JS_EXTERN void JS_FreeAtomRT(JSRuntime *rt, JSAtom v); JS_EXTERN JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom); JS_EXTERN JSValue JS_AtomToString(JSContext *ctx, JSAtom atom); -JS_EXTERN const char *JS_AtomToCString(JSContext *ctx, JSAtom atom); +JS_EXTERN const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom); +static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom) +{ + return JS_AtomToCStringLen(ctx, NULL, atom); +} JS_EXTERN JSAtom JS_ValueToAtom(JSContext *ctx, JSValueConst val); /* object class support */ From 1d20ef61423f8ef088e4727bb5a46f99db872e88 Mon Sep 17 00:00:00 2001 From: Haruma <114157573+Haruma-VN@users.noreply.github.com> Date: Sun, 25 May 2025 15:26:49 +0700 Subject: [PATCH 2/3] Add the missing exception handling --- quickjs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/quickjs.c b/quickjs.c index ad1cf207f..023a751df 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3468,6 +3468,7 @@ const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom) if (JS_IsException(str)) if (plen) *plen = 0; + return NULL; cstr = JS_ToCStringLen(ctx, plen, str); JS_FreeValue(ctx, str); return cstr; From bf2af8d3e0bd19cfc89b9cbd9ff0b20cc96d3588 Mon Sep 17 00:00:00 2001 From: Haruma <114157573+Haruma-VN@users.noreply.github.com> Date: Sun, 25 May 2025 21:15:35 +0700 Subject: [PATCH 3/3] Add the missing bracket --- quickjs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 023a751df..07e464fdc 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3465,10 +3465,11 @@ const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom) const char *cstr; str = JS_AtomToString(ctx, atom); - if (JS_IsException(str)) + if (JS_IsException(str)) { if (plen) *plen = 0; return NULL; + } cstr = JS_ToCStringLen(ctx, plen, str); JS_FreeValue(ctx, str); return cstr;