1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Can now attach a label to a block and have it break properly

This commit is contained in:
Robert Krimen 2013-04-27 15:52:46 +02:00
parent 8ab608b974
commit cc243b17d7
6 changed files with 38 additions and 3 deletions

View File

@ -23,7 +23,7 @@ fixme:
ack -l FIXME *.go
otto:
$(MAKE) -C otto
$(MAKE) -C otto
run:
go run -a ./otto/main.go ./otto.js

View File

@ -49,7 +49,7 @@ func (self *_runtime) evaluate(node _node) Value {
return self.evaluateBody(node.Body)
case *_blockNode:
return self.evaluateBody(node.Body)
return self.evaluateBlock(node)
case *_valueNode:
return self.evaluateValue(node)

View File

@ -104,6 +104,16 @@ func (self *_runtime) evaluateWith(node *_withNode) Value {
return self.evaluate(node.Body)
}
func (self *_runtime) evaluateBlock(node *_blockNode) Value {
body := node.Body
_labelSet := node._labelSet
return self.breakEvaluate(_labelSet, func() Value {
return self.evaluateBody(body)
})
}
func (self *_runtime) evaluateDoWhile(node *_doWhileNode) Value {
test := node.Test

View File

@ -7,12 +7,14 @@ import (
type _blockNode struct {
_nodeType
_node_
Body []_node
Body []_node
_labelSet _labelSet
}
func newBlockNode() *_blockNode {
return &_blockNode{
_nodeType: nodeBlock,
_labelSet: _labelSet{},
}
}

View File

@ -58,6 +58,8 @@ func (self *_parser) ParseStatement() _node {
{
_labelSet = nil
switch node := statement.(type) {
case *_blockNode:
_labelSet = node._labelSet
case *_doWhileNode:
_labelSet = node._labelSet
case *_whileNode:

View File

@ -1130,6 +1130,27 @@ func TestNewPrototype(t *testing.T) {
`, "Nothing happens.")
}
func TestBlock(t *testing.T) {
Terst(t)
test := runTest()
test(`
var abc=0;
var ghi;
def: {
do {
abc++;
if (!(abc < 10)) {
break def;
ghi = "ghi";
}
} while (true);
}
[ abc,ghi ];
`, "10,")
}
func TestWith(t *testing.T) {
Terst(t)