]> git.kaiwu.me - quickjs.git/commitdiff
added JS_ToCStringNZ() and exported JS_ToUTF32String()
authorFabrice Bellard <fabrice@bellard.org>
Sat, 10 Oct 2026 14:26:23 +0000 (16:26 +0200)
committerFabrice Bellard <fabrice@bellard.org>
Sat, 10 Oct 2026 14:26:23 +0000 (16:26 +0200)
quickjs.c
quickjs.h

index 88504ca9486a546510cfdf8dd1c4d8694f7c6218..639ab9ebb16c8b2200144dd1deb87edc651a46b8 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -4569,6 +4569,19 @@ const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, BO
     return NULL;
 }
 
+/* return an exception if the string contains a '\0' character */
+const char *JS_ToCStringNZ(JSContext *ctx, JSValueConst val1)
+{
+    size_t len;
+    const char *str = JS_ToCStringLen(ctx, &len, val1);
+    if (str && strlen(str) != len) {
+        JS_FreeCString(ctx, str);
+        JS_ThrowRangeError(ctx, "string contains embedded null character");
+        return NULL;
+    }
+    return str;
+}
+
 void JS_FreeCString(JSContext *ctx, const char *ptr)
 {
     JSString *p;
@@ -46752,7 +46765,7 @@ static JSValue js_string_toLowerCase(JSContext *ctx, JSValueConst this_val,
 #ifdef CONFIG_ALL_UNICODE
 
 /* return (-1, NULL) if exception, otherwise (len, buf) */
-static int JS_ToUTF32String(JSContext *ctx, uint32_t **pbuf, JSValueConst val1)
+int JS_ToUTF32String(JSContext *ctx, uint32_t **pbuf, JSValueConst val1)
 {
     JSValue val;
     JSString *p;
index 27750be56cba25219c2bbf482d518edf6e0724d5..5ba86c7c863640871b89011d2b7cef8c1a57d4ce 100644 (file)
--- a/quickjs.h
+++ b/quickjs.h
@@ -766,7 +766,9 @@ static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1)
 {
     return JS_ToCStringLen2(ctx, NULL, val1, 0);
 }
+const char *JS_ToCStringNZ(JSContext *ctx, JSValueConst val1);
 void JS_FreeCString(JSContext *ctx, const char *ptr);
+int JS_ToUTF32String(JSContext *ctx, uint32_t **pbuf, JSValueConst val);
 
 JSValue JS_NewSymbol(JSContext *ctx, const char *description, JS_BOOL is_global);