swiftwasm / swiftwasm/JavaScriptKit

Support exporting structs as a reference

未關閉
#737 9 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 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.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 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

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。