]> git.kaiwu.me - quickjs.git/commitdiff
added JS_AtomToCStringLen()
authorFabrice Bellard <fabrice@bellard.org>
Tue, 20 May 2025 16:03:29 +0000 (18:03 +0200)
committerFabrice Bellard <fabrice@bellard.org>
Tue, 20 May 2025 16:03:29 +0000 (18:03 +0200)
quickjs.c
quickjs.h

index beaa77896224027940390d4dcf1702125ad4ee71..eb943065b5ec70abcbfaf4978a53aad3be0428b4 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -3205,15 +3205,18 @@ static BOOL JS_AtomSymbolHasDescription(JSContext *ctx, JSAtom v)
 }
 
 /* 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))
+    if (JS_IsException(str)) {
+        if (plen)
+            *plen = 0;
         return NULL;
-    cstr = JS_ToCString(ctx, str);
+    }
+    cstr = JS_ToCStringLen(ctx, plen, str);
     JS_FreeValue(ctx, str);
     return cstr;
 }
index 64a633be208a9f326e6adb34f6ce029a29072288..60a8d49a52c7475ffdbd039494c7b4f0d7f4d041 100644 (file)
--- a/quickjs.h
+++ b/quickjs.h
@@ -456,7 +456,11 @@ void JS_FreeAtom(JSContext *ctx, JSAtom v);
 void JS_FreeAtomRT(JSRuntime *rt, JSAtom v);
 JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom);
 JSValue JS_AtomToString(JSContext *ctx, JSAtom atom);
-const char *JS_AtomToCString(JSContext *ctx, JSAtom atom);
+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);
+}
 JSAtom JS_ValueToAtom(JSContext *ctx, JSValueConst val);
 
 /* object class support */