| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Adapted from test/mjsunit/compiler/variables.js | 28 // Adapted from test/mjsunit/compiler/variables.js |
| 29 | 29 |
| 30 // TODO(jochen): Remove this after the setting is turned on globally. |
| 31 #define V8_IMMINENT_DEPRECATION_WARNINGS |
| 32 |
| 30 #include <limits.h> | 33 #include <limits.h> |
| 31 | 34 |
| 32 #include "src/v8.h" | 35 #include "src/v8.h" |
| 33 | 36 |
| 34 #include "src/api.h" | 37 #include "src/api.h" |
| 35 #include "src/base/platform/platform.h" | 38 #include "src/base/platform/platform.h" |
| 36 #include "src/compilation-cache.h" | 39 #include "src/compilation-cache.h" |
| 37 #include "src/execution.h" | 40 #include "src/execution.h" |
| 38 #include "src/isolate.h" | 41 #include "src/isolate.h" |
| 39 #include "src/parser.h" | 42 #include "src/parser.h" |
| 40 #include "src/unicode-inl.h" | 43 #include "src/unicode-inl.h" |
| 41 #include "src/utils.h" | 44 #include "src/utils.h" |
| 42 #include "test/cctest/cctest.h" | 45 #include "test/cctest/cctest.h" |
| 43 | 46 |
| 44 using ::v8::Context; | 47 using ::v8::Context; |
| 45 using ::v8::Extension; | 48 using ::v8::Extension; |
| 46 using ::v8::Function; | 49 using ::v8::Function; |
| 47 using ::v8::FunctionTemplate; | 50 using ::v8::FunctionTemplate; |
| 48 using ::v8::Handle; | |
| 49 using ::v8::HandleScope; | 51 using ::v8::HandleScope; |
| 50 using ::v8::Local; | 52 using ::v8::Local; |
| 51 using ::v8::Message; | 53 using ::v8::Message; |
| 52 using ::v8::MessageCallback; | 54 using ::v8::MessageCallback; |
| 53 using ::v8::Object; | 55 using ::v8::Object; |
| 54 using ::v8::ObjectTemplate; | 56 using ::v8::ObjectTemplate; |
| 55 using ::v8::Persistent; | 57 using ::v8::Persistent; |
| 56 using ::v8::Script; | 58 using ::v8::Script; |
| 57 using ::v8::StackTrace; | 59 using ::v8::StackTrace; |
| 58 using ::v8::String; | 60 using ::v8::String; |
| 59 using ::v8::TryCatch; | 61 using ::v8::TryCatch; |
| 60 using ::v8::Undefined; | 62 using ::v8::Undefined; |
| 61 using ::v8::V8; | 63 using ::v8::V8; |
| 62 using ::v8::Value; | 64 using ::v8::Value; |
| 63 | 65 |
| 64 static void ExpectInt32(int32_t expected, Local<Value> result) { | 66 static void ExpectInt32(Local<Context> context, int32_t expected, |
| 67 Local<Value> result) { |
| 65 CHECK(result->IsInt32()); | 68 CHECK(result->IsInt32()); |
| 66 CHECK_EQ(expected, result->Int32Value()); | 69 CHECK_EQ(expected, result->Int32Value(context).FromJust()); |
| 67 } | 70 } |
| 68 | 71 |
| 69 | 72 |
| 70 // Global variables. | 73 // Global variables. |
| 71 TEST(global_variables) { | 74 TEST(global_variables) { |
| 72 LocalContext env; | 75 LocalContext env; |
| 73 v8::HandleScope scope(env->GetIsolate()); | 76 v8::HandleScope scope(env->GetIsolate()); |
| 74 Local<Value> result = CompileRun( | 77 Local<Value> result = CompileRun( |
| 75 "var x = 0;" | 78 "var x = 0;" |
| 76 "function f0() { return x; }" | 79 "function f0() { return x; }" |
| 77 "f0();"); | 80 "f0();"); |
| 78 ExpectInt32(0, result); | 81 ExpectInt32(env.local(), 0, result); |
| 79 } | 82 } |
| 80 | 83 |
| 81 | 84 |
| 82 // Parameters. | 85 // Parameters. |
| 83 TEST(parameters) { | 86 TEST(parameters) { |
| 84 LocalContext env; | 87 LocalContext env; |
| 85 v8::HandleScope scope(env->GetIsolate()); | 88 v8::HandleScope scope(env->GetIsolate()); |
| 86 Local<Value> result = CompileRun( | 89 Local<Value> result = CompileRun( |
| 87 "function f1(x) { return x; }" | 90 "function f1(x) { return x; }" |
| 88 "f1(1);"); | 91 "f1(1);"); |
| 89 ExpectInt32(1, result); | 92 ExpectInt32(env.local(), 1, result); |
| 90 } | 93 } |
| 91 | 94 |
| 92 | 95 |
| 93 // Stack-allocated locals. | 96 // Stack-allocated locals. |
| 94 TEST(stack_allocated_locals) { | 97 TEST(stack_allocated_locals) { |
| 95 LocalContext env; | 98 LocalContext env; |
| 96 v8::HandleScope scope(env->GetIsolate()); | 99 v8::HandleScope scope(env->GetIsolate()); |
| 97 Local<Value> result = CompileRun( | 100 Local<Value> result = CompileRun( |
| 98 "function f2() { var x = 2; return x; }" | 101 "function f2() { var x = 2; return x; }" |
| 99 "f2();"); | 102 "f2();"); |
| 100 ExpectInt32(2, result); | 103 ExpectInt32(env.local(), 2, result); |
| 101 } | 104 } |
| 102 | 105 |
| 103 | 106 |
| 104 // Context-allocated locals. Local function forces x into f3's context. | 107 // Context-allocated locals. Local function forces x into f3's context. |
| 105 TEST(context_allocated_locals) { | 108 TEST(context_allocated_locals) { |
| 106 LocalContext env; | 109 LocalContext env; |
| 107 v8::HandleScope scope(env->GetIsolate()); | 110 v8::HandleScope scope(env->GetIsolate()); |
| 108 Local<Value> result = CompileRun( | 111 Local<Value> result = CompileRun( |
| 109 "function f3(x) {" | 112 "function f3(x) {" |
| 110 " function g() { return x; }" | 113 " function g() { return x; }" |
| 111 " return x;" | 114 " return x;" |
| 112 "}" | 115 "}" |
| 113 "f3(3);"); | 116 "f3(3);"); |
| 114 ExpectInt32(3, result); | 117 ExpectInt32(env.local(), 3, result); |
| 115 } | 118 } |
| 116 | 119 |
| 117 | 120 |
| 118 // Local function reads x from an outer context. | 121 // Local function reads x from an outer context. |
| 119 TEST(read_from_outer_context) { | 122 TEST(read_from_outer_context) { |
| 120 LocalContext env; | 123 LocalContext env; |
| 121 v8::HandleScope scope(env->GetIsolate()); | 124 v8::HandleScope scope(env->GetIsolate()); |
| 122 Local<Value> result = CompileRun( | 125 Local<Value> result = CompileRun( |
| 123 "function f4(x) {" | 126 "function f4(x) {" |
| 124 " function g() { return x; }" | 127 " function g() { return x; }" |
| 125 " return g();" | 128 " return g();" |
| 126 "}" | 129 "}" |
| 127 "f4(4);"); | 130 "f4(4);"); |
| 128 ExpectInt32(4, result); | 131 ExpectInt32(env.local(), 4, result); |
| 129 } | 132 } |
| 130 | 133 |
| 131 | 134 |
| 132 // Local function reads x from an outer context. | 135 // Local function reads x from an outer context. |
| 133 TEST(lookup_slots) { | 136 TEST(lookup_slots) { |
| 134 LocalContext env; | 137 LocalContext env; |
| 135 v8::HandleScope scope(env->GetIsolate()); | 138 v8::HandleScope scope(env->GetIsolate()); |
| 136 Local<Value> result = CompileRun( | 139 Local<Value> result = CompileRun( |
| 137 "function f5(x) {" | 140 "function f5(x) {" |
| 138 " with ({}) return x;" | 141 " with ({}) return x;" |
| 139 "}" | 142 "}" |
| 140 "f5(5);"); | 143 "f5(5);"); |
| 141 ExpectInt32(5, result); | 144 ExpectInt32(env.local(), 5, result); |
| 142 } | 145 } |
| OLD | NEW |