fix: 兼容 TinyGo wasm 编译行星星历初始化

- 将 planetViews 从包级初始化改为 sync.Once 懒加载,避免 TinyGo interp 在编译期处理大型 float64 表切片索引时触发 unsupported fcmp
- 将行星视图构建失败改为内部返回 error,并由兼容层统一 panic
- 补充无效行星数据切片边界测试
This commit is contained in:
2026-05-17 21:19:23 +08:00
parent bec7b8a0d8
commit d40c4dfcd9
3 changed files with 38 additions and 8 deletions
+23 -6
View File
@@ -1,6 +1,9 @@
package planet
import "fmt"
import (
"fmt"
"sync"
)
type coordSeriesView struct {
orders [6][]float64
@@ -11,13 +14,27 @@ type planetView struct {
coords [3]coordSeriesView
}
var planetViews = buildPlanetViews(planetRawData)
var (
planetViewsOnce sync.Once
planetViewsCache []planetView
planetViewsErr error
)
func buildPlanetViews(rawData [][]float64) []planetView {
func planetViews() []planetView {
planetViewsOnce.Do(func() {
planetViewsCache, planetViewsErr = buildPlanetViews(planetRawData)
})
if planetViewsErr != nil {
panic(planetViewsErr)
}
return planetViewsCache
}
func buildPlanetViews(rawData [][]float64) ([]planetView, error) {
views := make([]planetView, len(rawData))
for bodyIndex, raw := range rawData {
if len(raw) < 20 {
panic(fmt.Sprintf("planet raw data %d too short: %d", bodyIndex, len(raw)))
return nil, fmt.Errorf("planet raw data %d too short: %d", bodyIndex, len(raw))
}
view := planetView{scale: raw[0]}
for zn := 0; zn < 3; zn++ {
@@ -26,12 +43,12 @@ func buildPlanetViews(rawData [][]float64) []planetView {
start := int(raw[pn+order])
end := int(raw[pn+order+1])
if start < 0 || end < start || end > len(raw) {
panic(fmt.Sprintf("planet raw data %d coord %d order %d invalid cut: %d..%d (len=%d)", bodyIndex, zn, order, start, end, len(raw)))
return nil, fmt.Errorf("planet raw data %d coord %d order %d invalid cut: %d..%d (len=%d)", bodyIndex, zn, order, start, end, len(raw))
}
view.coords[zn].orders[order] = raw[start:end]
}
}
views[bodyIndex] = view
}
return views
return views, nil
}