| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "include/dart_api.h" | 5 #include "include/dart_api.h" |
| 6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 7 #include "platform/json.h" | 7 #include "platform/json.h" |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| 11 #include "vm/dart_api_state.h" | 11 #include "vm/dart_api_state.h" |
| 12 #include "vm/thread.h" | 12 #include "vm/thread.h" |
| 13 #include "vm/unit_test.h" | 13 #include "vm/unit_test.h" |
| 14 #include "vm/verifier.h" | 14 #include "vm/verifier.h" |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 DECLARE_FLAG(bool, enable_type_checks); | 18 DECLARE_FLAG(bool, enable_type_checks); |
| 19 | 19 |
| 20 #if defined(TARGET_ARCH_IA32) || \ | |
| 21 defined(TARGET_ARCH_X64) || \ | |
| 22 defined(TARGET_ARCH_ARM) | |
| 23 | |
| 24 TEST_CASE(ErrorHandleBasics) { | 20 TEST_CASE(ErrorHandleBasics) { |
| 25 const char* kScriptChars = | 21 const char* kScriptChars = |
| 26 "void testMain() {\n" | 22 "void testMain() {\n" |
| 27 " throw new Exception(\"bad news\");\n" | 23 " throw new Exception(\"bad news\");\n" |
| 28 "}\n"; | 24 "}\n"; |
| 29 | 25 |
| 30 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 26 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 31 | 27 |
| 32 Dart_Handle instance = Dart_True(); | 28 Dart_Handle instance = Dart_True(); |
| 33 Dart_Handle error = Api::NewError("myerror"); | 29 Dart_Handle error = Api::NewError("myerror"); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 EXPECT(Dart_IsError(result)); | 156 EXPECT(Dart_IsError(result)); |
| 161 EXPECT(!Dart_ErrorHasException(result)); | 157 EXPECT(!Dart_ErrorHasException(result)); |
| 162 EXPECT_SUBSTRING("semicolon expected", Dart_GetError(result)); | 158 EXPECT_SUBSTRING("semicolon expected", Dart_GetError(result)); |
| 163 | 159 |
| 164 result = Dart_Invoke(lib, NewString("Func2"), 0, NULL); | 160 result = Dart_Invoke(lib, NewString("Func2"), 0, NULL); |
| 165 EXPECT(Dart_IsError(result)); | 161 EXPECT(Dart_IsError(result)); |
| 166 EXPECT(Dart_ErrorHasException(result)); | 162 EXPECT(Dart_ErrorHasException(result)); |
| 167 EXPECT_SUBSTRING("myException", Dart_GetError(result)); | 163 EXPECT_SUBSTRING("myException", Dart_GetError(result)); |
| 168 } | 164 } |
| 169 | 165 |
| 170 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64 || TARGET_ARCH_ARM | |
| 171 | |
| 172 | 166 |
| 173 TEST_CASE(Dart_Error) { | 167 TEST_CASE(Dart_Error) { |
| 174 Dart_Handle error = Dart_Error("An %s", "error"); | 168 Dart_Handle error = Dart_Error("An %s", "error"); |
| 175 EXPECT(Dart_IsError(error)); | 169 EXPECT(Dart_IsError(error)); |
| 176 EXPECT_STREQ("An error", Dart_GetError(error)); | 170 EXPECT_STREQ("An error", Dart_GetError(error)); |
| 177 } | 171 } |
| 178 | 172 |
| 179 | 173 |
| 180 TEST_CASE(Null) { | 174 TEST_CASE(Null) { |
| 181 Dart_Handle null = Dart_Null(); | 175 Dart_Handle null = Dart_Null(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 209 Dart_Handle class1 = Api::NewHandle(isolate, Object::null_class()); | 203 Dart_Handle class1 = Api::NewHandle(isolate, Object::null_class()); |
| 210 Dart_Handle class2 = Api::NewHandle(isolate, Object::class_class()); | 204 Dart_Handle class2 = Api::NewHandle(isolate, Object::class_class()); |
| 211 | 205 |
| 212 EXPECT(Dart_IdentityEquals(class1, class1)); | 206 EXPECT(Dart_IdentityEquals(class1, class1)); |
| 213 | 207 |
| 214 EXPECT(!Dart_IdentityEquals(class1, class2)); | 208 EXPECT(!Dart_IdentityEquals(class1, class2)); |
| 215 } | 209 } |
| 216 } | 210 } |
| 217 | 211 |
| 218 | 212 |
| 219 #if defined(TARGET_ARCH_IA32) || \ | |
| 220 defined(TARGET_ARCH_X64) || \ | |
| 221 defined(TARGET_ARCH_ARM) | |
| 222 | |
| 223 TEST_CASE(ObjectEquals) { | 213 TEST_CASE(ObjectEquals) { |
| 224 bool equal = false; | 214 bool equal = false; |
| 225 Dart_Handle five = NewString("5"); | 215 Dart_Handle five = NewString("5"); |
| 226 Dart_Handle five_again = NewString("5"); | 216 Dart_Handle five_again = NewString("5"); |
| 227 Dart_Handle seven = NewString("7"); | 217 Dart_Handle seven = NewString("7"); |
| 228 | 218 |
| 229 // Same objects. | 219 // Same objects. |
| 230 EXPECT_VALID(Dart_ObjectEquals(five, five, &equal)); | 220 EXPECT_VALID(Dart_ObjectEquals(five, five, &equal)); |
| 231 EXPECT(equal); | 221 EXPECT(equal); |
| 232 | 222 |
| 233 // Equal objects. | 223 // Equal objects. |
| 234 EXPECT_VALID(Dart_ObjectEquals(five, five_again, &equal)); | 224 EXPECT_VALID(Dart_ObjectEquals(five, five_again, &equal)); |
| 235 EXPECT(equal); | 225 EXPECT(equal); |
| 236 | 226 |
| 237 // Different objects. | 227 // Different objects. |
| 238 EXPECT_VALID(Dart_ObjectEquals(five, seven, &equal)); | 228 EXPECT_VALID(Dart_ObjectEquals(five, seven, &equal)); |
| 239 EXPECT(!equal); | 229 EXPECT(!equal); |
| 240 } | 230 } |
| 241 | 231 |
| 242 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64 || TARGET_ARCH_ARM | |
| 243 | |
| 244 | 232 |
| 245 TEST_CASE(InstanceValues) { | 233 TEST_CASE(InstanceValues) { |
| 246 EXPECT(Dart_IsInstance(NewString("test"))); | 234 EXPECT(Dart_IsInstance(NewString("test"))); |
| 247 EXPECT(Dart_IsInstance(Dart_True())); | 235 EXPECT(Dart_IsInstance(Dart_True())); |
| 248 | 236 |
| 249 // By convention, our Is*() functions exclude null. | 237 // By convention, our Is*() functions exclude null. |
| 250 EXPECT(!Dart_IsInstance(Dart_Null())); | 238 EXPECT(!Dart_IsInstance(Dart_Null())); |
| 251 } | 239 } |
| 252 | 240 |
| 253 | 241 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 double out1, out2; | 319 double out1, out2; |
| 332 Dart_Handle result = Dart_DoubleValue(val1, &out1); | 320 Dart_Handle result = Dart_DoubleValue(val1, &out1); |
| 333 EXPECT_VALID(result); | 321 EXPECT_VALID(result); |
| 334 EXPECT_EQ(kDoubleVal1, out1); | 322 EXPECT_EQ(kDoubleVal1, out1); |
| 335 result = Dart_DoubleValue(val2, &out2); | 323 result = Dart_DoubleValue(val2, &out2); |
| 336 EXPECT_VALID(result); | 324 EXPECT_VALID(result); |
| 337 EXPECT_EQ(kDoubleVal2, out2); | 325 EXPECT_EQ(kDoubleVal2, out2); |
| 338 } | 326 } |
| 339 | 327 |
| 340 | 328 |
| 341 #if defined(TARGET_ARCH_IA32) || \ | |
| 342 defined(TARGET_ARCH_X64) || \ | |
| 343 defined(TARGET_ARCH_ARM) | |
| 344 | |
| 345 TEST_CASE(NumberValues) { | 329 TEST_CASE(NumberValues) { |
| 346 // TODO(antonm): add various kinds of ints (smi, mint, bigint). | 330 // TODO(antonm): add various kinds of ints (smi, mint, bigint). |
| 347 const char* kScriptChars = | 331 const char* kScriptChars = |
| 348 "int getInt() { return 1; }\n" | 332 "int getInt() { return 1; }\n" |
| 349 "double getDouble() { return 1.0; }\n" | 333 "double getDouble() { return 1.0; }\n" |
| 350 "bool getBool() { return false; }\n" | 334 "bool getBool() { return false; }\n" |
| 351 "getNull() { return null; }\n"; | 335 "getNull() { return null; }\n"; |
| 352 Dart_Handle result; | 336 Dart_Handle result; |
| 353 // Create a test library and Load up a test script in it. | 337 // Create a test library and Load up a test script in it. |
| 354 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 338 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 367 result = Dart_Invoke(lib, NewString("getBool"), 0, NULL); | 351 result = Dart_Invoke(lib, NewString("getBool"), 0, NULL); |
| 368 EXPECT_VALID(result); | 352 EXPECT_VALID(result); |
| 369 EXPECT(!Dart_IsNumber(result)); | 353 EXPECT(!Dart_IsNumber(result)); |
| 370 | 354 |
| 371 // Check null case. | 355 // Check null case. |
| 372 result = Dart_Invoke(lib, NewString("getNull"), 0, NULL); | 356 result = Dart_Invoke(lib, NewString("getNull"), 0, NULL); |
| 373 EXPECT_VALID(result); | 357 EXPECT_VALID(result); |
| 374 EXPECT(!Dart_IsNumber(result)); | 358 EXPECT(!Dart_IsNumber(result)); |
| 375 } | 359 } |
| 376 | 360 |
| 377 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64 || TARGET_ARCH_ARM | |
| 378 | |
| 379 | 361 |
| 380 TEST_CASE(IntegerValues) { | 362 TEST_CASE(IntegerValues) { |
| 381 const int64_t kIntegerVal1 = 100; | 363 const int64_t kIntegerVal1 = 100; |
| 382 const int64_t kIntegerVal2 = 0xffffffff; | 364 const int64_t kIntegerVal2 = 0xffffffff; |
| 383 const char* kIntegerVal3 = "0x123456789123456789123456789"; | 365 const char* kIntegerVal3 = "0x123456789123456789123456789"; |
| 384 | 366 |
| 385 Dart_Handle val1 = Dart_NewInteger(kIntegerVal1); | 367 Dart_Handle val1 = Dart_NewInteger(kIntegerVal1); |
| 386 EXPECT(Dart_IsInteger(val1)); | 368 EXPECT(Dart_IsInteger(val1)); |
| 387 bool fits = false; | 369 bool fits = false; |
| 388 Dart_Handle result = Dart_IntegerFitsIntoInt64(val1, &fits); | 370 Dart_Handle result = Dart_IntegerFitsIntoInt64(val1, &fits); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 // Not a String. | 614 // Not a String. |
| 633 peer = NULL; | 615 peer = NULL; |
| 634 result = Dart_ExternalStringGetPeer(Dart_True(), &peer); | 616 result = Dart_ExternalStringGetPeer(Dart_True(), &peer); |
| 635 EXPECT(Dart_IsError(result)); | 617 EXPECT(Dart_IsError(result)); |
| 636 EXPECT_STREQ("Dart_ExternalStringGetPeer expects argument 'object' to be " | 618 EXPECT_STREQ("Dart_ExternalStringGetPeer expects argument 'object' to be " |
| 637 "of type String.", Dart_GetError(result)); | 619 "of type String.", Dart_GetError(result)); |
| 638 EXPECT(peer == NULL); | 620 EXPECT(peer == NULL); |
| 639 } | 621 } |
| 640 | 622 |
| 641 | 623 |
| 642 #if defined(TARGET_ARCH_IA32) || \ | |
| 643 defined(TARGET_ARCH_X64) || \ | |
| 644 defined(TARGET_ARCH_ARM) | |
| 645 | |
| 646 static void ExternalStringCallbackFinalizer(void* peer) { | 624 static void ExternalStringCallbackFinalizer(void* peer) { |
| 647 *static_cast<int*>(peer) *= 2; | 625 *static_cast<int*>(peer) *= 2; |
| 648 } | 626 } |
| 649 | 627 |
| 650 | 628 |
| 651 TEST_CASE(ExternalStringCallback) { | 629 TEST_CASE(ExternalStringCallback) { |
| 652 int peer8 = 40; | 630 int peer8 = 40; |
| 653 int peer16 = 41; | 631 int peer16 = 41; |
| 654 | 632 |
| 655 { | 633 { |
| (...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1405 data, | 1383 data, |
| 1406 10, | 1384 10, |
| 1407 &peer, | 1385 &peer, |
| 1408 ExternalTypedDataCallbackFinalizer); | 1386 ExternalTypedDataCallbackFinalizer); |
| 1409 CheckFloat32x4Data(obj); | 1387 CheckFloat32x4Data(obj); |
| 1410 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); | 1388 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); |
| 1411 EXPECT(peer == 42); | 1389 EXPECT(peer == 42); |
| 1412 } | 1390 } |
| 1413 | 1391 |
| 1414 | 1392 |
| 1415 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64 || TARGET_ARCH_ARM | |
| 1416 | |
| 1417 | |
| 1418 // Unit test for entering a scope, creating a local handle and exiting | 1393 // Unit test for entering a scope, creating a local handle and exiting |
| 1419 // the scope. | 1394 // the scope. |
| 1420 UNIT_TEST_CASE(EnterExitScope) { | 1395 UNIT_TEST_CASE(EnterExitScope) { |
| 1421 TestIsolateScope __test_isolate__; | 1396 TestIsolateScope __test_isolate__; |
| 1422 | 1397 |
| 1423 Isolate* isolate = Isolate::Current(); | 1398 Isolate* isolate = Isolate::Current(); |
| 1424 EXPECT(isolate != NULL); | 1399 EXPECT(isolate != NULL); |
| 1425 ApiState* state = isolate->api_state(); | 1400 ApiState* state = isolate->api_state(); |
| 1426 EXPECT(state != NULL); | 1401 EXPECT(state != NULL); |
| 1427 ApiLocalScope* scope = state->top_scope(); | 1402 ApiLocalScope* scope = state->top_scope(); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1540 // perturb the test. | 1515 // perturb the test. |
| 1541 class GCTestHelper : public AllStatic { | 1516 class GCTestHelper : public AllStatic { |
| 1542 public: | 1517 public: |
| 1543 static void CollectNewSpace(Heap::ApiCallbacks api_callbacks) { | 1518 static void CollectNewSpace(Heap::ApiCallbacks api_callbacks) { |
| 1544 bool invoke_api_callbacks = (api_callbacks == Heap::kInvokeApiCallbacks); | 1519 bool invoke_api_callbacks = (api_callbacks == Heap::kInvokeApiCallbacks); |
| 1545 Isolate::Current()->heap()->new_space_->Scavenge(invoke_api_callbacks); | 1520 Isolate::Current()->heap()->new_space_->Scavenge(invoke_api_callbacks); |
| 1546 } | 1521 } |
| 1547 }; | 1522 }; |
| 1548 | 1523 |
| 1549 | 1524 |
| 1550 #if defined(TARGET_ARCH_IA32) || \ | |
| 1551 defined(TARGET_ARCH_X64) || \ | |
| 1552 defined(TARGET_ARCH_ARM) | |
| 1553 | |
| 1554 TEST_CASE(WeakPersistentHandle) { | 1525 TEST_CASE(WeakPersistentHandle) { |
| 1555 Dart_Handle weak_new_ref = Dart_Null(); | 1526 Dart_Handle weak_new_ref = Dart_Null(); |
| 1556 EXPECT(Dart_IsNull(weak_new_ref)); | 1527 EXPECT(Dart_IsNull(weak_new_ref)); |
| 1557 | 1528 |
| 1558 Dart_Handle weak_old_ref = Dart_Null(); | 1529 Dart_Handle weak_old_ref = Dart_Null(); |
| 1559 EXPECT(Dart_IsNull(weak_old_ref)); | 1530 EXPECT(Dart_IsNull(weak_old_ref)); |
| 1560 | 1531 |
| 1561 { | 1532 { |
| 1562 Dart_EnterScope(); | 1533 Dart_EnterScope(); |
| 1563 | 1534 |
| (...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2407 | 2378 |
| 2408 // Garbage collect old space. No callbacks should be invoked. No | 2379 // Garbage collect old space. No callbacks should be invoked. No |
| 2409 // status values should change. | 2380 // status values should change. |
| 2410 global_prologue_callback_status = 3; | 2381 global_prologue_callback_status = 3; |
| 2411 global_epilogue_callback_status = 7; | 2382 global_epilogue_callback_status = 7; |
| 2412 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); | 2383 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
| 2413 EXPECT_EQ(3, global_prologue_callback_status); | 2384 EXPECT_EQ(3, global_prologue_callback_status); |
| 2414 EXPECT_EQ(7, global_epilogue_callback_status); | 2385 EXPECT_EQ(7, global_epilogue_callback_status); |
| 2415 } | 2386 } |
| 2416 | 2387 |
| 2417 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64 || TARGET_ARCH_ARM | |
| 2418 | |
| 2419 | 2388 |
| 2420 // Unit test for creating multiple scopes and local handles within them. | 2389 // Unit test for creating multiple scopes and local handles within them. |
| 2421 // Ensure that the local handles get all cleaned out when exiting the | 2390 // Ensure that the local handles get all cleaned out when exiting the |
| 2422 // scope. | 2391 // scope. |
| 2423 UNIT_TEST_CASE(LocalHandles) { | 2392 UNIT_TEST_CASE(LocalHandles) { |
| 2424 TestCase::CreateTestIsolate(); | 2393 TestCase::CreateTestIsolate(); |
| 2425 Isolate* isolate = Isolate::Current(); | 2394 Isolate* isolate = Isolate::Current(); |
| 2426 EXPECT(isolate != NULL); | 2395 EXPECT(isolate != NULL); |
| 2427 ApiState* state = isolate->api_state(); | 2396 ApiState* state = isolate->api_state(); |
| 2428 EXPECT(state != NULL); | 2397 EXPECT(state != NULL); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2579 | 2548 |
| 2580 UNIT_TEST_CASE(SetMessageCallbacks) { | 2549 UNIT_TEST_CASE(SetMessageCallbacks) { |
| 2581 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); | 2550 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); |
| 2582 Dart_SetMessageNotifyCallback(&MyMessageNotifyCallback); | 2551 Dart_SetMessageNotifyCallback(&MyMessageNotifyCallback); |
| 2583 Isolate* isolate = reinterpret_cast<Isolate*>(dart_isolate); | 2552 Isolate* isolate = reinterpret_cast<Isolate*>(dart_isolate); |
| 2584 EXPECT_EQ(&MyMessageNotifyCallback, isolate->message_notify_callback()); | 2553 EXPECT_EQ(&MyMessageNotifyCallback, isolate->message_notify_callback()); |
| 2585 Dart_ShutdownIsolate(); | 2554 Dart_ShutdownIsolate(); |
| 2586 } | 2555 } |
| 2587 | 2556 |
| 2588 | 2557 |
| 2589 #if defined(TARGET_ARCH_IA32) || \ | |
| 2590 defined(TARGET_ARCH_X64) || \ | |
| 2591 defined(TARGET_ARCH_ARM) | |
| 2592 | |
| 2593 TEST_CASE(ClassTypedefsEtc) { | 2558 TEST_CASE(ClassTypedefsEtc) { |
| 2594 const char* kScriptChars = | 2559 const char* kScriptChars = |
| 2595 "class SomeClass {\n" | 2560 "class SomeClass {\n" |
| 2596 "}\n" | 2561 "}\n" |
| 2597 "typedef void SomeHandler(String a);\n"; | 2562 "typedef void SomeHandler(String a);\n"; |
| 2598 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 2563 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 2599 EXPECT_VALID(lib); | 2564 EXPECT_VALID(lib); |
| 2600 Dart_Handle normal_cls = Dart_GetClass(lib, NewString("SomeClass")); | 2565 Dart_Handle normal_cls = Dart_GetClass(lib, NewString("SomeClass")); |
| 2601 EXPECT_VALID(normal_cls); | 2566 EXPECT_VALID(normal_cls); |
| 2602 Dart_Handle typedef_cls = Dart_GetClass(lib, NewString("SomeHandler")); | 2567 Dart_Handle typedef_cls = Dart_GetClass(lib, NewString("SomeHandler")); |
| (...skipping 5041 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7644 Dart_Handle result = Dart_Invoke(lib, | 7609 Dart_Handle result = Dart_Invoke(lib, |
| 7645 NewString("main"), | 7610 NewString("main"), |
| 7646 0, | 7611 0, |
| 7647 NULL); | 7612 NULL); |
| 7648 int64_t value = 0; | 7613 int64_t value = 0; |
| 7649 result = Dart_IntegerToInt64(result, &value); | 7614 result = Dart_IntegerToInt64(result, &value); |
| 7650 EXPECT_VALID(result); | 7615 EXPECT_VALID(result); |
| 7651 EXPECT_EQ(260, value); | 7616 EXPECT_EQ(260, value); |
| 7652 } | 7617 } |
| 7653 | 7618 |
| 7654 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64 || TARGET_ARCH_ARM | |
| 7655 | |
| 7656 } // namespace dart | 7619 } // namespace dart |
| OLD | NEW |