swiftwasm / swiftwasm/JavaScriptKit
Support exporting structs as a reference
还没有人认领这个 Issue。
- 主要语言
- Swift
- 星标
- 986
- 派生
- 76
- 平均合并
- 21 小时 11 分钟
- 30 天内合并 PR
- 4
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先跟踪当前的 struct 导出机制,以及 struct 跨越 Swift–JavaScript 边界时使用的生成代码。将该行为与提议的 @JS(boxed: true) 形式和 issue #736 进行比较,然后验证 boxed structs 在不复制其内容的情况下保留预期的基于指针的行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, swift
- 领域
- api, web-dev
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100