swiftwasm / swiftwasm/JavaScriptKit
Support exporting structs as a reference
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Swift
- Estrellas
- 986
- Forks
- 76
- Merge medio
- 21 h 11 min
- PR fusionados (30 d)
- 4
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza rastreando el mecanismo actual de exportación de structs y el código generado que se utiliza cuando los structs atraviesan la frontera entre Swift y JavaScript. Compara ese comportamiento con la forma propuesta @JS(boxed: true) y el issue #736, y verifica después que los boxed structs conserven el comportamiento basado en punteros previsto sin copiar su contenido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, swift
- Área
- api, web-dev
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100