@@ -962,6 +962,58 @@ static void get_uint8array(void)
962962 JS_FreeRuntime (rt );
963963}
964964
965+ static void new_symbol (void )
966+ {
967+ JSRuntime * rt = JS_NewRuntime ();
968+ JSContext * ctx = JS_NewContext (rt );
969+ JSValue global = JS_GetGlobalObject (ctx );
970+ JSValue sym , ret ;
971+
972+ /* Local symbol with NULL description -> Symbol() */
973+ sym = JS_NewSymbol (ctx , NULL , false);
974+ assert (!JS_IsException (sym ));
975+ assert (JS_IsSymbol (sym ));
976+ JS_SetPropertyStr (ctx , global , "sym_local_null" , sym );
977+
978+ ret = eval (ctx , "typeof sym_local_null === 'symbol' && sym_local_null.description === undefined && Symbol.keyFor(sym_local_null) === undefined" );
979+ assert (JS_IsBool (ret ) && JS_VALUE_GET_BOOL (ret ));
980+ JS_FreeValue (ctx , ret );
981+
982+ /* Global symbol with NULL description -> Symbol.for() -> Symbol.for('undefined') */
983+ sym = JS_NewSymbol (ctx , NULL , true);
984+ assert (!JS_IsException (sym ));
985+ assert (JS_IsSymbol (sym ));
986+ JS_SetPropertyStr (ctx , global , "sym_global_null" , sym );
987+
988+ ret = eval (ctx , "typeof sym_global_null === 'symbol' && sym_global_null.description === 'undefined' && Symbol.keyFor(sym_global_null) === 'undefined'" );
989+ assert (JS_IsBool (ret ) && JS_VALUE_GET_BOOL (ret ));
990+ JS_FreeValue (ctx , ret );
991+
992+ /* Local symbol with description -> Symbol('test_local') */
993+ sym = JS_NewSymbol (ctx , "test_local" , false);
994+ assert (!JS_IsException (sym ));
995+ assert (JS_IsSymbol (sym ));
996+ JS_SetPropertyStr (ctx , global , "sym_local_str" , sym );
997+
998+ ret = eval (ctx , "sym_local_str.description === 'test_local' && Symbol.keyFor(sym_local_str) === undefined" );
999+ assert (JS_IsBool (ret ) && JS_VALUE_GET_BOOL (ret ));
1000+ JS_FreeValue (ctx , ret );
1001+
1002+ /* Global symbol with description -> Symbol.for('test_global') */
1003+ sym = JS_NewSymbol (ctx , "test_global" , true);
1004+ assert (!JS_IsException (sym ));
1005+ assert (JS_IsSymbol (sym ));
1006+ JS_SetPropertyStr (ctx , global , "sym_global_str" , sym );
1007+
1008+ ret = eval (ctx , "sym_global_str.description === 'test_global' && Symbol.keyFor(sym_global_str) === 'test_global'" );
1009+ assert (JS_IsBool (ret ) && JS_VALUE_GET_BOOL (ret ));
1010+ JS_FreeValue (ctx , ret );
1011+
1012+ JS_FreeValue (ctx , global );
1013+ JS_FreeContext (ctx );
1014+ JS_FreeRuntime (rt );
1015+ }
1016+
9651017int main (void )
9661018{
9671019 cfunctions ();
@@ -981,5 +1033,6 @@ int main(void)
9811033 slice_string_tocstring ();
9821034 immutable_array_buffer ();
9831035 get_uint8array ();
1036+ new_symbol ();
9841037 return 0 ;
9851038}
0 commit comments