You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(dsl): add constructor to js.Object(T) for one-line object creation #44
Add a constructor (init / from) to js.Object(T) so user code can create and populate a JS object in a single call, instead of manually creating a raw object, wrapping it, and calling set.
Object(T) (src/js/object.zig) currently exposes only validateArg, get, set, and toValue — there is no constructor. This boilerplate is repetitive and leaks N-API details into otherwise high-level DSL code.
Proposed solution
Add an init method to the type returned by Object(T). It already knows T and has set, so it just needs to create the underlying object and populate it:
/// Creates a new JS object and populates it from the Zig struct `T`.pubfninit(value: T) !Self {
constself=Self{ .val=tryenv().createObject() };
tryself.set(value);
returnself;
}
(using the in-scope env accessor, matching how String.from / Number.from reach the env)
The scalar DSL wrappers already use from(...) (String.from, Number.from, Boolean.from, Date.from, Uint8Array.from). For consistency we could name it from instead of init — open to either. init reads slightly better here since the argument is a struct of fields rather than a single scalar.
Notes / open questions
Should there also be an empty constructor (e.g. empty() / init(.{}) when all fields are optional) for incremental population? Probably out of scope for the first pass.
Keep the existing set/get/toValue API unchanged; this is purely additive.
Activity