feat(dsl): add constructor to js.Object(T) for one-line object creation
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 76/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Tranquilla
- Stack tecnologico
- zig
- Ambito
- api
Direzione di ricerca
Inizia in src/js/object.zig e analizza il tipo restituito da Object(T), il suo metodo set e il modo in cui String.from o Number.from accedono all’ambiente. Aggiungi il costruttore proposto utilizzando i percorsi esistenti per la creazione e il popolamento degli oggetti, mantenendo invariati get, set e toValue. Il lavoro sarà completato quando i chiamanti potranno creare e popolare un oggetto JS tipizzato in una sola chiamata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
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.
Motivation
Defining an object shape is ergonomic:
pub const BitArray = js.Object(struct {
uint8Array: js.Uint8Array,
bitLen: js.Number,
});
But creating an instance at runtime currently requires the low-level dance:
const e = js.env();
const raw = try e.createObject(); // napi.Value, a fresh {} object
var obj = BitArray{ .val = raw }; // wrap it
try obj.set(.{ .uint8Array = ..., .bitLen = ... });
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`.
pub fn init(value: T) !Self {
const self = Self{ .val = try env().createObject() };
try self.set(value);
return self;
}
(using the in-scope env accessor, matching how String.from / Number.from reach the env)
Call site collapses to:
const ba = try BitArray.init(.{
.uint8Array = js.Uint8Array.from(&bytes),
.bitLen = js.Number.from(@as(i32, 42)),
});
Naming
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/toValueAPI unchanged; this is purely additive.
- Lingua principale
- Zig
- Stelle
- 4
- Fork
- 4
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ChainSafe/zapi
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
-
Type: Maintenance
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 76/100
-
clearer errors Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
Tutte le issue di ChainSafe/zapi
Issue simili
-
[Bug]: Matrix progress drafts fail with "Matrix runtime not initialized" during tool activity Apertabug clawsweeper:linked-pr-open clawsweeper:needs-live-repro clawsweeper:no-new-fix-pr impact:message-loss issue-rating: 🐚 platinum hermit P2 regression
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
games-on-whales/wolf#509 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100