Skip to content

Commit

Permalink
luajit: fallbackUintMapSymStr
Browse files Browse the repository at this point in the history
  • Loading branch information
dlatchx committed Jun 21, 2023
1 parent c3a123a commit 3561c08
Showing 1 changed file with 31 additions and 52 deletions.
83 changes: 31 additions & 52 deletions format/luajit/luajit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ import (
"github.com/wader/fq/pkg/scalar"
)

// TODO: merge into scalar pkg
type fallbackUintMapSymStr struct {
fallback string
scalar.UintMapSymStr
}

func (m fallbackUintMapSymStr) MapUint(s scalar.Uint) (scalar.Uint, error) {
s.Sym = m.fallback
return m.UintMapSymStr.MapUint(s)
}

//go:embed luajit.md
var LuaJITFS embed.FS

Expand Down Expand Up @@ -115,26 +126,6 @@ func LuaJITDecodeBCIns(d *decode.D) {
}
}

type ktabType struct{}

func (t *ktabType) MapUint(u scalar.Uint) (scalar.Uint, error) {
switch u.Actual {
case 0:
u.Sym = "nil"
case 1:
u.Sym = "false"
case 2:
u.Sym = "true"
case 3:
u.Sym = "int"
case 4:
u.Sym = "num"
default:
u.Sym = "str"
}
return u, nil
}

func LuaJITDecodeNum(d *decode.D) {
d.FieldAnyFn("num", func(d *decode.D) any {
lo := d.ULEB128()
Expand All @@ -144,7 +135,16 @@ func LuaJITDecodeNum(d *decode.D) {
}

func LuaJITDecodeKTabK(d *decode.D) {
ktabtype := d.FieldULEB128("ktabtype", &ktabType{})
ktabtype := d.FieldULEB128("ktabtype", fallbackUintMapSymStr{
fallback: "str",
UintMapSymStr: scalar.UintMapSymStr{
0: "nil",
1: "false",
2: "true",
3: "int",
4: "num",
},
})

if ktabtype >= 5 {
sz := ktabtype - 5
Expand All @@ -160,36 +160,6 @@ func LuaJITDecodeKTabK(d *decode.D) {
}
}

func LuaJITDecodeCplx(d *decode.D) any {
lo := d.ULEB128()
if lo&1 == 0 {
return lo >> 1
} else {
hi := d.ULEB128()
return u64tof64((hi << 32) + (lo >> 1))
}
}

type kgcType struct{}

func (t *kgcType) MapUint(u scalar.Uint) (scalar.Uint, error) {
switch u.Actual {
case 0:
u.Sym = "child"
case 1:
u.Sym = "tab"
case 2:
u.Sym = "i64"
case 3:
u.Sym = "u64"
case 4:
u.Sym = "complex"
default:
u.Sym = "str"
}
return u, nil
}

func LuaJITDecodeTab(d *decode.D) {
narray := d.FieldULEB128("narray")
nhash := d.FieldULEB128("nhash")
Expand Down Expand Up @@ -243,7 +213,16 @@ func LuaJITDecodeComplex(d *decode.D) {
}

func LuaJITDecodeKGC(d *decode.D) {
kgctype := d.FieldULEB128("type", &kgcType{})
kgctype := d.FieldULEB128("type", fallbackUintMapSymStr{
fallback: "str",
UintMapSymStr: scalar.UintMapSymStr{
0: "child",
1: "tab",
2: "i64",
3: "u64",
4: "complex",
},
})

if kgctype >= 5 {
sz := kgctype - 5
Expand Down

0 comments on commit 3561c08

Please sign in to comment.