Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions api-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,44 @@ static void module_serde(void)
JS_FreeRuntime(rt);
}

static void runtime_cstring_free(void)
{
JSRuntime *rt = JS_NewRuntime();
JSContext *ctx = JS_NewContext(rt);
// string -> cstring + JS_FreeCStringRT
{
JSValue ret = eval(ctx, "\"testStringPleaseIgnore\"");
assert(JS_IsString(ret));
const char *s = JS_ToCString(ctx, ret);
assert(s);
assert(strcmp(s, "testStringPleaseIgnore") == 0);
JS_FreeCStringRT(rt, s);
JS_FreeValue(ctx, ret);
}
// string -> cstring + JS_FreeCStringRT, destroying the source value first
{
JSValue ret = eval(ctx, "\"testStringPleaseIgnore\"");
assert(JS_IsString(ret));
const char *s = JS_ToCString(ctx, ret);
assert(s);
JS_FreeValue(ctx, ret);
assert(strcmp(s, "testStringPleaseIgnore") == 0);
JS_FreeCStringRT(rt, s);
}
// number -> cstring + JS_FreeCStringRT
{
JSValue ret = eval(ctx, "123987");
assert(JS_IsNumber(ret));
const char *s = JS_ToCString(ctx, ret);
assert(s);
assert(strcmp(s, "123987") == 0);
JS_FreeCStringRT(rt, s);
JS_FreeValue(ctx, ret);
}
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
}

static void two_byte_string(void)
{
JSRuntime *rt = JS_NewRuntime();
Expand Down Expand Up @@ -804,6 +842,7 @@ int main(void)
raw_context_global_var();
is_array();
module_serde();
runtime_cstring_free();
two_byte_string();
weak_map_gc_check();
promise_hook();
Expand Down
8 changes: 8 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4360,6 +4360,14 @@ void JS_FreeCString(JSContext *ctx, const char *ptr)
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, (JSString *)ptr - 1));
}

void JS_FreeCStringRT(JSRuntime *rt, const char *ptr)
{
if (!ptr)
return;
/* purposely removing constness */
JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_STRING, (JSString *)ptr - 1));
}

static int memcmp16_8(const uint16_t *src1, const uint8_t *src2, int len)
{
int c, i;
Expand Down
1 change: 1 addition & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1)
return JS_ToCStringLen2(ctx, NULL, val1, 0);
}
JS_EXTERN void JS_FreeCString(JSContext *ctx, const char *ptr);
JS_EXTERN void JS_FreeCStringRT(JSRuntime *rt, const char *ptr);

JS_EXTERN JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto,
JSClassID class_id);
Expand Down