new string allocator

This commit is contained in:
Zia 2024-03-14 01:19:41 +03:30
parent 855fef2606
commit f058e2a5f3
2 changed files with 15 additions and 0 deletions

View file

@ -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)
{

View file

@ -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);