| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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 #include <limits.h> |
| 29 |
| 30 #include "v8.h" |
| 31 |
| 32 #include "api.h" |
| 33 #include "isolate.h" |
| 34 #include "compilation-cache.h" |
| 35 #include "execution.h" |
| 36 #include "snapshot.h" |
| 37 #include "platform.h" |
| 38 #include "utils.h" |
| 39 #include "cctest.h" |
| 40 #include "parser.h" |
| 41 #include "unicode-inl.h" |
| 42 |
| 43 using ::v8::AccessorInfo; |
| 44 using ::v8::Arguments; |
| 45 using ::v8::Context; |
| 46 using ::v8::Extension; |
| 47 using ::v8::Function; |
| 48 using ::v8::FunctionTemplate; |
| 49 using ::v8::Handle; |
| 50 using ::v8::HandleScope; |
| 51 using ::v8::Local; |
| 52 using ::v8::Message; |
| 53 using ::v8::MessageCallback; |
| 54 using ::v8::Object; |
| 55 using ::v8::ObjectTemplate; |
| 56 using ::v8::Persistent; |
| 57 using ::v8::Script; |
| 58 using ::v8::StackTrace; |
| 59 using ::v8::String; |
| 60 using ::v8::TryCatch; |
| 61 using ::v8::Undefined; |
| 62 using ::v8::V8; |
| 63 using ::v8::Value; |
| 64 |
| 65 static void ExpectBoolean(bool expected, Local<Value> result) { |
| 66 CHECK(result->IsBoolean()); |
| 67 CHECK_EQ(expected, result->BooleanValue()); |
| 68 } |
| 69 |
| 70 static void ExpectInt32(int32_t expected, Local<Value> result) { |
| 71 CHECK(result->IsInt32()); |
| 72 CHECK_EQ(expected, result->Int32Value()); |
| 73 } |
| 74 |
| 75 static void ExpectNumber(double expected, Local<Value> result) { |
| 76 CHECK(result->IsNumber()); |
| 77 CHECK_EQ(expected, result->NumberValue()); |
| 78 } |
| 79 |
| 80 static void ExpectUndefined(Local<Value> result) { |
| 81 CHECK(result->IsUndefined()); |
| 82 } |
| 83 |
| 84 // Tests are sorted by order of implementation. |
| 85 |
| 86 TEST(simple_value) { |
| 87 v8::HandleScope scope; |
| 88 LocalContext env; |
| 89 Local<Value> result = CompileRun("0x271828;"); |
| 90 ExpectInt32(0x271828, result); |
| 91 } |
| 92 |
| 93 |
| 94 TEST(global_variable) { |
| 95 v8::HandleScope scope; |
| 96 LocalContext env; |
| 97 Local<Value> result = CompileRun("var my_global_var = 0x123; my_global_var;"); |
| 98 ExpectInt32(0x123, result); |
| 99 } |
| 100 |
| 101 |
| 102 TEST(simple_function_call) { |
| 103 v8::HandleScope scope; |
| 104 LocalContext env; |
| 105 Local<Value> result = CompileRun( |
| 106 "function foo() { return 0x314; }" |
| 107 "foo();"); |
| 108 ExpectInt32(0x314, result); |
| 109 } |
| 110 |
| 111 |
| 112 TEST(binary_op) { |
| 113 v8::HandleScope scope; |
| 114 LocalContext env; |
| 115 Local<Value> result = CompileRun( |
| 116 "function foo() {" |
| 117 " var a = 0x1200;" |
| 118 " var b = 0x0035;" |
| 119 " return 2 * (a + b - 1);" |
| 120 "}" |
| 121 "foo();"); |
| 122 ExpectInt32(0x2468, result); |
| 123 } |
| 124 |
| 125 static void if_comparison_testcontext_helper( |
| 126 char const * op, |
| 127 char const * lhs, |
| 128 char const * rhs, |
| 129 int expect) { |
| 130 char buffer[256]; |
| 131 snprintf(buffer, sizeof(buffer), |
| 132 "var lhs = %s;" |
| 133 "var rhs = %s;" |
| 134 "if ( lhs %s rhs ) { 1; }" |
| 135 "else { 0; }", |
| 136 lhs, rhs, op); |
| 137 Local<Value> result = CompileRun(buffer); |
| 138 ExpectInt32(expect, result); |
| 139 } |
| 140 |
| 141 static void if_comparison_effectcontext_helper( |
| 142 char const * op, |
| 143 char const * lhs, |
| 144 char const * rhs, |
| 145 int expect) { |
| 146 char buffer[256]; |
| 147 snprintf(buffer, sizeof(buffer), |
| 148 "var lhs = %s;" |
| 149 "var rhs = %s;" |
| 150 "var test = lhs %s rhs;" |
| 151 "if ( test ) { 1; }" |
| 152 "else { 0; }", |
| 153 lhs, rhs, op); |
| 154 Local<Value> result = CompileRun(buffer); |
| 155 ExpectInt32(expect, result); |
| 156 } |
| 157 |
| 158 static void if_comparison_helper( |
| 159 char const * op, |
| 160 int expect_when_lt, |
| 161 int expect_when_eq, |
| 162 int expect_when_gt) { |
| 163 // TODO(all): Non-SMI tests. |
| 164 |
| 165 if_comparison_testcontext_helper(op, "1", "3", expect_when_lt); |
| 166 if_comparison_testcontext_helper(op, "5", "5", expect_when_eq); |
| 167 if_comparison_testcontext_helper(op, "9", "7", expect_when_gt); |
| 168 |
| 169 if_comparison_effectcontext_helper(op, "1", "3", expect_when_lt); |
| 170 if_comparison_effectcontext_helper(op, "5", "5", expect_when_eq); |
| 171 if_comparison_effectcontext_helper(op, "9", "7", expect_when_gt); |
| 172 } |
| 173 |
| 174 TEST(if_comparison) { |
| 175 v8::HandleScope scope; |
| 176 LocalContext env; |
| 177 |
| 178 if_comparison_helper("<", 1, 0, 0); |
| 179 if_comparison_helper("<=", 1, 1, 0); |
| 180 if_comparison_helper("==", 0, 1, 0); |
| 181 if_comparison_helper("===", 0, 1, 0); |
| 182 if_comparison_helper(">=", 0, 1, 1); |
| 183 if_comparison_helper(">", 0, 0, 1); |
| 184 if_comparison_helper("!=", 1, 0, 1); |
| 185 if_comparison_helper("!==", 1, 0, 1); |
| 186 } |
| 187 |
| 188 TEST(unary_plus) { |
| 189 v8::HandleScope scope; |
| 190 LocalContext env; |
| 191 Local<Value> result; |
| 192 // SMI |
| 193 result = CompileRun("var a = 1234; +a"); |
| 194 ExpectInt32(1234, result); |
| 195 // Number |
| 196 result = CompileRun("var a = 1234.5; +a"); |
| 197 ExpectNumber(1234.5, result); |
| 198 // String (SMI) |
| 199 result = CompileRun("var a = '1234'; +a"); |
| 200 ExpectInt32(1234, result); |
| 201 // String (Number) |
| 202 result = CompileRun("var a = '1234.5'; +a"); |
| 203 ExpectNumber(1234.5, result); |
| 204 // Check side effects. |
| 205 result = CompileRun("var a = 1234; +(a = 4321); a"); |
| 206 ExpectInt32(4321, result); |
| 207 } |
| 208 |
| 209 TEST(unary_minus) { |
| 210 v8::HandleScope scope; |
| 211 LocalContext env; |
| 212 Local<Value> result; |
| 213 result = CompileRun("var a = 1234; -a"); |
| 214 ExpectInt32(-1234, result); |
| 215 result = CompileRun("var a = 1234.5; -a"); |
| 216 ExpectNumber(-1234.5, result); |
| 217 result = CompileRun("var a = 1234; -(a = 4321); a"); |
| 218 ExpectInt32(4321, result); |
| 219 result = CompileRun("var a = '1234'; -a"); |
| 220 ExpectInt32(-1234, result); |
| 221 result = CompileRun("var a = '1234.5'; -a"); |
| 222 ExpectNumber(-1234.5, result); |
| 223 } |
| 224 |
| 225 TEST(unary_void) { |
| 226 v8::HandleScope scope; |
| 227 LocalContext env; |
| 228 Local<Value> result; |
| 229 result = CompileRun("var a = 1234; void (a);"); |
| 230 ExpectUndefined(result); |
| 231 result = CompileRun("var a = 0; void (a = 42); a"); |
| 232 ExpectInt32(42, result); |
| 233 result = CompileRun("var a = 0; void (a = 42);"); |
| 234 ExpectUndefined(result); |
| 235 } |
| 236 |
| 237 TEST(unary_not) { |
| 238 v8::HandleScope scope; |
| 239 LocalContext env; |
| 240 Local<Value> result; |
| 241 result = CompileRun("var a = 1234; !a"); |
| 242 ExpectBoolean(false, result); |
| 243 result = CompileRun("var a = 0; !a"); |
| 244 ExpectBoolean(true, result); |
| 245 result = CompileRun("var a = 0; !(a = 1234); a"); |
| 246 ExpectInt32(1234, result); |
| 247 result = CompileRun("var a = '1234'; !a"); |
| 248 ExpectBoolean(false, result); |
| 249 result = CompileRun("var a = ''; !a"); |
| 250 ExpectBoolean(true, result); |
| 251 result = CompileRun("var a = 1234; !!a"); |
| 252 ExpectBoolean(true, result); |
| 253 result = CompileRun("var a = 0; !!a"); |
| 254 ExpectBoolean(false, result); |
| 255 result = CompileRun("var a = 0; if ( !a ) { 1; } else { 0; }"); |
| 256 ExpectInt32(1, result); |
| 257 result = CompileRun("var a = 1; if ( !a ) { 1; } else { 0; }"); |
| 258 ExpectInt32(0, result); |
| 259 } |
| 260 |
| OLD | NEW |