| 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); |
| 19 |
| 18 // Only ia32 and x64 can run execution tests. | 20 // Only ia32 and x64 can run execution tests. |
| 19 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) | 21 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) |
| 20 | 22 |
| 21 TEST_CASE(ErrorHandleBasics) { | 23 TEST_CASE(ErrorHandleBasics) { |
| 22 const char* kScriptChars = | 24 const char* kScriptChars = |
| 23 "void testMain() {\n" | 25 "void testMain() {\n" |
| 24 " throw new Exception(\"bad news\");\n" | 26 " throw new Exception(\"bad news\");\n" |
| 25 "}\n"; | 27 "}\n"; |
| 26 | 28 |
| 27 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 29 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| (...skipping 2351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2379 EXPECT_VALID(Dart_StringToCString(dflt_name, &dflt_name_cstr)); | 2381 EXPECT_VALID(Dart_StringToCString(dflt_name, &dflt_name_cstr)); |
| 2380 EXPECT_STREQ("MyDefault", dflt_name_cstr); | 2382 EXPECT_STREQ("MyDefault", dflt_name_cstr); |
| 2381 | 2383 |
| 2382 EXPECT(Dart_IsNull(Dart_ClassGetDefault(cls))); | 2384 EXPECT(Dart_IsNull(Dart_ClassGetDefault(cls))); |
| 2383 EXPECT_ERROR( | 2385 EXPECT_ERROR( |
| 2384 Dart_ClassGetDefault(Dart_True()), | 2386 Dart_ClassGetDefault(Dart_True()), |
| 2385 "Dart_ClassGetDefault expects argument 'clazz' to be of type Class."); | 2387 "Dart_ClassGetDefault expects argument 'clazz' to be of type Class."); |
| 2386 EXPECT_ERROR(Dart_ClassGetDefault(Dart_NewApiError("MyError")), "MyError"); | 2388 EXPECT_ERROR(Dart_ClassGetDefault(Dart_NewApiError("MyError")), "MyError"); |
| 2387 } | 2389 } |
| 2388 | 2390 |
| 2391 TEST_CASE(ClassTypedefsEtc) { |
| 2392 const char* kScriptChars = |
| 2393 "class SomeClass {\n" |
| 2394 "}\n" |
| 2395 "typedef void SomeHandler(String a);\n"; |
| 2396 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 2397 EXPECT_VALID(lib); |
| 2398 Dart_Handle normal_cls = Dart_GetClass(lib, Dart_NewString("SomeClass")); |
| 2399 EXPECT_VALID(normal_cls); |
| 2400 Dart_Handle typedef_cls = Dart_GetClass(lib, Dart_NewString("SomeHandler")); |
| 2401 EXPECT_VALID(typedef_cls); |
| 2402 |
| 2403 EXPECT(Dart_IsClass(normal_cls)); |
| 2404 EXPECT(!Dart_ClassIsTypedef(normal_cls)); |
| 2405 EXPECT(!Dart_ClassIsFunctionType(normal_cls)); |
| 2406 |
| 2407 EXPECT(Dart_IsClass(typedef_cls)); |
| 2408 EXPECT(Dart_ClassIsTypedef(typedef_cls)); |
| 2409 EXPECT(!Dart_ClassIsFunctionType(typedef_cls)); |
| 2410 |
| 2411 // Exercise the typedef class api. |
| 2412 Dart_Handle functype_cls = Dart_ClassGetTypedefReferent(typedef_cls); |
| 2413 EXPECT_VALID(functype_cls); |
| 2414 |
| 2415 // Pass the wrong class kind to Dart_ClassGetTypedefReferent |
| 2416 EXPECT_ERROR(Dart_ClassGetTypedefReferent(normal_cls), |
| 2417 "class 'SomeClass' is not a typedef class."); |
| 2418 |
| 2419 EXPECT(Dart_IsClass(functype_cls)); |
| 2420 EXPECT(!Dart_ClassIsTypedef(functype_cls)); |
| 2421 EXPECT(Dart_ClassIsFunctionType(functype_cls)); |
| 2422 |
| 2423 // Exercise the function type class. |
| 2424 Dart_Handle sig_func = Dart_ClassGetFunctionTypeSignature(functype_cls); |
| 2425 EXPECT_VALID(sig_func); |
| 2426 EXPECT(Dart_IsFunction(sig_func)); |
| 2427 int64_t fixed_params = -1; |
| 2428 int64_t opt_params = -1; |
| 2429 EXPECT_VALID( |
| 2430 Dart_FunctionParameterCounts(sig_func, &fixed_params, &opt_params)); |
| 2431 EXPECT_EQ(1, fixed_params); |
| 2432 EXPECT_EQ(0, opt_params); |
| 2433 |
| 2434 // Pass the wrong class kind to Dart_ClassGetFunctionTypeSignature |
| 2435 EXPECT_ERROR(Dart_ClassGetFunctionTypeSignature(normal_cls), |
| 2436 "class 'SomeClass' is not a function-type class."); |
| 2437 } |
| 2389 | 2438 |
| 2390 #define CHECK_INTERFACE(handle, name) \ | 2439 #define CHECK_INTERFACE(handle, name) \ |
| 2391 { \ | 2440 { \ |
| 2392 Dart_Handle tmp = (handle); \ | 2441 Dart_Handle tmp = (handle); \ |
| 2393 EXPECT_VALID(tmp); \ | 2442 EXPECT_VALID(tmp); \ |
| 2394 EXPECT(Dart_IsInterface(tmp)); \ | 2443 EXPECT(Dart_IsInterface(tmp)); \ |
| 2395 Dart_Handle intf_name = Dart_ClassName(tmp); \ | 2444 Dart_Handle intf_name = Dart_ClassName(tmp); \ |
| 2396 EXPECT_VALID(intf_name); \ | 2445 EXPECT_VALID(intf_name); \ |
| 2397 const char* intf_name_cstr = ""; \ | 2446 const char* intf_name_cstr = ""; \ |
| 2398 EXPECT_VALID(Dart_StringToCString(intf_name, &intf_name_cstr)); \ | 2447 EXPECT_VALID(Dart_StringToCString(intf_name, &intf_name_cstr)); \ |
| (...skipping 2104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4503 // Lookup a class using an error class name. The error propagates. | 4552 // Lookup a class using an error class name. The error propagates. |
| 4504 func = Dart_LookupFunction(cls, Api::NewError("myerror")); | 4553 func = Dart_LookupFunction(cls, Api::NewError("myerror")); |
| 4505 EXPECT_ERROR(func, "myerror"); | 4554 EXPECT_ERROR(func, "myerror"); |
| 4506 | 4555 |
| 4507 // Lookup a class from an error library. The error propagates. | 4556 // Lookup a class from an error library. The error propagates. |
| 4508 func = Dart_LookupFunction(Api::NewError("myerror"), Dart_NewString("foo")); | 4557 func = Dart_LookupFunction(Api::NewError("myerror"), Dart_NewString("foo")); |
| 4509 EXPECT_ERROR(func, "myerror"); | 4558 EXPECT_ERROR(func, "myerror"); |
| 4510 } | 4559 } |
| 4511 | 4560 |
| 4512 | 4561 |
| 4562 TEST_CASE(TypeReflection) { |
| 4563 const char* kScriptChars = |
| 4564 "void func(String a, int b) {}\n" |
| 4565 "int variable;\n"; |
| 4566 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 4567 EXPECT_VALID(lib); |
| 4568 |
| 4569 Dart_Handle func = Dart_LookupFunction(lib, Dart_NewString("func")); |
| 4570 EXPECT_VALID(func); |
| 4571 EXPECT(Dart_IsFunction(func)); |
| 4572 |
| 4573 // Make sure parameter counts are right. |
| 4574 int64_t fixed_params = -1; |
| 4575 int64_t opt_params = -1; |
| 4576 EXPECT_VALID(Dart_FunctionParameterCounts(func, &fixed_params, &opt_params)); |
| 4577 EXPECT_EQ(2, fixed_params); |
| 4578 EXPECT_EQ(0, opt_params); |
| 4579 |
| 4580 // Check the return type. |
| 4581 Dart_Handle type = Dart_FunctionReturnType(func); |
| 4582 EXPECT_VALID(type); |
| 4583 Dart_Handle cls_name = Dart_ClassName(type); |
| 4584 EXPECT_VALID(cls_name); |
| 4585 const char* cls_name_cstr = ""; |
| 4586 EXPECT_VALID(Dart_StringToCString(cls_name, &cls_name_cstr)); |
| 4587 EXPECT_STREQ("void", cls_name_cstr); |
| 4588 |
| 4589 // Check a parameter type. |
| 4590 type = Dart_FunctionParameterType(func, 0); |
| 4591 EXPECT_VALID(type); |
| 4592 cls_name = Dart_ClassName(type); |
| 4593 EXPECT_VALID(cls_name); |
| 4594 cls_name_cstr = ""; |
| 4595 EXPECT_VALID(Dart_StringToCString(cls_name, &cls_name_cstr)); |
| 4596 EXPECT_STREQ("String", cls_name_cstr); |
| 4597 |
| 4598 Dart_Handle var = Dart_LookupVariable(lib, Dart_NewString("variable")); |
| 4599 EXPECT_VALID(var); |
| 4600 EXPECT(Dart_IsVariable(var)); |
| 4601 |
| 4602 // Check the variable type. |
| 4603 type = Dart_VariableType(var); |
| 4604 EXPECT_VALID(type); |
| 4605 cls_name = Dart_ClassName(type); |
| 4606 EXPECT_VALID(cls_name); |
| 4607 cls_name_cstr = ""; |
| 4608 EXPECT_VALID(Dart_StringToCString(cls_name, &cls_name_cstr)); |
| 4609 if (FLAG_enable_type_checks) { |
| 4610 EXPECT_STREQ("int", cls_name_cstr); |
| 4611 } else { |
| 4612 EXPECT_STREQ("Dynamic", cls_name_cstr); |
| 4613 } |
| 4614 } |
| 4615 |
| 4616 |
| 4513 static void BuildVariableDescription(TextBuffer* buffer, Dart_Handle var) { | 4617 static void BuildVariableDescription(TextBuffer* buffer, Dart_Handle var) { |
| 4514 buffer->Clear(); | 4618 buffer->Clear(); |
| 4515 Dart_Handle name = Dart_VariableName(var); | 4619 Dart_Handle name = Dart_VariableName(var); |
| 4516 EXPECT_VALID(name); | 4620 EXPECT_VALID(name); |
| 4517 const char* name_cstr = ""; | 4621 const char* name_cstr = ""; |
| 4518 EXPECT_VALID(Dart_StringToCString(name, &name_cstr)); | 4622 EXPECT_VALID(Dart_StringToCString(name, &name_cstr)); |
| 4519 bool is_static = false; | 4623 bool is_static = false; |
| 4520 bool is_final = false; | 4624 bool is_final = false; |
| 4521 EXPECT_VALID(Dart_VariableIsStatic(var, &is_static)); | 4625 EXPECT_VALID(Dart_VariableIsStatic(var, &is_static)); |
| 4522 EXPECT_VALID(Dart_VariableIsFinal(var, &is_final)); | 4626 EXPECT_VALID(Dart_VariableIsFinal(var, &is_final)); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4648 // Lookup a class from an error library. The error propagates. | 4752 // Lookup a class from an error library. The error propagates. |
| 4649 var = Dart_LookupVariable(Api::NewError("myerror"), Dart_NewString("foo")); | 4753 var = Dart_LookupVariable(Api::NewError("myerror"), Dart_NewString("foo")); |
| 4650 EXPECT_ERROR(var, "myerror"); | 4754 EXPECT_ERROR(var, "myerror"); |
| 4651 | 4755 |
| 4652 // Lookup a class using an error class name. The error propagates. | 4756 // Lookup a class using an error class name. The error propagates. |
| 4653 var = Dart_LookupVariable(lib, Api::NewError("myerror")); | 4757 var = Dart_LookupVariable(lib, Api::NewError("myerror")); |
| 4654 EXPECT_ERROR(var, "myerror"); | 4758 EXPECT_ERROR(var, "myerror"); |
| 4655 } | 4759 } |
| 4656 | 4760 |
| 4657 | 4761 |
| 4762 TEST_CASE(TypeVariableReflection) { |
| 4763 const char* kScriptChars = |
| 4764 "interface UpperBound {}\n" |
| 4765 "class GenericClass<U, T extends UpperBound> {\n" |
| 4766 " T func1() { return null; }\n" |
| 4767 " U func2() { return null; }\n" |
| 4768 "}\n"; |
| 4769 |
| 4770 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 4771 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("GenericClass")); |
| 4772 EXPECT_VALID(cls); |
| 4773 |
| 4774 // Test Dart_GetTypeVariableNames. |
| 4775 Dart_Handle names = Dart_GetTypeVariableNames(cls); |
| 4776 EXPECT_VALID(names); |
| 4777 Dart_Handle names_str = Dart_ToString(names); |
| 4778 EXPECT_VALID(names_str); |
| 4779 const char* cstr = ""; |
| 4780 EXPECT_VALID(Dart_StringToCString(names_str, &cstr)); |
| 4781 EXPECT_STREQ("[U, T]", cstr); |
| 4782 |
| 4783 // Test variable U. |
| 4784 Dart_Handle type_var = Dart_LookupTypeVariable(cls, Dart_NewString("U")); |
| 4785 EXPECT_VALID(type_var); |
| 4786 EXPECT(Dart_IsTypeVariable(type_var)); |
| 4787 Dart_Handle type_var_name = Dart_TypeVariableName(type_var); |
| 4788 EXPECT_VALID(type_var_name); |
| 4789 EXPECT_VALID(Dart_StringToCString(type_var_name, &cstr)); |
| 4790 EXPECT_STREQ("U", cstr); |
| 4791 Dart_Handle type_var_owner = Dart_TypeVariableOwner(type_var); |
| 4792 EXPECT_VALID(type_var_owner); |
| 4793 EXPECT(Dart_IdentityEquals(cls, type_var_owner)); |
| 4794 Dart_Handle type_var_bound = Dart_TypeVariableUpperBound(type_var); |
| 4795 Dart_Handle bound_name = Dart_ClassName(type_var_bound); |
| 4796 EXPECT_VALID(Dart_StringToCString(bound_name, &cstr)); |
| 4797 EXPECT_STREQ("Object", cstr); |
| 4798 |
| 4799 // Test variable T. |
| 4800 type_var = Dart_LookupTypeVariable(cls, Dart_NewString("T")); |
| 4801 EXPECT_VALID(type_var); |
| 4802 EXPECT(Dart_IsTypeVariable(type_var)); |
| 4803 type_var_name = Dart_TypeVariableName(type_var); |
| 4804 EXPECT_VALID(type_var_name); |
| 4805 EXPECT_VALID(Dart_StringToCString(type_var_name, &cstr)); |
| 4806 EXPECT_STREQ("T", cstr); |
| 4807 type_var_owner = Dart_TypeVariableOwner(type_var); |
| 4808 EXPECT_VALID(type_var_owner); |
| 4809 EXPECT(Dart_IdentityEquals(cls, type_var_owner)); |
| 4810 type_var_bound = Dart_TypeVariableUpperBound(type_var); |
| 4811 bound_name = Dart_ClassName(type_var_bound); |
| 4812 EXPECT_VALID(Dart_StringToCString(bound_name, &cstr)); |
| 4813 EXPECT_STREQ("UpperBound", cstr); |
| 4814 } |
| 4815 |
| 4816 |
| 4658 TEST_CASE(InstanceOf) { | 4817 TEST_CASE(InstanceOf) { |
| 4659 const char* kScriptChars = | 4818 const char* kScriptChars = |
| 4660 "class OtherClass {\n" | 4819 "class OtherClass {\n" |
| 4661 " static returnNull() { return null; }\n" | 4820 " static returnNull() { return null; }\n" |
| 4662 "}\n" | 4821 "}\n" |
| 4663 "class InstanceOfTest {\n" | 4822 "class InstanceOfTest {\n" |
| 4664 " InstanceOfTest() {}\n" | 4823 " InstanceOfTest() {}\n" |
| 4665 " static InstanceOfTest testMain() {\n" | 4824 " static InstanceOfTest testMain() {\n" |
| 4666 " return new InstanceOfTest();\n" | 4825 " return new InstanceOfTest();\n" |
| 4667 " }\n" | 4826 " }\n" |
| (...skipping 1824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6492 EXPECT(Dart_IsString(str)); | 6651 EXPECT(Dart_IsString(str)); |
| 6493 len = -1; | 6652 len = -1; |
| 6494 EXPECT_VALID(Dart_StringLength(str, &len)); | 6653 EXPECT_VALID(Dart_StringLength(str, &len)); |
| 6495 EXPECT_EQ(0, len); | 6654 EXPECT_EQ(0, len); |
| 6496 } | 6655 } |
| 6497 | 6656 |
| 6498 | 6657 |
| 6499 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 6658 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 6500 | 6659 |
| 6501 } // namespace dart | 6660 } // namespace dart |
| OLD | NEW |