Skip to content

Commit 1defa63

Browse files
authored
Add version to navigator.userAgent (#542)
A la Deno and Bun.
1 parent c25aad7 commit 1defa63

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

qjs.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

144152
static 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
}

quickjs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ typedef struct JSCFunctionListEntry {
982982
#define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
983983
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }
984984
#define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
985+
#define JS_CGETSET_DEF2(name, fgetter, fsetter, prop_flags) { name, prop_flags, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
985986
#define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } }
986987
#define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, { .str = cstr } }
987988
#define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } }

0 commit comments

Comments
 (0)