fix: 兼容 TinyGo wasm 编译行星星历初始化
- 将 planetViews 从包级初始化改为 sync.Once 懒加载,避免 TinyGo interp 在编译期处理大型 float64 表切片索引时触发 unsupported fcmp - 将行星视图构建失败改为内部返回 error,并由兼容层统一 panic - 补充无效行星数据切片边界测试
This commit is contained in:
+23
-6
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user