Merge pull request #155 from goccy/feature/add-bytes-test

Fix encoding of primitive pointer types and []byte type
This commit is contained in:
Masaaki Goshima 2021-03-20 00:14:53 +09:00 committed by GitHub
commit 094f8da49e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 2437 additions and 47 deletions

View File

@ -32,6 +32,27 @@ func TestCoverBool(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Bool",
data: bool(true),
},
{
name: "BoolPtr",
data: boolptr(true),
},
{
name: "BoolPtr3",
data: boolptr3(true),
},
{
name: "BoolPtrNil",
data: (*bool)(nil),
},
{
name: "BoolPtr3Nil",
data: (***bool)(nil),
},
// HeadBoolZero // HeadBoolZero
{ {
name: "HeadBoolZero", name: "HeadBoolZero",

1826
cover_bytes_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -32,6 +32,27 @@ func TestCoverFloat32(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Float32",
data: float32(10),
},
{
name: "Float32Ptr",
data: float32ptr(10),
},
{
name: "Float32Ptr3",
data: float32ptr3(10),
},
{
name: "Float32PtrNil",
data: (*float32)(nil),
},
{
name: "Float32Ptr3Nil",
data: (***float32)(nil),
},
// HeadFloat32Zero // HeadFloat32Zero
{ {
name: "HeadFloat32Zero", name: "HeadFloat32Zero",

View File

@ -32,6 +32,27 @@ func TestCoverFloat64(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Float64",
data: float64(10),
},
{
name: "Float64Ptr",
data: float64ptr(10),
},
{
name: "Float64Ptr3",
data: float64ptr3(10),
},
{
name: "Float64PtrNil",
data: (*float64)(nil),
},
{
name: "Float64Ptr3Nil",
data: (***float64)(nil),
},
// HeadFloat64Zero // HeadFloat64Zero
{ {
name: "HeadFloat64Zero", name: "HeadFloat64Zero",

View File

@ -6,19 +6,35 @@ import (
) )
func intptr(v int) *int { return &v } func intptr(v int) *int { return &v }
func intptr3(v int) ***int { vv := &v; vvv := &vv; return &vvv }
func int8ptr(v int8) *int8 { return &v } func int8ptr(v int8) *int8 { return &v }
func int8ptr3(v int8) ***int8 { vv := &v; vvv := &vv; return &vvv }
func int16ptr(v int16) *int16 { return &v } func int16ptr(v int16) *int16 { return &v }
func int16ptr3(v int16) ***int16 { vv := &v; vvv := &vv; return &vvv }
func int32ptr(v int32) *int32 { return &v } func int32ptr(v int32) *int32 { return &v }
func int32ptr3(v int32) ***int32 { vv := &v; vvv := &vv; return &vvv }
func int64ptr(v int64) *int64 { return &v } func int64ptr(v int64) *int64 { return &v }
func int64ptr3(v int64) ***int64 { vv := &v; vvv := &vv; return &vvv }
func uptr(v uint) *uint { return &v } func uptr(v uint) *uint { return &v }
func uintptr3(v uint) ***uint { vv := &v; vvv := &vv; return &vvv }
func uint8ptr(v uint8) *uint8 { return &v } func uint8ptr(v uint8) *uint8 { return &v }
func uint8ptr3(v uint8) ***uint8 { vv := &v; vvv := &vv; return &vvv }
func uint16ptr(v uint16) *uint16 { return &v } func uint16ptr(v uint16) *uint16 { return &v }
func uint16ptr3(v uint16) ***uint16 { vv := &v; vvv := &vv; return &vvv }
func uint32ptr(v uint32) *uint32 { return &v } func uint32ptr(v uint32) *uint32 { return &v }
func uint32ptr3(v uint32) ***uint32 { vv := &v; vvv := &vv; return &vvv }
func uint64ptr(v uint64) *uint64 { return &v } func uint64ptr(v uint64) *uint64 { return &v }
func uint64ptr3(v uint64) ***uint64 { vv := &v; vvv := &vv; return &vvv }
func float32ptr(v float32) *float32 { return &v } func float32ptr(v float32) *float32 { return &v }
func float32ptr3(v float32) ***float32 { vv := &v; vvv := &vv; return &vvv }
func float64ptr(v float64) *float64 { return &v } func float64ptr(v float64) *float64 { return &v }
func float64ptr3(v float64) ***float64 { vv := &v; vvv := &vv; return &vvv }
func stringptr(v string) *string { return &v } func stringptr(v string) *string { return &v }
func stringptr3(v string) ***string { vv := &v; vvv := &vv; return &vvv }
func boolptr(v bool) *bool { return &v } func boolptr(v bool) *bool { return &v }
func boolptr3(v bool) ***bool { vv := &v; vvv := &vv; return &vvv }
func bytesptr(v []byte) *[]byte { return &v }
func bytesptr3(v []byte) ***[]byte { vv := &v; vvv := &vv; return &vvv }
func sliceptr(v []int) *[]int { return &v } func sliceptr(v []int) *[]int { return &v }
func arrayptr(v [2]int) *[2]int { return &v } func arrayptr(v [2]int) *[2]int { return &v }
func mapptr(v map[string]int) *map[string]int { return &v } func mapptr(v map[string]int) *map[string]int { return &v }

View File

@ -32,6 +32,27 @@ func TestCoverInt16(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Int16",
data: int16(10),
},
{
name: "Int16Ptr",
data: int16ptr(10),
},
{
name: "Int16Ptr3",
data: int16ptr3(10),
},
{
name: "Int16PtrNil",
data: (*int16)(nil),
},
{
name: "Int16Ptr3Nil",
data: (***int16)(nil),
},
// HeadInt16Zero // HeadInt16Zero
{ {
name: "HeadInt16Zero", name: "HeadInt16Zero",

View File

@ -32,6 +32,27 @@ func TestCoverInt32(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Int32",
data: int32(10),
},
{
name: "Int32Ptr",
data: int32ptr(10),
},
{
name: "Int32Ptr3",
data: int32ptr3(10),
},
{
name: "Int32PtrNil",
data: (*int32)(nil),
},
{
name: "Int32Ptr3Nil",
data: (***int32)(nil),
},
// HeadInt32Zero // HeadInt32Zero
{ {
name: "HeadInt32Zero", name: "HeadInt32Zero",

View File

@ -32,6 +32,27 @@ func TestCoverInt64(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Int64",
data: int64(10),
},
{
name: "Int64Ptr",
data: int64ptr(10),
},
{
name: "Int64Ptr3",
data: int64ptr3(10),
},
{
name: "Int64PtrNil",
data: (*int64)(nil),
},
{
name: "Int64Ptr3Nil",
data: (***int64)(nil),
},
// HeadInt64Zero // HeadInt64Zero
{ {
name: "HeadInt64Zero", name: "HeadInt64Zero",

View File

@ -32,6 +32,27 @@ func TestCoverInt8(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Int8",
data: int8(10),
},
{
name: "Int8Ptr",
data: int8ptr(10),
},
{
name: "Int8Ptr3",
data: int8ptr3(10),
},
{
name: "Int8PtrNil",
data: (*int8)(nil),
},
{
name: "Int8Ptr3Nil",
data: (***int8)(nil),
},
// HeadInt8Zero // HeadInt8Zero
{ {
name: "HeadInt8Zero", name: "HeadInt8Zero",

View File

@ -32,6 +32,27 @@ func TestCoverInt(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Int",
data: 10,
},
{
name: "IntPtr",
data: intptr(10),
},
{
name: "IntPtr3",
data: intptr3(10),
},
{
name: "IntPtrNil",
data: (*int)(nil),
},
{
name: "IntPtr3Nil",
data: (***int)(nil),
},
// HeadIntZero // HeadIntZero
{ {
name: "HeadIntZero", name: "HeadIntZero",

View File

@ -32,6 +32,27 @@ func TestCoverString(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "String",
data: string("a"),
},
{
name: "StringPtr",
data: stringptr("a"),
},
{
name: "StringPtr3",
data: stringptr3("a"),
},
{
name: "StringPtrNil",
data: (*string)(nil),
},
{
name: "StringPtr3Nil",
data: (***string)(nil),
},
// HeadStringZero // HeadStringZero
{ {
name: "HeadStringZero", name: "HeadStringZero",

View File

@ -32,6 +32,27 @@ func TestCoverUint16(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Uint16",
data: uint16(10),
},
{
name: "Uint16Ptr",
data: uint16ptr(10),
},
{
name: "Uint16Ptr3",
data: uint16ptr3(10),
},
{
name: "Uint16PtrNil",
data: (*uint16)(nil),
},
{
name: "Uint16Ptr3Nil",
data: (***uint16)(nil),
},
// HeadUint16Zero // HeadUint16Zero
{ {
name: "HeadUint16Zero", name: "HeadUint16Zero",

View File

@ -32,6 +32,27 @@ func TestCoverUint32(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Uint32",
data: uint32(10),
},
{
name: "Uint32Ptr",
data: uint32ptr(10),
},
{
name: "Uint32Ptr3",
data: uint32ptr3(10),
},
{
name: "Uint32PtrNil",
data: (*uint32)(nil),
},
{
name: "Uint32Ptr3Nil",
data: (***uint32)(nil),
},
// HeadUint32Zero // HeadUint32Zero
{ {
name: "HeadUint32Zero", name: "HeadUint32Zero",

View File

@ -32,6 +32,27 @@ func TestCoverUint64(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Uint64",
data: uint64(10),
},
{
name: "Uint64Ptr",
data: uint64ptr(10),
},
{
name: "Uint64Ptr3",
data: uint64ptr3(10),
},
{
name: "Uint64PtrNil",
data: (*uint64)(nil),
},
{
name: "Uint64Ptr3Nil",
data: (***uint64)(nil),
},
// HeadUint64Zero // HeadUint64Zero
{ {
name: "HeadUint64Zero", name: "HeadUint64Zero",

View File

@ -32,6 +32,27 @@ func TestCoverUint8(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Uint8",
data: uint8(10),
},
{
name: "Uint8Ptr",
data: uint8ptr(10),
},
{
name: "Uint8Ptr3",
data: uint8ptr3(10),
},
{
name: "Uint8PtrNil",
data: (*uint8)(nil),
},
{
name: "Uint8Ptr3Nil",
data: (***uint8)(nil),
},
// HeadUint8Zero // HeadUint8Zero
{ {
name: "HeadUint8Zero", name: "HeadUint8Zero",

View File

@ -32,6 +32,27 @@ func TestCoverUint(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "Uint",
data: uint(10),
},
{
name: "UintPtr",
data: uptr(10),
},
{
name: "UintPtr3",
data: uintptr3(10),
},
{
name: "UintPtrNil",
data: (*uint)(nil),
},
{
name: "UintPtr3Nil",
data: (***uint)(nil),
},
// HeadUintZero // HeadUintZero
{ {
name: "HeadUintZero", name: "HeadUintZero",

View File

@ -90,12 +90,26 @@ func compileHead(ctx *compileContext) (*Opcode, error) {
case implementsMarshalText(typ): case implementsMarshalText(typ):
return compileMarshalText(ctx) return compileMarshalText(ctx)
} }
if typ.Kind() == reflect.Map { switch typ.Kind() {
case reflect.Slice:
ctx := ctx.withType(typ)
elem := typ.Elem()
if elem.Kind() == reflect.Uint8 {
p := runtime.PtrTo(elem)
if !p.Implements(marshalJSONType) && !p.Implements(marshalTextType) {
if isPtr {
return compileBytesPtr(ctx)
}
return compileBytes(ctx)
}
}
return compileSlice(ctx)
case reflect.Map:
if isPtr { if isPtr {
return compilePtr(ctx.withType(runtime.PtrTo(typ))) return compilePtr(ctx.withType(runtime.PtrTo(typ)))
} }
return compileMap(ctx.withType(typ)) return compileMap(ctx.withType(typ))
} else if typ.Kind() == reflect.Struct { case reflect.Struct:
code, err := compileStruct(ctx.withType(typ), isPtr) code, err := compileStruct(ctx.withType(typ), isPtr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -103,16 +117,108 @@ func compileHead(ctx *compileContext) (*Opcode, error) {
optimizeStructEnd(code) optimizeStructEnd(code)
linkRecursiveCode(code) linkRecursiveCode(code)
return code, nil return code, nil
} else if isPtr && typ.Implements(marshalTextType) { case reflect.Int:
typ = orgType ctx := ctx.withType(typ)
if isPtr {
return compileIntPtr(ctx)
}
return compileInt(ctx)
case reflect.Int8:
ctx := ctx.withType(typ)
if isPtr {
return compileInt8Ptr(ctx)
}
return compileInt8(ctx)
case reflect.Int16:
ctx := ctx.withType(typ)
if isPtr {
return compileInt16Ptr(ctx)
}
return compileInt16(ctx)
case reflect.Int32:
ctx := ctx.withType(typ)
if isPtr {
return compileInt32Ptr(ctx)
}
return compileInt32(ctx)
case reflect.Int64:
ctx := ctx.withType(typ)
if isPtr {
return compileInt64Ptr(ctx)
}
return compileInt64(ctx)
case reflect.Uint, reflect.Uintptr:
ctx := ctx.withType(typ)
if isPtr {
return compileUintPtr(ctx)
}
return compileUint(ctx)
case reflect.Uint8:
ctx := ctx.withType(typ)
if isPtr {
return compileUint8Ptr(ctx)
}
return compileUint8(ctx)
case reflect.Uint16:
ctx := ctx.withType(typ)
if isPtr {
return compileUint16Ptr(ctx)
}
return compileUint16(ctx)
case reflect.Uint32:
ctx := ctx.withType(typ)
if isPtr {
return compileUint32Ptr(ctx)
}
return compileUint32(ctx)
case reflect.Uint64:
ctx := ctx.withType(typ)
if isPtr {
return compileUint64Ptr(ctx)
}
return compileUint64(ctx)
case reflect.Float32:
ctx := ctx.withType(typ)
if isPtr {
return compileFloat32Ptr(ctx)
}
return compileFloat32(ctx)
case reflect.Float64:
ctx := ctx.withType(typ)
if isPtr {
return compileFloat64Ptr(ctx)
}
return compileFloat64(ctx)
case reflect.String:
ctx := ctx.withType(typ)
if isPtr {
return compileStringPtr(ctx)
}
return compileString(ctx)
case reflect.Bool:
ctx := ctx.withType(typ)
if isPtr {
return compileBoolPtr(ctx)
}
return compileBool(ctx)
case reflect.Interface:
ctx := ctx.withType(typ)
if isPtr {
return compileInterfacePtr(ctx)
}
return compileInterface(ctx)
default:
if isPtr && typ.Implements(marshalTextType) {
typ = orgType
}
code, err := compile(ctx.withType(typ), isPtr)
if err != nil {
return nil, err
}
optimizeStructEnd(code)
linkRecursiveCode(code)
return code, nil
} }
code, err := compile(ctx.withType(typ), isPtr)
if err != nil {
return nil, err
}
optimizeStructEnd(code)
linkRecursiveCode(code)
return code, nil
} }
func linkRecursiveCode(c *Opcode) { func linkRecursiveCode(c *Opcode) {
@ -418,6 +524,15 @@ func compileInt(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileIntPtr(ctx *compileContext) (*Opcode, error) {
code, err := compileInt(ctx)
if err != nil {
return nil, err
}
code.Op = OpIntPtr
return code, nil
}
func compileInt8(ctx *compileContext) (*Opcode, error) { func compileInt8(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpInt) code := newOpCode(ctx, OpInt)
code.setMaskAndRshiftNum(8) code.setMaskAndRshiftNum(8)
@ -425,6 +540,15 @@ func compileInt8(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileInt8Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileInt8(ctx)
if err != nil {
return nil, err
}
code.Op = OpIntPtr
return code, nil
}
func compileInt16(ctx *compileContext) (*Opcode, error) { func compileInt16(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpInt) code := newOpCode(ctx, OpInt)
code.setMaskAndRshiftNum(16) code.setMaskAndRshiftNum(16)
@ -432,6 +556,15 @@ func compileInt16(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileInt16Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileInt16(ctx)
if err != nil {
return nil, err
}
code.Op = OpIntPtr
return code, nil
}
func compileInt32(ctx *compileContext) (*Opcode, error) { func compileInt32(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpInt) code := newOpCode(ctx, OpInt)
code.setMaskAndRshiftNum(32) code.setMaskAndRshiftNum(32)
@ -439,6 +572,15 @@ func compileInt32(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileInt32Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileInt32(ctx)
if err != nil {
return nil, err
}
code.Op = OpIntPtr
return code, nil
}
func compileInt64(ctx *compileContext) (*Opcode, error) { func compileInt64(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpInt) code := newOpCode(ctx, OpInt)
code.setMaskAndRshiftNum(64) code.setMaskAndRshiftNum(64)
@ -446,6 +588,15 @@ func compileInt64(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileInt64Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileInt64(ctx)
if err != nil {
return nil, err
}
code.Op = OpIntPtr
return code, nil
}
func compileUint(ctx *compileContext) (*Opcode, error) { func compileUint(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpUint) code := newOpCode(ctx, OpUint)
code.setMaskAndRshiftNum(intSize) code.setMaskAndRshiftNum(intSize)
@ -453,6 +604,15 @@ func compileUint(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileUintPtr(ctx *compileContext) (*Opcode, error) {
code, err := compileUint(ctx)
if err != nil {
return nil, err
}
code.Op = OpUintPtr
return code, nil
}
func compileUint8(ctx *compileContext) (*Opcode, error) { func compileUint8(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpUint) code := newOpCode(ctx, OpUint)
code.setMaskAndRshiftNum(8) code.setMaskAndRshiftNum(8)
@ -460,6 +620,15 @@ func compileUint8(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileUint8Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileUint8(ctx)
if err != nil {
return nil, err
}
code.Op = OpUintPtr
return code, nil
}
func compileUint16(ctx *compileContext) (*Opcode, error) { func compileUint16(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpUint) code := newOpCode(ctx, OpUint)
code.setMaskAndRshiftNum(16) code.setMaskAndRshiftNum(16)
@ -467,6 +636,15 @@ func compileUint16(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileUint16Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileUint16(ctx)
if err != nil {
return nil, err
}
code.Op = OpUintPtr
return code, nil
}
func compileUint32(ctx *compileContext) (*Opcode, error) { func compileUint32(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpUint) code := newOpCode(ctx, OpUint)
code.setMaskAndRshiftNum(32) code.setMaskAndRshiftNum(32)
@ -474,6 +652,15 @@ func compileUint32(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileUint32Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileUint32(ctx)
if err != nil {
return nil, err
}
code.Op = OpUintPtr
return code, nil
}
func compileUint64(ctx *compileContext) (*Opcode, error) { func compileUint64(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpUint) code := newOpCode(ctx, OpUint)
code.setMaskAndRshiftNum(64) code.setMaskAndRshiftNum(64)
@ -481,6 +668,15 @@ func compileUint64(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileUint64Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileUint64(ctx)
if err != nil {
return nil, err
}
code.Op = OpUintPtr
return code, nil
}
func compileIntString(ctx *compileContext) (*Opcode, error) { func compileIntString(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpIntString) code := newOpCode(ctx, OpIntString)
code.setMaskAndRshiftNum(intSize) code.setMaskAndRshiftNum(intSize)
@ -557,12 +753,30 @@ func compileFloat32(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileFloat32Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileFloat32(ctx)
if err != nil {
return nil, err
}
code.Op = OpFloat32Ptr
return code, nil
}
func compileFloat64(ctx *compileContext) (*Opcode, error) { func compileFloat64(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpFloat64) code := newOpCode(ctx, OpFloat64)
ctx.incIndex() ctx.incIndex()
return code, nil return code, nil
} }
func compileFloat64Ptr(ctx *compileContext) (*Opcode, error) {
code, err := compileFloat64(ctx)
if err != nil {
return nil, err
}
code.Op = OpFloat64Ptr
return code, nil
}
func compileString(ctx *compileContext) (*Opcode, error) { func compileString(ctx *compileContext) (*Opcode, error) {
var op OpType var op OpType
if ctx.typ == runtime.Type2RType(jsonNumberType) { if ctx.typ == runtime.Type2RType(jsonNumberType) {
@ -575,24 +789,64 @@ func compileString(ctx *compileContext) (*Opcode, error) {
return code, nil return code, nil
} }
func compileStringPtr(ctx *compileContext) (*Opcode, error) {
code, err := compileString(ctx)
if err != nil {
return nil, err
}
if code.Op == OpNumber {
code.Op = OpNumberPtr
} else {
code.Op = OpStringPtr
}
return code, nil
}
func compileBool(ctx *compileContext) (*Opcode, error) { func compileBool(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpBool) code := newOpCode(ctx, OpBool)
ctx.incIndex() ctx.incIndex()
return code, nil return code, nil
} }
func compileBoolPtr(ctx *compileContext) (*Opcode, error) {
code, err := compileBool(ctx)
if err != nil {
return nil, err
}
code.Op = OpBoolPtr
return code, nil
}
func compileBytes(ctx *compileContext) (*Opcode, error) { func compileBytes(ctx *compileContext) (*Opcode, error) {
code := newOpCode(ctx, OpBytes) code := newOpCode(ctx, OpBytes)
ctx.incIndex() ctx.incIndex()
return code, nil return code, nil
} }
func compileBytesPtr(ctx *compileContext) (*Opcode, error) {
code, err := compileBytes(ctx)
if err != nil {
return nil, err
}
code.Op = OpBytesPtr
return code, nil
}
func compileInterface(ctx *compileContext) (*Opcode, error) { func compileInterface(ctx *compileContext) (*Opcode, error) {
code := newInterfaceCode(ctx) code := newInterfaceCode(ctx)
ctx.incIndex() ctx.incIndex()
return code, nil return code, nil
} }
func compileInterfacePtr(ctx *compileContext) (*Opcode, error) {
code, err := compileInterface(ctx)
if err != nil {
return nil, err
}
code.Op = OpInterfacePtr
return code, nil
}
func compileSlice(ctx *compileContext) (*Opcode, error) { func compileSlice(ctx *compileContext) (*Opcode, error) {
elem := ctx.typ.Elem() elem := ctx.typ.Elem()
size := elem.Size() size := elem.Size()

View File

@ -96,6 +96,10 @@ func (c *Opcode) ToHeaderType() OpType {
return OpStructHeadBool return OpStructHeadBool
case OpBoolPtr: case OpBoolPtr:
return OpStructHeadBoolPtr return OpStructHeadBoolPtr
case OpBytes:
return OpStructHeadBytes
case OpBytesPtr:
return OpStructHeadBytesPtr
case OpMap: case OpMap:
return OpStructHeadMap return OpStructHeadMap
case OpMapPtr: case OpMapPtr:
@ -153,6 +157,10 @@ func (c *Opcode) ToFieldType() OpType {
return OpStructFieldBool return OpStructFieldBool
case OpBoolPtr: case OpBoolPtr:
return OpStructFieldBoolPtr return OpStructFieldBoolPtr
case OpBytes:
return OpStructFieldBytes
case OpBytesPtr:
return OpStructFieldBytesPtr
case OpMap: case OpMap:
return OpStructFieldMap return OpStructFieldMap
case OpMapPtr: case OpMapPtr:

View File

@ -18,13 +18,16 @@ func store(base uintptr, idx uintptr, p uintptr) {
**(**uintptr)(unsafe.Pointer(&addr)) = p **(**uintptr)(unsafe.Pointer(&addr)) = p
} }
func loadNPtr(base uintptr, idx uintptr, _ int) uintptr { func loadNPtr(base uintptr, idx uintptr, ptrNum int) uintptr {
addr := base + idx addr := base + idx
p := **(**uintptr)(unsafe.Pointer(&addr)) p := **(**uintptr)(unsafe.Pointer(&addr))
if p == 0 { for i := 0; i < ptrNum; i++ {
return 0 if p == 0 {
return 0
}
p = ptrToPtr(p)
} }
return ptrToPtr(p) return p
} }
func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) } func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) }

View File

@ -2024,9 +2024,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
b = append(b, '{') b = append(b, '{')
} }
b = append(b, code.Key...) b = append(b, code.Key...)
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p+code.Offset)) b = appendByteSlice(b, ptrToBytes(p+code.Offset))
b = append(b, '"')
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadBytesPtr: case encoder.OpStructPtrHeadBytesPtr:
@ -2131,9 +2129,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -4386,9 +4382,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendStructEnd(b) b = appendStructEnd(b)
code = code.Next code = code.Next

View File

@ -18,13 +18,16 @@ func store(base uintptr, idx uintptr, p uintptr) {
**(**uintptr)(unsafe.Pointer(&addr)) = p **(**uintptr)(unsafe.Pointer(&addr)) = p
} }
func loadNPtr(base uintptr, idx uintptr, _ int) uintptr { func loadNPtr(base uintptr, idx uintptr, ptrNum int) uintptr {
addr := base + idx addr := base + idx
p := **(**uintptr)(unsafe.Pointer(&addr)) p := **(**uintptr)(unsafe.Pointer(&addr))
if p == 0 { for i := 0; i < ptrNum; i++ {
return 0 if p == 0 {
return 0
}
p = ptrToPtr(p)
} }
return ptrToPtr(p) return p
} }
func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) } func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) }

View File

@ -2024,9 +2024,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
b = append(b, '{') b = append(b, '{')
} }
b = append(b, code.EscapedKey...) b = append(b, code.EscapedKey...)
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p+code.Offset)) b = appendByteSlice(b, ptrToBytes(p+code.Offset))
b = append(b, '"')
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadBytesPtr: case encoder.OpStructPtrHeadBytesPtr:
@ -2131,9 +2129,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -4386,9 +4382,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendStructEnd(b) b = appendStructEnd(b)
code = code.Next code = code.Next

View File

@ -19,13 +19,16 @@ func store(base uintptr, idx uintptr, p uintptr) {
**(**uintptr)(unsafe.Pointer(&addr)) = p **(**uintptr)(unsafe.Pointer(&addr)) = p
} }
func loadNPtr(base uintptr, idx uintptr, _ int) uintptr { func loadNPtr(base uintptr, idx uintptr, ptrNum int) uintptr {
addr := base + idx addr := base + idx
p := **(**uintptr)(unsafe.Pointer(&addr)) p := **(**uintptr)(unsafe.Pointer(&addr))
if p == 0 { for i := 0; i < ptrNum; i++ {
return 0 if p == 0 {
return 0
}
p = ptrToPtr(p)
} }
return ptrToPtr(p) return p
} }
func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) } func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) }

View File

@ -2230,9 +2230,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -4848,9 +4846,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendStructEnd(ctx, b, code.Indent-1) b = appendStructEnd(ctx, b, code.Indent-1)
code = code.Next code = code.Next

View File

@ -19,13 +19,16 @@ func store(base uintptr, idx uintptr, p uintptr) {
**(**uintptr)(unsafe.Pointer(&addr)) = p **(**uintptr)(unsafe.Pointer(&addr)) = p
} }
func loadNPtr(base uintptr, idx uintptr, _ int) uintptr { func loadNPtr(base uintptr, idx uintptr, ptrNum int) uintptr {
addr := base + idx addr := base + idx
p := **(**uintptr)(unsafe.Pointer(&addr)) p := **(**uintptr)(unsafe.Pointer(&addr))
if p == 0 { for i := 0; i < ptrNum; i++ {
return 0 if p == 0 {
return 0
}
p = ptrToPtr(p)
} }
return ptrToPtr(p) return p
} }
func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) } func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) }

View File

@ -2236,9 +2236,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -4854,9 +4852,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = append(b, '"')
b = appendByteSlice(b, ptrToBytes(p)) b = appendByteSlice(b, ptrToBytes(p))
b = append(b, '"')
} }
b = appendStructEnd(ctx, b, code.Indent-1) b = appendStructEnd(ctx, b, code.Indent-1)
code = code.Next code = code.Next