Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: runtime/vm/dart_api_impl_test.cc

Issue 12496011: Added support for redirecting factories in dart api. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Restored comment. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 3263 matching lines...) Expand 10 before | Expand all | Expand 10 after
3274 // We now get the new value for fld2, not the initializer 3274 // We now get the new value for fld2, not the initializer
3275 result = Dart_GetField(cls, NewString("fld2")); 3275 result = Dart_GetField(cls, NewString("fld2"));
3276 EXPECT_VALID(result); 3276 EXPECT_VALID(result);
3277 result = Dart_IntegerToInt64(result, &value); 3277 result = Dart_IntegerToInt64(result, &value);
3278 EXPECT_EQ(13, value); 3278 EXPECT_EQ(13, value);
3279 } 3279 }
3280 3280
3281 3281
3282 TEST_CASE(New) { 3282 TEST_CASE(New) {
3283 const char* kScriptChars = 3283 const char* kScriptChars =
3284 "class MyClass implements MyInterface {\n" 3284 "class MyClass {\n"
3285 " MyClass() : foo = 7 {}\n" 3285 " MyClass() : foo = 7 {}\n"
3286 " MyClass.named(value) : foo = value {}\n" 3286 " MyClass.named(value) : foo = value {}\n"
3287 " MyClass._hidden(value) : foo = -value {}\n" 3287 " MyClass._hidden(value) : foo = -value {}\n"
3288 " MyClass.exception(value) : foo = value {\n" 3288 " MyClass.exception(value) : foo = value {\n"
3289 " throw 'ConstructorDeath';\n" 3289 " throw 'ConstructorDeath';\n"
3290 " }\n" 3290 " }\n"
3291 " factory MyClass.multiply(value) {\n" 3291 " factory MyClass.multiply(value) {\n"
3292 " return new MyClass.named(value * 100);\n" 3292 " return new MyClass.named(value * 100);\n"
3293 " }\n" 3293 " }\n"
3294 " factory MyClass.nullo() {\n" 3294 " factory MyClass.nullo() {\n"
3295 " return null;\n" 3295 " return null;\n"
3296 " }\n" 3296 " }\n"
3297 " factory MyInterface.multiply(value) { // won't get called.\n"
3298 " return new MyClass.named(value * 1000);\n"
3299 " }\n"
3300 " factory MyInterface2.unused(value) {\n"
3301 " return new MyClass2(-value);\n"
3302 " }\n"
3303 " factory MyInterface2.multiply(value) {\n"
3304 " return new MyClass2(value * 10000);\n"
3305 " }\n"
3306 " var foo;\n" 3297 " var foo;\n"
3307 "}\n" 3298 "}\n"
3308 "\n" 3299 "\n"
3309 "class MyClass2 implements MyInterface2 {\n" 3300 "abstract class MyExtraHop {\n"
3310 " MyClass2(value) : bar = value {}\n" 3301 " factory MyExtraHop.hop(value) = MyClass.named;\n"
3311 " var bar;\n"
3312 "}\n" 3302 "}\n"
3313 "\n" 3303 "\n"
3314 "interface MyInterface default MyClass {\n" 3304 "abstract class MyInterface {\n"
3315 " MyInterface.named(value);\n" 3305 " factory MyInterface.named(value) = MyExtraHop.hop;\n"
3316 " MyInterface.multiply(value);\n" 3306 " factory MyInterface.multiply(value) = MyClass.multiply;\n"
3317 " MyInterface.notfound(value);\n" 3307 " MyInterface.notfound(value);\n"
3318 "}\n"
3319 "\n"
3320 "interface MyInterface2 default MyClass {\n"
3321 " MyInterface2.multiply(value);\n"
3322 " MyInterface2.notfound(value);\n"
3323 "}\n"; 3308 "}\n";
3324 3309
3325 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 3310 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3326 Dart_Handle cls = Dart_GetClass(lib, NewString("MyClass")); 3311 Dart_Handle cls = Dart_GetClass(lib, NewString("MyClass"));
3327 EXPECT_VALID(cls); 3312 EXPECT_VALID(cls);
3328 Dart_Handle cls2 = Dart_GetClass(lib, NewString("MyClass2"));
3329 EXPECT_VALID(cls2);
3330 Dart_Handle intf = Dart_GetClass(lib, NewString("MyInterface")); 3313 Dart_Handle intf = Dart_GetClass(lib, NewString("MyInterface"));
3331 EXPECT_VALID(intf); 3314 EXPECT_VALID(intf);
3332 Dart_Handle intf2 = Dart_GetClass(lib, NewString("MyInterface2"));
3333 EXPECT_VALID(intf2);
3334 Dart_Handle args[1]; 3315 Dart_Handle args[1];
3335 args[0] = Dart_NewInteger(11); 3316 args[0] = Dart_NewInteger(11);
3336 Dart_Handle bad_args[1]; 3317 Dart_Handle bad_args[1];
3337 bad_args[0] = Dart_Error("myerror"); 3318 bad_args[0] = Dart_Error("myerror");
3338 3319
3339 // Invoke the unnamed constructor. 3320 // Invoke the unnamed constructor.
3340 Dart_Handle result = Dart_New(cls, Dart_Null(), 0, NULL); 3321 Dart_Handle result = Dart_New(cls, Dart_Null(), 0, NULL);
3341 EXPECT_VALID(result); 3322 EXPECT_VALID(result);
3342 bool instanceof = false; 3323 bool instanceof = false;
3343 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); 3324 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
3426 3407
3427 // Invoke a missing constructor. 3408 // Invoke a missing constructor.
3428 result = Dart_New(cls, NewString("missing"), 1, args); 3409 result = Dart_New(cls, NewString("missing"), 1, args);
3429 EXPECT_ERROR(result, 3410 EXPECT_ERROR(result,
3430 "Dart_New: could not find constructor 'MyClass.missing'."); 3411 "Dart_New: could not find constructor 'MyClass.missing'.");
3431 3412
3432 // Invoke a constructor which throws an exception. 3413 // Invoke a constructor which throws an exception.
3433 result = Dart_New(cls, NewString("exception"), 1, args); 3414 result = Dart_New(cls, NewString("exception"), 1, args);
3434 EXPECT_ERROR(result, "ConstructorDeath"); 3415 EXPECT_ERROR(result, "ConstructorDeath");
3435 3416
3436 // MyInterface has default class MyClass. 3417 // Invoke two-hop redirecting factory constructor.
3437 //
3438 // MyClass *implements* MyInterface.
3439 //
3440 // Therefore the constructor call:
3441 //
3442 // MyInterface.foo()
3443 //
3444 // Becomes:
3445 //
3446 // MyClass.foo() from the class MyClass.
3447
3448 // Invoke an interface constructor.
3449 result = Dart_New(intf, NewString("named"), 1, args); 3418 result = Dart_New(intf, NewString("named"), 1, args);
3450 EXPECT_VALID(result); 3419 EXPECT_VALID(result);
3451 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); 3420 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3452 EXPECT(instanceof); 3421 EXPECT(instanceof);
3453 int_value = 0; 3422 int_value = 0;
3454 foo = Dart_GetField(result, NewString("foo")); 3423 foo = Dart_GetField(result, NewString("foo"));
3455 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); 3424 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
3456 EXPECT_EQ(11, int_value); 3425 EXPECT_EQ(11, int_value);
3457 3426
3458 // Invoke an interface constructor which in turn calls a factory 3427 // Invoke one-hop redirecting factory constructor.
3459 // constructor.
3460 result = Dart_New(intf, NewString("multiply"), 1, args); 3428 result = Dart_New(intf, NewString("multiply"), 1, args);
3461 EXPECT_VALID(result); 3429 EXPECT_VALID(result);
3462 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); 3430 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3463 EXPECT(instanceof); 3431 EXPECT(instanceof);
3464 int_value = 0; 3432 int_value = 0;
3465 foo = Dart_GetField(result, NewString("foo")); 3433 foo = Dart_GetField(result, NewString("foo"));
3466 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); 3434 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
3467 EXPECT_EQ(1100, int_value); 3435 EXPECT_EQ(1100, int_value);
3468 3436
3469 // Invoke a constructor that is missing in the interface but present 3437 // Invoke a constructor that is missing in the interface.
3470 // in the default class.
3471 result = Dart_New(intf, Dart_Null(), 0, NULL); 3438 result = Dart_New(intf, Dart_Null(), 0, NULL);
3472 EXPECT_ERROR(result, 3439 EXPECT_ERROR(result,
3473 "Dart_New: could not find constructor 'MyInterface.'."); 3440 "Dart_New: could not find constructor 'MyInterface.'.");
3474 3441
3475 // Invoke a constructor that is present in the interface but missing 3442 // Invoke abstract constructor that is present in the interface.
3476 // in the default class.
3477 result = Dart_New(intf, NewString("notfound"), 1, args); 3443 result = Dart_New(intf, NewString("notfound"), 1, args);
3478 EXPECT_ERROR(result,
3479 "Dart_New: could not find constructor 'MyClass.notfound'.");
3480
3481 // MyInterface2 has default class MyClass.
3482 //
3483 // MyClass *does not implement* MyInterface2.
3484 //
3485 // Therefore the constructor call:
3486 //
3487 // new MyInterface2.foo()
3488 //
3489 // Becomes:
3490 //
3491 // new MyInterface2.foo() from the class MyClass.
3492
3493 // Invoke an interface constructor which in turn calls a factory
3494 // constructor.
3495 result = Dart_New(intf2, NewString("multiply"), 1, args);
3496 EXPECT_VALID(result); 3444 EXPECT_VALID(result);
3497 EXPECT_VALID(Dart_ObjectIsType(result, cls2, &instanceof)); 3445 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3498 EXPECT(instanceof); 3446 EXPECT(!instanceof);
3499 int_value = 0;
3500 Dart_Handle bar = Dart_GetField(result, NewString("bar"));
3501 EXPECT_VALID(Dart_IntegerToInt64(bar, &int_value));
3502 EXPECT_EQ(110000, int_value);
3503
3504 // Invoke a constructor that is missing in the interface but present
3505 // in the default class.
3506 result = Dart_New(intf2, NewString("unused"), 1, args);
3507 EXPECT_ERROR(result,
3508 "Dart_New: could not find constructor 'MyInterface2.unused'.");
3509
3510 // Invoke a constructor that is present in the interface but missing
3511 // in the default class.
3512 result = Dart_New(intf2, NewString("notfound"), 1, args);
3513 EXPECT_ERROR(result,
3514 "Dart_New: could not find factory 'MyInterface2.notfound' "
3515 "in class 'MyClass'.");
3516 } 3447 }
3517 3448
3518 3449
3519 TEST_CASE(New_Issue2971) { 3450 TEST_CASE(New_Issue2971) {
3520 // Issue 2971: We were unable to use Dart_New to construct an 3451 // Issue 2971: We were unable to use Dart_New to construct an
3521 // instance of List, due to problems implementing interface 3452 // instance of List, due to problems implementing interface
3522 // factories. 3453 // factories.
3523 Dart_Handle core_lib = Dart_LookupLibrary(NewString("dart:core")); 3454 Dart_Handle core_lib = Dart_LookupLibrary(NewString("dart:core"));
3524 EXPECT_VALID(core_lib); 3455 EXPECT_VALID(core_lib);
3525 Dart_Handle list_class = Dart_GetClass(core_lib, NewString("List")); 3456 Dart_Handle list_class = Dart_GetClass(core_lib, NewString("List"));
(...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after
4762 EXPECT_ERROR(var, "myerror"); 4693 EXPECT_ERROR(var, "myerror");
4763 4694
4764 // Lookup a class using an error class name. The error propagates. 4695 // Lookup a class using an error class name. The error propagates.
4765 var = Dart_LookupVariable(lib, Api::NewError("myerror")); 4696 var = Dart_LookupVariable(lib, Api::NewError("myerror"));
4766 EXPECT_ERROR(var, "myerror"); 4697 EXPECT_ERROR(var, "myerror");
4767 } 4698 }
4768 4699
4769 4700
4770 TEST_CASE(TypeVariableReflection) { 4701 TEST_CASE(TypeVariableReflection) {
4771 const char* kScriptChars = 4702 const char* kScriptChars =
4772 "interface UpperBound {}\n" 4703 "abstract class UpperBound {}\n"
4773 "class GenericClass<U, T extends UpperBound> {\n" 4704 "class GenericClass<U, T extends UpperBound> {\n"
4774 " T func1() { return null; }\n" 4705 " T func1() { return null; }\n"
4775 " U func2() { return null; }\n" 4706 " U func2() { return null; }\n"
4776 "}\n"; 4707 "}\n";
4777 4708
4778 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 4709 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
4779 Dart_Handle cls = Dart_GetClass(lib, NewString("GenericClass")); 4710 Dart_Handle cls = Dart_GetClass(lib, NewString("GenericClass"));
4780 EXPECT_VALID(cls); 4711 EXPECT_VALID(cls);
4781 4712
4782 // Test Dart_GetTypeVariableNames. 4713 // Test Dart_GetTypeVariableNames.
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
5151 EXPECT_STREQ("library1_url", cstr); 5082 EXPECT_STREQ("library1_url", cstr);
5152 } 5083 }
5153 5084
5154 5085
5155 TEST_CASE(LibraryGetClassNames) { 5086 TEST_CASE(LibraryGetClassNames) {
5156 const char* kLibraryChars = 5087 const char* kLibraryChars =
5157 "library library_name;\n" 5088 "library library_name;\n"
5158 "\n" 5089 "\n"
5159 "class A {}\n" 5090 "class A {}\n"
5160 "class B {}\n" 5091 "class B {}\n"
5161 "interface C {}\n" 5092 "abstract class C {}\n"
5162 "class _A {}\n" 5093 "class _A {}\n"
5163 "class _B {}\n" 5094 "class _B {}\n"
5164 "interface _C {}\n" 5095 "abstract class _C {}\n"
5165 "\n" 5096 "\n"
5166 "_compare(String a, String b) => a.compareTo(b);\n" 5097 "_compare(String a, String b) => a.compareTo(b);\n"
5167 "sort(list) => list.sort(_compare);\n"; 5098 "sort(list) => list.sort(_compare);\n";
5168 5099
5169 Dart_Handle url = NewString("library_url"); 5100 Dart_Handle url = NewString("library_url");
5170 Dart_Handle source = NewString(kLibraryChars); 5101 Dart_Handle source = NewString(kLibraryChars);
5171 Dart_Handle lib = Dart_LoadLibrary(url, source); 5102 Dart_Handle lib = Dart_LoadLibrary(url, source);
5172 EXPECT_VALID(lib); 5103 EXPECT_VALID(lib);
5173 5104
5174 Dart_Handle list = Dart_LibraryGetClassNames(lib); 5105 Dart_Handle list = Dart_LibraryGetClassNames(lib);
(...skipping 835 matching lines...) Expand 10 before | Expand all | Expand 10 after
6010 Dart_LoadLibrary(url, source); 5941 Dart_LoadLibrary(url, source);
6011 5942
6012 result = Dart_Invoke(result, NewString("main"), 0, NULL); 5943 result = Dart_Invoke(result, NewString("main"), 0, NULL);
6013 EXPECT_VALID(result); 5944 EXPECT_VALID(result);
6014 } 5945 }
6015 5946
6016 5947
6017 TEST_CASE(ImportLibrary5) { 5948 TEST_CASE(ImportLibrary5) {
6018 const char* kScriptChars = 5949 const char* kScriptChars =
6019 "import 'lib.dart';\n" 5950 "import 'lib.dart';\n"
6020 "interface Y {\n" 5951 "abstract class Y {\n"
6021 " void set handler(void callback(List<int> x));\n" 5952 " void set handler(void callback(List<int> x));\n"
6022 "}\n" 5953 "}\n"
6023 "void main() {}\n"; 5954 "void main() {}\n";
6024 const char* kLibraryChars = 5955 const char* kLibraryChars =
6025 "library lib.dart;\n" 5956 "library lib.dart;\n"
6026 "interface X {\n" 5957 "abstract class X {\n"
6027 " void set handler(void callback(List<int> x));\n" 5958 " void set handler(void callback(List<int> x));\n"
6028 "}\n"; 5959 "}\n";
6029 Dart_Handle result; 5960 Dart_Handle result;
6030 5961
6031 // Create a test library and Load up a test script in it. 5962 // Create a test library and Load up a test script in it.
6032 Dart_Handle url = NewString(TestCase::url()); 5963 Dart_Handle url = NewString(TestCase::url());
6033 Dart_Handle source = NewString(kScriptChars); 5964 Dart_Handle source = NewString(kScriptChars);
6034 result = Dart_SetLibraryTagHandler(library_handler); 5965 result = Dart_SetLibraryTagHandler(library_handler);
6035 EXPECT_VALID(result); 5966 EXPECT_VALID(result);
6036 result = Dart_LoadScript(url, source); 5967 result = Dart_LoadScript(url, source);
(...skipping 1392 matching lines...) Expand 10 before | Expand all | Expand 10 after
7429 NULL); 7360 NULL);
7430 int64_t value = 0; 7361 int64_t value = 0;
7431 result = Dart_IntegerToInt64(result, &value); 7362 result = Dart_IntegerToInt64(result, &value);
7432 EXPECT_VALID(result); 7363 EXPECT_VALID(result);
7433 EXPECT_EQ(260, value); 7364 EXPECT_EQ(260, value);
7434 } 7365 }
7435 7366
7436 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 7367 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
7437 7368
7438 } // namespace dart 7369 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698