Skip to content

Commit

Permalink
perf: prevent duplicate call to js
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Jun 17, 2024
1 parent 4e683f1 commit a6ace68
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 36 deletions.
10 changes: 3 additions & 7 deletions internal/entity/file_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

package entity

import (
"syscall/js"
)

func (f *File) MarshalJavaScript() js.Value {
return js.ValueOf(map[string]any{
func (f *File) MarshalJavaScript() any {
return map[string]any{
"file_path": f.FilePath,
"size": f.FullSize(),
"pcln_size": f.PclnSize(),
})
}
}
12 changes: 5 additions & 7 deletions internal/entity/package_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@
package entity

import (
"syscall/js"

"github.com/samber/lo"
)

func (m PackageMap) MarshalJavaScript() js.Value {
func (m PackageMap) MarshalJavaScript() any {
ret := map[string]any{}

for k, v := range m {
ret[k] = v.MarshalJavaScript()
}

return js.ValueOf(ret)
return ret
}

func (p *Package) MarshalJavaScript() js.Value {
func (p *Package) MarshalJavaScript() any {
var symbols, files []any
symbols = lo.Map(p.Symbols, func(s *Symbol, _ int) any { return s.MarshalJavaScript() })
files = lo.Map(p.Files, func(f *File, _ int) any { return f.MarshalJavaScript() })

return js.ValueOf(map[string]any{
return map[string]any{
"name": p.Name,
"type": p.Type,
"size": p.Size,
"symbols": symbols,
"subPackages": p.SubPackages.MarshalJavaScript(),
"files": files,
})
}
}
10 changes: 3 additions & 7 deletions internal/entity/section_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

package entity

import (
"syscall/js"
)

func (s Section) MarshalJavaScript() js.Value {
return js.ValueOf(map[string]any{
func (s Section) MarshalJavaScript() any {
return map[string]any{
"name": s.Name,
"size": s.Size,
"file_size": s.FileSize,
Expand All @@ -18,5 +14,5 @@ func (s Section) MarshalJavaScript() js.Value {
"addr_end": s.AddrEnd,
"only_in_memory": s.OnlyInMemory,
"debug": s.Debug,
})
}
}
10 changes: 3 additions & 7 deletions internal/entity/symbol_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

package entity

import (
"syscall/js"
)

func (s *Symbol) MarshalJavaScript() js.Value {
return js.ValueOf(map[string]any{
func (s *Symbol) MarshalJavaScript() any {
return map[string]any{
"name": s.Name,
"addr": s.Addr,
"size": s.Size,
"type": s.Type,
})
}
}
4 changes: 3 additions & 1 deletion internal/printer/wasm/obj_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ import (
)

func JavaScript(r *result.Result) js.Value {
return r.MarshalJavaScript()
ret := r.MarshalJavaScript()

return js.ValueOf(ret)
}
8 changes: 3 additions & 5 deletions internal/result/result_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
package result

import (
"syscall/js"

"github.com/samber/lo"

"github.com/Zxilly/go-size-analyzer/internal/entity"
)

func (r *Result) MarshalJavaScript() js.Value {
func (r *Result) MarshalJavaScript() any {
var sections []any
sections = lo.Map(r.Sections, func(s *entity.Section, _ int) any {
return s.MarshalJavaScript()
})

return js.ValueOf(map[string]any{
return map[string]any{
"name": r.Name,
"size": r.Size,
"packages": r.Packages.MarshalJavaScript(),
"sections": sections,
})
}
}
3 changes: 1 addition & 2 deletions internal/result/result_js_wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func TestResultMarshalJavaScript(t *testing.T) {

// use JSON.stringify to compare the result
JSON := js.Global().Get("JSON")
stringify := JSON.Get("stringify")
jsonValue := stringify.Invoke(jsValue).String()
jsonValue := JSON.Call("stringify", jsValue).String()

assert.JSONEq(t, testdataJSON, jsonValue)
}

0 comments on commit a6ace68

Please sign in to comment.