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.
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:
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
addVertexfunction onPolygonwould 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.