Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gen/kaitai struct #17

Merged
merged 23 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions ast2go/js/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//go:build js && wasm

package js

import (
"encoding/json"
"flag"
"syscall/js"

ast2go "github.com/on-keyday/brgen/ast2go/ast"
)

type Generator interface {
GenerateAndFormat(*ast2go.AstFile) ([]byte, error)
}

type GeneratorMaker interface {
New() Generator
ResetFlag()
}

func SetGoFunc(newGenerator GeneratorMaker) {
jsGo := js.Global().Get("Go")
if jsGo.IsUndefined() {
panic("Go is undefined")
}
f := js.FuncOf(func(this js.Value, args []js.Value) any {
result := map[string]any{
"stderr": "",
"stdout": "",
"code": 0,
}
if len(args) < 2 {
result["stderr"] = "args length is less than 2"
result["code"] = 1
return result
}
if args[0].Type() != js.TypeString {
result["stderr"] = "args[0] is not string"
result["code"] = 1
return result
}
jsArray := js.Global().Get("Array")
if !args[1].InstanceOf(jsArray) {
result["stderr"] = "args[1] is not array"
result["code"] = 1
return result
}
cmdargs := []string{}
for i := 0; i < args[1].Length(); i++ {
if args[1].Index(i).Type() != js.TypeString {
result["stderr"] = "args[1] is not string array"
result["code"] = 1
return result
}
cmdargs = append(cmdargs, args[1].Index(i).String())
}
sourceCode := args[0].String()
if sourceCode == "" {
result["stderr"] = "args[0] is empty"
result["code"] = 1
return result
}
newGenerator.ResetFlag()
err := flag.CommandLine.Parse(cmdargs[1:])
if err != nil {
result["stderr"] = err.Error()
result["code"] = 1
return result
}
file := ast2go.AstFile{}
err = json.Unmarshal([]byte(sourceCode), &file)
if err != nil {
result["stderr"] = err.Error()
result["code"] = 1
return result
}
g := newGenerator.New()
data, err := g.GenerateAndFormat(&file)
if err != nil {
result["stdout"] = string(data)
result["stderr"] = err.Error()
result["code"] = 1
return result
}
result["stdout"] = string(data)
return result
})
proto := jsGo.Get("prototype")
proto.Set("json2goGenerator", f)
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ require (
)

require golang.org/x/sys v0.16.0

require (
github.com/kaitai-io/kaitai_struct_go_runtime v0.10.0 // indirect
golang.org/x/text v0.14.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/kaitai-io/kaitai_struct_go_runtime v0.10.0 h1:bxazq0XLMSVMm/DIVFLl9BqIWehrqcLsyVWSacEjIKE=
github.com/kaitai-io/kaitai_struct_go_runtime v0.10.0/go.mod h1:fBebEoDoc0xNbZsIcRQWqDp4jViaTKv6uxAUjmCFGgM=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
85 changes: 12 additions & 73 deletions src/tool/json2go/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,21 @@
package main

import (
"encoding/json"
"flag"
"syscall/js"

ast2go "github.com/on-keyday/brgen/ast2go/ast"
js "github.com/on-keyday/brgen/ast2go/js"
)

type makeGenerator struct{}

func (m *makeGenerator) New() js.Generator {
return &Generator{}
}

func (m *makeGenerator) ResetFlag() {
ResetFlag()
}

func setFunc() {
jsGo := js.Global().Get("Go")
if jsGo.IsUndefined() {
panic("Go is undefined")
}
f := js.FuncOf(func(this js.Value, args []js.Value) any {
result := map[string]any{
"stderr": "",
"stdout": "",
"code": 0,
}
if len(args) < 2 {
result["stderr"] = "args length is less than 2"
result["code"] = 1
return result
}
if args[0].Type() != js.TypeString {
result["stderr"] = "args[0] is not string"
result["code"] = 1
return result
}
jsArray := js.Global().Get("Array")
if !args[1].InstanceOf(jsArray) {
result["stderr"] = "args[1] is not array"
result["code"] = 1
return result
}
cmdargs := []string{}
for i := 0; i < args[1].Length(); i++ {
if args[1].Index(i).Type() != js.TypeString {
result["stderr"] = "args[1] is not string array"
result["code"] = 1
return result
}
cmdargs = append(cmdargs, args[1].Index(i).String())
}
sourceCode := args[0].String()
if sourceCode == "" {
result["stderr"] = "args[0] is empty"
result["code"] = 1
return result
}
ResetFlag()
err := flag.CommandLine.Parse(cmdargs[1:])
if err != nil {
result["stderr"] = err.Error()
result["code"] = 1
return result
}
file := ast2go.AstFile{}
err = json.Unmarshal([]byte(sourceCode), &file)
if err != nil {
result["stderr"] = err.Error()
result["code"] = 1
return result
}
g := NewGenerator()
data, err := g.GenerateAndFormat(&file)
if err != nil {
result["stdout"] = string(data)
result["stderr"] = err.Error()
result["code"] = 1
return result
}
result["stdout"] = string(data)
return result
})
proto := jsGo.Get("prototype")
proto.Set("json2goGenerator", f)
js.SetGoFunc(&makeGenerator{})
}

func main() {
Expand Down
27 changes: 27 additions & 0 deletions src/tool/json2kaitai/js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build js && wasm

package main

import (
js "github.com/on-keyday/brgen/ast2go/js"
)

type makeGenerator struct{}

func (m *makeGenerator) New() js.Generator {
return &Generator{}
}

func (m *makeGenerator) ResetFlag() {

}

func setFunc() {
js.SetGoFunc(&makeGenerator{})
}

func main() {
c := make(chan struct{})
setFunc()
<-c
}
Loading
Loading