| 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" |
| (...skipping 2591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2602 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Fields")); | 2602 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Fields")); |
| 2603 EXPECT_VALID(cls); | 2603 EXPECT_VALID(cls); |
| 2604 Dart_Handle instance = Dart_Invoke(lib, Dart_NewString("test"), 0, NULL); | 2604 Dart_Handle instance = Dart_Invoke(lib, Dart_NewString("test"), 0, NULL); |
| 2605 EXPECT_VALID(instance); | 2605 EXPECT_VALID(instance); |
| 2606 Dart_Handle name; | 2606 Dart_Handle name; |
| 2607 | 2607 |
| 2608 // Load imported lib. | 2608 // Load imported lib. |
| 2609 Dart_Handle url = Dart_NewString("library_url"); | 2609 Dart_Handle url = Dart_NewString("library_url"); |
| 2610 Dart_Handle source = Dart_NewString(kImportedScriptChars); | 2610 Dart_Handle source = Dart_NewString(kImportedScriptChars); |
| 2611 Dart_Handle imported_lib = Dart_LoadLibrary(url, source); | 2611 Dart_Handle imported_lib = Dart_LoadLibrary(url, source); |
| 2612 Dart_Handle prefix = Dart_NewString(""); |
| 2612 EXPECT_VALID(imported_lib); | 2613 EXPECT_VALID(imported_lib); |
| 2613 Dart_Handle result = Dart_LibraryImportLibrary(lib, imported_lib); | 2614 Dart_Handle result = Dart_LibraryImportLibrary(lib, imported_lib, prefix); |
| 2614 EXPECT_VALID(result); | 2615 EXPECT_VALID(result); |
| 2615 result = Dart_Invoke(imported_lib, Dart_NewString("test2"), 0, NULL); | 2616 result = Dart_Invoke(imported_lib, Dart_NewString("test2"), 0, NULL); |
| 2616 EXPECT_VALID(result); | 2617 EXPECT_VALID(result); |
| 2617 | 2618 |
| 2618 // Instance field. | 2619 // Instance field. |
| 2619 name = Dart_NewString("instance_fld"); | 2620 name = Dart_NewString("instance_fld"); |
| 2620 TestFieldNotFound(lib, name); | 2621 TestFieldNotFound(lib, name); |
| 2621 TestFieldNotFound(cls, name); | 2622 TestFieldNotFound(cls, name); |
| 2622 TestFieldOk(instance, name, false, "instance"); | 2623 TestFieldOk(instance, name, false, "instance"); |
| 2623 | 2624 |
| (...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3693 Dart_Handle lib1 = Dart_LoadLibrary(url, source); | 3694 Dart_Handle lib1 = Dart_LoadLibrary(url, source); |
| 3694 EXPECT_VALID(lib1); | 3695 EXPECT_VALID(lib1); |
| 3695 | 3696 |
| 3696 // Load lib2 | 3697 // Load lib2 |
| 3697 url = Dart_NewString("library2_url"); | 3698 url = Dart_NewString("library2_url"); |
| 3698 source = Dart_NewString(kLibrary2Chars); | 3699 source = Dart_NewString(kLibrary2Chars); |
| 3699 Dart_Handle lib2 = Dart_LoadLibrary(url, source); | 3700 Dart_Handle lib2 = Dart_LoadLibrary(url, source); |
| 3700 EXPECT_VALID(lib2); | 3701 EXPECT_VALID(lib2); |
| 3701 | 3702 |
| 3702 // Import lib2 from lib1 | 3703 // Import lib2 from lib1 |
| 3703 Dart_Handle result = Dart_LibraryImportLibrary(lib1, lib2); | 3704 Dart_Handle result = Dart_LibraryImportLibrary(lib1, lib2, Dart_Null()); |
| 3704 EXPECT_VALID(result); | 3705 EXPECT_VALID(result); |
| 3705 | 3706 |
| 3706 // We can invoke both private and non-private local functions. | 3707 // We can invoke both private and non-private local functions. |
| 3707 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("local"), 0, NULL)); | 3708 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("local"), 0, NULL)); |
| 3708 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("_local"), 0, NULL)); | 3709 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("_local"), 0, NULL)); |
| 3709 | 3710 |
| 3710 // We can only invoke non-private imported functions. | 3711 // We can only invoke non-private imported functions. |
| 3711 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("imported"), 0, NULL)); | 3712 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("imported"), 0, NULL)); |
| 3712 EXPECT_ERROR(Dart_Invoke(lib1, Dart_NewString("_imported"), 0, NULL), | 3713 EXPECT_ERROR(Dart_Invoke(lib1, Dart_NewString("_imported"), 0, NULL), |
| 3713 "did not find top-level function '_imported'"); | 3714 "did not find top-level function '_imported'"); |
| (...skipping 1525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5239 Dart_Handle url = Dart_NewString("library1_url"); | 5240 Dart_Handle url = Dart_NewString("library1_url"); |
| 5240 Dart_Handle source = Dart_NewString(kLibrary1Chars); | 5241 Dart_Handle source = Dart_NewString(kLibrary1Chars); |
| 5241 Dart_Handle lib1 = Dart_LoadLibrary(url, source); | 5242 Dart_Handle lib1 = Dart_LoadLibrary(url, source); |
| 5242 EXPECT_VALID(lib1); | 5243 EXPECT_VALID(lib1); |
| 5243 | 5244 |
| 5244 url = Dart_NewString("library2_url"); | 5245 url = Dart_NewString("library2_url"); |
| 5245 source = Dart_NewString(kLibrary2Chars); | 5246 source = Dart_NewString(kLibrary2Chars); |
| 5246 Dart_Handle lib2 = Dart_LoadLibrary(url, source); | 5247 Dart_Handle lib2 = Dart_LoadLibrary(url, source); |
| 5247 EXPECT_VALID(lib2); | 5248 EXPECT_VALID(lib2); |
| 5248 | 5249 |
| 5249 result = Dart_LibraryImportLibrary(Dart_Null(), lib2); | 5250 result = Dart_LibraryImportLibrary(Dart_Null(), lib2, Dart_Null()); |
| 5250 EXPECT(Dart_IsError(result)); | 5251 EXPECT(Dart_IsError(result)); |
| 5251 EXPECT_STREQ( | 5252 EXPECT_STREQ( |
| 5252 "Dart_LibraryImportLibrary expects argument 'library' to be non-null.", | 5253 "Dart_LibraryImportLibrary expects argument 'library' to be non-null.", |
| 5253 Dart_GetError(result)); | 5254 Dart_GetError(result)); |
| 5254 | 5255 |
| 5255 result = Dart_LibraryImportLibrary(Dart_True(), lib2); | 5256 result = Dart_LibraryImportLibrary(Dart_True(), lib2, Dart_Null()); |
| 5256 EXPECT(Dart_IsError(result)); | 5257 EXPECT(Dart_IsError(result)); |
| 5257 EXPECT_STREQ("Dart_LibraryImportLibrary expects argument 'library' to be of " | 5258 EXPECT_STREQ("Dart_LibraryImportLibrary expects argument 'library' to be of " |
| 5258 "type Library.", | 5259 "type Library.", |
| 5259 Dart_GetError(result)); | 5260 Dart_GetError(result)); |
| 5260 | 5261 |
| 5261 result = Dart_LibraryImportLibrary(error, lib2); | 5262 result = Dart_LibraryImportLibrary(error, lib2, Dart_Null()); |
| 5262 EXPECT(Dart_IsError(result)); | 5263 EXPECT(Dart_IsError(result)); |
| 5263 EXPECT_STREQ("incoming error", Dart_GetError(result)); | 5264 EXPECT_STREQ("incoming error", Dart_GetError(result)); |
| 5264 | 5265 |
| 5265 result = Dart_LibraryImportLibrary(lib1, Dart_Null()); | 5266 result = Dart_LibraryImportLibrary(lib1, Dart_Null(), Dart_Null()); |
| 5266 EXPECT(Dart_IsError(result)); | 5267 EXPECT(Dart_IsError(result)); |
| 5267 EXPECT_STREQ( | 5268 EXPECT_STREQ( |
| 5268 "Dart_LibraryImportLibrary expects argument 'import' to be non-null.", | 5269 "Dart_LibraryImportLibrary expects argument 'import' to be non-null.", |
| 5269 Dart_GetError(result)); | 5270 Dart_GetError(result)); |
| 5270 | 5271 |
| 5271 result = Dart_LibraryImportLibrary(lib1, Dart_True()); | 5272 result = Dart_LibraryImportLibrary(lib1, Dart_True(), Dart_Null()); |
| 5272 EXPECT(Dart_IsError(result)); | 5273 EXPECT(Dart_IsError(result)); |
| 5273 EXPECT_STREQ("Dart_LibraryImportLibrary expects argument 'import' to be of " | 5274 EXPECT_STREQ("Dart_LibraryImportLibrary expects argument 'import' to be of " |
| 5274 "type Library.", | 5275 "type Library.", |
| 5275 Dart_GetError(result)); | 5276 Dart_GetError(result)); |
| 5276 | 5277 |
| 5277 result = Dart_LibraryImportLibrary(lib1, error); | 5278 result = Dart_LibraryImportLibrary(lib1, error, Dart_Null()); |
| 5278 EXPECT(Dart_IsError(result)); | 5279 EXPECT(Dart_IsError(result)); |
| 5279 EXPECT_STREQ("incoming error", Dart_GetError(result)); | 5280 EXPECT_STREQ("incoming error", Dart_GetError(result)); |
| 5280 | 5281 |
| 5281 result = Dart_LibraryImportLibrary(lib1, lib2); | 5282 result = Dart_LibraryImportLibrary(lib1, lib2, Dart_Null()); |
| 5282 EXPECT_VALID(result); | 5283 EXPECT_VALID(result); |
| 5283 } | 5284 } |
| 5284 | 5285 |
| 5285 | 5286 |
| 5287 TEST_CASE(ImportLibraryWithPrefix) { |
| 5288 const char* kLibrary1Chars = |
| 5289 "#library('library1_name');" |
| 5290 "int bar() => 42;"; |
| 5291 Dart_Handle url1 = Dart_NewString("library1_url"); |
| 5292 Dart_Handle source1 = Dart_NewString(kLibrary1Chars); |
| 5293 Dart_Handle lib1 = Dart_LoadLibrary(url1, source1); |
| 5294 EXPECT_VALID(lib1); |
| 5295 EXPECT(Dart_IsLibrary(lib1)); |
| 5296 |
| 5297 const char* kLibrary2Chars = |
| 5298 "#library('library2_name');" |
| 5299 "int foobar() => foo.bar();"; |
| 5300 Dart_Handle url2 = Dart_NewString("library2_url"); |
| 5301 Dart_Handle source2 = Dart_NewString(kLibrary2Chars); |
| 5302 Dart_Handle lib2 = Dart_LoadLibrary(url2, source2); |
| 5303 EXPECT_VALID(lib2); |
| 5304 EXPECT(Dart_IsLibrary(lib2)); |
| 5305 |
| 5306 Dart_Handle prefix = Dart_NewString("foo"); |
| 5307 Dart_Handle result = Dart_LibraryImportLibrary(lib2, lib1, prefix); |
| 5308 EXPECT_VALID(result); |
| 5309 |
| 5310 // Lib1 is imported under a library prefix and therefore 'foo' should |
| 5311 // not be found directly in lib2. |
| 5312 Dart_Handle method_name = Dart_NewString("foo"); |
| 5313 result = Dart_Invoke(lib2, method_name, 0, NULL); |
| 5314 EXPECT_ERROR(result, "Dart_Invoke: did not find top-level function 'foo'"); |
| 5315 |
| 5316 // Check that lib1 is available under the prefix in lib2. |
| 5317 method_name = Dart_NewString("foobar"); |
| 5318 result = Dart_Invoke(lib2, method_name, 0, NULL); |
| 5319 EXPECT_VALID(result); |
| 5320 EXPECT(Dart_IsInteger(result)); |
| 5321 int64_t value = 0; |
| 5322 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); |
| 5323 EXPECT_EQ(42, value); |
| 5324 } |
| 5325 |
| 5326 |
| 5286 | 5327 |
| 5287 TEST_CASE(LoadLibrary) { | 5328 TEST_CASE(LoadLibrary) { |
| 5288 const char* kLibrary1Chars = | 5329 const char* kLibrary1Chars = |
| 5289 "#library('library1_name');"; | 5330 "#library('library1_name');"; |
| 5290 Dart_Handle error = Dart_Error("incoming error"); | 5331 Dart_Handle error = Dart_Error("incoming error"); |
| 5291 Dart_Handle result; | 5332 Dart_Handle result; |
| 5292 | 5333 |
| 5293 Dart_Handle url = Dart_NewString("library1_url"); | 5334 Dart_Handle url = Dart_NewString("library1_url"); |
| 5294 Dart_Handle source = Dart_NewString(kLibrary1Chars); | 5335 Dart_Handle source = Dart_NewString(kLibrary1Chars); |
| 5295 | 5336 |
| (...skipping 1227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6523 EXPECT(Dart_IsString(str)); | 6564 EXPECT(Dart_IsString(str)); |
| 6524 len = -1; | 6565 len = -1; |
| 6525 EXPECT_VALID(Dart_StringLength(str, &len)); | 6566 EXPECT_VALID(Dart_StringLength(str, &len)); |
| 6526 EXPECT_EQ(0, len); | 6567 EXPECT_EQ(0, len); |
| 6527 } | 6568 } |
| 6528 | 6569 |
| 6529 | 6570 |
| 6530 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 6571 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 6531 | 6572 |
| 6532 } // namespace dart | 6573 } // namespace dart |
| OLD | NEW |