@@ -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+
385423static 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 ();
0 commit comments