Skip to content

Commit d96c04a

Browse files
Add 'JS_FreeCStringRT' (#1262)
1 parent be664ca commit d96c04a

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

api-test.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,44 @@ static void module_serde(void)
382382
JS_FreeRuntime(rt);
383383
}
384384

385+
static void runtime_cstring_free(void)
386+
{
387+
JSRuntime *rt = JS_NewRuntime();
388+
JSContext *ctx = JS_NewContext(rt);
389+
// string -> cstring + JS_FreeCStringRT
390+
{
391+
JSValue ret = eval(ctx, "\"testStringPleaseIgnore\"");
392+
assert(JS_IsString(ret));
393+
const char *s = JS_ToCString(ctx, ret);
394+
assert(s);
395+
assert(strcmp(s, "testStringPleaseIgnore") == 0);
396+
JS_FreeCStringRT(rt, s);
397+
JS_FreeValue(ctx, ret);
398+
}
399+
// string -> cstring + JS_FreeCStringRT, destroying the source value first
400+
{
401+
JSValue ret = eval(ctx, "\"testStringPleaseIgnore\"");
402+
assert(JS_IsString(ret));
403+
const char *s = JS_ToCString(ctx, ret);
404+
assert(s);
405+
JS_FreeValue(ctx, ret);
406+
assert(strcmp(s, "testStringPleaseIgnore") == 0);
407+
JS_FreeCStringRT(rt, s);
408+
}
409+
// number -> cstring + JS_FreeCStringRT
410+
{
411+
JSValue ret = eval(ctx, "123987");
412+
assert(JS_IsNumber(ret));
413+
const char *s = JS_ToCString(ctx, ret);
414+
assert(s);
415+
assert(strcmp(s, "123987") == 0);
416+
JS_FreeCStringRT(rt, s);
417+
JS_FreeValue(ctx, ret);
418+
}
419+
JS_FreeContext(ctx);
420+
JS_FreeRuntime(rt);
421+
}
422+
385423
static void two_byte_string(void)
386424
{
387425
JSRuntime *rt = JS_NewRuntime();
@@ -804,6 +842,7 @@ int main(void)
804842
raw_context_global_var();
805843
is_array();
806844
module_serde();
845+
runtime_cstring_free();
807846
two_byte_string();
808847
weak_map_gc_check();
809848
promise_hook();

quickjs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4360,6 +4360,14 @@ void JS_FreeCString(JSContext *ctx, const char *ptr)
43604360
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, (JSString *)ptr - 1));
43614361
}
43624362

4363+
void JS_FreeCStringRT(JSRuntime *rt, const char *ptr)
4364+
{
4365+
if (!ptr)
4366+
return;
4367+
/* purposely removing constness */
4368+
JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_STRING, (JSString *)ptr - 1));
4369+
}
4370+
43634371
static int memcmp16_8(const uint16_t *src1, const uint8_t *src2, int len)
43644372
{
43654373
int c, i;

quickjs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1)
820820
return JS_ToCStringLen2(ctx, NULL, val1, 0);
821821
}
822822
JS_EXTERN void JS_FreeCString(JSContext *ctx, const char *ptr);
823+
JS_EXTERN void JS_FreeCStringRT(JSRuntime *rt, const char *ptr);
823824

824825
JS_EXTERN JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto,
825826
JSClassID class_id);

0 commit comments

Comments
 (0)