Skip to content

Commit

Permalink
Fixed extended setindex (AplaProject#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
gentee committed Jan 19, 2018
1 parent 450bb84 commit 4f255ca
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/api/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestRead(t *testing.T) {
row = $data[i]
if i == 1 || i == 3 {
row["my"] = "No name"
//$data[i] = row
$data[i] = row
}
i = i+ 1
}
Expand Down
4 changes: 2 additions & 2 deletions packages/script/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ main:
if !call {
cmd = &ByteCode{cmdExtend, lexem.Value.(string)}
if i < len(*lexems)-1 && (*lexems)[i+1].Type == isLBrack {
buffer = append(buffer, &ByteCode{cmdIndex, 0})
buffer = append(buffer, &ByteCode{cmdIndex, &IndexInfo{Extend: lexem.Value.(string)}})
}
}
case lexIdent:
Expand Down Expand Up @@ -1122,7 +1122,7 @@ main:
logger.WithFields(log.Fields{"lex_value": lexem.Value.(string), "type": consts.ParseError}).Error("unknown variable")
return fmt.Errorf(`unknown variable %s`, lexem.Value.(string))
}
buffer = append(buffer, &ByteCode{cmdIndex, &IndexInfo{objInfo.Value.(int), tobj}})
buffer = append(buffer, &ByteCode{cmdIndex, &IndexInfo{objInfo.Value.(int), tobj, ``}})
}
}
if !call {
Expand Down
8 changes: 6 additions & 2 deletions packages/script/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ func TestVMCompile(t *testing.T) {
}
return m["id"] + "=" + GetData().WhereId(100).One("name")
}`, `result`, `123=Test value 100`},
{`func mapbug() string {
$data[10] = "extend ok"
return $data[10]
}`, `mapbug`, `extend ok`},
}
vm := NewVM()
vm.Extern = true
Expand All @@ -397,8 +401,8 @@ func TestVMCompile(t *testing.T) {
}
} else {
if out, err := vm.Call(item.Func, nil, &map[string]interface{}{
`rt_state`: uint32(ikey) + 22,
`test1`: 101, `test2`: `test 2`,
`rt_state`: uint32(ikey) + 22, `data`: make([]interface{}, 0),
`test1`: 101, `test2`: `test 2`,
"glob": map[string]interface{}{`test`: `String value`, `number`: 1001},
`test3`: func(param int64) string {
return fmt.Sprintf("test=%d=test", param)
Expand Down
12 changes: 8 additions & 4 deletions packages/script/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,14 @@ func (rt *RunTime) RunCode(block *Block) (status int, err error) {
if int(ind) >= len(slice) {
slice = append(slice, make([]interface{}, int(ind)-len(slice)+1)...)
indexInfo := cmd.Value.(*IndexInfo)
for i := len(rt.blocks) - 1; i >= 0; i-- {
if indexInfo.Owner == rt.blocks[i].Block {
rt.vars[rt.blocks[i].Offset+indexInfo.VarOffset] = slice
break
if indexInfo.Owner == nil { // Extend variable $varname
(*rt.extend)[indexInfo.Extend] = slice
} else {
for i := len(rt.blocks) - 1; i >= 0; i-- {
if indexInfo.Owner == rt.blocks[i].Block {
rt.vars[rt.blocks[i].Offset+indexInfo.VarOffset] = slice
break
}
}
}
rt.stack[size-3] = slice
Expand Down
1 change: 1 addition & 0 deletions packages/script/vminit.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type VarInfo struct {
type IndexInfo struct {
VarOffset int
Owner *Block
Extend string
}

// ObjInfo is the common object type
Expand Down

0 comments on commit 4f255ca

Please sign in to comment.