1
0
mirror of https://github.com/robertkrimen/otto synced 2025-09-28 18:45:22 +08:00

Change getIdentifierReference to accept a node argument

To track/report where errors are coming from
This commit is contained in:
Robert Krimen 2012-10-09 17:26:08 -07:00
parent ade6a169be
commit c6e45a7118
5 changed files with 12 additions and 11 deletions

View File

@ -112,14 +112,14 @@ func (self *_objectEnvironment) ImplicitThisValue() *_object {
return nil
}
func getIdentifierReference(environment _environment, name string, strict bool) _reference {
func getIdentifierReference(environment _environment, name string, strict bool, node _node) _reference {
if environment == nil {
return newObjectReference(nil, name, strict, nil)
return newObjectReference(nil, name, strict, node)
}
if environment.HasBinding(name) {
return environment.newReference(name, strict)
}
return getIdentifierReference(environment.Outer(), name, strict)
return getIdentifierReference(environment.Outer(), name, strict, node)
}
// ---
@ -144,7 +144,7 @@ func (self *_objectEnvironment) newReference(name string, strict bool) _referenc
}
func (self *_objectEnvironment) GetReference(name string) _reference {
return getIdentifierReference(self, name, false)
return getIdentifierReference(self, name, false, nil)
}
func (self *_objectEnvironment) GetValue(name string, throw bool) Value {

View File

@ -104,7 +104,8 @@ func catchPanic(function func()) (err error) {
if caught.Line == -1 {
err = errors.New(caught.String())
} else {
err = errors.New(fmt.Sprintf("%s (line %d)", caught.String(), caught.Line))
// We're 0-based (for now), hence the + 1
err = errors.New(fmt.Sprintf("%s (line %d)", caught.String(), caught.Line + 1))
}
return
case _result:

View File

@ -524,7 +524,7 @@ func (self *_runtime) evaluateIdentifier(node *_identifierNode) Value {
// TODO Should be true or false (strictness) depending on context
// TODO Associate the node with reference... how?
// TODO Can/Will getIdentifierReference ever return nil?
reference := getIdentifierReference(self.LexicalEnvironment(), name, false)
reference := getIdentifierReference(self.LexicalEnvironment(), name, false, node)
if reference == nil {
panic("referenceError: " + name)
}

View File

@ -34,7 +34,7 @@ func (self *_runtime) evaluateVariableDeclarationList(node *_variableDeclaration
func (self *_runtime) evaluateVariableDeclaration(node *_variableDeclarationNode) Value {
if node.Operator != "" {
// FIXME If reference is nil
left := getIdentifierReference(self.LexicalEnvironment(), node.Identifier, false)
left := getIdentifierReference(self.LexicalEnvironment(), node.Identifier, false, node)
right := self.evaluate(node.Initializer)
rightValue := self.GetValue(right)
@ -192,7 +192,7 @@ func (self *_runtime) evaluateForIn(node *_forInNode) Value {
if into.reference() == nil {
identifier := toString(into)
// TODO Should be true or false (strictness) depending on context
into = toValue(getIdentifierReference(self.LexicalEnvironment(), identifier, false))
into = toValue(getIdentifierReference(self.LexicalEnvironment(), identifier, false, node))
}
self.PutValue(into.reference(), toValue(name))
value, _ := self.continueEvaluate(body, _labelSet)

View File

@ -27,7 +27,7 @@ func TestOttoError(t *testing.T) {
return abcdef.length
})()
`)
Is(err, "ReferenceError: abcdef is not defined (line 2)")
Is(err, "ReferenceError: abcdef is not defined (line 3)")
_, err = Otto.Run(`
function start() {
@ -37,13 +37,13 @@ func TestOttoError(t *testing.T) {
xyzzy()
`)
Is(err, "ReferenceError: xyzzy is not defined (line 6)")
Is(err, "ReferenceError: xyzzy is not defined (line 7)")
_, err = Otto.Run(`
// Just a comment
xyzzy
`)
Is(err, "ReferenceError: xyzzy is not defined (line 3)")
Is(err, "ReferenceError: xyzzy is not defined (line 4)")
}