diff --git a/quickjs.c b/quickjs.c index ebf45a9..c01e2ca 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3874,6 +3874,20 @@ static JSValue string_buffer_end(StringBuffer *s) return JS_MKPTR(JS_TAG_STRING, str); } +JSValue JS_NewStringWLen(JSContext *ctx, size_t buf_len) +{ + JSString *str; + if (buf_len <= 0) { + return JS_AtomToString(ctx, JS_ATOM_empty_string); + } + str = js_alloc_string_rt(ctx->rt, buf_len, 0); + if (unlikely(!str)){ + JS_ThrowOutOfMemory(ctx); + return JS_EXCEPTION; + } + memset(str->u.str8, 0, buf_len+1); + return JS_MKPTR(JS_TAG_STRING, str); +} /* create a string from a UTF-8 buffer */ JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len) { diff --git a/quickjs.h b/quickjs.h index a951e67..09ad07d 100644 --- a/quickjs.h +++ b/quickjs.h @@ -696,6 +696,7 @@ int JS_ToBigInt64(JSContext *ctx, int64_t *pres, JSValueConst val); /* same as JS_ToInt64() but allow BigInt */ int JS_ToInt64Ext(JSContext *ctx, int64_t *pres, JSValueConst val); +JSValue JS_NewStringWLen(JSContext *ctx, size_t len); JSValue JS_NewStringLen(JSContext *ctx, const char *str1, size_t len1); JSValue JS_NewString(JSContext *ctx, const char *str); JSValue JS_NewAtomString(JSContext *ctx, const char *str);