about summary refs log tree commit diff stats
path: root/lib/quickjs
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-10-21 23:34:56 +0200
committerbptato <nincsnevem662@gmail.com>2023-10-21 23:40:24 +0200
commit18008acc141a55449b28c1af487a080c4bbcb355 (patch)
treea81872bfc2e2add0b0c9b6f65f3be15f4d2790c8 /lib/quickjs
parent69870f3b974e65d61b564b396e01d21cc023e6e9 (diff)
downloadchawan-18008acc141a55449b28c1af487a080c4bbcb355.tar.gz
base64: reduce pointless re-coding using JSString
We now expose some functions from QuickJS to interact with JavaScript
strings without re-encoding them into UTF-8.
Diffstat (limited to 'lib/quickjs')
-rw-r--r--lib/quickjs/quickjs.c29
-rw-r--r--lib/quickjs/quickjs.h7
2 files changed, 35 insertions, 1 deletions
diff --git a/lib/quickjs/quickjs.c b/lib/quickjs/quickjs.c
index 61bcce93..697ced39 100644
--- a/lib/quickjs/quickjs.c
+++ b/lib/quickjs/quickjs.c
@@ -208,7 +208,6 @@ typedef enum JSErrorEnum {
 #define __exception __attribute__((warn_unused_result))
 
 typedef struct JSShape JSShape;
-typedef struct JSString JSString;
 typedef struct JSString JSAtomStruct;
 
 typedef enum {
@@ -4101,6 +4100,34 @@ void JS_FreeCString(JSContext *ctx, const char *ptr)
     JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
 }
 
+/* create a string from a narrow Unicode (latin-1) buffer */
+JSValue JS_NewNarrowStringLen(JSContext *ctx, const char *buf, size_t buf_len)
+{
+    if (buf_len > JS_STRING_LEN_MAX)
+        return JS_ThrowInternalError(ctx, "string too long");
+    return js_new_string8(ctx, (const uint8_t *)buf, buf_len);
+}
+
+JS_BOOL JS_IsStringWideChar(JSString *str)
+{
+    return str->is_wide_char;
+}
+
+uint8_t *JS_GetNarrowStringBuffer(JSString *str)
+{
+    return str->u.str8;
+}
+
+uint16_t *JS_GetWideStringBuffer(JSString *str)
+{
+    return str->u.str16;
+}
+
+uint32_t JS_GetStringLength(JSString *str)
+{
+    return str->len;
+}
+
 static int memcmp16_8(const uint16_t *src1, const uint8_t *src2, int len)
 {
     int c, i;
diff --git a/lib/quickjs/quickjs.h b/lib/quickjs/quickjs.h
index 1d761379..c831e3c8 100644
--- a/lib/quickjs/quickjs.h
+++ b/lib/quickjs/quickjs.h
@@ -49,6 +49,7 @@ extern "C" {
 typedef struct JSRuntime JSRuntime;
 typedef struct JSContext JSContext;
 typedef struct JSObject JSObject;
+typedef struct JSString JSString;
 typedef struct JSClass JSClass;
 typedef uint32_t JSClassID;
 typedef uint32_t JSAtom;
@@ -710,6 +711,12 @@ static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1)
 }
 void JS_FreeCString(JSContext *ctx, const char *ptr);
 
+JSValue JS_NewNarrowStringLen(JSContext *ctx, const char *str, size_t len);
+JS_BOOL JS_IsStringWideChar(JSString *str);
+uint8_t *JS_GetNarrowStringBuffer(JSString *str);
+uint16_t *JS_GetWideStringBuffer(JSString *str);
+uint32_t JS_GetWideStringLength(JSString *str);
+
 JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto, JSClassID class_id);
 JSValue JS_NewObjectClass(JSContext *ctx, int class_id);
 JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto);