URLSearchParam constructor support for object initialization

This adds support for:

```
new URLSearchParams({over: 9000});
```

The spec says that any thing that produces/iterates a sequence of string pairs
is valid. By using the lower-level JsObject, this hopefully takes care of the
most common cases. But I don't think it's complete, and I don't think we
currently capture enough data to make this work. There's no way for the JS
runtime to know if a value (say, a netsurf instance, or even a Zig instance)
provides an string=>string iterator.
This commit is contained in:
Karl Seguin
2025-06-05 09:44:36 +08:00
parent fff0a8a522
commit c758054250
3 changed files with 71 additions and 0 deletions

View File

@@ -1366,6 +1366,13 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
}
}
pub fn get(self: JsObject, key: []const u8) !Value {
const scope = self.scope;
const js_key = v8.String.initUtf8(scope.isolate, key);
const js_val = try self.js_obj.getValue(scope.context, js_key);
return scope.createValue(js_val);
}
pub fn isTruthy(self: JsObject) bool {
const js_value = self.js_obj.toValue();
return js_value.toBool(self.scope.isolate);
@@ -1421,6 +1428,20 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
pub fn isNullOrUndefined(self: JsObject) bool {
return self.js_obj.toValue().isNullOrUndefined();
}
pub fn nameIterator(self: JsObject) ValueIterator {
const scope = self.scope;
const js_obj = self.js_obj;
const array = js_obj.getPropertyNames(scope.context);
const count = array.length();
return .{
.count = count,
.scope = scope,
.js_obj = array.castTo(v8.Object),
};
}
};
// This only exists so that we know whether a function wants the opaque
@@ -1626,6 +1647,25 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
}
};
pub const ValueIterator = struct {
count: u32,
idx: u32 = 0,
js_obj: v8.Object,
scope: *const Scope,
pub fn next(self: *ValueIterator) !?Value {
const idx = self.idx;
if (idx == self.count) {
return null;
}
self.idx += 1;
const scope = self.scope;
const js_val = try self.js_obj.getAtIndex(scope.context, idx);
return scope.createValue(js_val);
}
};
fn compileModule(isolate: v8.Isolate, src: []const u8, name: []const u8) !v8.Module {
// compile
const script_name = v8.String.initUtf8(isolate, name);