Skip to content

Commit 7891004

Browse files
authored
Add JS_NewTwoByteString (#1000)
Fixes: #992
1 parent cfa0755 commit 7891004

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

api-test.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,42 @@ static void module_serde(void)
233233
JS_FreeRuntime(rt);
234234
}
235235

236+
static void two_byte_string(void)
237+
{
238+
JSRuntime *rt = JS_NewRuntime();
239+
JSContext *ctx = JS_NewContext(rt);
240+
{
241+
JSValue v = JS_NewTwoByteString(ctx, NULL, 0);
242+
assert(!JS_IsException(v));
243+
const char *s = JS_ToCString(ctx, v);
244+
assert(s);
245+
assert(!strcmp(s, ""));
246+
JS_FreeCString(ctx, s);
247+
JS_FreeValue(ctx, v);
248+
}
249+
{
250+
JSValue v = JS_NewTwoByteString(ctx, (uint16_t[]){'o','k'}, 2);
251+
assert(!JS_IsException(v));
252+
const char *s = JS_ToCString(ctx, v);
253+
assert(s);
254+
assert(!strcmp(s, "ok"));
255+
JS_FreeCString(ctx, s);
256+
JS_FreeValue(ctx, v);
257+
}
258+
{
259+
JSValue v = JS_NewTwoByteString(ctx, (uint16_t[]){0xD800}, 1);
260+
assert(!JS_IsException(v));
261+
const char *s = JS_ToCString(ctx, v);
262+
assert(s);
263+
// questionable but surrogates don't map to UTF-8 without WTF-8
264+
assert(!strcmp(s, "\xED\xA0\x80"));
265+
JS_FreeCString(ctx, s);
266+
JS_FreeValue(ctx, v);
267+
}
268+
JS_FreeContext(ctx);
269+
JS_FreeRuntime(rt);
270+
}
271+
236272
int main(void)
237273
{
238274
sync_call();
@@ -241,5 +277,6 @@ int main(void)
241277
raw_context_global_var();
242278
is_array();
243279
module_serde();
280+
two_byte_string();
244281
return 0;
245282
}

quickjs.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4030,6 +4030,19 @@ JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len)
40304030
return JS_MKPTR(JS_TAG_STRING, str);
40314031
}
40324032

4033+
JSValue JS_NewTwoByteString(JSContext *ctx, const uint16_t *buf, size_t len)
4034+
{
4035+
JSString *str;
4036+
4037+
if (!len)
4038+
return JS_AtomToString(ctx, JS_ATOM_empty_string);
4039+
str = js_alloc_string(ctx, len, 1);
4040+
if (!str)
4041+
return JS_EXCEPTION;
4042+
memcpy(str16(str), buf, len * sizeof(*buf));
4043+
return JS_MKPTR(JS_TAG_STRING, str);
4044+
}
4045+
40334046
static JSValue JS_ConcatString3(JSContext *ctx, const char *str1,
40344047
JSValue str2, const char *str3)
40354048
{

quickjs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,10 @@ JS_EXTERN JSValue JS_NewStringLen(JSContext *ctx, const char *str1, size_t len1)
761761
static inline JSValue JS_NewString(JSContext *ctx, const char *str) {
762762
return JS_NewStringLen(ctx, str, strlen(str));
763763
}
764+
// makes a copy of the input; does not check if the input is valid UTF-16,
765+
// that is the responsibility of the caller
766+
JS_EXTERN JSValue JS_NewTwoByteString(JSContext *ctx, const uint16_t *buf,
767+
size_t len);
764768
JS_EXTERN JSValue JS_NewAtomString(JSContext *ctx, const char *str);
765769
JS_EXTERN JSValue JS_ToString(JSContext *ctx, JSValueConst val);
766770
JS_EXTERN JSValue JS_ToPropertyKey(JSContext *ctx, JSValueConst val);

0 commit comments

Comments
 (0)