JavaScriptCore bytecompiler does not compute scope depth correctly
authorddkilzer@apple.com <ddkilzer@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 21 Aug 2011 05:21:50 +0000 (05:21 +0000)
committerddkilzer@apple.com <ddkilzer@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 21 Aug 2011 05:21:50 +0000 (05:21 +0000)
in the case of constant declarations
https://bugs.webkit.org/show_bug.cgi?id=66572

Source/JavaScriptCore:

Patch by Filip Pizlo <fpizlo@apple.com> on 2011-08-20
Reviewed by Oliver Hunt.

Changed the handling of const to add the dynamic scope depth.

* bytecompiler/NodesCodegen.cpp:
(JSC::ConstDeclNode::emitCodeSingle):

LayoutTests:

Patch by Filip Pizlo <fpizlo@apple.com> on 2011-08-20
Reviewed by Oliver Hunt.

Added tests that reproduce the failure.

* fast/js/const-expected.txt:
* fast/js/resources/const.js:
(tryCatch1):
(tryCatch2):
(with1):
(with2):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@93487 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/js/const-expected.txt
LayoutTests/fast/js/resources/const.js
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp

index c1ca5f1..0378419 100755 (executable)
@@ -1,3 +1,20 @@
+2011-08-20  Filip Pizlo  <fpizlo@apple.com>
+
+        JavaScriptCore bytecompiler does not compute scope depth correctly
+        in the case of constant declarations
+        https://bugs.webkit.org/show_bug.cgi?id=66572
+
+        Reviewed by Oliver Hunt.
+        
+        Added tests that reproduce the failure.
+
+        * fast/js/const-expected.txt:
+        * fast/js/resources/const.js:
+        (tryCatch1):
+        (tryCatch2):
+        (with1):
+        (with2):
+
 2011-08-20  Tom Zakrajsek  <tomz@codeaurora.org>
 
         instanceof HTMLSourceElement Fails
index fbb5401..03aeea6 100644 (file)
@@ -51,6 +51,10 @@ PASS f() is f
 PASS const a; is undefined
 PASS bodyId is document.getElementById('bodyId')
 PASS ranConstInitialiser is true
+PASS tryCatch1Result is 5
+PASS tryCatch2Result is 5
+PASS with1Result is 5
+PASS with2Result is 5
 PASS successfullyParsed is true
 
 TEST COMPLETE
index cc5e127..7e83394 100644 (file)
@@ -117,4 +117,50 @@ var ranConstInitialiser = false;
 const bodyId = (ranConstInitialiser = true, "Const initialiser overwrote existing property");
 shouldBe("bodyId", "document.getElementById('bodyId')");
 shouldBeTrue("ranConstInitialiser");
+
+// Make sure that dynamic scopes (catch, with) don't break const declarations
+function tryCatch1() {
+    var bar;
+    eval("try {\
+        stuff();\
+    } catch (e) {\
+        print(\"here.\");\
+        const bar = 5;\
+    }");
+    return bar;
+}
+
+function tryCatch2() {
+    var bar;
+    try {
+        stuff();
+    } catch (e) {
+        print("here.");
+        const bar = 5;
+    }
+    return bar;
+}
+
+tryCatch1Result = tryCatch1();
+shouldBe("tryCatch1Result", "5");
+tryCatch2Result = tryCatch2();
+shouldBe("tryCatch2Result", "5");
+
+function with1() {
+    var bar;
+    eval("with({foo:42}) const bar = 5;");
+    return bar;
+}
+
+function with2() {
+    var bar;
+    with({foo:42}) const bar = 5;
+    return bar;
+}
+
+with1Result = with1();
+shouldBe("with1Result", "5");
+with2Result = with2();
+shouldBe("with2Result", "5");
+
 var successfullyParsed = true;
index bce8512..1832edb 100644 (file)
@@ -1,3 +1,16 @@
+2011-08-20  Filip Pizlo  <fpizlo@apple.com>
+
+        JavaScriptCore bytecompiler does not compute scope depth correctly
+        in the case of constant declarations
+        https://bugs.webkit.org/show_bug.cgi?id=66572
+
+        Reviewed by Oliver Hunt.
+        
+        Changed the handling of const to add the dynamic scope depth.
+
+        * bytecompiler/NodesCodegen.cpp:
+        (JSC::ConstDeclNode::emitCodeSingle):
+
 2011-08-19  Daniel Bates  <dbates@webkit.org>
 
         Only #include <signal.h> and require SA_RESTART when building with JSC_MULTIPLE_THREADS
index 4bdd4da..9b7761a 100644 (file)
@@ -1308,7 +1308,7 @@ RegisterID* ConstDeclNode::emitCodeSingle(BytecodeGenerator& generator)
         if (entry.isNull())
             continue;
 
-        return generator.emitPutScopedVar(depth, entry.getIndex(), value.get(), currentVariableObject->isGlobalObject() ? currentVariableObject : 0);
+        return generator.emitPutScopedVar(generator.scopeDepth() + depth, entry.getIndex(), value.get(), currentVariableObject->isGlobalObject() ? currentVariableObject : 0);
     }
 
     if (generator.codeType() != EvalCode)