swiftwasm / swiftwasm/JavaScriptKit

Support exporting structs as a reference

Aperta
#737 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Swift
Stelle
986
Fork
76
Merge medio
21h 11m
PR unite (30g)
4

Descrizione

The current mechanism for exporting structs works great for simple structs, like @JS struct Point { let x: Double; let y: Double }. When passed out or into Swift, all the members of the struct are copied.

However, it is also common in Swift to have a less trivial struct, for example:

@JS struct Polygon {

    var vertices: [Point]

}

A complex polygon could have tens of thousands of vertices. This works fine in Swift, since the Array is COW. However, this is less than ideal when interoperating with JavaScript, where all the vertices would have to be copied every time the struct crosses the boundary.

Mutating functions currently don’t work on structs, but if they were working, this would also require copying the whole contents of the struct after each call. For example, a mutating addVertex function on Polygon would require copying every vertex across the boundary.

To workaround this issue, you could convert the struct into a class. This improves interoperability with JavaScript, however it can make Swift code less idiomatic.

Proposed Solution

A solution would be allowing structs to be exported using a heap allocated box, which could be expressed using @JS(boxed: true). A “boxed” struct would lead to very similar generated code to a class, where JavaScript just has a Swift-owned pointer.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando l’attuale meccanismo di esportazione delle struct e il codice generato utilizzato quando le struct attraversano il confine Swift–JavaScript. Confronta tale comportamento con la forma proposta @JS(boxed: true) e l’issue #736, quindi verifica che le boxed struct mantengano il comportamento basato su puntatori previsto senza copiare i loro contenuti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, swift
Ambito
api, web-dev
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.