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