@@ -137,13 +137,20 @@ static JSValue js_gc(JSContext *ctx, JSValue this_val,
137137 return JS_UNDEFINED ;
138138}
139139
140- static const JSCFunctionListEntry navigator_obj [] = {
141- JS_PROP_STRING_DEF ("userAgent" , "quickjs-ng" , JS_PROP_ENUMERABLE ),
140+ static JSValue js_navigator_get_userAgent (JSContext * ctx , JSValue this_val )
141+ {
142+ char version [32 ];
143+ snprintf (version , sizeof (version ), "quickjs-ng/%s" , JS_GetVersion ());
144+ return JS_NewString (ctx , version );
145+ }
146+
147+ static const JSCFunctionListEntry navigator_proto_funcs [] = {
148+ JS_CGETSET_DEF2 ("userAgent" , js_navigator_get_userAgent , NULL , JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE ),
149+ JS_PROP_STRING_DEF ("[Symbol.toStringTag]" , "Navigator" , JS_PROP_CONFIGURABLE ),
142150};
143151
144152static const JSCFunctionListEntry global_obj [] = {
145153 JS_CFUNC_DEF ("gc" , 0 , js_gc ),
146- JS_OBJECT_DEF ("navigator" , navigator_obj , countof (navigator_obj ), JS_PROP_C_W_E ),
147154#if defined(__ASAN__ ) || defined (__UBSAN__ )
148155 JS_PROP_INT32_DEF ("__running_with_sanitizer__" , 1 , JS_PROP_C_W_E ),
149156#endif
@@ -164,7 +171,12 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
164171 JSValue global = JS_GetGlobalObject (ctx );
165172 JS_SetPropertyFunctionList (ctx , global , global_obj , countof (global_obj ));
166173 JS_SetPropertyFunctionList (ctx , global , & argv0 , 1 );
174+ JSValue navigator_proto = JS_NewObject (ctx );
175+ JS_SetPropertyFunctionList (ctx , navigator_proto , navigator_proto_funcs , countof (navigator_proto_funcs ));
176+ JSValue navigator = JS_NewObjectProto (ctx , navigator_proto );
177+ JS_DefinePropertyValueStr (ctx , global , "navigator" , navigator , JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE );
167178 JS_FreeValue (ctx , global );
179+ JS_FreeValue (ctx , navigator_proto );
168180
169181 return ctx ;
170182}
0 commit comments