Browse Source

Fixed context variables mapping.

Dmitry Panov 9 years ago
parent
commit
3edc8dfbf4
4 changed files with 33 additions and 3 deletions
  1. 1 0
      compiler_stmt.go
  2. 31 1
      compiler_test.go
  3. 0 1
      object_gomap_reflect_test.go
  4. 1 1
      vm.go

+ 1 - 0
compiler_stmt.go

@@ -506,6 +506,7 @@ func (c *compiler) compileIfStatement(v *ast.IfStatement, needResult bool) {
 				c.p = p
 			}
 		} else {
+			// TODO: Properly implement dummy compilation (no garbage in block, scope, etc..)
 			p := c.p
 			c.p = &Program{}
 			c.compileStatement(v.Consequent, false)

+ 31 - 1
compiler_test.go

@@ -158,7 +158,22 @@ var rv = A(1, 2, 3);
 	testScript(SCRIPT, intToValue(3), t)
 }
 
-func TestCallLessArgs1(t *testing.T) {
+func TestCallMoreArgsDynamic(t *testing.T) {
+	const SCRIPT = `
+function A(a, b) {
+	var c = 4;
+	if (false) {
+		eval("");
+	}
+	return a - b + c;
+}
+
+var rv = A(1, 2, 3);
+`
+	testScript(SCRIPT, intToValue(3), t)
+}
+
+func TestCallLessArgsDynamic(t *testing.T) {
 	const SCRIPT = `
 function A(a, b, c) {
 	// Make it stashful
@@ -173,6 +188,21 @@ var rv = A(1, 2);
 	testScript(SCRIPT, asciiString("1 2 undefined"), t)
 }
 
+func TestCallLessArgsDynamicLocalVar(t *testing.T) {
+	const SCRIPT = `
+	function f(param) {
+		var a = 42;
+		if (false) {
+			eval("");
+		}
+		return a;
+	}
+	f();
+`
+
+	testScript1(SCRIPT, intToValue(42), t)
+}
+
 /*
 func TestFib(t *testing.T) {
 	testScript(TEST_FIB, valueInt(9227465), t)

+ 0 - 1
object_gomap_reflect_test.go

@@ -111,7 +111,6 @@ func TestGoMapReflectProto(t *testing.T) {
 		"t": "42",
 	}
 	vm.Set("m", m)
-	_ = "breakpoint"
 	v, err := vm.RunString(SCRIPT)
 	if err != nil {
 		t.Fatal(err)

+ 1 - 1
vm.go

@@ -249,7 +249,7 @@ func (s *stash) createBinding(name string) {
 	if s.names == nil {
 		s.names = make(map[string]uint32)
 	}
-	s.names[name] = uint32(len(s.values))
+	s.names[name] = uint32(len(s.names))
 	s.values = append(s.values, _undefined)
 }