swiftwasm / swiftwasm/JavaScriptKit

Support exporting structs as a reference

Offen
#737 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Swift
Sterne
986
Forks
76
Ø Merge
21 Std. 11 Min.
Gemergte PRs (30 T.)
4

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den aktuellen Struct-Export-Mechanismus und den generierten Code nachzuverfolgen, der verwendet wird, wenn Strukturen die Swift–JavaScript-Grenze überschreiten. Vergleiche dieses Verhalten mit der vorgeschlagenen Form @JS(boxed: true) und Issue #736 und überprüfe anschließend, dass boxed structs das beabsichtigte pointerbasierte Verhalten beibehalten, ohne ihre Inhalte zu kopieren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, swift
Bereich
api, web-dev
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.