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

Side by Side Diff: vm/dart_api_impl_test.cc

Issue 10021072: Remove the deprecated method invocation and field access apis from the (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 8 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 | « vm/dart_api_impl.cc ('k') | vm/debugger_api_impl_test.cc » ('j') | 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/utils.h" 7 #include "platform/utils.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_api_state.h" 10 #include "vm/dart_api_state.h"
11 #include "vm/thread.h" 11 #include "vm/thread.h"
12 #include "vm/unit_test.h" 12 #include "vm/unit_test.h"
13 #include "vm/verifier.h" 13 #include "vm/verifier.h"
14 14
15 namespace dart { 15 namespace dart {
16 16
17 // Only ia32 and x64 can run execution tests. 17 // Only ia32 and x64 can run execution tests.
18 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) 18 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64)
19 19
20 TEST_CASE(ErrorHandles) { 20 TEST_CASE(ErrorHandles) {
21 const char* kScriptChars = 21 const char* kScriptChars =
22 "class TestClass {\n" 22 "void testMain() {\n"
23 " static void testMain() {\n" 23 " throw new Exception(\"bad news\");\n"
24 " throw new Exception(\"bad news\");\n"
25 " }\n"
26 "}\n"; 24 "}\n";
27 25
28 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 26 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
29 27
30 Dart_Handle instance = Dart_True(); 28 Dart_Handle instance = Dart_True();
31 Dart_Handle error = Api::NewError("myerror"); 29 Dart_Handle error = Api::NewError("myerror");
32 Dart_Handle exception = Dart_InvokeStatic(lib, 30 Dart_Handle exception = Dart_Invoke(lib,
33 Dart_NewString("TestClass"), 31 Dart_NewString("testMain"),
34 Dart_NewString("testMain"), 32 0,
35 0, 33 NULL);
36 NULL);
37 34
38 EXPECT(!Dart_IsError(instance)); 35 EXPECT(!Dart_IsError(instance));
39 EXPECT(Dart_IsError(error)); 36 EXPECT(Dart_IsError(error));
40 EXPECT(Dart_IsError(exception)); 37 EXPECT(Dart_IsError(exception));
41 38
42 EXPECT(!Dart_ErrorHasException(instance)); 39 EXPECT(!Dart_ErrorHasException(instance));
43 EXPECT(!Dart_ErrorHasException(error)); 40 EXPECT(!Dart_ErrorHasException(error));
44 EXPECT(Dart_ErrorHasException(exception)); 41 EXPECT(Dart_ErrorHasException(exception));
45 42
46 EXPECT_STREQ("", Dart_GetError(instance)); 43 EXPECT_STREQ("", Dart_GetError(instance));
47 EXPECT_STREQ("myerror", Dart_GetError(error)); 44 EXPECT_STREQ("myerror", Dart_GetError(error));
48 EXPECT_STREQ( 45 EXPECT_STREQ(
49 "Unhandled exception:\n" 46 "Unhandled exception:\n"
50 "Exception: bad news\n" 47 "Exception: bad news\n"
51 " 0. Function: 'TestClass.testMain' url: 'dart:test-lib' line:3 col:5\n", 48 " 0. Function: '::testMain' url: 'dart:test-lib' line:2 col:3\n",
52 Dart_GetError(exception)); 49 Dart_GetError(exception));
53 50
54 EXPECT(Dart_IsError(Dart_ErrorGetException(instance))); 51 EXPECT(Dart_IsError(Dart_ErrorGetException(instance)));
55 EXPECT(Dart_IsError(Dart_ErrorGetException(error))); 52 EXPECT(Dart_IsError(Dart_ErrorGetException(error)));
56 EXPECT_VALID(Dart_ErrorGetException(exception)); 53 EXPECT_VALID(Dart_ErrorGetException(exception));
57 54
58 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(instance))); 55 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(instance)));
59 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(error))); 56 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(error)));
60 EXPECT_VALID(Dart_ErrorGetStacktrace(exception)); 57 EXPECT_VALID(Dart_ErrorGetStacktrace(exception));
61 } 58 }
(...skipping 12 matching lines...) Expand all
74 71
75 72
76 static Dart_NativeFunction PropagateError_native_lookup( 73 static Dart_NativeFunction PropagateError_native_lookup(
77 Dart_Handle name, int argument_count) { 74 Dart_Handle name, int argument_count) {
78 return reinterpret_cast<Dart_NativeFunction>(&PropagateErrorNative); 75 return reinterpret_cast<Dart_NativeFunction>(&PropagateErrorNative);
79 } 76 }
80 77
81 78
82 TEST_CASE(Dart_PropagateError) { 79 TEST_CASE(Dart_PropagateError) {
83 const char* kScriptChars = 80 const char* kScriptChars =
84 "class Test {\n" 81 "void raiseCompileError() {\n"
85 " static void raiseCompileError() {\n" 82 " return badIdent;\n"
86 " return badIdent;\n" 83 "}\n"
87 " }\n"
88 "\n" 84 "\n"
89 " static void throwException() {\n" 85 "void throwException() {\n"
90 " throw new Exception('myException');\n" 86 " throw new Exception('myException');\n"
91 " }\n" 87 "}\n"
92 " static void nativeFunc(closure) native 'Test_nativeFunc';\n"
93 "\n" 88 "\n"
94 " static void Func1() {\n" 89 "void nativeFunc(closure) native 'Test_nativeFunc';\n"
95 " nativeFunc(() => raiseCompileError());\n"
96 " }\n"
97 "\n" 90 "\n"
98 " static void Func2() {\n" 91 "void Func1() {\n"
99 " nativeFunc(() => throwException());\n" 92 " nativeFunc(() => raiseCompileError());\n"
100 " }\n" 93 "}\n"
94 "\n"
95 "void Func2() {\n"
96 " nativeFunc(() => throwException());\n"
101 "}\n"; 97 "}\n";
102 Dart_Handle lib = TestCase::LoadTestScript( 98 Dart_Handle lib = TestCase::LoadTestScript(
103 kScriptChars, &PropagateError_native_lookup); 99 kScriptChars, &PropagateError_native_lookup);
104 Dart_Handle result; 100 Dart_Handle result;
105 101
106 result = Dart_InvokeStatic(lib, 102 result = Dart_Invoke(lib, Dart_NewString("Func1"), 0, NULL);
107 Dart_NewString("Test"),
108 Dart_NewString("Func1"),
109 0,
110 NULL);
111 EXPECT(Dart_IsError(result)); 103 EXPECT(Dart_IsError(result));
112 EXPECT(!Dart_ErrorHasException(result)); 104 EXPECT(!Dart_ErrorHasException(result));
113 EXPECT_SUBSTRING("badIdent", Dart_GetError(result)); 105 EXPECT_SUBSTRING("badIdent", Dart_GetError(result));
114 106
115 result = Dart_InvokeStatic(lib, 107 result = Dart_Invoke(lib, Dart_NewString("Func2"), 0, NULL);
116 Dart_NewString("Test"),
117 Dart_NewString("Func2"),
118 0,
119 NULL);
120 EXPECT(Dart_IsError(result)); 108 EXPECT(Dart_IsError(result));
121 EXPECT(Dart_ErrorHasException(result)); 109 EXPECT(Dart_ErrorHasException(result));
122 EXPECT_SUBSTRING("myException", Dart_GetError(result)); 110 EXPECT_SUBSTRING("myException", Dart_GetError(result));
123 } 111 }
124 112
125 #endif 113 #endif
126 114
127 115
128 TEST_CASE(Dart_Error) { 116 TEST_CASE(Dart_Error) {
129 Dart_Handle error = Dart_Error("An %s", "error"); 117 Dart_Handle error = Dart_Error("An %s", "error");
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 EXPECT_EQ(kDoubleVal2, out2); 250 EXPECT_EQ(kDoubleVal2, out2);
263 } 251 }
264 252
265 253
266 // Only ia32 and x64 can run execution tests. 254 // Only ia32 and x64 can run execution tests.
267 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) 255 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64)
268 256
269 TEST_CASE(NumberValues) { 257 TEST_CASE(NumberValues) {
270 // TODO(antonm): add various kinds of ints (smi, mint, bigint). 258 // TODO(antonm): add various kinds of ints (smi, mint, bigint).
271 const char* kScriptChars = 259 const char* kScriptChars =
272 "class NumberValuesHelper {\n" 260 "int getInt() { return 1; }\n"
273 " static int getInt() { return 1; }\n" 261 "double getDouble() { return 1.0; }\n"
274 " static double getDouble() { return 1.0; }\n" 262 "bool getBool() { return false; }\n"
275 " static bool getBool() { return false; }\n" 263 "getNull() { return null; }\n";
276 " static getNull() { return null; }\n"
277 "}\n";
278 Dart_Handle result; 264 Dart_Handle result;
279 // Create a test library and Load up a test script in it. 265 // Create a test library and Load up a test script in it.
280 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 266 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
281 267
282 Dart_Handle class_name = Dart_NewString("NumberValuesHelper");
283 // Check int case. 268 // Check int case.
284 result = Dart_InvokeStatic(lib, 269 result = Dart_Invoke(lib, Dart_NewString("getInt"), 0, NULL);
285 class_name,
286 Dart_NewString("getInt"),
287 0,
288 NULL);
289 EXPECT_VALID(result); 270 EXPECT_VALID(result);
290 EXPECT(Dart_IsNumber(result)); 271 EXPECT(Dart_IsNumber(result));
291 272
292 // Check double case. 273 // Check double case.
293 result = Dart_InvokeStatic(lib, 274 result = Dart_Invoke(lib, Dart_NewString("getDouble"), 0, NULL);
294 class_name,
295 Dart_NewString("getDouble"),
296 0,
297 NULL);
298 EXPECT_VALID(result); 275 EXPECT_VALID(result);
299 EXPECT(Dart_IsNumber(result)); 276 EXPECT(Dart_IsNumber(result));
300 277
301 // Check bool case. 278 // Check bool case.
302 result = Dart_InvokeStatic(lib, 279 result = Dart_Invoke(lib, Dart_NewString("getBool"), 0, NULL);
303 class_name,
304 Dart_NewString("getBool"),
305 0,
306 NULL);
307 EXPECT_VALID(result); 280 EXPECT_VALID(result);
308 EXPECT(!Dart_IsNumber(result)); 281 EXPECT(!Dart_IsNumber(result));
309 282
310 // Check null case. 283 // Check null case.
311 result = Dart_InvokeStatic(lib, 284 result = Dart_Invoke(lib, Dart_NewString("getNull"), 0, NULL);
312 class_name,
313 Dart_NewString("getNull"),
314 0,
315 NULL);
316 EXPECT_VALID(result); 285 EXPECT_VALID(result);
317 EXPECT(!Dart_IsNumber(result)); 286 EXPECT(!Dart_IsNumber(result));
318 } 287 }
319 288
320 #endif 289 #endif
321 290
322 291
323 TEST_CASE(IntegerValues) { 292 TEST_CASE(IntegerValues) {
324 const int64_t kIntegerVal1 = 100; 293 const int64_t kIntegerVal1 = 100;
325 const int64_t kIntegerVal2 = 0xffffffff; 294 const int64_t kIntegerVal2 = 0xffffffff;
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 EXPECT_EQ(peer32, 42); 584 EXPECT_EQ(peer32, 42);
616 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); 585 Isolate::Current()->heap()->CollectGarbage(Heap::kNew);
617 EXPECT_EQ(peer8, 80); 586 EXPECT_EQ(peer8, 80);
618 EXPECT_EQ(peer16, 82); 587 EXPECT_EQ(peer16, 82);
619 EXPECT_EQ(peer32, 84); 588 EXPECT_EQ(peer32, 84);
620 } 589 }
621 590
622 591
623 TEST_CASE(ListAccess) { 592 TEST_CASE(ListAccess) {
624 const char* kScriptChars = 593 const char* kScriptChars =
625 "class ListAccessTest {" 594 "List testMain() {"
626 " ListAccessTest() {}" 595 " List a = new List();"
627 " static List testMain() {" 596 " a.add(10);"
628 " List a = new List();" 597 " a.add(20);"
629 " a.add(10);" 598 " a.add(30);"
630 " a.add(20);" 599 " return a;"
631 " a.add(30);"
632 " return a;"
633 " }"
634 "}"; 600 "}";
635 Dart_Handle result; 601 Dart_Handle result;
636 602
637 // Create a test library and Load up a test script in it. 603 // Create a test library and Load up a test script in it.
638 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 604 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
639 605
640 // Invoke a function which returns an object of type InstanceOf.. 606 // Invoke a function which returns an object of type List.
641 result = Dart_InvokeStatic(lib, 607 result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
642 Dart_NewString("ListAccessTest"),
643 Dart_NewString("testMain"),
644 0,
645 NULL);
646 EXPECT_VALID(result); 608 EXPECT_VALID(result);
647 609
648 // First ensure that the returned object is an array. 610 // First ensure that the returned object is an array.
649 Dart_Handle ListAccessTestObj = result; 611 Dart_Handle ListAccessTestObj = result;
650 612
651 EXPECT(Dart_IsList(ListAccessTestObj)); 613 EXPECT(Dart_IsList(ListAccessTestObj));
652 614
653 // Get length of array object. 615 // Get length of array object.
654 intptr_t len = 0; 616 intptr_t len = 0;
655 result = Dart_ListLength(ListAccessTestObj, &len); 617 result = Dart_ListLength(ListAccessTestObj, &len);
(...skipping 1680 matching lines...) Expand 10 before | Expand all | Expand 10 after
2336 "var _gs_fld6;\n" 2298 "var _gs_fld6;\n"
2337 "\n" 2299 "\n"
2338 "Fields test() {\n" 2300 "Fields test() {\n"
2339 " Fields.Init();\n" 2301 " Fields.Init();\n"
2340 " top_fld = 'top';\n" 2302 " top_fld = 'top';\n"
2341 " _top_fld = 'hidden top';\n" 2303 " _top_fld = 'hidden top';\n"
2342 " top_getset_fld = 'top getset';\n" 2304 " top_getset_fld = 'top getset';\n"
2343 " _top_getset_fld = 'hidden top getset';\n" 2305 " _top_getset_fld = 'hidden top getset';\n"
2344 " return new Fields();\n" 2306 " return new Fields();\n"
2345 "}\n"; 2307 "}\n";
2308 const char* kImportedScriptChars =
2309 "#library('library_name');\n"
2310 "var imported_fld = 'imported';\n"
2311 "var _imported_fld = 'hidden imported';\n"
2312 "get imported_getset_fld() { return _gs_fld1; }\n"
2313 "void set imported_getset_fld(var value) { _gs_fld1 = value; }\n"
2314 "get _imported_getset_fld() { return _gs_fld2; }\n"
2315 "void set _imported_getset_fld(var value) { _gs_fld2 = value; }\n"
2316 "var _gs_fld1;\n"
2317 "var _gs_fld2;\n"
2318 "void test2() {\n"
2319 " imported_getset_fld = 'imported getset';\n"
2320 " _imported_getset_fld = 'hidden imported getset';\n"
2321 "}\n";
2346 2322
2347 // Shared setup. 2323 // Shared setup.
2348 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 2324 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2349 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Fields")); 2325 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Fields"));
2350 EXPECT_VALID(cls); 2326 EXPECT_VALID(cls);
2351 Dart_Handle instance = Dart_InvokeStatic(lib, 2327 Dart_Handle instance = Dart_Invoke(lib, Dart_NewString("test"), 0, NULL);
2352 Dart_Null(),
2353 Dart_NewString("test"),
2354 0,
2355 NULL);
2356 EXPECT_VALID(instance); 2328 EXPECT_VALID(instance);
2357 Dart_Handle name; 2329 Dart_Handle name;
2358 2330
2331 // Load imported lib.
2332 Dart_Handle url = Dart_NewString("library_url");
2333 Dart_Handle source = Dart_NewString(kImportedScriptChars);
2334 Dart_Handle import_map = Dart_NewList(0);
2335 Dart_Handle imported_lib = Dart_LoadLibrary(url, source, import_map);
2336 EXPECT_VALID(imported_lib);
2337 Dart_Handle result = Dart_LibraryImportLibrary(lib, imported_lib);
2338 EXPECT_VALID(result);
2339 result = Dart_Invoke(imported_lib, Dart_NewString("test2"), 0, NULL);
2340 EXPECT_VALID(result);
2341
2359 // Instance field. 2342 // Instance field.
2360 name = Dart_NewString("instance_fld"); 2343 name = Dart_NewString("instance_fld");
2361 TestFieldNotFound(lib, name); 2344 TestFieldNotFound(lib, name);
2362 TestFieldNotFound(cls, name); 2345 TestFieldNotFound(cls, name);
2363 TestFieldOk(instance, name, false, "instance"); 2346 TestFieldOk(instance, name, false, "instance");
2364 2347
2365 // Hidden instance field. 2348 // Hidden instance field.
2366 name = Dart_NewString("_instance_fld"); 2349 name = Dart_NewString("_instance_fld");
2367 TestFieldNotFound(lib, name); 2350 TestFieldNotFound(lib, name);
2368 TestFieldNotFound(cls, name); 2351 TestFieldNotFound(cls, name);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
2468 name = Dart_NewString("top_getset_fld"); 2451 name = Dart_NewString("top_getset_fld");
2469 TestFieldNotFound(cls, name); 2452 TestFieldNotFound(cls, name);
2470 TestFieldNotFound(instance, name); 2453 TestFieldNotFound(instance, name);
2471 TestFieldOk(lib, name, false, "top getset"); 2454 TestFieldOk(lib, name, false, "top getset");
2472 2455
2473 // Hidden top-level get/set field. 2456 // Hidden top-level get/set field.
2474 name = Dart_NewString("_top_getset_fld"); 2457 name = Dart_NewString("_top_getset_fld");
2475 TestFieldNotFound(cls, name); 2458 TestFieldNotFound(cls, name);
2476 TestFieldNotFound(instance, name); 2459 TestFieldNotFound(instance, name);
2477 TestFieldOk(lib, name, false, "hidden top getset"); 2460 TestFieldOk(lib, name, false, "hidden top getset");
2461
2462 // Imported top-Level field.
2463 name = Dart_NewString("imported_fld");
2464 TestFieldNotFound(cls, name);
2465 TestFieldNotFound(instance, name);
2466 TestFieldOk(lib, name, false, "imported");
2467
2468 // Hidden imported top-level field. Not found at any level.
2469 name = Dart_NewString("_imported_fld");
2470 TestFieldNotFound(cls, name);
2471 TestFieldNotFound(instance, name);
2472 TestFieldNotFound(lib, name);
2473
2474 // Imported top-Level get/set field.
2475 name = Dart_NewString("imported_getset_fld");
2476 TestFieldNotFound(cls, name);
2477 TestFieldNotFound(instance, name);
2478 TestFieldOk(lib, name, false, "imported getset");
2479
2480 // Hidden imported top-level get/set field. Not found at any level.
2481 name = Dart_NewString("_imported_getset_fld");
2482 TestFieldNotFound(cls, name);
2483 TestFieldNotFound(instance, name);
2484 TestFieldNotFound(lib, name);
2478 } 2485 }
2479 2486
2480 2487
2481 TEST_CASE(SetField_FunnyValue) { 2488 TEST_CASE(SetField_FunnyValue) {
2482 const char* kScriptChars = 2489 const char* kScriptChars =
2483 "var top;\n"; 2490 "var top;\n";
2484 2491
2485 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 2492 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2486 Dart_Handle name = Dart_NewString("top"); 2493 Dart_Handle name = Dart_NewString("top");
2487 bool value; 2494 bool value;
(...skipping 18 matching lines...) Expand all
2506 EXPECT_STREQ("Dart_SetField expects argument 'value' to be of type Instance.", 2513 EXPECT_STREQ("Dart_SetField expects argument 'value' to be of type Instance.",
2507 Dart_GetError(result)); 2514 Dart_GetError(result));
2508 2515
2509 // Pass an error handle. The error is contagious. 2516 // Pass an error handle. The error is contagious.
2510 result = Dart_SetField(lib, name, Api::NewError("myerror")); 2517 result = Dart_SetField(lib, name, Api::NewError("myerror"));
2511 EXPECT(Dart_IsError(result)); 2518 EXPECT(Dart_IsError(result));
2512 EXPECT_STREQ("myerror", Dart_GetError(result)); 2519 EXPECT_STREQ("myerror", Dart_GetError(result));
2513 } 2520 }
2514 2521
2515 2522
2516 TEST_CASE(FieldAccessOld) {
2517 const char* kScriptChars =
2518 "class Fields {\n"
2519 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n"
2520 " int fld1;\n"
2521 " final int fld2;\n"
2522 " static int fld3;\n"
2523 " static final int fld4 = 10;\n"
2524 "}\n"
2525 "class FieldsTest {\n"
2526 " static Fields testMain() {\n"
2527 " Fields obj = new Fields(10, 20);\n"
2528 " return obj;\n"
2529 " }\n"
2530 "}\n";
2531 Dart_Handle result;
2532 // Create a test library and Load up a test script in it.
2533 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2534
2535 // Invoke a function which returns an object.
2536 Dart_Handle retobj = Dart_InvokeStatic(lib,
2537 Dart_NewString("FieldsTest"),
2538 Dart_NewString("testMain"),
2539 0,
2540 NULL);
2541 EXPECT_VALID(retobj);
2542
2543 // Now access and set various static fields of Fields class.
2544 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Fields"));
2545 EXPECT_VALID(cls);
2546 result = Dart_GetStaticField(cls, Dart_NewString("fld1"));
2547 EXPECT(Dart_IsError(result));
2548 result = Dart_GetInstanceField(retobj, Dart_NewString("fld3"));
2549 EXPECT(Dart_IsError(result));
2550 result = Dart_GetStaticField(cls, Dart_NewString("fld4"));
2551 EXPECT_VALID(result);
2552 int64_t value = 0;
2553 result = Dart_IntegerToInt64(result, &value);
2554 EXPECT_EQ(10, value);
2555 result = Dart_SetStaticField(cls,
2556 Dart_NewString("fld4"),
2557 Dart_NewInteger(20));
2558 EXPECT(Dart_IsError(result));
2559 result = Dart_GetStaticField(cls, Dart_NewString("fld3"));
2560 EXPECT_VALID(result);
2561 result = Dart_SetStaticField(cls,
2562 Dart_NewString("fld3"),
2563 Dart_NewInteger(200));
2564 EXPECT_VALID(result);
2565 result = Dart_IntegerToInt64(result, &value);
2566 EXPECT_EQ(200, value);
2567
2568 // Now access and set various instance fields of the returned object.
2569 result = Dart_GetInstanceField(retobj, Dart_NewString("fld3"));
2570 EXPECT(Dart_IsError(result));
2571 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1"));
2572 EXPECT_VALID(result);
2573 result = Dart_IntegerToInt64(result, &value);
2574 EXPECT_EQ(10, value);
2575 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2"));
2576 EXPECT_VALID(result);
2577 result = Dart_IntegerToInt64(result, &value);
2578 EXPECT_EQ(20, value);
2579 result = Dart_SetInstanceField(retobj,
2580 Dart_NewString("fld2"),
2581 Dart_NewInteger(40));
2582 EXPECT(Dart_IsError(result));
2583 result = Dart_SetInstanceField(retobj,
2584 Dart_NewString("fld1"),
2585 Dart_NewInteger(40));
2586 EXPECT_VALID(result);
2587 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1"));
2588 EXPECT_VALID(result);
2589 result = Dart_IntegerToInt64(result, &value);
2590 EXPECT_EQ(40, value);
2591 }
2592
2593
2594 TEST_CASE(HiddenFieldAccess) {
2595 const char* kScriptChars =
2596 "class HiddenFields {\n"
2597 " HiddenFields(int i, int j) : _fld1 = i, _fld2 = j {}\n"
2598 " int _fld1;\n"
2599 " final int _fld2;\n"
2600 " static int _fld3;\n"
2601 " static final int _fld4 = 10;\n"
2602 "}\n"
2603 "class HiddenFieldsTest {\n"
2604 " static HiddenFields testMain() {\n"
2605 " HiddenFields obj = new HiddenFields(10, 20);\n"
2606 " return obj;\n"
2607 " }\n"
2608 "}\n";
2609 Dart_Handle result;
2610 // Load up a test script which extends the native wrapper class.
2611 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2612
2613 // Invoke a function which returns an object.
2614 Dart_Handle retobj = Dart_InvokeStatic(lib,
2615 Dart_NewString("HiddenFieldsTest"),
2616 Dart_NewString("testMain"),
2617 0,
2618 NULL);
2619 EXPECT_VALID(retobj);
2620
2621 // Now access and set various static fields of HiddenFields class.
2622 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("HiddenFields"));
2623 EXPECT_VALID(cls);
2624 result = Dart_GetStaticField(cls, Dart_NewString("_fld1"));
2625 EXPECT(Dart_IsError(result));
2626 result = Dart_GetInstanceField(retobj, Dart_NewString("_fld3"));
2627 EXPECT(Dart_IsError(result));
2628 result = Dart_GetStaticField(cls, Dart_NewString("_fld4"));
2629 EXPECT_VALID(result);
2630 int64_t value = 0;
2631 result = Dart_IntegerToInt64(result, &value);
2632 EXPECT_EQ(10, value);
2633 result = Dart_SetStaticField(cls,
2634 Dart_NewString("_fld4"),
2635 Dart_NewInteger(20));
2636 EXPECT(Dart_IsError(result));
2637 result = Dart_GetStaticField(cls, Dart_NewString("_fld3"));
2638 EXPECT_VALID(result);
2639 result = Dart_SetStaticField(cls,
2640 Dart_NewString("_fld3"),
2641 Dart_NewInteger(200));
2642 EXPECT_VALID(result);
2643 result = Dart_IntegerToInt64(result, &value);
2644 EXPECT_EQ(200, value);
2645
2646 // Now access and set various instance fields of the returned object.
2647 result = Dart_GetInstanceField(retobj, Dart_NewString("_fld3"));
2648 EXPECT(Dart_IsError(result));
2649 result = Dart_GetInstanceField(retobj, Dart_NewString("_fld1"));
2650 EXPECT_VALID(result);
2651 result = Dart_IntegerToInt64(result, &value);
2652 EXPECT_EQ(10, value);
2653 result = Dart_GetInstanceField(retobj, Dart_NewString("_fld2"));
2654 EXPECT_VALID(result);
2655 result = Dart_IntegerToInt64(result, &value);
2656 EXPECT_EQ(20, value);
2657 result = Dart_SetInstanceField(retobj,
2658 Dart_NewString("_fld2"),
2659 Dart_NewInteger(40));
2660 EXPECT(Dart_IsError(result));
2661 result = Dart_SetInstanceField(retobj,
2662 Dart_NewString("_fld1"),
2663 Dart_NewInteger(40));
2664 EXPECT_VALID(result);
2665 result = Dart_GetInstanceField(retobj, Dart_NewString("_fld1"));
2666 EXPECT_VALID(result);
2667 result = Dart_IntegerToInt64(result, &value);
2668 EXPECT_EQ(40, value);
2669 }
2670
2671
2672 void NativeFieldLookup(Dart_NativeArguments args) { 2523 void NativeFieldLookup(Dart_NativeArguments args) {
2673 UNREACHABLE(); 2524 UNREACHABLE();
2674 } 2525 }
2675 2526
2676 2527
2677 static Dart_NativeFunction native_field_lookup(Dart_Handle name, 2528 static Dart_NativeFunction native_field_lookup(Dart_Handle name,
2678 int argument_count) { 2529 int argument_count) {
2679 return reinterpret_cast<Dart_NativeFunction>(&NativeFieldLookup); 2530 return reinterpret_cast<Dart_NativeFunction>(&NativeFieldLookup);
2680 } 2531 }
2681 2532
2682 2533
2683 TEST_CASE(InjectNativeFields1) { 2534 TEST_CASE(InjectNativeFields1) {
2684 const char* kScriptChars = 2535 const char* kScriptChars =
2685 "class NativeFields extends NativeFieldsWrapper {\n" 2536 "class NativeFields extends NativeFieldsWrapper {\n"
2686 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 2537 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
2687 " int fld1;\n" 2538 " int fld1;\n"
2688 " final int fld2;\n" 2539 " final int fld2;\n"
2689 " static int fld3;\n" 2540 " static int fld3;\n"
2690 " static final int fld4 = 10;\n" 2541 " static final int fld4 = 10;\n"
2691 "}\n" 2542 "}\n"
2692 "class NativeFieldsTest {\n" 2543 "NativeFields testMain() {\n"
2693 " static NativeFields testMain() {\n" 2544 " NativeFields obj = new NativeFields(10, 20);\n"
2694 " NativeFields obj = new NativeFields(10, 20);\n" 2545 " return obj;\n"
2695 " return obj;\n"
2696 " }\n"
2697 "}\n"; 2546 "}\n";
2698 Dart_Handle result; 2547 Dart_Handle result;
2699 2548
2700 const int kNumNativeFields = 4; 2549 const int kNumNativeFields = 4;
2701 2550
2702 // Create a test library. 2551 // Create a test library.
2703 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 2552 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
2704 native_field_lookup); 2553 native_field_lookup);
2705 2554
2706 // Create a native wrapper class with native fields. 2555 // Create a native wrapper class with native fields.
2707 result = Dart_CreateNativeWrapperClass( 2556 result = Dart_CreateNativeWrapperClass(
2708 lib, 2557 lib,
2709 Dart_NewString("NativeFieldsWrapper"), 2558 Dart_NewString("NativeFieldsWrapper"),
2710 kNumNativeFields); 2559 kNumNativeFields);
2711 2560
2712 // Load up a test script in the test library. 2561 // Load up a test script in the test library.
2713 2562
2714 // Invoke a function which returns an object of type NativeFields. 2563 // Invoke a function which returns an object of type NativeFields.
2715 result = Dart_InvokeStatic(lib, 2564 result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
2716 Dart_NewString("NativeFieldsTest"),
2717 Dart_NewString("testMain"),
2718 0,
2719 NULL);
2720 EXPECT_VALID(result); 2565 EXPECT_VALID(result);
2721 DARTSCOPE_NOCHECKS(Isolate::Current()); 2566 DARTSCOPE_NOCHECKS(Isolate::Current());
2722 Instance& obj = Instance::Handle(); 2567 Instance& obj = Instance::Handle();
2723 obj ^= Api::UnwrapHandle(result); 2568 obj ^= Api::UnwrapHandle(result);
2724 const Class& cls = Class::Handle(obj.clazz()); 2569 const Class& cls = Class::Handle(obj.clazz());
2725 // We expect the newly created "NativeFields" object to have 2570 // We expect the newly created "NativeFields" object to have
2726 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. 2571 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields.
2727 // Hence the size of an instance of "NativeFields" should be 2572 // Hence the size of an instance of "NativeFields" should be
2728 // (kNumNativeFields + 2) * kWordSize + size of object header. 2573 // (kNumNativeFields + 2) * kWordSize + size of object header.
2729 // We check to make sure the instance size computed by the VM matches 2574 // We check to make sure the instance size computed by the VM matches
2730 // our expectations. 2575 // our expectations.
2731 intptr_t header_size = sizeof(RawObject); 2576 intptr_t header_size = sizeof(RawObject);
2732 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + header_size, 2577 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + header_size,
2733 kObjectAlignment), 2578 kObjectAlignment),
2734 cls.instance_size()); 2579 cls.instance_size());
2735 } 2580 }
2736 2581
2737 2582
2738 TEST_CASE(InjectNativeFields2) { 2583 TEST_CASE(InjectNativeFields2) {
2739 const char* kScriptChars = 2584 const char* kScriptChars =
2740 "class NativeFields extends NativeFieldsWrapper {\n" 2585 "class NativeFields extends NativeFieldsWrapper {\n"
2741 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 2586 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
2742 " int fld1;\n" 2587 " int fld1;\n"
2743 " final int fld2;\n" 2588 " final int fld2;\n"
2744 " static int fld3;\n" 2589 " static int fld3;\n"
2745 " static final int fld4 = 10;\n" 2590 " static final int fld4 = 10;\n"
2746 "}\n" 2591 "}\n"
2747 "class NativeFieldsTest {\n" 2592 "NativeFields testMain() {\n"
2748 " static NativeFields testMain() {\n" 2593 " NativeFields obj = new NativeFields(10, 20);\n"
2749 " NativeFields obj = new NativeFields(10, 20);\n" 2594 " return obj;\n"
2750 " return obj;\n"
2751 " }\n"
2752 "}\n"; 2595 "}\n";
2753 Dart_Handle result; 2596 Dart_Handle result;
2754 // Create a test library and Load up a test script in it. 2597 // Create a test library and Load up a test script in it.
2755 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 2598 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2756 2599
2757 // Invoke a function which returns an object of type NativeFields. 2600 // Invoke a function which returns an object of type NativeFields.
2758 result = Dart_InvokeStatic(lib, 2601 result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
2759 Dart_NewString("NativeFieldsTest"), 2602
2760 Dart_NewString("testMain"),
2761 0,
2762 NULL);
2763 // We expect this to fail as class "NativeFields" extends 2603 // We expect this to fail as class "NativeFields" extends
2764 // "NativeFieldsWrapper" and there is no definition of it either 2604 // "NativeFieldsWrapper" and there is no definition of it either
2765 // in the dart code or through the native field injection mechanism. 2605 // in the dart code or through the native field injection mechanism.
2766 EXPECT(Dart_IsError(result)); 2606 EXPECT(Dart_IsError(result));
2767 } 2607 }
2768 2608
2769 2609
2770 TEST_CASE(InjectNativeFields3) { 2610 TEST_CASE(InjectNativeFields3) {
2771 const char* kScriptChars = 2611 const char* kScriptChars =
2772 "#import('dart:nativewrappers');" 2612 "#import('dart:nativewrappers');"
2773 "class NativeFields extends NativeFieldWrapperClass2 {\n" 2613 "class NativeFields extends NativeFieldWrapperClass2 {\n"
2774 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 2614 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
2775 " int fld1;\n" 2615 " int fld1;\n"
2776 " final int fld2;\n" 2616 " final int fld2;\n"
2777 " static int fld3;\n" 2617 " static int fld3;\n"
2778 " static final int fld4 = 10;\n" 2618 " static final int fld4 = 10;\n"
2779 "}\n" 2619 "}\n"
2780 "class NativeFieldsTest {\n" 2620 "NativeFields testMain() {\n"
2781 " static NativeFields testMain() {\n" 2621 " NativeFields obj = new NativeFields(10, 20);\n"
2782 " NativeFields obj = new NativeFields(10, 20);\n" 2622 " return obj;\n"
2783 " return obj;\n"
2784 " }\n"
2785 "}\n"; 2623 "}\n";
2786 Dart_Handle result; 2624 Dart_Handle result;
2787 const int kNumNativeFields = 2; 2625 const int kNumNativeFields = 2;
2788 2626
2789 // Load up a test script in the test library. 2627 // Load up a test script in the test library.
2790 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 2628 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
2791 native_field_lookup); 2629 native_field_lookup);
2792 2630
2793 // Invoke a function which returns an object of type NativeFields. 2631 // Invoke a function which returns an object of type NativeFields.
2794 result = Dart_InvokeStatic(lib, 2632 result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
2795 Dart_NewString("NativeFieldsTest"),
2796 Dart_NewString("testMain"),
2797 0,
2798 NULL);
2799 EXPECT_VALID(result); 2633 EXPECT_VALID(result);
2800 DARTSCOPE_NOCHECKS(Isolate::Current()); 2634 DARTSCOPE_NOCHECKS(Isolate::Current());
2801 Instance& obj = Instance::Handle(); 2635 Instance& obj = Instance::Handle();
2802 obj ^= Api::UnwrapHandle(result); 2636 obj ^= Api::UnwrapHandle(result);
2803 const Class& cls = Class::Handle(obj.clazz()); 2637 const Class& cls = Class::Handle(obj.clazz());
2804 // We expect the newly created "NativeFields" object to have 2638 // We expect the newly created "NativeFields" object to have
2805 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. 2639 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields.
2806 // Hence the size of an instance of "NativeFields" should be 2640 // Hence the size of an instance of "NativeFields" should be
2807 // (kNumNativeFields + 2) * kWordSize + size of object header. 2641 // (kNumNativeFields + 2) * kWordSize + size of object header.
2808 // We check to make sure the instance size computed by the VM matches 2642 // We check to make sure the instance size computed by the VM matches
2809 // our expectations. 2643 // our expectations.
2810 intptr_t header_size = sizeof(RawObject); 2644 intptr_t header_size = sizeof(RawObject);
2811 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + header_size, 2645 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + header_size,
2812 kObjectAlignment), 2646 kObjectAlignment),
2813 cls.instance_size()); 2647 cls.instance_size());
2814 } 2648 }
2815 2649
2816 2650
2817 TEST_CASE(InjectNativeFields4) { 2651 TEST_CASE(InjectNativeFields4) {
2818 const char* kScriptChars = 2652 const char* kScriptChars =
2819 "#import('dart:nativewrappers');" 2653 "#import('dart:nativewrappers');"
2820 "class NativeFields extends NativeFieldWrapperClass2 {\n" 2654 "class NativeFields extends NativeFieldWrapperClass2 {\n"
2821 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 2655 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
2822 " int fld1;\n" 2656 " int fld1;\n"
2823 " final int fld2;\n" 2657 " final int fld2;\n"
2824 " static int fld3;\n" 2658 " static int fld3;\n"
2825 " static final int fld4 = 10;\n" 2659 " static final int fld4 = 10;\n"
2826 "}\n" 2660 "}\n"
2827 "class NativeFieldsTest {\n" 2661 "NativeFields testMain() {\n"
2828 " static NativeFields testMain() {\n" 2662 " NativeFields obj = new NativeFields(10, 20);\n"
2829 " NativeFields obj = new NativeFields(10, 20);\n" 2663 " return obj;\n"
2830 " return obj;\n"
2831 " }\n"
2832 "}\n"; 2664 "}\n";
2833 Dart_Handle result; 2665 Dart_Handle result;
2834 // Load up a test script in the test library. 2666 // Load up a test script in the test library.
2835 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 2667 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2836 2668
2837 // Invoke a function which returns an object of type NativeFields. 2669 // Invoke a function which returns an object of type NativeFields.
2838 result = Dart_InvokeStatic(lib, 2670 result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
2839 Dart_NewString("NativeFieldsTest"), 2671
2840 Dart_NewString("testMain"),
2841 0,
2842 NULL);
2843 // We expect the test script to fail finalization with the error below: 2672 // We expect the test script to fail finalization with the error below:
2844 EXPECT(Dart_IsError(result)); 2673 EXPECT(Dart_IsError(result));
2845 Dart_Handle expected_error = Dart_Error( 2674 Dart_Handle expected_error = Dart_Error(
2846 "'dart:test-lib': Error: line 1 pos 38: " 2675 "'dart:test-lib': Error: line 1 pos 38: "
2847 "class 'NativeFields' is trying to extend a native fields class, " 2676 "class 'NativeFields' is trying to extend a native fields class, "
2848 "but library '%s' has no native resolvers", 2677 "but library '%s' has no native resolvers",
2849 TestCase::url()); 2678 TestCase::url());
2850 EXPECT_SUBSTRING(Dart_GetError(expected_error), Dart_GetError(result)); 2679 EXPECT_SUBSTRING(Dart_GetError(expected_error), Dart_GetError(result));
2851 } 2680 }
2852 2681
2853 2682
2854 static void TestNativeFields(Dart_Handle retobj) { 2683 static void TestNativeFields(Dart_Handle retobj) {
2855 // Access and set various instance fields of the object. 2684 // Access and set various instance fields of the object.
2856 Dart_Handle result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); 2685 Dart_Handle result = Dart_GetField(retobj, Dart_NewString("fld3"));
2857 EXPECT(Dart_IsError(result)); 2686 EXPECT(Dart_IsError(result));
2858 result = Dart_GetInstanceField(retobj, Dart_NewString("fld0")); 2687 result = Dart_GetField(retobj, Dart_NewString("fld0"));
2859 EXPECT_VALID(result); 2688 EXPECT_VALID(result);
2860 EXPECT(Dart_IsNull(result)); 2689 EXPECT(Dart_IsNull(result));
2861 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); 2690 result = Dart_GetField(retobj, Dart_NewString("fld1"));
2862 EXPECT_VALID(result); 2691 EXPECT_VALID(result);
2863 int64_t value = 0; 2692 int64_t value = 0;
2864 result = Dart_IntegerToInt64(result, &value); 2693 result = Dart_IntegerToInt64(result, &value);
2865 EXPECT_EQ(10, value); 2694 EXPECT_EQ(10, value);
2866 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); 2695 result = Dart_GetField(retobj, Dart_NewString("fld2"));
2867 EXPECT_VALID(result); 2696 EXPECT_VALID(result);
2868 result = Dart_IntegerToInt64(result, &value); 2697 result = Dart_IntegerToInt64(result, &value);
2869 EXPECT_EQ(20, value); 2698 EXPECT_EQ(20, value);
2870 result = Dart_SetInstanceField(retobj, 2699 result = Dart_SetField(retobj,
2871 Dart_NewString("fld2"), 2700 Dart_NewString("fld2"),
2872 Dart_NewInteger(40)); 2701 Dart_NewInteger(40));
2873 EXPECT(Dart_IsError(result)); 2702 EXPECT(Dart_IsError(result));
2874 result = Dart_SetInstanceField(retobj, 2703 result = Dart_SetField(retobj,
2875 Dart_NewString("fld1"), 2704 Dart_NewString("fld1"),
2876 Dart_NewInteger(40)); 2705 Dart_NewInteger(40));
2877 EXPECT_VALID(result); 2706 EXPECT_VALID(result);
2878 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); 2707 result = Dart_GetField(retobj, Dart_NewString("fld1"));
2879 EXPECT_VALID(result); 2708 EXPECT_VALID(result);
2880 result = Dart_IntegerToInt64(result, &value); 2709 result = Dart_IntegerToInt64(result, &value);
2881 EXPECT_EQ(40, value); 2710 EXPECT_EQ(40, value);
2882 2711
2883 // Now access and set various native instance fields of the returned object. 2712 // Now access and set various native instance fields of the returned object.
2884 const int kNativeFld0 = 0; 2713 const int kNativeFld0 = 0;
2885 const int kNativeFld1 = 1; 2714 const int kNativeFld1 = 1;
2886 const int kNativeFld2 = 2; 2715 const int kNativeFld2 = 2;
2887 const int kNativeFld3 = 3; 2716 const int kNativeFld3 = 3;
2888 const int kNativeFld4 = 4; 2717 const int kNativeFld4 = 4;
(...skipping 21 matching lines...) Expand all
2910 result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); 2739 result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400);
2911 EXPECT_VALID(result); 2740 EXPECT_VALID(result);
2912 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); 2741 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000);
2913 EXPECT_VALID(result); 2742 EXPECT_VALID(result);
2914 result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); 2743 result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value);
2915 EXPECT_VALID(result); 2744 EXPECT_VALID(result);
2916 EXPECT_EQ(4000, field_value); 2745 EXPECT_EQ(4000, field_value);
2917 2746
2918 // Now re-access various dart instance fields of the returned object 2747 // Now re-access various dart instance fields of the returned object
2919 // to ensure that there was no corruption while setting native fields. 2748 // to ensure that there was no corruption while setting native fields.
2920 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); 2749 result = Dart_GetField(retobj, Dart_NewString("fld1"));
2921 EXPECT_VALID(result); 2750 EXPECT_VALID(result);
2922 result = Dart_IntegerToInt64(result, &value); 2751 result = Dart_IntegerToInt64(result, &value);
2923 EXPECT_EQ(40, value); 2752 EXPECT_EQ(40, value);
2924 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); 2753 result = Dart_GetField(retobj, Dart_NewString("fld2"));
2925 EXPECT_VALID(result); 2754 EXPECT_VALID(result);
2926 result = Dart_IntegerToInt64(result, &value); 2755 result = Dart_IntegerToInt64(result, &value);
2927 EXPECT_EQ(20, value); 2756 EXPECT_EQ(20, value);
2928 } 2757 }
2929 2758
2930 2759
2931 TEST_CASE(NativeFieldAccess) { 2760 TEST_CASE(NativeFieldAccess) {
2932 const char* kScriptChars = 2761 const char* kScriptChars =
2933 "class NativeFields extends NativeFieldsWrapper {\n" 2762 "class NativeFields extends NativeFieldsWrapper {\n"
2934 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 2763 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
2935 " int fld0;\n" 2764 " int fld0;\n"
2936 " int fld1;\n" 2765 " int fld1;\n"
2937 " final int fld2;\n" 2766 " final int fld2;\n"
2938 " static int fld3;\n" 2767 " static int fld3;\n"
2939 " static final int fld4 = 10;\n" 2768 " static final int fld4 = 10;\n"
2940 "}\n" 2769 "}\n"
2941 "class NativeFieldsTest {\n" 2770 "NativeFields testMain() {\n"
2942 " static NativeFields testMain() {\n" 2771 " NativeFields obj = new NativeFields(10, 20);\n"
2943 " NativeFields obj = new NativeFields(10, 20);\n" 2772 " return obj;\n"
2944 " return obj;\n"
2945 " }\n"
2946 "}\n"; 2773 "}\n";
2947 const int kNumNativeFields = 4; 2774 const int kNumNativeFields = 4;
2948 2775
2949 // Create a test library. 2776 // Create a test library.
2950 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 2777 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
2951 native_field_lookup); 2778 native_field_lookup);
2952 2779
2953 // Create a native wrapper class with native fields. 2780 // Create a native wrapper class with native fields.
2954 Dart_Handle result = Dart_CreateNativeWrapperClass( 2781 Dart_Handle result = Dart_CreateNativeWrapperClass(
2955 lib, 2782 lib,
2956 Dart_NewString("NativeFieldsWrapper"), 2783 Dart_NewString("NativeFieldsWrapper"),
2957 kNumNativeFields); 2784 kNumNativeFields);
2958 EXPECT_VALID(result); 2785 EXPECT_VALID(result);
2959 2786
2960 // Load up a test script in it. 2787 // Load up a test script in it.
2961 2788
2962 // Invoke a function which returns an object of type NativeFields. 2789 // Invoke a function which returns an object of type NativeFields.
2963 Dart_Handle retobj = Dart_InvokeStatic(lib, 2790 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
2964 Dart_NewString("NativeFieldsTest"),
2965 Dart_NewString("testMain"),
2966 0,
2967 NULL);
2968 EXPECT_VALID(retobj); 2791 EXPECT_VALID(retobj);
2969 2792
2970 // Now access and set various instance fields of the returned object. 2793 // Now access and set various instance fields of the returned object.
2971 TestNativeFields(retobj); 2794 TestNativeFields(retobj);
2972 } 2795 }
2973 2796
2974 2797
2975 TEST_CASE(ImplicitNativeFieldAccess) { 2798 TEST_CASE(ImplicitNativeFieldAccess) {
2976 const char* kScriptChars = 2799 const char* kScriptChars =
2977 "#import('dart:nativewrappers');" 2800 "#import('dart:nativewrappers');"
2978 "class NativeFields extends NativeFieldWrapperClass4 {\n" 2801 "class NativeFields extends NativeFieldWrapperClass4 {\n"
2979 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 2802 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
2980 " int fld0;\n" 2803 " int fld0;\n"
2981 " int fld1;\n" 2804 " int fld1;\n"
2982 " final int fld2;\n" 2805 " final int fld2;\n"
2983 " static int fld3;\n" 2806 " static int fld3;\n"
2984 " static final int fld4 = 10;\n" 2807 " static final int fld4 = 10;\n"
2985 "}\n" 2808 "}\n"
2986 "class NativeFieldsTest {\n" 2809 "NativeFields testMain() {\n"
2987 " static NativeFields testMain() {\n" 2810 " NativeFields obj = new NativeFields(10, 20);\n"
2988 " NativeFields obj = new NativeFields(10, 20);\n" 2811 " return obj;\n"
2989 " return obj;\n"
2990 " }\n"
2991 "}\n"; 2812 "}\n";
2992 // Load up a test script in the test library. 2813 // Load up a test script in the test library.
2993 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 2814 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
2994 native_field_lookup); 2815 native_field_lookup);
2995 2816
2996 // Invoke a function which returns an object of type NativeFields. 2817 // Invoke a function which returns an object of type NativeFields.
2997 Dart_Handle retobj = Dart_InvokeStatic(lib, 2818 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
2998 Dart_NewString("NativeFieldsTest"),
2999 Dart_NewString("testMain"),
3000 0,
3001 NULL);
3002 EXPECT_VALID(retobj); 2819 EXPECT_VALID(retobj);
3003 2820
3004 // Now access and set various instance fields of the returned object. 2821 // Now access and set various instance fields of the returned object.
3005 TestNativeFields(retobj); 2822 TestNativeFields(retobj);
3006 } 2823 }
3007 2824
3008 2825
3009 TEST_CASE(NegativeNativeFieldAccess) { 2826 TEST_CASE(NegativeNativeFieldAccess) {
3010 const char* kScriptChars = 2827 const char* kScriptChars =
3011 "class NativeFields {\n" 2828 "class NativeFields {\n"
3012 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 2829 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
3013 " int fld1;\n" 2830 " int fld1;\n"
3014 " final int fld2;\n" 2831 " final int fld2;\n"
3015 " static int fld3;\n" 2832 " static int fld3;\n"
3016 " static final int fld4 = 10;\n" 2833 " static final int fld4 = 10;\n"
3017 "}\n" 2834 "}\n"
3018 "class NativeFieldsTest {\n" 2835 "NativeFields testMain1() {\n"
3019 " static NativeFields testMain1() {\n" 2836 " NativeFields obj = new NativeFields(10, 20);\n"
3020 " NativeFields obj = new NativeFields(10, 20);\n" 2837 " return obj;\n"
3021 " return obj;\n" 2838 "}\n"
3022 " }\n" 2839 "Function testMain2() {\n"
3023 " static Function testMain2() {\n" 2840 " return function() {};\n"
3024 " return function() {};\n"
3025 " }\n"
3026 "}\n"; 2841 "}\n";
3027 Dart_Handle result; 2842 Dart_Handle result;
3028 DARTSCOPE_NOCHECKS(Isolate::Current()); 2843 DARTSCOPE_NOCHECKS(Isolate::Current());
3029 2844
3030 // Create a test library and Load up a test script in it. 2845 // Create a test library and Load up a test script in it.
3031 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 2846 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3032 2847
3033 // Invoke a function which returns an object of type NativeFields. 2848 // Invoke a function which returns an object of type NativeFields.
3034 Dart_Handle retobj = Dart_InvokeStatic(lib, 2849 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("testMain1"), 0, NULL);
3035 Dart_NewString("NativeFieldsTest"),
3036 Dart_NewString("testMain1"),
3037 0,
3038 NULL);
3039 EXPECT_VALID(retobj); 2850 EXPECT_VALID(retobj);
3040 2851
3041 // Now access and set various native instance fields of the returned object. 2852 // Now access and set various native instance fields of the returned object.
3042 // All of these tests are expected to return failure as there are no 2853 // All of these tests are expected to return failure as there are no
3043 // native fields in an instance of NativeFields. 2854 // native fields in an instance of NativeFields.
3044 const int kNativeFld0 = 0; 2855 const int kNativeFld0 = 0;
3045 const int kNativeFld1 = 1; 2856 const int kNativeFld1 = 1;
3046 const int kNativeFld2 = 2; 2857 const int kNativeFld2 = 2;
3047 const int kNativeFld3 = 3; 2858 const int kNativeFld3 = 3;
3048 const int kNativeFld4 = 4; 2859 const int kNativeFld4 = 4;
3049 intptr_t value = 0; 2860 intptr_t value = 0;
3050 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &value); 2861 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &value);
3051 EXPECT(Dart_IsError(result)); 2862 EXPECT(Dart_IsError(result));
3052 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &value); 2863 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &value);
3053 EXPECT(Dart_IsError(result)); 2864 EXPECT(Dart_IsError(result));
3054 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &value); 2865 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &value);
3055 EXPECT(Dart_IsError(result)); 2866 EXPECT(Dart_IsError(result));
3056 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &value); 2867 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &value);
3057 EXPECT(Dart_IsError(result)); 2868 EXPECT(Dart_IsError(result));
3058 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); 2869 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40);
3059 EXPECT(Dart_IsError(result)); 2870 EXPECT(Dart_IsError(result));
3060 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 40); 2871 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 40);
3061 EXPECT(Dart_IsError(result)); 2872 EXPECT(Dart_IsError(result));
3062 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 400); 2873 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 400);
3063 EXPECT(Dart_IsError(result)); 2874 EXPECT(Dart_IsError(result));
3064 2875
3065 // Invoke a function which returns a closure object. 2876 // Invoke a function which returns a closure object.
3066 retobj = Dart_InvokeStatic(lib, 2877 retobj = Dart_Invoke(lib, Dart_NewString("testMain2"), 0, NULL);
3067 Dart_NewString("NativeFieldsTest"),
3068 Dart_NewString("testMain2"),
3069 0,
3070 NULL);
3071 EXPECT_VALID(retobj); 2878 EXPECT_VALID(retobj);
2879
3072 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &value); 2880 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &value);
3073 EXPECT(Dart_IsError(result)); 2881 EXPECT(Dart_IsError(result));
3074 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &value); 2882 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &value);
3075 EXPECT(Dart_IsError(result)); 2883 EXPECT(Dart_IsError(result));
3076 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &value); 2884 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &value);
3077 EXPECT(Dart_IsError(result)); 2885 EXPECT(Dart_IsError(result));
3078 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &value); 2886 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &value);
3079 EXPECT(Dart_IsError(result)); 2887 EXPECT(Dart_IsError(result));
3080 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); 2888 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40);
3081 EXPECT(Dart_IsError(result)); 2889 EXPECT(Dart_IsError(result));
3082 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 40); 2890 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 40);
3083 EXPECT(Dart_IsError(result)); 2891 EXPECT(Dart_IsError(result));
3084 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 400); 2892 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 400);
3085 EXPECT(Dart_IsError(result)); 2893 EXPECT(Dart_IsError(result));
3086 } 2894 }
3087 2895
3088 2896
3089 TEST_CASE(GetStaticField_RunsInitializer) { 2897 TEST_CASE(GetStaticField_RunsInitializer) {
3090 const char* kScriptChars = 2898 const char* kScriptChars =
3091 "class TestClass {\n" 2899 "class TestClass {\n"
3092 " static final int fld1 = 7;\n" 2900 " static final int fld1 = 7;\n"
3093 " static int fld2 = 11;\n" 2901 " static int fld2 = 11;\n"
3094 " static void testMain() {\n" 2902 " static void testMain() {\n"
3095 " }\n" 2903 " }\n"
3096 "}\n"; 2904 "}\n";
3097 Dart_Handle result; 2905 Dart_Handle result;
3098 // Create a test library and Load up a test script in it. 2906 // Create a test library and Load up a test script in it.
3099 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 2907 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3100
3101 // Invoke a function which returns an object.
3102 result = Dart_InvokeStatic(lib,
3103 Dart_NewString("TestClass"),
3104 Dart_NewString("testMain"),
3105 0,
3106 NULL);
3107
3108 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("TestClass")); 2908 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("TestClass"));
3109 EXPECT_VALID(cls); 2909 EXPECT_VALID(cls);
3110 2910
2911 // Invoke a function which returns an object.
2912 result = Dart_Invoke(cls, Dart_NewString("testMain"), 0, NULL);
2913 EXPECT_VALID(result);
2914
3111 // For uninitialized fields, the getter is returned 2915 // For uninitialized fields, the getter is returned
3112 result = Dart_GetStaticField(cls, Dart_NewString("fld1")); 2916 result = Dart_GetField(cls, Dart_NewString("fld1"));
3113 EXPECT_VALID(result); 2917 EXPECT_VALID(result);
3114 int64_t value = 0; 2918 int64_t value = 0;
3115 result = Dart_IntegerToInt64(result, &value); 2919 result = Dart_IntegerToInt64(result, &value);
3116 EXPECT_EQ(7, value); 2920 EXPECT_EQ(7, value);
3117 2921
3118 result = Dart_GetStaticField(cls, Dart_NewString("fld2")); 2922 result = Dart_GetField(cls, Dart_NewString("fld2"));
3119 EXPECT_VALID(result); 2923 EXPECT_VALID(result);
3120 result = Dart_IntegerToInt64(result, &value); 2924 result = Dart_IntegerToInt64(result, &value);
3121 EXPECT_EQ(11, value); 2925 EXPECT_EQ(11, value);
3122 2926
3123 // Overwrite fld2 2927 // Overwrite fld2
3124 result = Dart_SetStaticField(cls, 2928 result = Dart_SetField(cls,
3125 Dart_NewString("fld2"), 2929 Dart_NewString("fld2"),
3126 Dart_NewInteger(13)); 2930 Dart_NewInteger(13));
3127 EXPECT_VALID(result); 2931 EXPECT_VALID(result);
3128 2932
3129 // We now get the new value for fld2, not the initializer 2933 // We now get the new value for fld2, not the initializer
3130 result = Dart_GetStaticField(cls, Dart_NewString("fld2")); 2934 result = Dart_GetField(cls, Dart_NewString("fld2"));
3131 EXPECT_VALID(result); 2935 EXPECT_VALID(result);
3132 result = Dart_IntegerToInt64(result, &value); 2936 result = Dart_IntegerToInt64(result, &value);
3133 EXPECT_EQ(13, value); 2937 EXPECT_EQ(13, value);
3134 } 2938 }
3135 2939
3136 2940
3137 TEST_CASE(StaticFieldNotFound) {
3138 const char* kScriptChars =
3139 "class TestClass {\n"
3140 " static void testMain() {\n"
3141 " }\n"
3142 "}\n";
3143 Dart_Handle result;
3144 // Create a test library and Load up a test script in it.
3145 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3146
3147 // Invoke a function.
3148 result = Dart_InvokeStatic(lib,
3149 Dart_NewString("TestClass"),
3150 Dart_NewString("testMain"),
3151 0,
3152 NULL);
3153
3154 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("TestClass"));
3155 EXPECT_VALID(cls);
3156
3157 result = Dart_GetStaticField(cls, Dart_NewString("not_found"));
3158 EXPECT(Dart_IsError(result));
3159 EXPECT_STREQ("Specified field is not found in the class",
3160 Dart_GetError(result));
3161
3162 result = Dart_SetStaticField(cls,
3163 Dart_NewString("not_found"),
3164 Dart_NewInteger(13));
3165 EXPECT(Dart_IsError(result));
3166 EXPECT_STREQ("Specified field is not found in the class",
3167 Dart_GetError(result));
3168 }
3169
3170
3171 TEST_CASE(Invoke) { 2941 TEST_CASE(Invoke) {
3172 const char* kScriptChars = 2942 const char* kScriptChars =
3173 "class BaseMethods {\n" 2943 "class BaseMethods {\n"
3174 " inheritedMethod(arg) => 'inherited $arg';\n" 2944 " inheritedMethod(arg) => 'inherited $arg';\n"
3175 " static nonInheritedMethod(arg) => 'noninherited $arg';\n" 2945 " static nonInheritedMethod(arg) => 'noninherited $arg';\n"
3176 "}\n" 2946 "}\n"
3177 "\n" 2947 "\n"
3178 "class Methods extends BaseMethods {\n" 2948 "class Methods extends BaseMethods {\n"
3179 " instanceMethod(arg) => 'instance $arg';\n" 2949 " instanceMethod(arg) => 'instance $arg';\n"
3180 " _instanceMethod(arg) => 'hidden instance $arg';\n" 2950 " _instanceMethod(arg) => 'hidden instance $arg';\n"
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
3317 Dart_GetError(result)); 3087 Dart_GetError(result));
3318 3088
3319 // Pass an error handle. The error is contagious. 3089 // Pass an error handle. The error is contagious.
3320 args[0] = Api::NewError("myerror"); 3090 args[0] = Api::NewError("myerror");
3321 result = Dart_Invoke(lib, Dart_NewString("test"), 1, args); 3091 result = Dart_Invoke(lib, Dart_NewString("test"), 1, args);
3322 EXPECT(Dart_IsError(result)); 3092 EXPECT(Dart_IsError(result));
3323 EXPECT_STREQ("myerror", Dart_GetError(result)); 3093 EXPECT_STREQ("myerror", Dart_GetError(result));
3324 } 3094 }
3325 3095
3326 3096
3327 TEST_CASE(InvokeDynamic) { 3097 TEST_CASE(Invoke_Null) {
3328 const char* kScriptChars = 3098 Dart_Handle result = Dart_Invoke(Dart_Null(),
3329 "class InvokeDynamic {\n" 3099 Dart_NewString("toString"),
3330 " InvokeDynamic(int i, int j) : fld1 = i, fld2 = j {}\n" 3100 0,
3331 " int method1(int i) { return i + fld1 + fld2 + fld4; }\n" 3101 NULL);
3332 " static int method2(int i) { return i + fld4; }\n" 3102 EXPECT_VALID(result);
3333 " int fld1;\n" 3103 EXPECT(Dart_IsString(result));
3334 " final int fld2;\n"
3335 " static final int fld4 = 10;\n"
3336 "}\n"
3337 "class InvokeDynamicTest {\n"
3338 " static InvokeDynamic testMain() {\n"
3339 " InvokeDynamic obj = new InvokeDynamic(10, 20);\n"
3340 " return obj;\n"
3341 " }\n"
3342 "}\n";
3343 Dart_Handle result;
3344 DARTSCOPE_NOCHECKS(Isolate::Current());
3345 3104
3346 // Create a test library and Load up a test script in it. 3105 const char* value = "";
3347 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 3106 EXPECT_VALID(Dart_StringToCString(result, &value));
3107 EXPECT_STREQ("null", value);
3348 3108
3349 // Invoke a function which returns an object of type InvokeDynamic. 3109 // Should throw a NullPointerException. Disabled due to bug 5415268.
3350 Dart_Handle retobj = Dart_InvokeStatic(lib, 3110 /*
3351 Dart_NewString("InvokeDynamicTest"), 3111 Dart_Handle function_name2 = Dart_NewString("NoNoNo");
3352 Dart_NewString("testMain"), 3112 result = Dart_Invoke(null_receiver,
3353 0, 3113 function_name2,
3354 NULL); 3114 number_of_arguments,
3355 EXPECT_VALID(retobj); 3115 dart_arguments);
3356 3116 EXPECT(Dart_IsError(result));
3357 3117 EXPECT(Dart_ErrorHasException(result)); */
3358 // Now invoke a dynamic method and check the result.
3359 Dart_Handle dart_arguments[1];
3360 dart_arguments[0] = Dart_NewInteger(1);
3361 result = Dart_InvokeDynamic(retobj,
3362 Dart_NewString("method1"),
3363 1,
3364 dart_arguments);
3365 EXPECT_VALID(result);
3366 EXPECT(Dart_IsInteger(result));
3367 int64_t value = 0;
3368 result = Dart_IntegerToInt64(result, &value);
3369 EXPECT_EQ(41, value);
3370
3371 result = Dart_InvokeDynamic(retobj, Dart_NewString("method2"), 0, NULL);
3372 EXPECT(Dart_IsError(result));
3373
3374 result = Dart_InvokeDynamic(retobj, Dart_NewString("method1"), 0, NULL);
3375 EXPECT(Dart_IsError(result));
3376 } 3118 }
3377 3119
3378 3120
3121 TEST_CASE(Invoke_CrossLibrary) {
3122 const char* kLibrary1Chars =
3123 "#library('library1_name');\n"
3124 "void local() {}\n"
3125 "void _local() {}\n";
3126 const char* kLibrary2Chars =
3127 "#library('library2_name');\n"
3128 "void imported() {}\n"
3129 "void _imported() {}\n";
3130
3131 // Load lib1
3132 Dart_Handle url = Dart_NewString("library1_url");
3133 Dart_Handle source = Dart_NewString(kLibrary1Chars);
3134 Dart_Handle import_map = Dart_NewList(0);
3135 Dart_Handle lib1 = Dart_LoadLibrary(url, source, import_map);
3136 EXPECT_VALID(lib1);
3137
3138 // Load lib2
3139 url = Dart_NewString("library2_url");
3140 source = Dart_NewString(kLibrary2Chars);
3141 Dart_Handle lib2 = Dart_LoadLibrary(url, source, import_map);
3142 EXPECT_VALID(lib2);
3143
3144 // Import lib2 from lib1
3145 Dart_Handle result = Dart_LibraryImportLibrary(lib1, lib2);
3146 EXPECT_VALID(result);
3147
3148 // We can invoke both private and non-private local functions.
3149 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("local"), 0, NULL));
3150 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("_local"), 0, NULL));
3151
3152 // We can only invoke non-private imported functions.
3153 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("imported"), 0, NULL));
3154 EXPECT_ERROR(Dart_Invoke(lib1, Dart_NewString("_imported"), 0, NULL),
3155 "did not find top-level function '_imported'");
3156 }
3157
3158
3379 TEST_CASE(InvokeClosure) { 3159 TEST_CASE(InvokeClosure) {
3380 const char* kScriptChars = 3160 const char* kScriptChars =
3381 "class InvokeClosure {\n" 3161 "class InvokeClosure {\n"
3382 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" 3162 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n"
3383 " Function method1(int i) {\n" 3163 " Function method1(int i) {\n"
3384 " f(int j) => j + i + fld1 + fld2 + fld4; \n" 3164 " f(int j) => j + i + fld1 + fld2 + fld4; \n"
3385 " return f;\n" 3165 " return f;\n"
3386 " }\n" 3166 " }\n"
3387 " static Function method2(int i) {\n" 3167 " static Function method2(int i) {\n"
3388 " n(int j) => true + i + fld4; \n" 3168 " n(int j) => true + i + fld4; \n"
3389 " return n;\n" 3169 " return n;\n"
3390 " }\n" 3170 " }\n"
3391 " int fld1;\n" 3171 " int fld1;\n"
3392 " final int fld2;\n" 3172 " final int fld2;\n"
3393 " static final int fld4 = 10;\n" 3173 " static final int fld4 = 10;\n"
3394 "}\n" 3174 "}\n"
3395 "class InvokeClosureTest {\n" 3175 "Function testMain1() {\n"
3396 " static Function testMain1() {\n" 3176 " InvokeClosure obj = new InvokeClosure(10, 20);\n"
3397 " InvokeClosure obj = new InvokeClosure(10, 20);\n" 3177 " return obj.method1(10);\n"
3398 " return obj.method1(10);\n" 3178 "}\n"
3399 " }\n" 3179 "Function testMain2() {\n"
3400 " static Function testMain2() {\n" 3180 " return InvokeClosure.method2(10);\n"
3401 " return InvokeClosure.method2(10);\n"
3402 " }\n"
3403 "}\n"; 3181 "}\n";
3404 Dart_Handle result; 3182 Dart_Handle result;
3405 DARTSCOPE_NOCHECKS(Isolate::Current()); 3183 DARTSCOPE_NOCHECKS(Isolate::Current());
3406 3184
3407 // Create a test library and Load up a test script in it. 3185 // Create a test library and Load up a test script in it.
3408 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 3186 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3409 3187
3410 // Invoke a function which returns a closure. 3188 // Invoke a function which returns a closure.
3411 Dart_Handle retobj = Dart_InvokeStatic(lib, 3189 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("testMain1"), 0, NULL);
3412 Dart_NewString("InvokeClosureTest"),
3413 Dart_NewString("testMain1"),
3414 0,
3415 NULL);
3416 EXPECT_VALID(retobj); 3190 EXPECT_VALID(retobj);
3417 3191
3418 EXPECT(Dart_IsClosure(retobj)); 3192 EXPECT(Dart_IsClosure(retobj));
3419 EXPECT(!Dart_IsClosure(Dart_NewInteger(101))); 3193 EXPECT(!Dart_IsClosure(Dart_NewInteger(101)));
3420 3194
3421 // Now invoke the closure and check the result. 3195 // Now invoke the closure and check the result.
3422 Dart_Handle dart_arguments[1]; 3196 Dart_Handle dart_arguments[1];
3423 dart_arguments[0] = Dart_NewInteger(1); 3197 dart_arguments[0] = Dart_NewInteger(1);
3424 result = Dart_InvokeClosure(retobj, 1, dart_arguments); 3198 result = Dart_InvokeClosure(retobj, 1, dart_arguments);
3425 EXPECT_VALID(result); 3199 EXPECT_VALID(result);
3426 EXPECT(Dart_IsInteger(result)); 3200 EXPECT(Dart_IsInteger(result));
3427 int64_t value = 0; 3201 int64_t value = 0;
3428 result = Dart_IntegerToInt64(result, &value); 3202 result = Dart_IntegerToInt64(result, &value);
3429 EXPECT_EQ(51, value); 3203 EXPECT_EQ(51, value);
3430 3204
3431 // Invoke closure with wrong number of args, should result in exception. 3205 // Invoke closure with wrong number of args, should result in exception.
3432 result = Dart_InvokeClosure(retobj, 0, NULL); 3206 result = Dart_InvokeClosure(retobj, 0, NULL);
3433 EXPECT(Dart_IsError(result)); 3207 EXPECT(Dart_IsError(result));
3434 EXPECT(Dart_ErrorHasException(result)); 3208 EXPECT(Dart_ErrorHasException(result));
3435 3209
3436 // Invoke a function which returns a closure. 3210 // Invoke a function which returns a closure.
3437 retobj = Dart_InvokeStatic(lib, 3211 retobj = Dart_Invoke(lib, Dart_NewString("testMain2"), 0, NULL);
3438 Dart_NewString("InvokeClosureTest"),
3439 Dart_NewString("testMain2"),
3440 0,
3441 NULL);
3442 EXPECT_VALID(retobj); 3212 EXPECT_VALID(retobj);
3443 3213
3444 EXPECT(Dart_IsClosure(retobj)); 3214 EXPECT(Dart_IsClosure(retobj));
3445 EXPECT(!Dart_IsClosure(Dart_NewString("abcdef"))); 3215 EXPECT(!Dart_IsClosure(Dart_NewString("abcdef")));
3446 3216
3447 // Now invoke the closure and check the result (should be an exception). 3217 // Now invoke the closure and check the result (should be an exception).
3448 dart_arguments[0] = Dart_NewInteger(1); 3218 dart_arguments[0] = Dart_NewInteger(1);
3449 result = Dart_InvokeClosure(retobj, 1, dart_arguments); 3219 result = Dart_InvokeClosure(retobj, 1, dart_arguments);
3450 EXPECT(Dart_IsError(result)); 3220 EXPECT(Dart_IsError(result));
3451 EXPECT(Dart_ErrorHasException(result)); 3221 EXPECT(Dart_ErrorHasException(result));
(...skipping 16 matching lines...) Expand all
3468 TEST_CASE(ThrowException) { 3238 TEST_CASE(ThrowException) {
3469 const char* kScriptChars = 3239 const char* kScriptChars =
3470 "class ThrowException {\n" 3240 "class ThrowException {\n"
3471 " ThrowException(int i) : fld1 = i {}\n" 3241 " ThrowException(int i) : fld1 = i {}\n"
3472 " int method1(int i) native \"ThrowException_native\";" 3242 " int method1(int i) native \"ThrowException_native\";"
3473 " int method2() {\n" 3243 " int method2() {\n"
3474 " try { method1(10); } catch(var a) { return 5; } return 10;\n" 3244 " try { method1(10); } catch(var a) { return 5; } return 10;\n"
3475 " }\n" 3245 " }\n"
3476 " int fld1;\n" 3246 " int fld1;\n"
3477 "}\n" 3247 "}\n"
3478 "class ThrowExceptionTest {\n" 3248 "ThrowException testMain() {\n"
3479 " static ThrowException testMain() {\n" 3249 " ThrowException obj = new ThrowException(10);\n"
3480 " ThrowException obj = new ThrowException(10);\n" 3250 " return obj;\n"
3481 " return obj;\n"
3482 " }\n"
3483 "}\n"; 3251 "}\n";
3484 Dart_Handle result; 3252 Dart_Handle result;
3485 Isolate* isolate = Isolate::Current(); 3253 Isolate* isolate = Isolate::Current();
3486 EXPECT(isolate != NULL); 3254 EXPECT(isolate != NULL);
3487 ApiState* state = isolate->api_state(); 3255 ApiState* state = isolate->api_state();
3488 EXPECT(state != NULL); 3256 EXPECT(state != NULL);
3489 intptr_t size = state->ZoneSizeInBytes(); 3257 intptr_t size = state->ZoneSizeInBytes();
3490 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. 3258 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
3491 3259
3492 // Load up a test script which extends the native wrapper class. 3260 // Load up a test script which extends the native wrapper class.
3493 Dart_Handle lib = TestCase::LoadTestScript( 3261 Dart_Handle lib = TestCase::LoadTestScript(
3494 kScriptChars, 3262 kScriptChars,
3495 reinterpret_cast<Dart_NativeEntryResolver>(native_lookup)); 3263 reinterpret_cast<Dart_NativeEntryResolver>(native_lookup));
3496 3264
3497 // Invoke a function which returns an object of type ThrowException. 3265 // Invoke a function which returns an object of type ThrowException.
3498 Dart_Handle retobj = Dart_InvokeStatic(lib, 3266 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
3499 Dart_NewString("ThrowExceptionTest"),
3500 Dart_NewString("testMain"),
3501 0,
3502 NULL);
3503 EXPECT_VALID(retobj); 3267 EXPECT_VALID(retobj);
3504 3268
3505 // Throwing an exception here should result in an error. 3269 // Throwing an exception here should result in an error.
3506 result = Dart_ThrowException(retobj); 3270 result = Dart_ThrowException(retobj);
3507 EXPECT(Dart_IsError(result)); 3271 EXPECT(Dart_IsError(result));
3508 3272
3509 // Now invoke method2 which invokes a natve method where it is 3273 // Now invoke method2 which invokes a natve method where it is
3510 // ok to throw an exception, check the result which would indicate 3274 // ok to throw an exception, check the result which would indicate
3511 // if an exception was thrown or not. 3275 // if an exception was thrown or not.
3512 result = Dart_InvokeDynamic(retobj, Dart_NewString("method2"), 0, NULL); 3276 result = Dart_Invoke(retobj, Dart_NewString("method2"), 0, NULL);
3513 EXPECT_VALID(result); 3277 EXPECT_VALID(result);
3514 EXPECT(Dart_IsInteger(result)); 3278 EXPECT(Dart_IsInteger(result));
3515 int64_t value = 0; 3279 int64_t value = 0;
3516 result = Dart_IntegerToInt64(result, &value); 3280 result = Dart_IntegerToInt64(result, &value);
3517 EXPECT_EQ(5, value); 3281 EXPECT_EQ(5, value);
3518 3282
3519 Dart_ExitScope(); // Exit the Dart API scope. 3283 Dart_ExitScope(); // Exit the Dart API scope.
3520 EXPECT_EQ(size, state->ZoneSizeInBytes()); 3284 EXPECT_EQ(size, state->ZoneSizeInBytes());
3521 } 3285 }
3522 3286
3523 3287
3524 void NativeArgumentCounter(Dart_NativeArguments args) { 3288 void NativeArgumentCounter(Dart_NativeArguments args) {
3525 Dart_EnterScope(); 3289 Dart_EnterScope();
3526 int count = Dart_GetNativeArgumentCount(args); 3290 int count = Dart_GetNativeArgumentCount(args);
3527 Dart_SetReturnValue(args, Dart_NewInteger(count)); 3291 Dart_SetReturnValue(args, Dart_NewInteger(count));
3528 Dart_ExitScope(); 3292 Dart_ExitScope();
3529 } 3293 }
3530 3294
3531 3295
3532 static Dart_NativeFunction gnac_lookup(Dart_Handle name, int argument_count) { 3296 static Dart_NativeFunction gnac_lookup(Dart_Handle name, int argument_count) {
3533 return reinterpret_cast<Dart_NativeFunction>(&NativeArgumentCounter); 3297 return reinterpret_cast<Dart_NativeFunction>(&NativeArgumentCounter);
3534 } 3298 }
3535 3299
3536 3300
3537 TEST_CASE(GetNativeArgumentCount) { 3301 TEST_CASE(GetNativeArgumentCount) {
3538 const char* kScriptChars = 3302 const char* kScriptChars =
3539 "class MyObject {" 3303 "class MyObject {"
3540 " int method1(int i, int j) native 'Name_Does_Not_Matter';" 3304 " int method1(int i, int j) native 'Name_Does_Not_Matter';"
3541 "}" 3305 "}"
3542 "class Test {" 3306 "testMain() {"
3543 " static testMain() {" 3307 " MyObject obj = new MyObject();"
3544 " MyObject obj = new MyObject();" 3308 " return obj.method1(77, 125);"
3545 " return obj.method1(77, 125);"
3546 " }"
3547 "}"; 3309 "}";
3548 3310
3549 Dart_Handle lib = TestCase::LoadTestScript( 3311 Dart_Handle lib = TestCase::LoadTestScript(
3550 kScriptChars, 3312 kScriptChars,
3551 reinterpret_cast<Dart_NativeEntryResolver>(gnac_lookup)); 3313 reinterpret_cast<Dart_NativeEntryResolver>(gnac_lookup));
3552 3314
3553 Dart_Handle result = Dart_InvokeStatic(lib, 3315 Dart_Handle result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
3554 Dart_NewString("Test"),
3555 Dart_NewString("testMain"),
3556 0,
3557 NULL);
3558 EXPECT_VALID(result); 3316 EXPECT_VALID(result);
3559 EXPECT(Dart_IsInteger(result)); 3317 EXPECT(Dart_IsInteger(result));
3560 3318
3561 int64_t value = 0; 3319 int64_t value = 0;
3562 result = Dart_IntegerToInt64(result, &value); 3320 result = Dart_IntegerToInt64(result, &value);
3563 EXPECT_VALID(result); 3321 EXPECT_VALID(result);
3564 EXPECT_EQ(3, value); 3322 EXPECT_EQ(3, value);
3565 } 3323 }
3566 3324
3567 3325
(...skipping 24 matching lines...) Expand all
3592 "class InstanceOfTest {\n" 3350 "class InstanceOfTest {\n"
3593 " InstanceOfTest() {}\n" 3351 " InstanceOfTest() {}\n"
3594 " static InstanceOfTest testMain() {\n" 3352 " static InstanceOfTest testMain() {\n"
3595 " return new InstanceOfTest();\n" 3353 " return new InstanceOfTest();\n"
3596 " }\n" 3354 " }\n"
3597 "}\n"; 3355 "}\n";
3598 Dart_Handle result; 3356 Dart_Handle result;
3599 // Create a test library and Load up a test script in it. 3357 // Create a test library and Load up a test script in it.
3600 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 3358 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3601 3359
3602 // Invoke a function which returns an object of type InstanceOf..
3603 Dart_Handle instanceOfTestObj =
3604 Dart_InvokeStatic(lib,
3605 Dart_NewString("InstanceOfTest"),
3606 Dart_NewString("testMain"),
3607 0,
3608 NULL);
3609 EXPECT_VALID(instanceOfTestObj);
3610
3611 // Fetch InstanceOfTest class. 3360 // Fetch InstanceOfTest class.
3612 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("InstanceOfTest")); 3361 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("InstanceOfTest"));
3613 EXPECT_VALID(cls); 3362 EXPECT_VALID(cls);
3614 3363
3364 // Invoke a function which returns an object of type InstanceOf..
3365 Dart_Handle instanceOfTestObj =
3366 Dart_Invoke(cls, Dart_NewString("testMain"), 0, NULL);
3367 EXPECT_VALID(instanceOfTestObj);
3368
3615 // Now check instanceOfTestObj reported as an instance of 3369 // Now check instanceOfTestObj reported as an instance of
3616 // InstanceOfTest class. 3370 // InstanceOfTest class.
3617 bool is_instance = false; 3371 bool is_instance = false;
3618 result = Dart_ObjectIsType(instanceOfTestObj, cls, &is_instance); 3372 result = Dart_ObjectIsType(instanceOfTestObj, cls, &is_instance);
3619 EXPECT_VALID(result); 3373 EXPECT_VALID(result);
3620 EXPECT(is_instance); 3374 EXPECT(is_instance);
3621 3375
3622 // Fetch OtherClass and check if instanceOfTestObj is instance of it. 3376 // Fetch OtherClass and check if instanceOfTestObj is instance of it.
3623 Dart_Handle otherClass = Dart_GetClass(lib, Dart_NewString("OtherClass")); 3377 Dart_Handle otherClass = Dart_GetClass(lib, Dart_NewString("OtherClass"));
3624 EXPECT_VALID(otherClass); 3378 EXPECT_VALID(otherClass);
(...skipping 10 matching lines...) Expand all
3635 3389
3636 result = Dart_ObjectIsType(Dart_NewInteger(42), otherClass, &is_instance); 3390 result = Dart_ObjectIsType(Dart_NewInteger(42), otherClass, &is_instance);
3637 EXPECT_VALID(result); 3391 EXPECT_VALID(result);
3638 EXPECT(!is_instance); 3392 EXPECT(!is_instance);
3639 3393
3640 result = Dart_ObjectIsType(Dart_NewBoolean(true), otherClass, &is_instance); 3394 result = Dart_ObjectIsType(Dart_NewBoolean(true), otherClass, &is_instance);
3641 EXPECT_VALID(result); 3395 EXPECT_VALID(result);
3642 EXPECT(!is_instance); 3396 EXPECT(!is_instance);
3643 3397
3644 // Check that null is not an instance of InstanceOfTest class. 3398 // Check that null is not an instance of InstanceOfTest class.
3645 Dart_Handle null = Dart_InvokeStatic(lib, 3399 Dart_Handle null = Dart_Invoke(otherClass,
3646 Dart_NewString("OtherClass"), 3400 Dart_NewString("returnNull"),
3647 Dart_NewString("returnNull"), 3401 0,
3648 0, 3402 NULL);
3649 NULL);
3650 EXPECT_VALID(null); 3403 EXPECT_VALID(null);
3651 3404
3652 result = Dart_ObjectIsType(null, otherClass, &is_instance); 3405 result = Dart_ObjectIsType(null, otherClass, &is_instance);
3653 EXPECT_VALID(result); 3406 EXPECT_VALID(result);
3654 EXPECT(!is_instance); 3407 EXPECT(!is_instance);
3655 3408
3656 // Check that error is returned if null is passed as a class argument. 3409 // Check that error is returned if null is passed as a class argument.
3657 result = Dart_ObjectIsType(null, null, &is_instance); 3410 result = Dart_ObjectIsType(null, null, &is_instance);
3658 EXPECT(Dart_IsError(result)); 3411 EXPECT(Dart_IsError(result));
3659 } 3412 }
3660 3413
3661 3414
3662 TEST_CASE(NullReceiver) {
3663 Isolate* isolate = Isolate::Current();
3664 DARTSCOPE_NOCHECKS(isolate);
3665
3666 Dart_Handle function_name = Dart_NewString("toString");
3667 const int number_of_arguments = 0;
3668 Dart_Handle result = Dart_InvokeDynamic(Dart_Null(),
3669 function_name,
3670 number_of_arguments,
3671 NULL);
3672 EXPECT_VALID(result);
3673 EXPECT(Dart_IsString(result));
3674
3675 // Should throw a NullPointerException. Disabled due to bug 5415268.
3676 /*
3677 Dart_Handle function_name2 = Dart_NewString("NoNoNo");
3678 result = Dart_InvokeDynamic(null_receiver,
3679 function_name2,
3680 number_of_arguments,
3681 dart_arguments);
3682 EXPECT(Dart_IsError(result));
3683 EXPECT(Dart_ErrorHasException(result)); */
3684 }
3685
3686
3687 static Dart_Handle library_handler(Dart_LibraryTag tag, 3415 static Dart_Handle library_handler(Dart_LibraryTag tag,
3688 Dart_Handle library, 3416 Dart_Handle library,
3689 Dart_Handle url, 3417 Dart_Handle url,
3690 Dart_Handle import_url_map) { 3418 Dart_Handle import_url_map) {
3691 if (tag == kCanonicalizeUrl) { 3419 if (tag == kCanonicalizeUrl) {
3692 return url; 3420 return url;
3693 } 3421 }
3694 return Api::Success(Isolate::Current()); 3422 return Api::Success(Isolate::Current());
3695 } 3423 }
3696 3424
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3732 Dart_GetError(result)); 3460 Dart_GetError(result));
3733 3461
3734 result = Dart_LoadScript(url, error, library_handler, import_map); 3462 result = Dart_LoadScript(url, error, library_handler, import_map);
3735 EXPECT(Dart_IsError(result)); 3463 EXPECT(Dart_IsError(result));
3736 EXPECT_STREQ("incoming error", Dart_GetError(result)); 3464 EXPECT_STREQ("incoming error", Dart_GetError(result));
3737 3465
3738 // Load a script successfully. 3466 // Load a script successfully.
3739 result = Dart_LoadScript(url, source, library_handler, import_map); 3467 result = Dart_LoadScript(url, source, library_handler, import_map);
3740 EXPECT_VALID(result); 3468 EXPECT_VALID(result);
3741 3469
3742 result = Dart_InvokeStatic(result, 3470 result = Dart_Invoke(result, Dart_NewString("main"), 0, NULL);
3743 Dart_NewString(""),
3744 Dart_NewString("main"),
3745 0,
3746 NULL);
3747 EXPECT_VALID(result); 3471 EXPECT_VALID(result);
3748 EXPECT(Dart_IsInteger(result)); 3472 EXPECT(Dart_IsInteger(result));
3749 int64_t value = 0; 3473 int64_t value = 0;
3750 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); 3474 EXPECT_VALID(Dart_IntegerToInt64(result, &value));
3751 EXPECT_EQ(12345, value); 3475 EXPECT_EQ(12345, value);
3752 3476
3753 // Further calls to LoadScript are errors. 3477 // Further calls to LoadScript are errors.
3754 result = Dart_LoadScript(url, source, library_handler, import_map); 3478 result = Dart_LoadScript(url, source, library_handler, import_map);
3755 EXPECT(Dart_IsError(result)); 3479 EXPECT(Dart_IsError(result));
3756 EXPECT_STREQ("Dart_LoadScript: " 3480 EXPECT_STREQ("Dart_LoadScript: "
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
4213 Dart_Handle error = Dart_Error("incoming error"); 3937 Dart_Handle error = Dart_Error("incoming error");
4214 Dart_Handle result; 3938 Dart_Handle result;
4215 3939
4216 // Load a test script. 3940 // Load a test script.
4217 Dart_Handle url = Dart_NewString(TestCase::url()); 3941 Dart_Handle url = Dart_NewString(TestCase::url());
4218 Dart_Handle source = Dart_NewString(kScriptChars); 3942 Dart_Handle source = Dart_NewString(kScriptChars);
4219 Dart_Handle import_map = Dart_NewList(0); 3943 Dart_Handle import_map = Dart_NewList(0);
4220 Dart_Handle lib = Dart_LoadScript(url, source, library_handler, import_map); 3944 Dart_Handle lib = Dart_LoadScript(url, source, library_handler, import_map);
4221 EXPECT_VALID(lib); 3945 EXPECT_VALID(lib);
4222 EXPECT(Dart_IsLibrary(lib)); 3946 EXPECT(Dart_IsLibrary(lib));
3947 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Test"));
3948 EXPECT_VALID(cls);
4223 3949
4224 result = Dart_SetNativeResolver(Dart_Null(), &MyNativeResolver1); 3950 result = Dart_SetNativeResolver(Dart_Null(), &MyNativeResolver1);
4225 EXPECT(Dart_IsError(result)); 3951 EXPECT(Dart_IsError(result));
4226 EXPECT_STREQ( 3952 EXPECT_STREQ(
4227 "Dart_SetNativeResolver expects argument 'library' to be non-null.", 3953 "Dart_SetNativeResolver expects argument 'library' to be non-null.",
4228 Dart_GetError(result)); 3954 Dart_GetError(result));
4229 3955
4230 result = Dart_SetNativeResolver(Dart_True(), &MyNativeResolver1); 3956 result = Dart_SetNativeResolver(Dart_True(), &MyNativeResolver1);
4231 EXPECT(Dart_IsError(result)); 3957 EXPECT(Dart_IsError(result));
4232 EXPECT_STREQ("Dart_SetNativeResolver expects argument 'library' to be of " 3958 EXPECT_STREQ("Dart_SetNativeResolver expects argument 'library' to be of "
4233 "type Library.", 3959 "type Library.",
4234 Dart_GetError(result)); 3960 Dart_GetError(result));
4235 3961
4236 result = Dart_SetNativeResolver(error, &MyNativeResolver1); 3962 result = Dart_SetNativeResolver(error, &MyNativeResolver1);
4237 EXPECT(Dart_IsError(result)); 3963 EXPECT(Dart_IsError(result));
4238 EXPECT_STREQ("incoming error", Dart_GetError(result)); 3964 EXPECT_STREQ("incoming error", Dart_GetError(result));
4239 3965
4240 result = Dart_SetNativeResolver(lib, &MyNativeResolver1); 3966 result = Dart_SetNativeResolver(lib, &MyNativeResolver1);
4241 EXPECT_VALID(result); 3967 EXPECT_VALID(result);
4242 3968
4243 // Call a function and make sure native resolution works. 3969 // Call a function and make sure native resolution works.
4244 result = Dart_InvokeStatic(lib, 3970 result = Dart_Invoke(cls, Dart_NewString("foo"), 0, NULL);
4245 Dart_NewString("Test"),
4246 Dart_NewString("foo"),
4247 0,
4248 NULL);
4249 EXPECT_VALID(result); 3971 EXPECT_VALID(result);
4250 EXPECT(Dart_IsInteger(result)); 3972 EXPECT(Dart_IsInteger(result));
4251 int64_t value = 0; 3973 int64_t value = 0;
4252 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); 3974 EXPECT_VALID(Dart_IntegerToInt64(result, &value));
4253 EXPECT_EQ(654321, value); 3975 EXPECT_EQ(654321, value);
4254 3976
4255 // A second call succeeds. 3977 // A second call succeeds.
4256 result = Dart_SetNativeResolver(lib, &MyNativeResolver2); 3978 result = Dart_SetNativeResolver(lib, &MyNativeResolver2);
4257 EXPECT_VALID(result); 3979 EXPECT_VALID(result);
4258 3980
4259 // 'foo' has already been resolved so gets the old value. 3981 // 'foo' has already been resolved so gets the old value.
4260 result = Dart_InvokeStatic(lib, 3982 result = Dart_Invoke(cls, Dart_NewString("foo"), 0, NULL);
4261 Dart_NewString("Test"),
4262 Dart_NewString("foo"),
4263 0,
4264 NULL);
4265 EXPECT_VALID(result); 3983 EXPECT_VALID(result);
4266 EXPECT(Dart_IsInteger(result)); 3984 EXPECT(Dart_IsInteger(result));
4267 value = 0; 3985 value = 0;
4268 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); 3986 EXPECT_VALID(Dart_IntegerToInt64(result, &value));
4269 EXPECT_EQ(654321, value); 3987 EXPECT_EQ(654321, value);
4270 3988
4271 // 'bar' has not yet been resolved so gets the new value. 3989 // 'bar' has not yet been resolved so gets the new value.
4272 result = Dart_InvokeStatic(lib, 3990 result = Dart_Invoke(cls, Dart_NewString("bar"), 0, NULL);
4273 Dart_NewString("Test"),
4274 Dart_NewString("bar"),
4275 0,
4276 NULL);
4277 EXPECT_VALID(result); 3991 EXPECT_VALID(result);
4278 EXPECT(Dart_IsInteger(result)); 3992 EXPECT(Dart_IsInteger(result));
4279 value = 0; 3993 value = 0;
4280 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); 3994 EXPECT_VALID(Dart_IntegerToInt64(result, &value));
4281 EXPECT_EQ(123456, value); 3995 EXPECT_EQ(123456, value);
4282 3996
4283 // A NULL resolver is okay, but resolution will fail. 3997 // A NULL resolver is okay, but resolution will fail.
4284 result = Dart_SetNativeResolver(lib, NULL); 3998 result = Dart_SetNativeResolver(lib, NULL);
4285 EXPECT_VALID(result); 3999 EXPECT_VALID(result);
4286 4000
4287 result = Dart_InvokeStatic(lib, 4001 EXPECT_ERROR(Dart_Invoke(cls, Dart_NewString("baz"), 0, NULL),
4288 Dart_NewString("Test"), 4002 "native function 'SomeNativeFunction3' cannot be found");
4289 Dart_NewString("baz"),
4290 0,
4291 NULL);
4292 EXPECT(Dart_IsError(result));
4293 EXPECT(strstr(Dart_GetError(result),
4294 "native function 'SomeNativeFunction3' cannot be found"));
4295 } 4003 }
4296 4004
4297 4005
4298 TEST_CASE(ImportLibrary1) { 4006 TEST_CASE(ImportLibrary1) {
4299 const char* kScriptChars = 4007 const char* kScriptChars =
4300 "#import('library1.dart');" 4008 "#import('library1.dart');"
4301 "#import('library2.dart');" 4009 "#import('library2.dart');"
4302 "var foo; // library2 defines foo, so should be error." 4010 "var foo; // library2 defines foo, so should be error."
4303 "main() {}"; 4011 "main() {}";
4304 const char* kLibrary1Chars = 4012 const char* kLibrary1Chars =
(...skipping 10 matching lines...) Expand all
4315 result = Dart_LoadScript(url, source, library_handler, Dart_Null()); 4023 result = Dart_LoadScript(url, source, library_handler, Dart_Null());
4316 4024
4317 url = Dart_NewString("library1.dart"); 4025 url = Dart_NewString("library1.dart");
4318 source = Dart_NewString(kLibrary1Chars); 4026 source = Dart_NewString(kLibrary1Chars);
4319 Dart_LoadLibrary(url, source, Dart_Null()); 4027 Dart_LoadLibrary(url, source, Dart_Null());
4320 4028
4321 url = Dart_NewString("library2.dart"); 4029 url = Dart_NewString("library2.dart");
4322 source = Dart_NewString(kLibrary2Chars); 4030 source = Dart_NewString(kLibrary2Chars);
4323 Dart_LoadLibrary(url, source, Dart_Null()); 4031 Dart_LoadLibrary(url, source, Dart_Null());
4324 4032
4325 result = Dart_InvokeStatic(result, 4033 result = Dart_Invoke(result, Dart_NewString("main"), 0, NULL);
4326 Dart_NewString(""),
4327 Dart_NewString("main"),
4328 0,
4329 NULL);
4330 EXPECT(Dart_IsError(result)); 4034 EXPECT(Dart_IsError(result));
4331 EXPECT_STREQ("Duplicate definition : 'foo' is defined in" 4035 EXPECT_STREQ("Duplicate definition : 'foo' is defined in"
4332 " 'library2.dart' and 'dart:test-lib'\n", 4036 " 'library2.dart' and 'dart:test-lib'\n",
4333 Dart_GetError(result)); 4037 Dart_GetError(result));
4334 } 4038 }
4335 4039
4336 4040
4337 TEST_CASE(ImportLibrary2) { 4041 TEST_CASE(ImportLibrary2) {
4338 const char* kScriptChars = 4042 const char* kScriptChars =
4339 "#import('library1.dart');" 4043 "#import('library1.dart');"
(...skipping 15 matching lines...) Expand all
4355 result = Dart_LoadScript(url, source, library_handler, import_map); 4059 result = Dart_LoadScript(url, source, library_handler, import_map);
4356 4060
4357 url = Dart_NewString("library1.dart"); 4061 url = Dart_NewString("library1.dart");
4358 source = Dart_NewString(kLibrary1Chars); 4062 source = Dart_NewString(kLibrary1Chars);
4359 Dart_LoadLibrary(url, source, import_map); 4063 Dart_LoadLibrary(url, source, import_map);
4360 4064
4361 url = Dart_NewString("library2.dart"); 4065 url = Dart_NewString("library2.dart");
4362 source = Dart_NewString(kLibrary2Chars); 4066 source = Dart_NewString(kLibrary2Chars);
4363 Dart_LoadLibrary(url, source, import_map); 4067 Dart_LoadLibrary(url, source, import_map);
4364 4068
4365 result = Dart_InvokeStatic(result, 4069 result = Dart_Invoke(result, Dart_NewString("main"), 0, NULL);
4366 Dart_NewString(""),
4367 Dart_NewString("main"),
4368 0,
4369 NULL);
4370 EXPECT_VALID(result); 4070 EXPECT_VALID(result);
4371 } 4071 }
4372 4072
4373 4073
4374 TEST_CASE(ImportLibrary3) { 4074 TEST_CASE(ImportLibrary3) {
4375 const char* kScriptChars = 4075 const char* kScriptChars =
4376 "#import('library2.dart');" 4076 "#import('library2.dart');"
4377 "#import('library1.dart');" 4077 "#import('library1.dart');"
4378 "var foo_top = 10; // foo has dup def. So should be an error." 4078 "var foo_top = 10; // foo has dup def. So should be an error."
4379 "main() {}"; 4079 "main() {}";
(...skipping 12 matching lines...) Expand all
4392 result = Dart_LoadScript(url, source, library_handler, import_map); 4092 result = Dart_LoadScript(url, source, library_handler, import_map);
4393 4093
4394 url = Dart_NewString("library2.dart"); 4094 url = Dart_NewString("library2.dart");
4395 source = Dart_NewString(kLibrary2Chars); 4095 source = Dart_NewString(kLibrary2Chars);
4396 Dart_LoadLibrary(url, source, import_map); 4096 Dart_LoadLibrary(url, source, import_map);
4397 4097
4398 url = Dart_NewString("library1.dart"); 4098 url = Dart_NewString("library1.dart");
4399 source = Dart_NewString(kLibrary1Chars); 4099 source = Dart_NewString(kLibrary1Chars);
4400 Dart_LoadLibrary(url, source, import_map); 4100 Dart_LoadLibrary(url, source, import_map);
4401 4101
4402 result = Dart_InvokeStatic(result, 4102 result = Dart_Invoke(result, Dart_NewString("main"), 0, NULL);
4403 Dart_NewString(""),
4404 Dart_NewString("main"),
4405 0,
4406 NULL);
4407 EXPECT(Dart_IsError(result)); 4103 EXPECT(Dart_IsError(result));
4408 EXPECT_STREQ("Duplicate definition : 'foo' is defined in" 4104 EXPECT_STREQ("Duplicate definition : 'foo' is defined in"
4409 " 'library1.dart' and 'library2.dart'\n", 4105 " 'library1.dart' and 'library2.dart'\n",
4410 Dart_GetError(result)); 4106 Dart_GetError(result));
4411 } 4107 }
4412 4108
4413 4109
4414 TEST_CASE(ImportLibrary4) { 4110 TEST_CASE(ImportLibrary4) {
4415 const char* kScriptChars = 4111 const char* kScriptChars =
4416 "#import('libraryA.dart');" 4112 "#import('libraryA.dart');"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
4467 Dart_LoadLibrary(url, source, import_map); 4163 Dart_LoadLibrary(url, source, import_map);
4468 4164
4469 url = Dart_NewString("libraryF.dart"); 4165 url = Dart_NewString("libraryF.dart");
4470 source = Dart_NewString(kLibraryFChars); 4166 source = Dart_NewString(kLibraryFChars);
4471 Dart_LoadLibrary(url, source, import_map); 4167 Dart_LoadLibrary(url, source, import_map);
4472 4168
4473 url = Dart_NewString("libraryE.dart"); 4169 url = Dart_NewString("libraryE.dart");
4474 source = Dart_NewString(kLibraryEChars); 4170 source = Dart_NewString(kLibraryEChars);
4475 Dart_LoadLibrary(url, source, import_map); 4171 Dart_LoadLibrary(url, source, import_map);
4476 4172
4477 result = Dart_InvokeStatic(result, 4173 result = Dart_Invoke(result, Dart_NewString("main"), 0, NULL);
4478 Dart_NewString(""),
4479 Dart_NewString("main"),
4480 0,
4481 NULL);
4482 EXPECT(Dart_IsError(result)); 4174 EXPECT(Dart_IsError(result));
4483 EXPECT_STREQ("Duplicate definition : 'fooC' is defined in" 4175 EXPECT_STREQ("Duplicate definition : 'fooC' is defined in"
4484 " 'libraryF.dart' and 'libraryC.dart'\n", 4176 " 'libraryF.dart' and 'libraryC.dart'\n",
4485 Dart_GetError(result)); 4177 Dart_GetError(result));
4486 } 4178 }
4487 4179
4488 4180
4489 TEST_CASE(ImportLibrary5) { 4181 TEST_CASE(ImportLibrary5) {
4490 const char* kScriptChars = 4182 const char* kScriptChars =
4491 "#import('lib.dart');" 4183 "#import('lib.dart');"
(...skipping 11 matching lines...) Expand all
4503 // Create a test library and Load up a test script in it. 4195 // Create a test library and Load up a test script in it.
4504 Dart_Handle url = Dart_NewString(TestCase::url()); 4196 Dart_Handle url = Dart_NewString(TestCase::url());
4505 Dart_Handle source = Dart_NewString(kScriptChars); 4197 Dart_Handle source = Dart_NewString(kScriptChars);
4506 Dart_Handle import_map = Dart_NewList(0); 4198 Dart_Handle import_map = Dart_NewList(0);
4507 result = Dart_LoadScript(url, source, library_handler, import_map); 4199 result = Dart_LoadScript(url, source, library_handler, import_map);
4508 4200
4509 url = Dart_NewString("lib.dart"); 4201 url = Dart_NewString("lib.dart");
4510 source = Dart_NewString(kLibraryChars); 4202 source = Dart_NewString(kLibraryChars);
4511 Dart_LoadLibrary(url, source, import_map); 4203 Dart_LoadLibrary(url, source, import_map);
4512 4204
4513 result = Dart_InvokeStatic(result, 4205 result = Dart_Invoke(result, Dart_NewString("main"), 0, NULL);
4514 Dart_NewString(""),
4515 Dart_NewString("main"),
4516 0,
4517 NULL);
4518 EXPECT_VALID(result); 4206 EXPECT_VALID(result);
4519 } 4207 }
4520 4208
4521 4209
4522 void NewNativePort_send123(Dart_Port dest_port_id, 4210 void NewNativePort_send123(Dart_Port dest_port_id,
4523 Dart_Port reply_port_id, 4211 Dart_Port reply_port_id,
4524 Dart_CObject *message) { 4212 Dart_CObject *message) {
4525 // Gets a null message. 4213 // Gets a null message.
4526 EXPECT_NOTNULL(message); 4214 EXPECT_NOTNULL(message);
4527 EXPECT_EQ(Dart_CObject::kNull, message->type); 4215 EXPECT_EQ(Dart_CObject::kNull, message->type);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
4576 Dart_NewNativePort("Port321", NewNativePort_send321, true); 4264 Dart_NewNativePort("Port321", NewNativePort_send321, true);
4577 4265
4578 Dart_Handle send_port1 = Dart_NewSendPort(port_id1); 4266 Dart_Handle send_port1 = Dart_NewSendPort(port_id1);
4579 EXPECT_VALID(send_port1); 4267 EXPECT_VALID(send_port1);
4580 Dart_Handle send_port2 = Dart_NewSendPort(port_id2); 4268 Dart_Handle send_port2 = Dart_NewSendPort(port_id2);
4581 EXPECT_VALID(send_port2); 4269 EXPECT_VALID(send_port2);
4582 4270
4583 // Test first port. 4271 // Test first port.
4584 Dart_Handle dart_args[1]; 4272 Dart_Handle dart_args[1];
4585 dart_args[0] = send_port1; 4273 dart_args[0] = send_port1;
4586 Dart_Handle result = Dart_InvokeStatic(lib, 4274 Dart_Handle result =
4587 Dart_NewString(""), 4275 Dart_Invoke(lib, Dart_NewString("callPort"), 1, dart_args);
4588 Dart_NewString("callPort"),
4589 1,
4590 dart_args);
4591 EXPECT_VALID(result); 4276 EXPECT_VALID(result);
4592 result = Dart_RunLoop(); 4277 result = Dart_RunLoop();
4593 EXPECT(Dart_IsError(result)); 4278 EXPECT(Dart_IsError(result));
4594 EXPECT(Dart_ErrorHasException(result)); 4279 EXPECT(Dart_ErrorHasException(result));
4595 EXPECT_SUBSTRING("Exception: 123\n", Dart_GetError(result)); 4280 EXPECT_SUBSTRING("Exception: 123\n", Dart_GetError(result));
4596 4281
4597 // result second port. 4282 // result second port.
4598 dart_args[0] = send_port2; 4283 dart_args[0] = send_port2;
4599 result = Dart_InvokeStatic(lib, 4284 result = Dart_Invoke(lib, Dart_NewString("callPort"), 1, dart_args);
4600 Dart_NewString(""),
4601 Dart_NewString("callPort"),
4602 1,
4603 dart_args);
4604 EXPECT_VALID(result); 4285 EXPECT_VALID(result);
4605 result = Dart_RunLoop(); 4286 result = Dart_RunLoop();
4606 EXPECT(Dart_IsError(result)); 4287 EXPECT(Dart_IsError(result));
4607 EXPECT(Dart_ErrorHasException(result)); 4288 EXPECT(Dart_ErrorHasException(result));
4608 EXPECT_SUBSTRING("Exception: 321\n", Dart_GetError(result)); 4289 EXPECT_SUBSTRING("Exception: 321\n", Dart_GetError(result));
4609 4290
4610 Dart_ExitScope(); 4291 Dart_ExitScope();
4611 4292
4612 // Delete the native ports. 4293 // Delete the native ports.
4613 EXPECT(Dart_CloseNativePort(port_id1)); 4294 EXPECT(Dart_CloseNativePort(port_id1));
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
4670 RunLoopTestCallback(NULL, NULL, NULL); 4351 RunLoopTestCallback(NULL, NULL, NULL);
4671 4352
4672 Dart_EnterScope(); 4353 Dart_EnterScope();
4673 Dart_Handle lib = Dart_LookupLibrary(Dart_NewString(TestCase::url())); 4354 Dart_Handle lib = Dart_LookupLibrary(Dart_NewString(TestCase::url()));
4674 EXPECT_VALID(lib); 4355 EXPECT_VALID(lib);
4675 4356
4676 Dart_Handle result; 4357 Dart_Handle result;
4677 Dart_Handle args[2]; 4358 Dart_Handle args[2];
4678 args[0] = (throw_exception_child ? Dart_True() : Dart_False()); 4359 args[0] = (throw_exception_child ? Dart_True() : Dart_False());
4679 args[1] = (throw_exception_parent ? Dart_True() : Dart_False()); 4360 args[1] = (throw_exception_parent ? Dart_True() : Dart_False());
4680 result = Dart_InvokeStatic(lib, 4361 result = Dart_Invoke(lib, Dart_NewString("main"), 2, args);
4681 Dart_NewString(""),
4682 Dart_NewString("main"),
4683 2,
4684 args);
4685 EXPECT_VALID(result); 4362 EXPECT_VALID(result);
4686 result = Dart_RunLoop(); 4363 result = Dart_RunLoop();
4687 if (throw_exception_parent) { 4364 if (throw_exception_parent) {
4688 EXPECT(Dart_IsError(result)); 4365 EXPECT_ERROR(result, "Exception: MakeParentExit");
4689 // TODO(turnidge): Once EXPECT_SUBSTRING is submitted use it here.
4690 } else { 4366 } else {
4691 EXPECT_VALID(result); 4367 EXPECT_VALID(result);
4692 } 4368 }
4693 4369
4694 Dart_ExitScope(); 4370 Dart_ExitScope();
4695 Dart_ShutdownIsolate(); 4371 Dart_ShutdownIsolate();
4696 4372
4697 Isolate::SetCreateCallback(saved); 4373 Isolate::SetCreateCallback(saved);
4698 } 4374 }
4699 4375
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
4765 lib = Dart_LoadScript(url, source, TestCase::library_handler, import_map); 4441 lib = Dart_LoadScript(url, source, TestCase::library_handler, import_map);
4766 EXPECT_VALID(lib); 4442 EXPECT_VALID(lib);
4767 Dart_Handle result = Dart_SetNativeResolver( 4443 Dart_Handle result = Dart_SetNativeResolver(
4768 lib, &IsolateInterruptTestNativeLookup); 4444 lib, &IsolateInterruptTestNativeLookup);
4769 DART_CHECK_VALID(result); 4445 DART_CHECK_VALID(result);
4770 4446
4771 sync->Notify(); 4447 sync->Notify();
4772 sync->Exit(); 4448 sync->Exit();
4773 } 4449 }
4774 4450
4775 Dart_Handle result = Dart_InvokeStatic(lib, 4451 Dart_Handle result = Dart_Invoke(lib, Dart_NewString("main"), 0, NULL);
4776 Dart_NewString(""),
4777 Dart_NewString("main"),
4778 0,
4779 NULL);
4780 EXPECT(Dart_IsError(result)); 4452 EXPECT(Dart_IsError(result));
4781 EXPECT(Dart_ErrorHasException(result)); 4453 EXPECT(Dart_ErrorHasException(result));
4782 EXPECT_SUBSTRING("Unhandled exception:\nfoo\n", 4454 EXPECT_SUBSTRING("Unhandled exception:\nfoo\n",
4783 Dart_GetError(result)); 4455 Dart_GetError(result));
4784 4456
4785 Dart_ExitScope(); 4457 Dart_ExitScope();
4786 Dart_ShutdownIsolate(); 4458 Dart_ShutdownIsolate();
4787 4459
4788 // Tell the other thread that we are done (don't use MonitorLocker 4460 // Tell the other thread that we are done (don't use MonitorLocker
4789 // as there is no current isolate any more). 4461 // as there is no current isolate any more).
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
4971 EXPECT_VALID(result); 4643 EXPECT_VALID(result);
4972 EXPECT(Dart_IsDouble(result)); 4644 EXPECT(Dart_IsDouble(result));
4973 double out; 4645 double out;
4974 result = Dart_DoubleValue(result, &out); 4646 result = Dart_DoubleValue(result, &out);
4975 fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out); 4647 fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out);
4976 } 4648 }
4977 4649
4978 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 4650 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
4979 4651
4980 } // namespace dart 4652 } // namespace dart
OLDNEW
« no previous file with comments | « vm/dart_api_impl.cc ('k') | vm/debugger_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698