| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 | 6 |
| 7 #include "vm/assert.h" | 7 #include "vm/assert.h" |
| 8 #include "vm/dart_api_impl.h" | 8 #include "vm/dart_api_impl.h" |
| 9 #include "vm/dart_api_state.h" | 9 #include "vm/dart_api_state.h" |
| 10 #include "vm/unit_test.h" | 10 #include "vm/unit_test.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // Equal objects. | 59 // Equal objects. |
| 60 EXPECT_VALID(Dart_IsSame(five, five_again, &same)); | 60 EXPECT_VALID(Dart_IsSame(five, five_again, &same)); |
| 61 EXPECT(!same); | 61 EXPECT(!same); |
| 62 | 62 |
| 63 // Different objects. | 63 // Different objects. |
| 64 EXPECT_VALID(Dart_IsSame(five, seven, &same)); | 64 EXPECT_VALID(Dart_IsSame(five, seven, &same)); |
| 65 EXPECT(!same); | 65 EXPECT(!same); |
| 66 | 66 |
| 67 // Non-instance objects. | 67 // Non-instance objects. |
| 68 { | 68 { |
| 69 Zone zone; | 69 Isolate* isolate = Isolate::Current(); |
| 70 HandleScope hs; | 70 DARTSCOPE(isolate); |
| 71 const Object& cls1 = Object::Handle(Object::null_class()); | 71 const Object& cls1 = Object::Handle(Object::null_class()); |
| 72 const Object& cls2 = Object::Handle(Object::class_class()); | 72 const Object& cls2 = Object::Handle(Object::class_class()); |
| 73 Dart_Handle class1 = Api::NewLocalHandle(cls1); | 73 Dart_Handle class1 = Api::NewLocalHandle(cls1); |
| 74 Dart_Handle class2 = Api::NewLocalHandle(cls2); | 74 Dart_Handle class2 = Api::NewLocalHandle(cls2); |
| 75 | 75 |
| 76 EXPECT_VALID(Dart_IsSame(class1, class1, &same)); | 76 EXPECT_VALID(Dart_IsSame(class1, class1, &same)); |
| 77 EXPECT(same); | 77 EXPECT(same); |
| 78 | 78 |
| 79 EXPECT_VALID(Dart_IsSame(class1, class2, &same)); | 79 EXPECT_VALID(Dart_IsSame(class1, class2, &same)); |
| 80 EXPECT(!same); | 80 EXPECT(!same); |
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 UNIT_TEST_CASE(EnterExitScope) { | 486 UNIT_TEST_CASE(EnterExitScope) { |
| 487 Dart_CreateIsolate(NULL, NULL); | 487 Dart_CreateIsolate(NULL, NULL); |
| 488 Isolate* isolate = Isolate::Current(); | 488 Isolate* isolate = Isolate::Current(); |
| 489 EXPECT(isolate != NULL); | 489 EXPECT(isolate != NULL); |
| 490 ApiState* state = isolate->api_state(); | 490 ApiState* state = isolate->api_state(); |
| 491 EXPECT(state != NULL); | 491 EXPECT(state != NULL); |
| 492 ApiLocalScope* scope = state->top_scope(); | 492 ApiLocalScope* scope = state->top_scope(); |
| 493 Dart_EnterScope(); | 493 Dart_EnterScope(); |
| 494 { | 494 { |
| 495 EXPECT(state->top_scope() != NULL); | 495 EXPECT(state->top_scope() != NULL); |
| 496 Zone zone; | 496 DARTSCOPE(isolate); |
| 497 HandleScope hs; | |
| 498 const String& str1 = String::Handle(String::New("Test String")); | 497 const String& str1 = String::Handle(String::New("Test String")); |
| 499 Dart_Handle ref = Api::NewLocalHandle(str1); | 498 Dart_Handle ref = Api::NewLocalHandle(str1); |
| 500 String& str2 = String::Handle(); | 499 String& str2 = String::Handle(); |
| 501 str2 ^= Api::UnwrapHandle(ref); | 500 str2 ^= Api::UnwrapHandle(ref); |
| 502 EXPECT(str1.Equals(str2)); | 501 EXPECT(str1.Equals(str2)); |
| 503 } | 502 } |
| 504 Dart_ExitScope(); | 503 Dart_ExitScope(); |
| 505 EXPECT(scope == state->top_scope()); | 504 EXPECT(scope == state->top_scope()); |
| 506 Dart_ShutdownIsolate(); | 505 Dart_ShutdownIsolate(); |
| 507 } | 506 } |
| 508 | 507 |
| 509 | 508 |
| 510 // Unit test for creating and deleting persistent handles. | 509 // Unit test for creating and deleting persistent handles. |
| 511 UNIT_TEST_CASE(PersistentHandles) { | 510 UNIT_TEST_CASE(PersistentHandles) { |
| 512 const char* kTestString1 = "Test String1"; | 511 const char* kTestString1 = "Test String1"; |
| 513 const char* kTestString2 = "Test String2"; | 512 const char* kTestString2 = "Test String2"; |
| 514 Dart_CreateIsolate(NULL, NULL); | 513 Dart_CreateIsolate(NULL, NULL); |
| 515 Isolate* isolate = Isolate::Current(); | 514 Isolate* isolate = Isolate::Current(); |
| 516 EXPECT(isolate != NULL); | 515 EXPECT(isolate != NULL); |
| 517 ApiState* state = isolate->api_state(); | 516 ApiState* state = isolate->api_state(); |
| 518 EXPECT(state != NULL); | 517 EXPECT(state != NULL); |
| 519 ApiLocalScope* scope = state->top_scope(); | 518 ApiLocalScope* scope = state->top_scope(); |
| 520 Dart_Handle handles[2000]; | 519 Dart_Handle handles[2000]; |
| 521 Dart_EnterScope(); | 520 Dart_EnterScope(); |
| 522 { | 521 { |
| 523 Zone zone; | 522 DARTSCOPE(isolate); |
| 524 HandleScope hs; | |
| 525 const String& str1 = String::Handle(String::New(kTestString1)); | 523 const String& str1 = String::Handle(String::New(kTestString1)); |
| 526 Dart_Handle ref1 = Api::NewLocalHandle(str1); | 524 Dart_Handle ref1 = Api::NewLocalHandle(str1); |
| 527 for (int i = 0; i < 1000; i++) { | 525 for (int i = 0; i < 1000; i++) { |
| 528 handles[i] = Dart_NewPersistentHandle(ref1); | 526 handles[i] = Dart_NewPersistentHandle(ref1); |
| 529 } | 527 } |
| 530 Dart_EnterScope(); | 528 Dart_EnterScope(); |
| 531 const String& str2 = String::Handle(String::New(kTestString2)); | 529 const String& str2 = String::Handle(String::New(kTestString2)); |
| 532 Dart_Handle ref2 = Api::NewLocalHandle(str2); | 530 Dart_Handle ref2 = Api::NewLocalHandle(str2); |
| 533 for (int i = 1000; i < 2000; i++) { | 531 for (int i = 1000; i < 2000; i++) { |
| 534 handles[i] = Dart_NewPersistentHandle(ref2); | 532 handles[i] = Dart_NewPersistentHandle(ref2); |
| 535 } | 533 } |
| 536 for (int i = 500; i < 1500; i++) { | 534 for (int i = 500; i < 1500; i++) { |
| 537 Dart_DeletePersistentHandle(handles[i]); | 535 Dart_DeletePersistentHandle(handles[i]); |
| 538 } | 536 } |
| 539 for (int i = 500; i < 1000; i++) { | 537 for (int i = 500; i < 1000; i++) { |
| 540 handles[i] = Dart_NewPersistentHandle(ref2); | 538 handles[i] = Dart_NewPersistentHandle(ref2); |
| 541 } | 539 } |
| 542 for (int i = 1000; i < 1500; i++) { | 540 for (int i = 1000; i < 1500; i++) { |
| 543 handles[i] = Dart_NewPersistentHandle(ref1); | 541 handles[i] = Dart_NewPersistentHandle(ref1); |
| 544 } | 542 } |
| 545 VERIFY_ON_TRANSITION; | 543 VERIFY_ON_TRANSITION; |
| 546 Dart_ExitScope(); | 544 Dart_ExitScope(); |
| 547 } | 545 } |
| 548 Dart_ExitScope(); | 546 Dart_ExitScope(); |
| 549 { | 547 { |
| 550 Zone zone; | 548 DARTSCOPE(isolate); |
| 551 HandleScope hs; | |
| 552 for (int i = 0; i < 500; i++) { | 549 for (int i = 0; i < 500; i++) { |
| 553 String& str = String::Handle(); | 550 String& str = String::Handle(); |
| 554 str ^= Api::UnwrapHandle(handles[i]); | 551 str ^= Api::UnwrapHandle(handles[i]); |
| 555 EXPECT(str.Equals(kTestString1)); | 552 EXPECT(str.Equals(kTestString1)); |
| 556 } | 553 } |
| 557 for (int i = 500; i < 1000; i++) { | 554 for (int i = 500; i < 1000; i++) { |
| 558 String& str = String::Handle(); | 555 String& str = String::Handle(); |
| 559 str ^= Api::UnwrapHandle(handles[i]); | 556 str ^= Api::UnwrapHandle(handles[i]); |
| 560 EXPECT(str.Equals(kTestString2)); | 557 EXPECT(str.Equals(kTestString2)); |
| 561 } | 558 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 // scope. | 607 // scope. |
| 611 UNIT_TEST_CASE(LocalHandles) { | 608 UNIT_TEST_CASE(LocalHandles) { |
| 612 Dart_CreateIsolate(NULL, NULL); | 609 Dart_CreateIsolate(NULL, NULL); |
| 613 Isolate* isolate = Isolate::Current(); | 610 Isolate* isolate = Isolate::Current(); |
| 614 EXPECT(isolate != NULL); | 611 EXPECT(isolate != NULL); |
| 615 ApiState* state = isolate->api_state(); | 612 ApiState* state = isolate->api_state(); |
| 616 EXPECT(state != NULL); | 613 EXPECT(state != NULL); |
| 617 ApiLocalScope* scope = state->top_scope(); | 614 ApiLocalScope* scope = state->top_scope(); |
| 618 Dart_Handle handles[300]; | 615 Dart_Handle handles[300]; |
| 619 { | 616 { |
| 620 Zone zone; | 617 DARTSCOPE(isolate); |
| 621 HandleScope hs; | |
| 622 Smi& val = Smi::Handle(); | 618 Smi& val = Smi::Handle(); |
| 623 | 619 |
| 624 // Start a new scope and allocate some local handles. | 620 // Start a new scope and allocate some local handles. |
| 625 Dart_EnterScope(); | 621 Dart_EnterScope(); |
| 626 for (int i = 0; i < 100; i++) { | 622 for (int i = 0; i < 100; i++) { |
| 627 val ^= Smi::New(i); | 623 val ^= Smi::New(i); |
| 628 handles[i] = Api::NewLocalHandle(val); | 624 handles[i] = Api::NewLocalHandle(val); |
| 629 } | 625 } |
| 630 EXPECT_EQ(100, state->CountLocalHandles()); | 626 EXPECT_EQ(100, state->CountLocalHandles()); |
| 631 for (int i = 0; i < 100; i++) { | 627 for (int i = 0; i < 100; i++) { |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 958 "class NativeFieldsTest {\n" | 954 "class NativeFieldsTest {\n" |
| 959 " static NativeFields testMain() {\n" | 955 " static NativeFields testMain() {\n" |
| 960 " NativeFields obj = new NativeFields(10, 20);\n" | 956 " NativeFields obj = new NativeFields(10, 20);\n" |
| 961 " return obj;\n" | 957 " return obj;\n" |
| 962 " }\n" | 958 " }\n" |
| 963 "}\n"; | 959 "}\n"; |
| 964 Dart_Handle result; | 960 Dart_Handle result; |
| 965 | 961 |
| 966 Dart_CreateIsolate(NULL, NULL); | 962 Dart_CreateIsolate(NULL, NULL); |
| 967 { | 963 { |
| 968 Zone zone; | 964 Isolate* isolate = Isolate::Current(); |
| 969 HandleScope scope; | 965 DARTSCOPE(isolate); |
| 970 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 966 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 971 const int kNumNativeFields = 4; | 967 const int kNumNativeFields = 4; |
| 972 | 968 |
| 973 // Create a test library. | 969 // Create a test library. |
| 974 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 970 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 975 | 971 |
| 976 // Create a native wrapper class with native fields. | 972 // Create a native wrapper class with native fields. |
| 977 result = Dart_CreateNativeWrapperClass( | 973 result = Dart_CreateNativeWrapperClass( |
| 978 lib, | 974 lib, |
| 979 Dart_NewString("NativeFieldsWrapper"), | 975 Dart_NewString("NativeFieldsWrapper"), |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1180 " return obj;\n" | 1176 " return obj;\n" |
| 1181 " }\n" | 1177 " }\n" |
| 1182 " static Function testMain2() {\n" | 1178 " static Function testMain2() {\n" |
| 1183 " return function() {};\n" | 1179 " return function() {};\n" |
| 1184 " }\n" | 1180 " }\n" |
| 1185 "}\n"; | 1181 "}\n"; |
| 1186 Dart_Handle result; | 1182 Dart_Handle result; |
| 1187 | 1183 |
| 1188 Dart_CreateIsolate(NULL, NULL); | 1184 Dart_CreateIsolate(NULL, NULL); |
| 1189 { | 1185 { |
| 1190 Zone zone; | 1186 Isolate* isolate = Isolate::Current(); |
| 1191 HandleScope scope; | 1187 DARTSCOPE(isolate); |
| 1192 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 1188 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 1193 | 1189 |
| 1194 // Create a test library and Load up a test script in it. | 1190 // Create a test library and Load up a test script in it. |
| 1195 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 1191 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 1196 | 1192 |
| 1197 // Invoke a function which returns an object of type NativeFields. | 1193 // Invoke a function which returns an object of type NativeFields. |
| 1198 Dart_Handle retobj = Dart_InvokeStatic(lib, | 1194 Dart_Handle retobj = Dart_InvokeStatic(lib, |
| 1199 Dart_NewString("NativeFieldsTest"), | 1195 Dart_NewString("NativeFieldsTest"), |
| 1200 Dart_NewString("testMain1"), | 1196 Dart_NewString("testMain1"), |
| 1201 0, | 1197 0, |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1369 "class InvokeDynamicTest {\n" | 1365 "class InvokeDynamicTest {\n" |
| 1370 " static InvokeDynamic testMain() {\n" | 1366 " static InvokeDynamic testMain() {\n" |
| 1371 " InvokeDynamic obj = new InvokeDynamic(10, 20);\n" | 1367 " InvokeDynamic obj = new InvokeDynamic(10, 20);\n" |
| 1372 " return obj;\n" | 1368 " return obj;\n" |
| 1373 " }\n" | 1369 " }\n" |
| 1374 "}\n"; | 1370 "}\n"; |
| 1375 Dart_Handle result; | 1371 Dart_Handle result; |
| 1376 | 1372 |
| 1377 Dart_CreateIsolate(NULL, NULL); | 1373 Dart_CreateIsolate(NULL, NULL); |
| 1378 { | 1374 { |
| 1379 Zone zone; | 1375 Isolate* isolate = Isolate::Current(); |
| 1380 HandleScope scope; | 1376 DARTSCOPE(isolate); |
| 1381 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 1377 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 1382 | 1378 |
| 1383 // Create a test library and Load up a test script in it. | 1379 // Create a test library and Load up a test script in it. |
| 1384 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 1380 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 1385 | 1381 |
| 1386 // Invoke a function which returns an object of type InvokeDynamic. | 1382 // Invoke a function which returns an object of type InvokeDynamic. |
| 1387 Dart_Handle retobj = Dart_InvokeStatic(lib, | 1383 Dart_Handle retobj = Dart_InvokeStatic(lib, |
| 1388 Dart_NewString("InvokeDynamicTest"), | 1384 Dart_NewString("InvokeDynamicTest"), |
| 1389 Dart_NewString("testMain"), | 1385 Dart_NewString("testMain"), |
| 1390 0, | 1386 0, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1441 " return obj.method1(10);\n" | 1437 " return obj.method1(10);\n" |
| 1442 " }\n" | 1438 " }\n" |
| 1443 " static Function testMain2() {\n" | 1439 " static Function testMain2() {\n" |
| 1444 " return InvokeClosure.method2(10);\n" | 1440 " return InvokeClosure.method2(10);\n" |
| 1445 " }\n" | 1441 " }\n" |
| 1446 "}\n"; | 1442 "}\n"; |
| 1447 Dart_Handle result; | 1443 Dart_Handle result; |
| 1448 | 1444 |
| 1449 Dart_CreateIsolate(NULL, NULL); | 1445 Dart_CreateIsolate(NULL, NULL); |
| 1450 { | 1446 { |
| 1451 Zone zone; | 1447 Isolate* isolate = Isolate::Current(); |
| 1452 HandleScope scope; | 1448 DARTSCOPE(isolate); |
| 1453 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 1449 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 1454 | 1450 |
| 1455 // Create a test library and Load up a test script in it. | 1451 // Create a test library and Load up a test script in it. |
| 1456 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 1452 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 1457 | 1453 |
| 1458 // Invoke a function which returns a closure. | 1454 // Invoke a function which returns a closure. |
| 1459 Dart_Handle retobj = Dart_InvokeStatic(lib, | 1455 Dart_Handle retobj = Dart_InvokeStatic(lib, |
| 1460 Dart_NewString("InvokeClosureTest"), | 1456 Dart_NewString("InvokeClosureTest"), |
| 1461 Dart_NewString("testMain1"), | 1457 Dart_NewString("testMain1"), |
| 1462 0, | 1458 0, |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1744 Dart_ExitScope(); // Exit the Dart API scope. | 1740 Dart_ExitScope(); // Exit the Dart API scope. |
| 1745 } | 1741 } |
| 1746 Dart_ShutdownIsolate(); | 1742 Dart_ShutdownIsolate(); |
| 1747 } | 1743 } |
| 1748 | 1744 |
| 1749 | 1745 |
| 1750 UNIT_TEST_CASE(NullReceiver) { | 1746 UNIT_TEST_CASE(NullReceiver) { |
| 1751 Dart_CreateIsolate(NULL, NULL); | 1747 Dart_CreateIsolate(NULL, NULL); |
| 1752 Dart_EnterScope(); // Enter a Dart API scope for the unit test. | 1748 Dart_EnterScope(); // Enter a Dart API scope for the unit test. |
| 1753 { | 1749 { |
| 1754 Zone zone; | 1750 Isolate* isolate = Isolate::Current(); |
| 1755 HandleScope hs; | 1751 DARTSCOPE(isolate); |
| 1756 | 1752 |
| 1757 Dart_Handle function_name = Dart_NewString("toString"); | 1753 Dart_Handle function_name = Dart_NewString("toString"); |
| 1758 const int number_of_arguments = 0; | 1754 const int number_of_arguments = 0; |
| 1759 Dart_Handle null_receiver = Api::NewLocalHandle(Object::Handle()); | 1755 Dart_Handle null_receiver = Api::NewLocalHandle(Object::Handle()); |
| 1760 Dart_Handle result = Dart_InvokeDynamic(null_receiver, | 1756 Dart_Handle result = Dart_InvokeDynamic(null_receiver, |
| 1761 function_name, | 1757 function_name, |
| 1762 number_of_arguments, | 1758 number_of_arguments, |
| 1763 NULL); | 1759 NULL); |
| 1764 EXPECT_VALID(result); | 1760 EXPECT_VALID(result); |
| 1765 EXPECT(Dart_IsString(result)); | 1761 EXPECT(Dart_IsString(result)); |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2096 NULL); | 2092 NULL); |
| 2097 EXPECT_VALID(result); | 2093 EXPECT_VALID(result); |
| 2098 | 2094 |
| 2099 Dart_ExitScope(); // Exit the Dart API scope. | 2095 Dart_ExitScope(); // Exit the Dart API scope. |
| 2100 } | 2096 } |
| 2101 Dart_ShutdownIsolate(); | 2097 Dart_ShutdownIsolate(); |
| 2102 } | 2098 } |
| 2103 #endif // TARGET_ARCH_IA32. | 2099 #endif // TARGET_ARCH_IA32. |
| 2104 | 2100 |
| 2105 } // namespace dart | 2101 } // namespace dart |
| OLD | NEW |