| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include <wchar.h> | 29 #include <wchar.h> |
| 30 | 30 |
| 31 #include "v8.h" | 31 #include "v8.h" |
| 32 | 32 |
| 33 #include "compiler.h" | 33 #include "compiler.h" |
| 34 #include "disasm.h" | 34 #include "disasm.h" |
| 35 #include "cctest.h" | 35 #include "cctest.h" |
| 36 | 36 |
| 37 using namespace v8::internal; | 37 using namespace v8::internal; |
| 38 | 38 |
| 39 static MaybeObject* GetGlobalProperty(const char* name) { | 39 static Handle<Object> GetGlobalProperty(const char* name) { |
| 40 Isolate* isolate = CcTest::i_isolate(); | 40 Isolate* isolate = CcTest::i_isolate(); |
| 41 Handle<String> internalized_name = | 41 Handle<String> internalized_name = |
| 42 isolate->factory()->InternalizeUtf8String(name); | 42 isolate->factory()->InternalizeUtf8String(name); |
| 43 return isolate->context()->global_object()->GetProperty(*internalized_name); | 43 return GlobalObject::GetPropertyNoExceptionThrown( |
| 44 isolate->global_object(), internalized_name); |
| 44 } | 45 } |
| 45 | 46 |
| 46 | 47 |
| 47 static void SetGlobalProperty(const char* name, Object* value) { | 48 static void SetGlobalProperty(const char* name, Object* value) { |
| 48 Isolate* isolate = CcTest::i_isolate(); | 49 Isolate* isolate = CcTest::i_isolate(); |
| 49 Handle<Object> object(value, isolate); | 50 Handle<Object> object(value, isolate); |
| 50 Handle<String> internalized_name = | 51 Handle<String> internalized_name = |
| 51 isolate->factory()->InternalizeUtf8String(name); | 52 isolate->factory()->InternalizeUtf8String(name); |
| 52 Handle<JSObject> global(isolate->context()->global_object()); | 53 Handle<JSObject> global(isolate->context()->global_object()); |
| 53 Runtime::SetObjectProperty(isolate, global, internalized_name, object, NONE, | 54 Runtime::SetObjectProperty(isolate, global, internalized_name, object, NONE, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 78 EmbeddedVector<char, 512> buffer; | 79 EmbeddedVector<char, 512> buffer; |
| 79 OS::SNPrintF(buffer, source, x); | 80 OS::SNPrintF(buffer, source, x); |
| 80 | 81 |
| 81 Handle<JSFunction> fun = Compile(buffer.start()); | 82 Handle<JSFunction> fun = Compile(buffer.start()); |
| 82 if (fun.is_null()) return -1; | 83 if (fun.is_null()) return -1; |
| 83 | 84 |
| 84 bool has_pending_exception; | 85 bool has_pending_exception; |
| 85 Handle<JSObject> global(isolate->context()->global_object()); | 86 Handle<JSObject> global(isolate->context()->global_object()); |
| 86 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 87 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 87 CHECK(!has_pending_exception); | 88 CHECK(!has_pending_exception); |
| 88 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 89 return GetGlobalProperty("result")->Number(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 | 92 |
| 92 TEST(Inc) { | 93 TEST(Inc) { |
| 93 CcTest::InitializeVM(); | 94 CcTest::InitializeVM(); |
| 94 v8::HandleScope scope(CcTest::isolate()); | 95 v8::HandleScope scope(CcTest::isolate()); |
| 95 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); | 96 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); |
| 96 } | 97 } |
| 97 | 98 |
| 98 | 99 |
| 99 static double Add(Isolate* isolate, int x, int y) { | 100 static double Add(Isolate* isolate, int x, int y) { |
| 100 Handle<JSFunction> fun = Compile("result = x + y;"); | 101 Handle<JSFunction> fun = Compile("result = x + y;"); |
| 101 if (fun.is_null()) return -1; | 102 if (fun.is_null()) return -1; |
| 102 | 103 |
| 103 SetGlobalProperty("x", Smi::FromInt(x)); | 104 SetGlobalProperty("x", Smi::FromInt(x)); |
| 104 SetGlobalProperty("y", Smi::FromInt(y)); | 105 SetGlobalProperty("y", Smi::FromInt(y)); |
| 105 bool has_pending_exception; | 106 bool has_pending_exception; |
| 106 Handle<JSObject> global(isolate->context()->global_object()); | 107 Handle<JSObject> global(isolate->context()->global_object()); |
| 107 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 108 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 108 CHECK(!has_pending_exception); | 109 CHECK(!has_pending_exception); |
| 109 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 110 return GetGlobalProperty("result")->Number(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 | 113 |
| 113 TEST(Add) { | 114 TEST(Add) { |
| 114 CcTest::InitializeVM(); | 115 CcTest::InitializeVM(); |
| 115 v8::HandleScope scope(CcTest::isolate()); | 116 v8::HandleScope scope(CcTest::isolate()); |
| 116 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); | 117 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); |
| 117 } | 118 } |
| 118 | 119 |
| 119 | 120 |
| 120 static double Abs(Isolate* isolate, int x) { | 121 static double Abs(Isolate* isolate, int x) { |
| 121 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); | 122 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 122 if (fun.is_null()) return -1; | 123 if (fun.is_null()) return -1; |
| 123 | 124 |
| 124 SetGlobalProperty("x", Smi::FromInt(x)); | 125 SetGlobalProperty("x", Smi::FromInt(x)); |
| 125 bool has_pending_exception; | 126 bool has_pending_exception; |
| 126 Handle<JSObject> global(isolate->context()->global_object()); | 127 Handle<JSObject> global(isolate->context()->global_object()); |
| 127 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 128 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 128 CHECK(!has_pending_exception); | 129 CHECK(!has_pending_exception); |
| 129 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 130 return GetGlobalProperty("result")->Number(); |
| 130 } | 131 } |
| 131 | 132 |
| 132 | 133 |
| 133 TEST(Abs) { | 134 TEST(Abs) { |
| 134 CcTest::InitializeVM(); | 135 CcTest::InitializeVM(); |
| 135 v8::HandleScope scope(CcTest::isolate()); | 136 v8::HandleScope scope(CcTest::isolate()); |
| 136 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); | 137 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); |
| 137 } | 138 } |
| 138 | 139 |
| 139 | 140 |
| 140 static double Sum(Isolate* isolate, int n) { | 141 static double Sum(Isolate* isolate, int n) { |
| 141 Handle<JSFunction> fun = | 142 Handle<JSFunction> fun = |
| 142 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); | 143 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 143 if (fun.is_null()) return -1; | 144 if (fun.is_null()) return -1; |
| 144 | 145 |
| 145 SetGlobalProperty("n", Smi::FromInt(n)); | 146 SetGlobalProperty("n", Smi::FromInt(n)); |
| 146 bool has_pending_exception; | 147 bool has_pending_exception; |
| 147 Handle<JSObject> global(isolate->context()->global_object()); | 148 Handle<JSObject> global(isolate->context()->global_object()); |
| 148 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 149 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 149 CHECK(!has_pending_exception); | 150 CHECK(!has_pending_exception); |
| 150 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 151 return GetGlobalProperty("result")->Number(); |
| 151 } | 152 } |
| 152 | 153 |
| 153 | 154 |
| 154 TEST(Sum) { | 155 TEST(Sum) { |
| 155 CcTest::InitializeVM(); | 156 CcTest::InitializeVM(); |
| 156 v8::HandleScope scope(CcTest::isolate()); | 157 v8::HandleScope scope(CcTest::isolate()); |
| 157 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); | 158 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); |
| 158 } | 159 } |
| 159 | 160 |
| 160 | 161 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 "function Cons2(x, y) { this.sum = x + y; }\n" | 198 "function Cons2(x, y) { this.sum = x + y; }\n" |
| 198 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 | 199 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 199 | 200 |
| 200 Handle<JSFunction> fun = Compile(source); | 201 Handle<JSFunction> fun = Compile(source); |
| 201 CHECK(!fun.is_null()); | 202 CHECK(!fun.is_null()); |
| 202 bool has_pending_exception; | 203 bool has_pending_exception; |
| 203 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); | 204 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); |
| 204 Execution::Call( | 205 Execution::Call( |
| 205 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception); | 206 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception); |
| 206 CHECK(!has_pending_exception); | 207 CHECK(!has_pending_exception); |
| 207 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); | 208 CHECK_EQ(511.0, GetGlobalProperty("r")->Number()); |
| 208 } | 209 } |
| 209 | 210 |
| 210 | 211 |
| 211 TEST(UncaughtThrow) { | 212 TEST(UncaughtThrow) { |
| 212 CcTest::InitializeVM(); | 213 CcTest::InitializeVM(); |
| 213 v8::HandleScope scope(CcTest::isolate()); | 214 v8::HandleScope scope(CcTest::isolate()); |
| 214 | 215 |
| 215 const char* source = "throw 42;"; | 216 const char* source = "throw 42;"; |
| 216 Handle<JSFunction> fun = Compile(source); | 217 Handle<JSFunction> fun = Compile(source); |
| 217 CHECK(!fun.is_null()); | 218 CHECK(!fun.is_null()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 243 CHECK(!fun0.is_null()); | 244 CHECK(!fun0.is_null()); |
| 244 Isolate* isolate = fun0->GetIsolate(); | 245 Isolate* isolate = fun0->GetIsolate(); |
| 245 | 246 |
| 246 // Run the generated code to populate the global object with 'foo'. | 247 // Run the generated code to populate the global object with 'foo'. |
| 247 bool has_pending_exception; | 248 bool has_pending_exception; |
| 248 Handle<JSObject> global(isolate->context()->global_object()); | 249 Handle<JSObject> global(isolate->context()->global_object()); |
| 249 Execution::Call( | 250 Execution::Call( |
| 250 isolate, fun0, global, 0, NULL, &has_pending_exception); | 251 isolate, fun0, global, 0, NULL, &has_pending_exception); |
| 251 CHECK(!has_pending_exception); | 252 CHECK(!has_pending_exception); |
| 252 | 253 |
| 253 Object* foo_string = isolate->factory()->InternalizeOneByteString( | 254 Handle<String> foo_string = isolate->factory()->InternalizeOneByteString( |
| 254 STATIC_ASCII_VECTOR("foo"))->ToObjectChecked(); | 255 STATIC_ASCII_VECTOR("foo")); |
| 255 MaybeObject* fun1_object = isolate->context()->global_object()-> | 256 Handle<Object> fun1 = Object::GetProperty( |
| 256 GetProperty(String::cast(foo_string)); | 257 isolate->global_object(), foo_string); |
| 257 Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate); | |
| 258 CHECK(fun1->IsJSFunction()); | 258 CHECK(fun1->IsJSFunction()); |
| 259 | 259 |
| 260 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( | 260 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( |
| 261 STATIC_ASCII_VECTOR("hello")) }; | 261 STATIC_ASCII_VECTOR("hello")) }; |
| 262 Execution::Call(isolate, | 262 Execution::Call(isolate, |
| 263 Handle<JSFunction>::cast(fun1), | 263 Handle<JSFunction>::cast(fun1), |
| 264 global, | 264 global, |
| 265 ARRAY_SIZE(argv), | 265 ARRAY_SIZE(argv), |
| 266 argv, | 266 argv, |
| 267 &has_pending_exception); | 267 &has_pending_exception); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 CompileRun("function f() { a = 12345678 }; f();"); | 393 CompileRun("function f() { a = 12345678 }; f();"); |
| 394 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 394 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 395 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 395 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
| 396 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 396 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 397 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 397 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
| 398 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 398 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 399 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 399 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
| 400 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 400 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 401 } | 401 } |
| 402 #endif | 402 #endif |
| OLD | NEW |