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

Side by Side Diff: vm/dart_api_impl_test.cc

Issue 8673002: - Refactor the isolate callback mechanism to also include creation of the (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: '' Created 9 years 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/isolate.h » ('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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/assert.h" 7 #include "vm/assert.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_api_state.h" 9 #include "vm/dart_api_state.h"
10 #include "vm/unit_test.h" 10 #include "vm/unit_test.h"
11 #include "vm/utils.h" 11 #include "vm/utils.h"
12 #include "vm/verifier.h" 12 #include "vm/verifier.h"
13 13
14 namespace dart { 14 namespace dart {
15 15
16
17 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests. 16 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests.
18 17
19 UNIT_TEST_CASE(ErrorHandles) { 18 UNIT_TEST_CASE(ErrorHandles) {
20 const char* kScriptChars = 19 const char* kScriptChars =
21 "class TestClass {\n" 20 "class TestClass {\n"
22 " static void testMain() {\n" 21 " static void testMain() {\n"
23 " throw new Exception(\"bad news\");\n" 22 " throw new Exception(\"bad news\");\n"
24 " }\n" 23 " }\n"
25 "}\n"; 24 "}\n";
26 25
27 Dart_CreateIsolate(NULL, NULL); 26 TestIsolateScope __test_isolate__;
28 Dart_EnterScope(); 27
29 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 28 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
30 29
31 Dart_Handle instance = Dart_True(); 30 Dart_Handle instance = Dart_True();
32 Dart_Handle error = Api::Error("myerror"); 31 Dart_Handle error = Api::Error("myerror");
33 Dart_Handle exception = Dart_InvokeStatic(lib, 32 Dart_Handle exception = Dart_InvokeStatic(lib,
34 Dart_NewString("TestClass"), 33 Dart_NewString("TestClass"),
35 Dart_NewString("testMain"), 34 Dart_NewString("testMain"),
36 0, 35 0,
37 NULL); 36 NULL);
38 37
(...skipping 13 matching lines...) Expand all
52 " 0. Function: 'TestClass.testMain' url: 'dart:test-lib' line:3 col:5\n", 51 " 0. Function: 'TestClass.testMain' url: 'dart:test-lib' line:3 col:5\n",
53 Dart_GetError(exception)); 52 Dart_GetError(exception));
54 53
55 EXPECT(Dart_IsError(Dart_ErrorGetException(instance))); 54 EXPECT(Dart_IsError(Dart_ErrorGetException(instance)));
56 EXPECT(Dart_IsError(Dart_ErrorGetException(error))); 55 EXPECT(Dart_IsError(Dart_ErrorGetException(error)));
57 EXPECT_VALID(Dart_ErrorGetException(exception)); 56 EXPECT_VALID(Dart_ErrorGetException(exception));
58 57
59 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(instance))); 58 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(instance)));
60 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(error))); 59 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(error)));
61 EXPECT_VALID(Dart_ErrorGetStacktrace(exception)); 60 EXPECT_VALID(Dart_ErrorGetStacktrace(exception));
62
63 Dart_ExitScope();
64 Dart_ShutdownIsolate();
65 } 61 }
66 62
67 #endif 63 #endif
68 64
69 65
70 UNIT_TEST_CASE(Dart_Error) { 66 UNIT_TEST_CASE(Dart_Error) {
71 Dart_CreateIsolate(NULL, NULL); 67 TestIsolateScope __test_isolate__;
72 Dart_EnterScope();
73 68
74 Dart_Handle error = Dart_Error("An %s", "error"); 69 Dart_Handle error = Dart_Error("An %s", "error");
75 EXPECT(Dart_IsError(error)); 70 EXPECT(Dart_IsError(error));
76 EXPECT_STREQ("An error", Dart_GetError(error)); 71 EXPECT_STREQ("An error", Dart_GetError(error));
77
78 Dart_ExitScope();
79 Dart_ShutdownIsolate();
80 } 72 }
81 73
82 74
83 UNIT_TEST_CASE(Null) { 75 UNIT_TEST_CASE(Null) {
84 Dart_CreateIsolate(NULL, NULL); 76 TestIsolateScope __test_isolate__;
85 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
86 77
87 Dart_Handle null = Dart_Null(); 78 Dart_Handle null = Dart_Null();
88 EXPECT_VALID(null); 79 EXPECT_VALID(null);
89 EXPECT(Dart_IsNull(null)); 80 EXPECT(Dart_IsNull(null));
90 81
91 Dart_Handle str = Dart_NewString("test"); 82 Dart_Handle str = Dart_NewString("test");
92 EXPECT_VALID(str); 83 EXPECT_VALID(str);
93 EXPECT(!Dart_IsNull(str)); 84 EXPECT(!Dart_IsNull(str));
94
95 Dart_ExitScope(); // Exit the Dart API scope.
96 Dart_ShutdownIsolate();
97 } 85 }
98 86
99 87
100 UNIT_TEST_CASE(IsSame) { 88 UNIT_TEST_CASE(IsSame) {
101 Dart_CreateIsolate(NULL, NULL); 89 TestIsolateScope __test_isolate__;
102 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
103 90
104 bool same = false; 91 bool same = false;
105 Dart_Handle five = Dart_NewString("5"); 92 Dart_Handle five = Dart_NewString("5");
106 Dart_Handle five_again = Dart_NewString("5"); 93 Dart_Handle five_again = Dart_NewString("5");
107 Dart_Handle seven = Dart_NewString("7"); 94 Dart_Handle seven = Dart_NewString("7");
108 95
109 // Same objects. 96 // Same objects.
110 EXPECT_VALID(Dart_IsSame(five, five, &same)); 97 EXPECT_VALID(Dart_IsSame(five, five, &same));
111 EXPECT(same); 98 EXPECT(same);
112 99
(...skipping 12 matching lines...) Expand all
125 const Object& cls2 = Object::Handle(Object::class_class()); 112 const Object& cls2 = Object::Handle(Object::class_class());
126 Dart_Handle class1 = Api::NewLocalHandle(cls1); 113 Dart_Handle class1 = Api::NewLocalHandle(cls1);
127 Dart_Handle class2 = Api::NewLocalHandle(cls2); 114 Dart_Handle class2 = Api::NewLocalHandle(cls2);
128 115
129 EXPECT_VALID(Dart_IsSame(class1, class1, &same)); 116 EXPECT_VALID(Dart_IsSame(class1, class1, &same));
130 EXPECT(same); 117 EXPECT(same);
131 118
132 EXPECT_VALID(Dart_IsSame(class1, class2, &same)); 119 EXPECT_VALID(Dart_IsSame(class1, class2, &same));
133 EXPECT(!same); 120 EXPECT(!same);
134 } 121 }
135
136 Dart_ExitScope(); // Exit the Dart API scope.
137 Dart_ShutdownIsolate();
138 } 122 }
139 123
140 124
141 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests. 125 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests.
142 126
143 UNIT_TEST_CASE(ObjectEquals) { 127 UNIT_TEST_CASE(ObjectEquals) {
144 Dart_CreateIsolate(NULL, NULL); 128 TestIsolateScope __test_isolate__;
145 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
146 129
147 bool equal = false; 130 bool equal = false;
148 Dart_Handle five = Dart_NewString("5"); 131 Dart_Handle five = Dart_NewString("5");
149 Dart_Handle five_again = Dart_NewString("5"); 132 Dart_Handle five_again = Dart_NewString("5");
150 Dart_Handle seven = Dart_NewString("7"); 133 Dart_Handle seven = Dart_NewString("7");
151 134
152 // Same objects. 135 // Same objects.
153 EXPECT_VALID(Dart_ObjectEquals(five, five, &equal)); 136 EXPECT_VALID(Dart_ObjectEquals(five, five, &equal));
154 EXPECT(equal); 137 EXPECT(equal);
155 138
156 // Equal objects. 139 // Equal objects.
157 EXPECT_VALID(Dart_ObjectEquals(five, five_again, &equal)); 140 EXPECT_VALID(Dart_ObjectEquals(five, five_again, &equal));
158 EXPECT(equal); 141 EXPECT(equal);
159 142
160 // Different objects. 143 // Different objects.
161 EXPECT_VALID(Dart_ObjectEquals(five, seven, &equal)); 144 EXPECT_VALID(Dart_ObjectEquals(five, seven, &equal));
162 EXPECT(!equal); 145 EXPECT(!equal);
163
164 Dart_ExitScope(); // Exit the Dart API scope.
165 Dart_ShutdownIsolate();
166 } 146 }
167 147
168 #endif 148 #endif
169 149
170 UNIT_TEST_CASE(BooleanValues) { 150 UNIT_TEST_CASE(BooleanValues) {
171 Dart_CreateIsolate(NULL, NULL); 151 TestIsolateScope __test_isolate__;
172 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
173 152
174 Dart_Handle str = Dart_NewString("test"); 153 Dart_Handle str = Dart_NewString("test");
175 EXPECT(!Dart_IsBoolean(str)); 154 EXPECT(!Dart_IsBoolean(str));
176 155
177 bool value = false; 156 bool value = false;
178 Dart_Handle result = Dart_BooleanValue(str, &value); 157 Dart_Handle result = Dart_BooleanValue(str, &value);
179 EXPECT(Dart_IsError(result)); 158 EXPECT(Dart_IsError(result));
180 159
181 Dart_Handle val1 = Dart_NewBoolean(true); 160 Dart_Handle val1 = Dart_NewBoolean(true);
182 EXPECT(Dart_IsBoolean(val1)); 161 EXPECT(Dart_IsBoolean(val1));
183 162
184 result = Dart_BooleanValue(val1, &value); 163 result = Dart_BooleanValue(val1, &value);
185 EXPECT_VALID(result); 164 EXPECT_VALID(result);
186 EXPECT(value); 165 EXPECT(value);
187 166
188 Dart_Handle val2 = Dart_NewBoolean(false); 167 Dart_Handle val2 = Dart_NewBoolean(false);
189 EXPECT(Dart_IsBoolean(val2)); 168 EXPECT(Dart_IsBoolean(val2));
190 169
191 result = Dart_BooleanValue(val2, &value); 170 result = Dart_BooleanValue(val2, &value);
192 EXPECT_VALID(result); 171 EXPECT_VALID(result);
193 EXPECT(!value); 172 EXPECT(!value);
194
195 Dart_ExitScope(); // Exit the Dart API scope.
196 Dart_ShutdownIsolate();
197 } 173 }
198 174
199 175
200 UNIT_TEST_CASE(BooleanConstants) { 176 UNIT_TEST_CASE(BooleanConstants) {
201 Dart_CreateIsolate(NULL, NULL); 177 TestIsolateScope __test_isolate__;
202 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
203 178
204 Dart_Handle true_handle = Dart_True(); 179 Dart_Handle true_handle = Dart_True();
205 EXPECT_VALID(true_handle); 180 EXPECT_VALID(true_handle);
206 EXPECT(Dart_IsBoolean(true_handle)); 181 EXPECT(Dart_IsBoolean(true_handle));
207 182
208 bool value = false; 183 bool value = false;
209 Dart_Handle result = Dart_BooleanValue(true_handle, &value); 184 Dart_Handle result = Dart_BooleanValue(true_handle, &value);
210 EXPECT_VALID(result); 185 EXPECT_VALID(result);
211 EXPECT(value); 186 EXPECT(value);
212 187
213 Dart_Handle false_handle = Dart_False(); 188 Dart_Handle false_handle = Dart_False();
214 EXPECT_VALID(false_handle); 189 EXPECT_VALID(false_handle);
215 EXPECT(Dart_IsBoolean(false_handle)); 190 EXPECT(Dart_IsBoolean(false_handle));
216 191
217 result = Dart_BooleanValue(false_handle, &value); 192 result = Dart_BooleanValue(false_handle, &value);
218 EXPECT_VALID(result); 193 EXPECT_VALID(result);
219 EXPECT(!value); 194 EXPECT(!value);
220
221 Dart_ExitScope(); // Exit the Dart API scope.
222 Dart_ShutdownIsolate();
223 } 195 }
224 196
225 197
226 UNIT_TEST_CASE(DoubleValues) { 198 UNIT_TEST_CASE(DoubleValues) {
227 Dart_CreateIsolate(NULL, NULL); 199 TestIsolateScope __test_isolate__;
228 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
229 200
230 const double kDoubleVal1 = 201.29; 201 const double kDoubleVal1 = 201.29;
231 const double kDoubleVal2 = 101.19; 202 const double kDoubleVal2 = 101.19;
232 Dart_Handle val1 = Dart_NewDouble(kDoubleVal1); 203 Dart_Handle val1 = Dart_NewDouble(kDoubleVal1);
233 EXPECT(Dart_IsDouble(val1)); 204 EXPECT(Dart_IsDouble(val1));
234 Dart_Handle val2 = Dart_NewDouble(kDoubleVal2); 205 Dart_Handle val2 = Dart_NewDouble(kDoubleVal2);
235 EXPECT(Dart_IsDouble(val2)); 206 EXPECT(Dart_IsDouble(val2));
236 double out1, out2; 207 double out1, out2;
237 Dart_Handle result = Dart_DoubleValue(val1, &out1); 208 Dart_Handle result = Dart_DoubleValue(val1, &out1);
238 EXPECT_VALID(result); 209 EXPECT_VALID(result);
239 EXPECT_EQ(kDoubleVal1, out1); 210 EXPECT_EQ(kDoubleVal1, out1);
240 result = Dart_DoubleValue(val2, &out2); 211 result = Dart_DoubleValue(val2, &out2);
241 EXPECT_VALID(result); 212 EXPECT_VALID(result);
242 EXPECT_EQ(kDoubleVal2, out2); 213 EXPECT_EQ(kDoubleVal2, out2);
243
244 Dart_ExitScope(); // Exit the Dart API scope.
245 Dart_ShutdownIsolate();
246 } 214 }
247 215
248 216
249 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests. 217 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests.
250 218
251 UNIT_TEST_CASE(NumberValues) { 219 UNIT_TEST_CASE(NumberValues) {
252 // TODO(antonm): add various kinds of ints (smi, mint, bigint). 220 // TODO(antonm): add various kinds of ints (smi, mint, bigint).
253 const char* kScriptChars = 221 const char* kScriptChars =
254 "class NumberValuesHelper {\n" 222 "class NumberValuesHelper {\n"
255 " static int getInt() { return 1; }\n" 223 " static int getInt() { return 1; }\n"
256 " static double getDouble() { return 1.0; }\n" 224 " static double getDouble() { return 1.0; }\n"
257 " static bool getBool() { return false; }\n" 225 " static bool getBool() { return false; }\n"
258 " static getNull() { return null; }\n" 226 " static getNull() { return null; }\n"
259 "}\n"; 227 "}\n";
260 Dart_Handle result; 228 Dart_Handle result;
261 229
262 Dart_CreateIsolate(NULL, NULL); 230 TestIsolateScope __test_isolate__;
263 { 231 {
264 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
265
266 // Create a test library and Load up a test script in it. 232 // Create a test library and Load up a test script in it.
267 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 233 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
268 234
269 Dart_Handle class_name = Dart_NewString("NumberValuesHelper"); 235 Dart_Handle class_name = Dart_NewString("NumberValuesHelper");
270 // Check int case. 236 // Check int case.
271 result = Dart_InvokeStatic(lib, 237 result = Dart_InvokeStatic(lib,
272 class_name, 238 class_name,
273 Dart_NewString("getInt"), 239 Dart_NewString("getInt"),
274 0, 240 0,
275 NULL); 241 NULL);
(...skipping 19 matching lines...) Expand all
295 EXPECT(!Dart_IsNumber(result)); 261 EXPECT(!Dart_IsNumber(result));
296 262
297 // Check null case. 263 // Check null case.
298 result = Dart_InvokeStatic(lib, 264 result = Dart_InvokeStatic(lib,
299 class_name, 265 class_name,
300 Dart_NewString("getNull"), 266 Dart_NewString("getNull"),
301 0, 267 0,
302 NULL); 268 NULL);
303 EXPECT_VALID(result); 269 EXPECT_VALID(result);
304 EXPECT(!Dart_IsNumber(result)); 270 EXPECT(!Dart_IsNumber(result));
305
306 Dart_ExitScope(); // Exit the Dart API scope.
307 } 271 }
308 Dart_ShutdownIsolate();
309 } 272 }
310 273
311 #endif 274 #endif
312 275
313 276
314 UNIT_TEST_CASE(IntegerValues) { 277 UNIT_TEST_CASE(IntegerValues) {
315 Dart_CreateIsolate(NULL, NULL); 278 TestIsolateScope __test_isolate__;
316 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
317 279
318 const int64_t kIntegerVal1 = 100; 280 const int64_t kIntegerVal1 = 100;
319 const int64_t kIntegerVal2 = 0xffffffff; 281 const int64_t kIntegerVal2 = 0xffffffff;
320 const char* kIntegerVal3 = "0x123456789123456789123456789"; 282 const char* kIntegerVal3 = "0x123456789123456789123456789";
321 283
322 Dart_Handle val1 = Dart_NewInteger(kIntegerVal1); 284 Dart_Handle val1 = Dart_NewInteger(kIntegerVal1);
323 EXPECT(Dart_IsInteger(val1)); 285 EXPECT(Dart_IsInteger(val1));
324 bool fits = false; 286 bool fits = false;
325 Dart_Handle result = Dart_IntegerFitsIntoInt64(val1, &fits); 287 Dart_Handle result = Dart_IntegerFitsIntoInt64(val1, &fits);
326 EXPECT_VALID(result); 288 EXPECT_VALID(result);
(...skipping 17 matching lines...) Expand all
344 EXPECT_EQ(kIntegerVal1, out); 306 EXPECT_EQ(kIntegerVal1, out);
345 307
346 result = Dart_IntegerValue(val2, &out); 308 result = Dart_IntegerValue(val2, &out);
347 EXPECT_VALID(result); 309 EXPECT_VALID(result);
348 EXPECT_EQ(kIntegerVal2, out); 310 EXPECT_EQ(kIntegerVal2, out);
349 311
350 const char* chars = NULL; 312 const char* chars = NULL;
351 result = Dart_IntegerValueHexCString(val3, &chars); 313 result = Dart_IntegerValueHexCString(val3, &chars);
352 EXPECT_VALID(result); 314 EXPECT_VALID(result);
353 EXPECT(!strcmp(kIntegerVal3, chars)); 315 EXPECT(!strcmp(kIntegerVal3, chars));
354
355 Dart_ExitScope(); // Exit the Dart API scope.
356 Dart_ShutdownIsolate();
357 } 316 }
358 317
359 318
360 UNIT_TEST_CASE(ArrayValues) { 319 UNIT_TEST_CASE(ArrayValues) {
361 Dart_CreateIsolate(NULL, NULL); 320 TestIsolateScope __test_isolate__;
362 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
363 321
364 const int kArrayLength = 10; 322 const int kArrayLength = 10;
365 Dart_Handle str = Dart_NewString("test"); 323 Dart_Handle str = Dart_NewString("test");
366 EXPECT(!Dart_IsList(str)); 324 EXPECT(!Dart_IsList(str));
367 Dart_Handle val = Dart_NewList(kArrayLength); 325 Dart_Handle val = Dart_NewList(kArrayLength);
368 EXPECT(Dart_IsList(val)); 326 EXPECT(Dart_IsList(val));
369 intptr_t len = 0; 327 intptr_t len = 0;
370 Dart_Handle result = Dart_ListLength(val, &len); 328 Dart_Handle result = Dart_ListLength(val, &len);
371 EXPECT_VALID(result); 329 EXPECT_VALID(result);
372 EXPECT_EQ(kArrayLength, len); 330 EXPECT_EQ(kArrayLength, len);
(...skipping 13 matching lines...) Expand all
386 EXPECT_VALID(result); 344 EXPECT_VALID(result);
387 } 345 }
388 for (int i = 0; i < kArrayLength; i++) { 346 for (int i = 0; i < kArrayLength; i++) {
389 result = Dart_ListGetAt(val, i); 347 result = Dart_ListGetAt(val, i);
390 EXPECT_VALID(result); 348 EXPECT_VALID(result);
391 int64_t value; 349 int64_t value;
392 result = Dart_IntegerValue(result, &value); 350 result = Dart_IntegerValue(result, &value);
393 EXPECT_VALID(result); 351 EXPECT_VALID(result);
394 EXPECT_EQ(i, value); 352 EXPECT_EQ(i, value);
395 } 353 }
396
397 Dart_ExitScope(); // Exit the Dart API scope.
398 Dart_ShutdownIsolate();
399 } 354 }
400 355
401 356
402 UNIT_TEST_CASE(IsString) { 357 UNIT_TEST_CASE(IsString) {
403 Dart_CreateIsolate(NULL, NULL); 358 TestIsolateScope __test_isolate__;
404 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
405 359
406 uint8_t data8[] = { 'o', 'n', 'e', 0xFF }; 360 uint8_t data8[] = { 'o', 'n', 'e', 0xFF };
407 361
408 Dart_Handle str8 = Dart_NewString8(data8, ARRAY_SIZE(data8)); 362 Dart_Handle str8 = Dart_NewString8(data8, ARRAY_SIZE(data8));
409 EXPECT_VALID(str8); 363 EXPECT_VALID(str8);
410 EXPECT(Dart_IsString(str8)); 364 EXPECT(Dart_IsString(str8));
411 EXPECT(Dart_IsString8(str8)); 365 EXPECT(Dart_IsString8(str8));
412 EXPECT(Dart_IsString16(str8)); 366 EXPECT(Dart_IsString16(str8));
413 367
414 Dart_Handle ext8 = Dart_NewExternalString8(data8, ARRAY_SIZE(data8), 368 Dart_Handle ext8 = Dart_NewExternalString8(data8, ARRAY_SIZE(data8),
(...skipping 25 matching lines...) Expand all
440 EXPECT(Dart_IsString(str32)); 394 EXPECT(Dart_IsString(str32));
441 EXPECT(!Dart_IsString8(str32)); 395 EXPECT(!Dart_IsString8(str32));
442 EXPECT(!Dart_IsString16(str32)); 396 EXPECT(!Dart_IsString16(str32));
443 397
444 Dart_Handle ext32 = Dart_NewExternalString32(data32, ARRAY_SIZE(data32), 398 Dart_Handle ext32 = Dart_NewExternalString32(data32, ARRAY_SIZE(data32),
445 NULL, NULL); 399 NULL, NULL);
446 EXPECT_VALID(ext32); 400 EXPECT_VALID(ext32);
447 EXPECT(Dart_IsString(ext32)); 401 EXPECT(Dart_IsString(ext32));
448 EXPECT(!Dart_IsString8(ext32)); 402 EXPECT(!Dart_IsString8(ext32));
449 EXPECT(!Dart_IsString16(ext32)); 403 EXPECT(!Dart_IsString16(ext32));
450
451 Dart_ExitScope(); // Exit the Dart API scope.
452 Dart_ShutdownIsolate();
453 } 404 }
454 405
455 406
456 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests. 407 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests.
457 408
458 UNIT_TEST_CASE(ListAccess) { 409 UNIT_TEST_CASE(ListAccess) {
459 const char* kScriptChars = 410 const char* kScriptChars =
460 "class ListAccessTest {" 411 "class ListAccessTest {"
461 " ListAccessTest() {}" 412 " ListAccessTest() {}"
462 " static List testMain() {" 413 " static List testMain() {"
463 " List a = new List();" 414 " List a = new List();"
464 " a.add(10);" 415 " a.add(10);"
465 " a.add(20);" 416 " a.add(20);"
466 " a.add(30);" 417 " a.add(30);"
467 " return a;" 418 " return a;"
468 " }" 419 " }"
469 "}"; 420 "}";
470 Dart_Handle result; 421 Dart_Handle result;
471 422
472 Dart_CreateIsolate(NULL, NULL); 423 TestIsolateScope __test_isolate__;
473 { 424 {
474 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
475
476 // Create a test library and Load up a test script in it. 425 // Create a test library and Load up a test script in it.
477 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 426 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
478 427
479 // Invoke a function which returns an object of type InstanceOf.. 428 // Invoke a function which returns an object of type InstanceOf..
480 result = Dart_InvokeStatic(lib, 429 result = Dart_InvokeStatic(lib,
481 Dart_NewString("ListAccessTest"), 430 Dart_NewString("ListAccessTest"),
482 Dart_NewString("testMain"), 431 Dart_NewString("testMain"),
483 0, 432 0,
484 NULL); 433 NULL);
485 EXPECT_VALID(result); 434 EXPECT_VALID(result);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 EXPECT_EQ(30, native_array[2]); 516 EXPECT_EQ(30, native_array[2]);
568 result = Dart_ListGetAt(ListAccessTestObj, 2); 517 result = Dart_ListGetAt(ListAccessTestObj, 2);
569 EXPECT_VALID(result); 518 EXPECT_VALID(result);
570 result = Dart_IntegerValue(result, &value); 519 result = Dart_IntegerValue(result, &value);
571 EXPECT_VALID(result); 520 EXPECT_VALID(result);
572 EXPECT_EQ(30, value); 521 EXPECT_EQ(30, value);
573 522
574 // Check if we get an exception when accessing beyond limit. 523 // Check if we get an exception when accessing beyond limit.
575 result = Dart_ListGetAt(ListAccessTestObj, 4); 524 result = Dart_ListGetAt(ListAccessTestObj, 4);
576 EXPECT(Dart_IsError(result)); 525 EXPECT(Dart_IsError(result));
577
578 Dart_ExitScope(); // Exit the Dart API scope.
579 } 526 }
580 Dart_ShutdownIsolate();
581 } 527 }
582 528
583 #endif // TARGET_ARCH_IA32. 529 #endif // TARGET_ARCH_IA32.
584 530
585 531
586 // Unit test for entering a scope, creating a local handle and exiting 532 // Unit test for entering a scope, creating a local handle and exiting
587 // the scope. 533 // the scope.
588 UNIT_TEST_CASE(EnterExitScope) { 534 UNIT_TEST_CASE(EnterExitScope) {
589 Dart_CreateIsolate(NULL, NULL); 535 TestIsolateScope __test_isolate__;
536
590 Isolate* isolate = Isolate::Current(); 537 Isolate* isolate = Isolate::Current();
591 EXPECT(isolate != NULL); 538 EXPECT(isolate != NULL);
592 ApiState* state = isolate->api_state(); 539 ApiState* state = isolate->api_state();
593 EXPECT(state != NULL); 540 EXPECT(state != NULL);
594 ApiLocalScope* scope = state->top_scope(); 541 ApiLocalScope* scope = state->top_scope();
595 Dart_EnterScope(); 542 Dart_EnterScope();
596 { 543 {
597 EXPECT(state->top_scope() != NULL); 544 EXPECT(state->top_scope() != NULL);
598 DARTSCOPE(isolate); 545 DARTSCOPE(isolate);
599 const String& str1 = String::Handle(String::New("Test String")); 546 const String& str1 = String::Handle(String::New("Test String"));
600 Dart_Handle ref = Api::NewLocalHandle(str1); 547 Dart_Handle ref = Api::NewLocalHandle(str1);
601 String& str2 = String::Handle(); 548 String& str2 = String::Handle();
602 str2 ^= Api::UnwrapHandle(ref); 549 str2 ^= Api::UnwrapHandle(ref);
603 EXPECT(str1.Equals(str2)); 550 EXPECT(str1.Equals(str2));
604 } 551 }
605 Dart_ExitScope(); 552 Dart_ExitScope();
606 EXPECT(scope == state->top_scope()); 553 EXPECT(scope == state->top_scope());
607 Dart_ShutdownIsolate();
608 } 554 }
609 555
610 556
611 // Unit test for creating and deleting persistent handles. 557 // Unit test for creating and deleting persistent handles.
612 UNIT_TEST_CASE(PersistentHandles) { 558 UNIT_TEST_CASE(PersistentHandles) {
613 const char* kTestString1 = "Test String1"; 559 const char* kTestString1 = "Test String1";
614 const char* kTestString2 = "Test String2"; 560 const char* kTestString2 = "Test String2";
615 Dart_CreateIsolate(NULL, NULL); 561 TestCase::CreateTestIsolate();
616 Isolate* isolate = Isolate::Current(); 562 Isolate* isolate = Isolate::Current();
617 EXPECT(isolate != NULL); 563 EXPECT(isolate != NULL);
618 ApiState* state = isolate->api_state(); 564 ApiState* state = isolate->api_state();
619 EXPECT(state != NULL); 565 EXPECT(state != NULL);
620 ApiLocalScope* scope = state->top_scope(); 566 ApiLocalScope* scope = state->top_scope();
621 Dart_Handle handles[2000]; 567 Dart_Handle handles[2000];
622 Dart_EnterScope(); 568 Dart_EnterScope();
623 { 569 {
624 DARTSCOPE(isolate); 570 DARTSCOPE(isolate);
625 const String& str1 = String::Handle(String::New(kTestString1)); 571 const String& str1 = String::Handle(String::New(kTestString1));
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 } 617 }
672 EXPECT(scope == state->top_scope()); 618 EXPECT(scope == state->top_scope());
673 EXPECT_EQ(2000, state->CountPersistentHandles()); 619 EXPECT_EQ(2000, state->CountPersistentHandles());
674 Dart_ShutdownIsolate(); 620 Dart_ShutdownIsolate();
675 } 621 }
676 622
677 623
678 // Test that we are able to create a persistent handle from a 624 // Test that we are able to create a persistent handle from a
679 // persistent handle. 625 // persistent handle.
680 UNIT_TEST_CASE(NewPersistentHandle_FromPersistentHandle) { 626 UNIT_TEST_CASE(NewPersistentHandle_FromPersistentHandle) {
681 Dart_CreateIsolate(NULL, NULL); 627 TestIsolateScope __test_isolate__;
682 628
683 Isolate* isolate = Isolate::Current(); 629 Isolate* isolate = Isolate::Current();
684 EXPECT(isolate != NULL); 630 EXPECT(isolate != NULL);
685 ApiState* state = isolate->api_state(); 631 ApiState* state = isolate->api_state();
686 EXPECT(state != NULL); 632 EXPECT(state != NULL);
687 633
688 // Start with a known persistent handle. 634 // Start with a known persistent handle.
689 Dart_Handle obj1 = Dart_True(); 635 Dart_Handle obj1 = Dart_True();
690 EXPECT(state->IsValidPersistentHandle(obj1)); 636 EXPECT(state->IsValidPersistentHandle(obj1));
691 637
692 // And use it to allocate a second persistent handle. 638 // And use it to allocate a second persistent handle.
693 Dart_Handle obj2 = Dart_NewPersistentHandle(obj1); 639 Dart_Handle obj2 = Dart_NewPersistentHandle(obj1);
694 EXPECT(state->IsValidPersistentHandle(obj2)); 640 EXPECT(state->IsValidPersistentHandle(obj2));
695 641
696 // Make sure that the value transferred. 642 // Make sure that the value transferred.
697 EXPECT(Dart_IsBoolean(obj2)); 643 EXPECT(Dart_IsBoolean(obj2));
698 bool value = false; 644 bool value = false;
699 Dart_Handle result = Dart_BooleanValue(obj2, &value); 645 Dart_Handle result = Dart_BooleanValue(obj2, &value);
700 EXPECT_VALID(result); 646 EXPECT_VALID(result);
701 EXPECT(value); 647 EXPECT(value);
702
703 Dart_ShutdownIsolate();
704 } 648 }
705 649
706 650
707 // Unit test for creating multiple scopes and local handles within them. 651 // Unit test for creating multiple scopes and local handles within them.
708 // Ensure that the local handles get all cleaned out when exiting the 652 // Ensure that the local handles get all cleaned out when exiting the
709 // scope. 653 // scope.
710 UNIT_TEST_CASE(LocalHandles) { 654 UNIT_TEST_CASE(LocalHandles) {
711 Dart_CreateIsolate(NULL, NULL); 655 TestCase::CreateTestIsolate();
712 Isolate* isolate = Isolate::Current(); 656 Isolate* isolate = Isolate::Current();
713 EXPECT(isolate != NULL); 657 EXPECT(isolate != NULL);
714 ApiState* state = isolate->api_state(); 658 ApiState* state = isolate->api_state();
715 EXPECT(state != NULL); 659 EXPECT(state != NULL);
716 ApiLocalScope* scope = state->top_scope(); 660 ApiLocalScope* scope = state->top_scope();
717 Dart_Handle handles[300]; 661 Dart_Handle handles[300];
718 { 662 {
719 DARTSCOPE(isolate); 663 DARTSCOPE(isolate);
720 Smi& val = Smi::Handle(); 664 Smi& val = Smi::Handle();
721 665
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 EXPECT_EQ(0, state->CountLocalHandles()); 712 EXPECT_EQ(0, state->CountLocalHandles());
769 EXPECT(scope == state->top_scope()); 713 EXPECT(scope == state->top_scope());
770 Dart_ShutdownIsolate(); 714 Dart_ShutdownIsolate();
771 } 715 }
772 716
773 717
774 // Unit test for creating multiple scopes and allocating objects in the 718 // Unit test for creating multiple scopes and allocating objects in the
775 // zone for the scope. Ensure that the memory is freed when the scope 719 // zone for the scope. Ensure that the memory is freed when the scope
776 // exits. 720 // exits.
777 UNIT_TEST_CASE(LocalZoneMemory) { 721 UNIT_TEST_CASE(LocalZoneMemory) {
778 Dart_CreateIsolate(NULL, NULL); 722 TestCase::CreateTestIsolate();
779 Isolate* isolate = Isolate::Current(); 723 Isolate* isolate = Isolate::Current();
780 EXPECT(isolate != NULL); 724 EXPECT(isolate != NULL);
781 ApiState* state = isolate->api_state(); 725 ApiState* state = isolate->api_state();
782 EXPECT(state != NULL); 726 EXPECT(state != NULL);
783 ApiLocalScope* scope = state->top_scope(); 727 ApiLocalScope* scope = state->top_scope();
784 { 728 {
785 // Start a new scope and allocate some memory. 729 // Start a new scope and allocate some memory.
786 Dart_EnterScope(); 730 Dart_EnterScope();
787 for (int i = 0; i < 100; i++) { 731 for (int i = 0; i < 100; i++) {
788 Api::Allocate(16); 732 Api::Allocate(16);
(...skipping 25 matching lines...) Expand all
814 } 758 }
815 EXPECT_EQ(0, state->ZoneSizeInBytes()); 759 EXPECT_EQ(0, state->ZoneSizeInBytes());
816 EXPECT(scope == state->top_scope()); 760 EXPECT(scope == state->top_scope());
817 Dart_ShutdownIsolate(); 761 Dart_ShutdownIsolate();
818 } 762 }
819 763
820 764
821 UNIT_TEST_CASE(Isolates) { 765 UNIT_TEST_CASE(Isolates) {
822 // This test currently assumes that the Dart_Isolate type is an opaque 766 // This test currently assumes that the Dart_Isolate type is an opaque
823 // representation of Isolate*. 767 // representation of Isolate*.
824 Dart_Isolate iso_1 = Dart_CreateIsolate(NULL, NULL); 768 Dart_Isolate iso_1 = TestCase::CreateTestIsolate();
825 EXPECT_EQ(iso_1, Isolate::Current()); 769 EXPECT_EQ(iso_1, Isolate::Current());
826 Dart_Isolate isolate = Dart_CurrentIsolate(); 770 Dart_Isolate isolate = Dart_CurrentIsolate();
827 EXPECT_EQ(iso_1, isolate); 771 EXPECT_EQ(iso_1, isolate);
828 Dart_ExitIsolate(); 772 Dart_ExitIsolate();
829 EXPECT(NULL == Isolate::Current()); 773 EXPECT(NULL == Isolate::Current());
830 EXPECT(NULL == Dart_CurrentIsolate()); 774 EXPECT(NULL == Dart_CurrentIsolate());
831 Dart_Isolate iso_2 = Dart_CreateIsolate(NULL, NULL); 775 Dart_Isolate iso_2 = TestCase::CreateTestIsolate();
832 EXPECT_EQ(iso_2, Isolate::Current()); 776 EXPECT_EQ(iso_2, Isolate::Current());
833 Dart_ExitIsolate(); 777 Dart_ExitIsolate();
834 EXPECT(NULL == Isolate::Current()); 778 EXPECT(NULL == Isolate::Current());
835 Dart_EnterIsolate(iso_2); 779 Dart_EnterIsolate(iso_2);
836 EXPECT_EQ(iso_2, Isolate::Current()); 780 EXPECT_EQ(iso_2, Isolate::Current());
837 Dart_ShutdownIsolate(); 781 Dart_ShutdownIsolate();
838 EXPECT(NULL == Isolate::Current()); 782 EXPECT(NULL == Isolate::Current());
839 Dart_EnterIsolate(iso_1); 783 Dart_EnterIsolate(iso_1);
840 EXPECT_EQ(iso_1, Isolate::Current()); 784 EXPECT_EQ(iso_1, Isolate::Current());
841 Dart_ShutdownIsolate(); 785 Dart_ShutdownIsolate();
842 EXPECT(NULL == Isolate::Current()); 786 EXPECT(NULL == Isolate::Current());
843 } 787 }
844 788
845 789
846 static bool MyPostMessageCallback(Dart_Isolate dest_isolate, 790 static bool MyPostMessageCallback(Dart_Isolate dest_isolate,
847 Dart_Port send_port, 791 Dart_Port send_port,
848 Dart_Port reply_port, 792 Dart_Port reply_port,
849 Dart_Message message) { 793 Dart_Message message) {
850 return true; 794 return true;
851 } 795 }
852 796
853 797
854 static void MyClosePortCallback(Dart_Isolate dest_isolate, 798 static void MyClosePortCallback(Dart_Isolate dest_isolate,
855 Dart_Port port) { 799 Dart_Port port) {
856 } 800 }
857 801
858 802
859 UNIT_TEST_CASE(SetMessageCallbacks) { 803 UNIT_TEST_CASE(SetMessageCallbacks) {
860 Dart_Isolate dart_isolate = Dart_CreateIsolate(NULL, NULL); 804 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate();
861 Dart_SetMessageCallbacks(&MyPostMessageCallback, &MyClosePortCallback); 805 Dart_SetMessageCallbacks(&MyPostMessageCallback, &MyClosePortCallback);
862 Isolate* isolate = reinterpret_cast<Isolate*>(dart_isolate); 806 Isolate* isolate = reinterpret_cast<Isolate*>(dart_isolate);
863 EXPECT_EQ(&MyPostMessageCallback, isolate->post_message_callback()); 807 EXPECT_EQ(&MyPostMessageCallback, isolate->post_message_callback());
864 EXPECT_EQ(&MyClosePortCallback, isolate->close_port_callback()); 808 EXPECT_EQ(&MyClosePortCallback, isolate->close_port_callback());
865 Dart_ShutdownIsolate(); 809 Dart_ShutdownIsolate();
866 } 810 }
867 811
868 812
869 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests. 813 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests.
870 814
871 UNIT_TEST_CASE(FieldAccess) { 815 UNIT_TEST_CASE(FieldAccess) {
872 const char* kScriptChars = 816 const char* kScriptChars =
873 "class Fields {\n" 817 "class Fields {\n"
874 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n" 818 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n"
875 " int fld1;\n" 819 " int fld1;\n"
876 " final int fld2;\n" 820 " final int fld2;\n"
877 " static int fld3;\n" 821 " static int fld3;\n"
878 " static final int fld4 = 10;\n" 822 " static final int fld4 = 10;\n"
879 "}\n" 823 "}\n"
880 "class FieldsTest {\n" 824 "class FieldsTest {\n"
881 " static Fields testMain() {\n" 825 " static Fields testMain() {\n"
882 " Fields obj = new Fields(10, 20);\n" 826 " Fields obj = new Fields(10, 20);\n"
883 " return obj;\n" 827 " return obj;\n"
884 " }\n" 828 " }\n"
885 "}\n"; 829 "}\n";
886 Dart_Handle result; 830 Dart_Handle result;
887 831
888 Dart_CreateIsolate(NULL, NULL); 832 TestIsolateScope __test_isolate__;
889 { 833 {
890 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
891
892 // Create a test library and Load up a test script in it. 834 // Create a test library and Load up a test script in it.
893 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 835 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
894 836
895 // Invoke a function which returns an object. 837 // Invoke a function which returns an object.
896 Dart_Handle retobj = Dart_InvokeStatic(lib, 838 Dart_Handle retobj = Dart_InvokeStatic(lib,
897 Dart_NewString("FieldsTest"), 839 Dart_NewString("FieldsTest"),
898 Dart_NewString("testMain"), 840 Dart_NewString("testMain"),
899 0, 841 0,
900 NULL); 842 NULL);
901 EXPECT_VALID(retobj); 843 EXPECT_VALID(retobj);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
941 Dart_NewInteger(40)); 883 Dart_NewInteger(40));
942 EXPECT(Dart_IsError(result)); 884 EXPECT(Dart_IsError(result));
943 result = Dart_SetInstanceField(retobj, 885 result = Dart_SetInstanceField(retobj,
944 Dart_NewString("fld1"), 886 Dart_NewString("fld1"),
945 Dart_NewInteger(40)); 887 Dart_NewInteger(40));
946 EXPECT_VALID(result); 888 EXPECT_VALID(result);
947 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); 889 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1"));
948 EXPECT_VALID(result); 890 EXPECT_VALID(result);
949 result = Dart_IntegerValue(result, &value); 891 result = Dart_IntegerValue(result, &value);
950 EXPECT_EQ(40, value); 892 EXPECT_EQ(40, value);
951
952 Dart_ExitScope(); // Exit the Dart API scope.
953 } 893 }
954 Dart_ShutdownIsolate();
955 } 894 }
956 895
957 896
958 UNIT_TEST_CASE(HiddenFieldAccess) { 897 UNIT_TEST_CASE(HiddenFieldAccess) {
959 const char* kScriptChars = 898 const char* kScriptChars =
960 "class HiddenFields {\n" 899 "class HiddenFields {\n"
961 " HiddenFields(int i, int j) : _fld1 = i, _fld2 = j {}\n" 900 " HiddenFields(int i, int j) : _fld1 = i, _fld2 = j {}\n"
962 " int _fld1;\n" 901 " int _fld1;\n"
963 " final int _fld2;\n" 902 " final int _fld2;\n"
964 " static int _fld3;\n" 903 " static int _fld3;\n"
965 " static final int _fld4 = 10;\n" 904 " static final int _fld4 = 10;\n"
966 "}\n" 905 "}\n"
967 "class HiddenFieldsTest {\n" 906 "class HiddenFieldsTest {\n"
968 " static HiddenFields testMain() {\n" 907 " static HiddenFields testMain() {\n"
969 " HiddenFields obj = new HiddenFields(10, 20);\n" 908 " HiddenFields obj = new HiddenFields(10, 20);\n"
970 " return obj;\n" 909 " return obj;\n"
971 " }\n" 910 " }\n"
972 "}\n"; 911 "}\n";
973 Dart_Handle result; 912 Dart_Handle result;
974 913
975 Dart_CreateIsolate(NULL, NULL); 914 TestIsolateScope __test_isolate__;
976 { 915 {
977 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
978
979 // Load up a test script which extends the native wrapper class. 916 // Load up a test script which extends the native wrapper class.
980 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 917 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
981 918
982 // Invoke a function which returns an object. 919 // Invoke a function which returns an object.
983 Dart_Handle retobj = Dart_InvokeStatic(lib, 920 Dart_Handle retobj = Dart_InvokeStatic(lib,
984 Dart_NewString("HiddenFieldsTest"), 921 Dart_NewString("HiddenFieldsTest"),
985 Dart_NewString("testMain"), 922 Dart_NewString("testMain"),
986 0, 923 0,
987 NULL); 924 NULL);
988 EXPECT_VALID(retobj); 925 EXPECT_VALID(retobj);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 Dart_NewInteger(40)); 965 Dart_NewInteger(40));
1029 EXPECT(Dart_IsError(result)); 966 EXPECT(Dart_IsError(result));
1030 result = Dart_SetInstanceField(retobj, 967 result = Dart_SetInstanceField(retobj,
1031 Dart_NewString("_fld1"), 968 Dart_NewString("_fld1"),
1032 Dart_NewInteger(40)); 969 Dart_NewInteger(40));
1033 EXPECT_VALID(result); 970 EXPECT_VALID(result);
1034 result = Dart_GetInstanceField(retobj, Dart_NewString("_fld1")); 971 result = Dart_GetInstanceField(retobj, Dart_NewString("_fld1"));
1035 EXPECT_VALID(result); 972 EXPECT_VALID(result);
1036 result = Dart_IntegerValue(result, &value); 973 result = Dart_IntegerValue(result, &value);
1037 EXPECT_EQ(40, value); 974 EXPECT_EQ(40, value);
1038
1039 Dart_ExitScope(); // Exit the Dart API scope.
1040 } 975 }
1041 Dart_ShutdownIsolate();
1042 } 976 }
1043 977
1044 978
1045 void NativeFieldLookup(Dart_NativeArguments args) { 979 void NativeFieldLookup(Dart_NativeArguments args) {
1046 UNREACHABLE(); 980 UNREACHABLE();
1047 } 981 }
1048 982
1049 983
1050 static Dart_NativeFunction native_field_lookup(Dart_Handle name, 984 static Dart_NativeFunction native_field_lookup(Dart_Handle name,
1051 int argument_count) { 985 int argument_count) {
(...skipping 11 matching lines...) Expand all
1063 " static final int fld4 = 10;\n" 997 " static final int fld4 = 10;\n"
1064 "}\n" 998 "}\n"
1065 "class NativeFieldsTest {\n" 999 "class NativeFieldsTest {\n"
1066 " static NativeFields testMain() {\n" 1000 " static NativeFields testMain() {\n"
1067 " NativeFields obj = new NativeFields(10, 20);\n" 1001 " NativeFields obj = new NativeFields(10, 20);\n"
1068 " return obj;\n" 1002 " return obj;\n"
1069 " }\n" 1003 " }\n"
1070 "}\n"; 1004 "}\n";
1071 Dart_Handle result; 1005 Dart_Handle result;
1072 1006
1073 Dart_CreateIsolate(NULL, NULL); 1007 TestIsolateScope __test_isolate__;
1074 { 1008 {
1075 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1076 const int kNumNativeFields = 4; 1009 const int kNumNativeFields = 4;
1077 1010
1078 // Create a test library. 1011 // Create a test library.
1079 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 1012 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
1080 native_field_lookup); 1013 native_field_lookup);
1081 1014
1082 // Create a native wrapper class with native fields. 1015 // Create a native wrapper class with native fields.
1083 result = Dart_CreateNativeWrapperClass( 1016 result = Dart_CreateNativeWrapperClass(
1084 lib, 1017 lib,
1085 Dart_NewString("NativeFieldsWrapper"), 1018 Dart_NewString("NativeFieldsWrapper"),
(...skipping 14 matching lines...) Expand all
1100 const Class& cls = Class::Handle(obj.clazz()); 1033 const Class& cls = Class::Handle(obj.clazz());
1101 // We expect the newly created "NativeFields" object to have 1034 // We expect the newly created "NativeFields" object to have
1102 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. 1035 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields.
1103 // Hence the size of an instance of "NativeFields" should be 1036 // Hence the size of an instance of "NativeFields" should be
1104 // (kNumNativeFields + 2) * kWordSize + sizeof the header word. 1037 // (kNumNativeFields + 2) * kWordSize + sizeof the header word.
1105 // We check to make sure the instance size computed by the VM matches 1038 // We check to make sure the instance size computed by the VM matches
1106 // our expectations. 1039 // our expectations.
1107 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize, 1040 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize,
1108 kObjectAlignment), 1041 kObjectAlignment),
1109 cls.instance_size()); 1042 cls.instance_size());
1110
1111 Dart_ExitScope(); // Exit the Dart API scope.
1112 } 1043 }
1113 Dart_ShutdownIsolate();
1114 } 1044 }
1115 1045
1116 1046
1117 UNIT_TEST_CASE(InjectNativeFields2) { 1047 UNIT_TEST_CASE(InjectNativeFields2) {
1118 const char* kScriptChars = 1048 const char* kScriptChars =
1119 "class NativeFields extends NativeFieldsWrapper {\n" 1049 "class NativeFields extends NativeFieldsWrapper {\n"
1120 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 1050 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
1121 " int fld1;\n" 1051 " int fld1;\n"
1122 " final int fld2;\n" 1052 " final int fld2;\n"
1123 " static int fld3;\n" 1053 " static int fld3;\n"
1124 " static final int fld4 = 10;\n" 1054 " static final int fld4 = 10;\n"
1125 "}\n" 1055 "}\n"
1126 "class NativeFieldsTest {\n" 1056 "class NativeFieldsTest {\n"
1127 " static NativeFields testMain() {\n" 1057 " static NativeFields testMain() {\n"
1128 " NativeFields obj = new NativeFields(10, 20);\n" 1058 " NativeFields obj = new NativeFields(10, 20);\n"
1129 " return obj;\n" 1059 " return obj;\n"
1130 " }\n" 1060 " }\n"
1131 "}\n"; 1061 "}\n";
1132 Dart_Handle result; 1062 Dart_Handle result;
1133 1063
1134 Dart_CreateIsolate(NULL, NULL); 1064 TestIsolateScope __test_isolate__;
1135 { 1065 {
1136 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1137
1138 // Create a test library and Load up a test script in it. 1066 // Create a test library and Load up a test script in it.
1139 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1067 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1140 1068
1141 // Invoke a function which returns an object of type NativeFields. 1069 // Invoke a function which returns an object of type NativeFields.
1142 result = Dart_InvokeStatic(lib, 1070 result = Dart_InvokeStatic(lib,
1143 Dart_NewString("NativeFieldsTest"), 1071 Dart_NewString("NativeFieldsTest"),
1144 Dart_NewString("testMain"), 1072 Dart_NewString("testMain"),
1145 0, 1073 0,
1146 NULL); 1074 NULL);
1147 // We expect this to fail as class "NativeFields" extends 1075 // We expect this to fail as class "NativeFields" extends
1148 // "NativeFieldsWrapper" and there is no definition of it either 1076 // "NativeFieldsWrapper" and there is no definition of it either
1149 // in the dart code or through the native field injection mechanism. 1077 // in the dart code or through the native field injection mechanism.
1150 EXPECT(Dart_IsError(result)); 1078 EXPECT(Dart_IsError(result));
1151
1152 Dart_ExitScope(); // Exit the Dart API scope.
1153 } 1079 }
1154 Dart_ShutdownIsolate();
1155 } 1080 }
1156 1081
1157 1082
1158 UNIT_TEST_CASE(InjectNativeFields3) { 1083 UNIT_TEST_CASE(InjectNativeFields3) {
1159 const char* kScriptChars = 1084 const char* kScriptChars =
1160 "#import('dart:nativewrappers');" 1085 "#import('dart:nativewrappers');"
1161 "class NativeFields extends NativeFieldWrapperClass2 {\n" 1086 "class NativeFields extends NativeFieldWrapperClass2 {\n"
1162 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 1087 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
1163 " int fld1;\n" 1088 " int fld1;\n"
1164 " final int fld2;\n" 1089 " final int fld2;\n"
1165 " static int fld3;\n" 1090 " static int fld3;\n"
1166 " static final int fld4 = 10;\n" 1091 " static final int fld4 = 10;\n"
1167 "}\n" 1092 "}\n"
1168 "class NativeFieldsTest {\n" 1093 "class NativeFieldsTest {\n"
1169 " static NativeFields testMain() {\n" 1094 " static NativeFields testMain() {\n"
1170 " NativeFields obj = new NativeFields(10, 20);\n" 1095 " NativeFields obj = new NativeFields(10, 20);\n"
1171 " return obj;\n" 1096 " return obj;\n"
1172 " }\n" 1097 " }\n"
1173 "}\n"; 1098 "}\n";
1174 Dart_Handle result; 1099 Dart_Handle result;
1175 1100
1176 Dart_CreateIsolate(NULL, NULL); 1101 TestIsolateScope __test_isolate__;
1177 { 1102 {
1178 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1179 const int kNumNativeFields = 2; 1103 const int kNumNativeFields = 2;
1180 1104
1181 // Load up a test script in the test library. 1105 // Load up a test script in the test library.
1182 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 1106 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
1183 native_field_lookup); 1107 native_field_lookup);
1184 1108
1185 // Invoke a function which returns an object of type NativeFields. 1109 // Invoke a function which returns an object of type NativeFields.
1186 result = Dart_InvokeStatic(lib, 1110 result = Dart_InvokeStatic(lib,
1187 Dart_NewString("NativeFieldsTest"), 1111 Dart_NewString("NativeFieldsTest"),
1188 Dart_NewString("testMain"), 1112 Dart_NewString("testMain"),
1189 0, 1113 0,
1190 NULL); 1114 NULL);
1191 EXPECT_VALID(result); 1115 EXPECT_VALID(result);
1192 DARTSCOPE(Isolate::Current()); 1116 DARTSCOPE(Isolate::Current());
1193 Instance& obj = Instance::Handle(); 1117 Instance& obj = Instance::Handle();
1194 obj ^= Api::UnwrapHandle(result); 1118 obj ^= Api::UnwrapHandle(result);
1195 const Class& cls = Class::Handle(obj.clazz()); 1119 const Class& cls = Class::Handle(obj.clazz());
1196 // We expect the newly created "NativeFields" object to have 1120 // We expect the newly created "NativeFields" object to have
1197 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. 1121 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields.
1198 // Hence the size of an instance of "NativeFields" should be 1122 // Hence the size of an instance of "NativeFields" should be
1199 // (kNumNativeFields + 2) * kWordSize + sizeof the header word. 1123 // (kNumNativeFields + 2) * kWordSize + sizeof the header word.
1200 // We check to make sure the instance size computed by the VM matches 1124 // We check to make sure the instance size computed by the VM matches
1201 // our expectations. 1125 // our expectations.
1202 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize, 1126 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize,
1203 kObjectAlignment), 1127 kObjectAlignment),
1204 cls.instance_size()); 1128 cls.instance_size());
1205
1206 Dart_ExitScope(); // Exit the Dart API scope.
1207 } 1129 }
1208 Dart_ShutdownIsolate();
1209 } 1130 }
1210 1131
1211 1132
1212 UNIT_TEST_CASE(InjectNativeFields4) { 1133 UNIT_TEST_CASE(InjectNativeFields4) {
1213 const char* kScriptChars = 1134 const char* kScriptChars =
1214 "#import('dart:nativewrappers');" 1135 "#import('dart:nativewrappers');"
1215 "class NativeFields extends NativeFieldWrapperClass2 {\n" 1136 "class NativeFields extends NativeFieldWrapperClass2 {\n"
1216 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 1137 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
1217 " int fld1;\n" 1138 " int fld1;\n"
1218 " final int fld2;\n" 1139 " final int fld2;\n"
1219 " static int fld3;\n" 1140 " static int fld3;\n"
1220 " static final int fld4 = 10;\n" 1141 " static final int fld4 = 10;\n"
1221 "}\n" 1142 "}\n"
1222 "class NativeFieldsTest {\n" 1143 "class NativeFieldsTest {\n"
1223 " static NativeFields testMain() {\n" 1144 " static NativeFields testMain() {\n"
1224 " NativeFields obj = new NativeFields(10, 20);\n" 1145 " NativeFields obj = new NativeFields(10, 20);\n"
1225 " return obj;\n" 1146 " return obj;\n"
1226 " }\n" 1147 " }\n"
1227 "}\n"; 1148 "}\n";
1228 Dart_Handle result; 1149 Dart_Handle result;
1229 1150
1230 Dart_CreateIsolate(NULL, NULL); 1151 TestIsolateScope __test_isolate__;
1231 { 1152 {
1232 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1233
1234 // Load up a test script in the test library. 1153 // Load up a test script in the test library.
1235 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1154 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1236 1155
1237 // Invoke a function which returns an object of type NativeFields. 1156 // Invoke a function which returns an object of type NativeFields.
1238 result = Dart_InvokeStatic(lib, 1157 result = Dart_InvokeStatic(lib,
1239 Dart_NewString("NativeFieldsTest"), 1158 Dart_NewString("NativeFieldsTest"),
1240 Dart_NewString("testMain"), 1159 Dart_NewString("testMain"),
1241 0, 1160 0,
1242 NULL); 1161 NULL);
1243 // We expect the test script to fail finalization with the error below: 1162 // We expect the test script to fail finalization with the error below:
1244 EXPECT(Dart_IsError(result)); 1163 EXPECT(Dart_IsError(result));
1245 Dart_Handle expected_error = Dart_Error( 1164 Dart_Handle expected_error = Dart_Error(
1246 "'dart:test-lib': Error: class 'NativeFields' is trying to extend a " 1165 "'dart:test-lib': Error: class 'NativeFields' is trying to extend a "
1247 "native fields class, but library '%s' has no native resolvers", 1166 "native fields class, but library '%s' has no native resolvers",
1248 TestCase::url()); 1167 TestCase::url());
1249 EXPECT_STREQ(Dart_GetError(expected_error), Dart_GetError(result)); 1168 EXPECT_STREQ(Dart_GetError(expected_error), Dart_GetError(result));
1250 Dart_ExitScope(); // Exit the Dart API scope.
1251 } 1169 }
1252 Dart_ShutdownIsolate();
1253 } 1170 }
1254 1171
1255 1172
1256 static void TestNativeFields(Dart_Handle retobj) { 1173 static void TestNativeFields(Dart_Handle retobj) {
1257 // Access and set various instance fields of the object. 1174 // Access and set various instance fields of the object.
1258 Dart_Handle result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); 1175 Dart_Handle result = Dart_GetInstanceField(retobj, Dart_NewString("fld3"));
1259 EXPECT(Dart_IsError(result)); 1176 EXPECT(Dart_IsError(result));
1260 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); 1177 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1"));
1261 EXPECT_VALID(result); 1178 EXPECT_VALID(result);
1262 int64_t value = 0; 1179 int64_t value = 0;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 " static final int fld4 = 10;\n" 1251 " static final int fld4 = 10;\n"
1335 "}\n" 1252 "}\n"
1336 "class NativeFieldsTest {\n" 1253 "class NativeFieldsTest {\n"
1337 " static NativeFields testMain() {\n" 1254 " static NativeFields testMain() {\n"
1338 " NativeFields obj = new NativeFields(10, 20);\n" 1255 " NativeFields obj = new NativeFields(10, 20);\n"
1339 " return obj;\n" 1256 " return obj;\n"
1340 " }\n" 1257 " }\n"
1341 "}\n"; 1258 "}\n";
1342 Dart_Handle result; 1259 Dart_Handle result;
1343 1260
1344 Dart_CreateIsolate(NULL, NULL); 1261 TestIsolateScope __test_isolate__;
1345 { 1262 {
1346 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1347 const int kNumNativeFields = 4; 1263 const int kNumNativeFields = 4;
1348 1264
1349 // Create a test library. 1265 // Create a test library.
1350 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 1266 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
1351 native_field_lookup); 1267 native_field_lookup);
1352 1268
1353 // Create a native wrapper class with native fields. 1269 // Create a native wrapper class with native fields.
1354 result = Dart_CreateNativeWrapperClass( 1270 result = Dart_CreateNativeWrapperClass(
1355 lib, 1271 lib,
1356 Dart_NewString("NativeFieldsWrapper"), 1272 Dart_NewString("NativeFieldsWrapper"),
1357 kNumNativeFields); 1273 kNumNativeFields);
1358 1274
1359 // Load up a test script in it. 1275 // Load up a test script in it.
1360 1276
1361 // Invoke a function which returns an object of type NativeFields. 1277 // Invoke a function which returns an object of type NativeFields.
1362 Dart_Handle retobj = Dart_InvokeStatic(lib, 1278 Dart_Handle retobj = Dart_InvokeStatic(lib,
1363 Dart_NewString("NativeFieldsTest"), 1279 Dart_NewString("NativeFieldsTest"),
1364 Dart_NewString("testMain"), 1280 Dart_NewString("testMain"),
1365 0, 1281 0,
1366 NULL); 1282 NULL);
1367 EXPECT_VALID(retobj); 1283 EXPECT_VALID(retobj);
1368 1284
1369 // Now access and set various instance fields of the returned object. 1285 // Now access and set various instance fields of the returned object.
1370 TestNativeFields(retobj); 1286 TestNativeFields(retobj);
1371
1372 Dart_ExitScope(); // Exit the Dart API scope.
1373 } 1287 }
1374 Dart_ShutdownIsolate();
1375 } 1288 }
1376 1289
1377 1290
1378 UNIT_TEST_CASE(ImplicitNativeFieldAccess) { 1291 UNIT_TEST_CASE(ImplicitNativeFieldAccess) {
1379 const char* kScriptChars = 1292 const char* kScriptChars =
1380 "#import('dart:nativewrappers');" 1293 "#import('dart:nativewrappers');"
1381 "class NativeFields extends NativeFieldWrapperClass4 {\n" 1294 "class NativeFields extends NativeFieldWrapperClass4 {\n"
1382 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 1295 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
1383 " int fld1;\n" 1296 " int fld1;\n"
1384 " final int fld2;\n" 1297 " final int fld2;\n"
1385 " static int fld3;\n" 1298 " static int fld3;\n"
1386 " static final int fld4 = 10;\n" 1299 " static final int fld4 = 10;\n"
1387 "}\n" 1300 "}\n"
1388 "class NativeFieldsTest {\n" 1301 "class NativeFieldsTest {\n"
1389 " static NativeFields testMain() {\n" 1302 " static NativeFields testMain() {\n"
1390 " NativeFields obj = new NativeFields(10, 20);\n" 1303 " NativeFields obj = new NativeFields(10, 20);\n"
1391 " return obj;\n" 1304 " return obj;\n"
1392 " }\n" 1305 " }\n"
1393 "}\n"; 1306 "}\n";
1394 Dart_CreateIsolate(NULL, NULL); 1307 TestIsolateScope __test_isolate__;
1395 { 1308 {
1396 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1397
1398 // Load up a test script in the test library. 1309 // Load up a test script in the test library.
1399 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, 1310 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
1400 native_field_lookup); 1311 native_field_lookup);
1401 1312
1402 // Invoke a function which returns an object of type NativeFields. 1313 // Invoke a function which returns an object of type NativeFields.
1403 Dart_Handle retobj = Dart_InvokeStatic(lib, 1314 Dart_Handle retobj = Dart_InvokeStatic(lib,
1404 Dart_NewString("NativeFieldsTest"), 1315 Dart_NewString("NativeFieldsTest"),
1405 Dart_NewString("testMain"), 1316 Dart_NewString("testMain"),
1406 0, 1317 0,
1407 NULL); 1318 NULL);
1408 EXPECT_VALID(retobj); 1319 EXPECT_VALID(retobj);
1409 1320
1410 // Now access and set various instance fields of the returned object. 1321 // Now access and set various instance fields of the returned object.
1411 TestNativeFields(retobj); 1322 TestNativeFields(retobj);
1412
1413 Dart_ExitScope(); // Exit the Dart API scope.
1414 } 1323 }
1415 Dart_ShutdownIsolate();
1416 } 1324 }
1417 1325
1418 1326
1419 UNIT_TEST_CASE(NegativeNativeFieldAccess) { 1327 UNIT_TEST_CASE(NegativeNativeFieldAccess) {
1420 const char* kScriptChars = 1328 const char* kScriptChars =
1421 "class NativeFields {\n" 1329 "class NativeFields {\n"
1422 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" 1330 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
1423 " int fld1;\n" 1331 " int fld1;\n"
1424 " final int fld2;\n" 1332 " final int fld2;\n"
1425 " static int fld3;\n" 1333 " static int fld3;\n"
1426 " static final int fld4 = 10;\n" 1334 " static final int fld4 = 10;\n"
1427 "}\n" 1335 "}\n"
1428 "class NativeFieldsTest {\n" 1336 "class NativeFieldsTest {\n"
1429 " static NativeFields testMain1() {\n" 1337 " static NativeFields testMain1() {\n"
1430 " NativeFields obj = new NativeFields(10, 20);\n" 1338 " NativeFields obj = new NativeFields(10, 20);\n"
1431 " return obj;\n" 1339 " return obj;\n"
1432 " }\n" 1340 " }\n"
1433 " static Function testMain2() {\n" 1341 " static Function testMain2() {\n"
1434 " return function() {};\n" 1342 " return function() {};\n"
1435 " }\n" 1343 " }\n"
1436 "}\n"; 1344 "}\n";
1437 Dart_Handle result; 1345 Dart_Handle result;
1438 1346
1439 Dart_CreateIsolate(NULL, NULL); 1347 TestIsolateScope __test_isolate__;
1440 { 1348 {
1441 DARTSCOPE(Isolate::Current()); 1349 DARTSCOPE(Isolate::Current());
1442 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1443 1350
1444 // Create a test library and Load up a test script in it. 1351 // Create a test library and Load up a test script in it.
1445 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1352 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1446 1353
1447 // Invoke a function which returns an object of type NativeFields. 1354 // Invoke a function which returns an object of type NativeFields.
1448 Dart_Handle retobj = Dart_InvokeStatic(lib, 1355 Dart_Handle retobj = Dart_InvokeStatic(lib,
1449 Dart_NewString("NativeFieldsTest"), 1356 Dart_NewString("NativeFieldsTest"),
1450 Dart_NewString("testMain1"), 1357 Dart_NewString("testMain1"),
1451 0, 1358 0,
1452 NULL); 1359 NULL);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1490 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &value); 1397 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &value);
1491 EXPECT(Dart_IsError(result)); 1398 EXPECT(Dart_IsError(result));
1492 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &value); 1399 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &value);
1493 EXPECT(Dart_IsError(result)); 1400 EXPECT(Dart_IsError(result));
1494 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); 1401 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40);
1495 EXPECT(Dart_IsError(result)); 1402 EXPECT(Dart_IsError(result));
1496 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 40); 1403 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 40);
1497 EXPECT(Dart_IsError(result)); 1404 EXPECT(Dart_IsError(result));
1498 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 400); 1405 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 400);
1499 EXPECT(Dart_IsError(result)); 1406 EXPECT(Dart_IsError(result));
1500
1501 Dart_ExitScope(); // Exit the Dart API scope.
1502 } 1407 }
1503 Dart_ShutdownIsolate();
1504 } 1408 }
1505 1409
1506 1410
1507 UNIT_TEST_CASE(GetStaticField_RunsInitializer) { 1411 UNIT_TEST_CASE(GetStaticField_RunsInitializer) {
1508 const char* kScriptChars = 1412 const char* kScriptChars =
1509 "class TestClass {\n" 1413 "class TestClass {\n"
1510 " static final int fld1 = 7;\n" 1414 " static final int fld1 = 7;\n"
1511 " static int fld2 = 11;\n" 1415 " static int fld2 = 11;\n"
1512 " static void testMain() {\n" 1416 " static void testMain() {\n"
1513 " }\n" 1417 " }\n"
1514 "}\n"; 1418 "}\n";
1515 Dart_Handle result; 1419 Dart_Handle result;
1516 1420
1517 Dart_CreateIsolate(NULL, NULL); 1421 TestIsolateScope __test_isolate__;
1518 { 1422 {
1519 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1520
1521 // Create a test library and Load up a test script in it. 1423 // Create a test library and Load up a test script in it.
1522 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1424 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1523 1425
1524 // Invoke a function which returns an object. 1426 // Invoke a function which returns an object.
1525 result = Dart_InvokeStatic(lib, 1427 result = Dart_InvokeStatic(lib,
1526 Dart_NewString("TestClass"), 1428 Dart_NewString("TestClass"),
1527 Dart_NewString("testMain"), 1429 Dart_NewString("testMain"),
1528 0, 1430 0,
1529 NULL); 1431 NULL);
1530 1432
(...skipping 16 matching lines...) Expand all
1547 result = Dart_SetStaticField(cls, 1449 result = Dart_SetStaticField(cls,
1548 Dart_NewString("fld2"), 1450 Dart_NewString("fld2"),
1549 Dart_NewInteger(13)); 1451 Dart_NewInteger(13));
1550 EXPECT_VALID(result); 1452 EXPECT_VALID(result);
1551 1453
1552 // We now get the new value for fld2, not the initializer 1454 // We now get the new value for fld2, not the initializer
1553 result = Dart_GetStaticField(cls, Dart_NewString("fld2")); 1455 result = Dart_GetStaticField(cls, Dart_NewString("fld2"));
1554 EXPECT_VALID(result); 1456 EXPECT_VALID(result);
1555 result = Dart_IntegerValue(result, &value); 1457 result = Dart_IntegerValue(result, &value);
1556 EXPECT_EQ(13, value); 1458 EXPECT_EQ(13, value);
1557
1558 Dart_ExitScope(); // Exit the Dart API scope.
1559 } 1459 }
1560 Dart_ShutdownIsolate();
1561 } 1460 }
1562 1461
1563 1462
1564 UNIT_TEST_CASE(StaticFieldNotFound) { 1463 UNIT_TEST_CASE(StaticFieldNotFound) {
1565 const char* kScriptChars = 1464 const char* kScriptChars =
1566 "class TestClass {\n" 1465 "class TestClass {\n"
1567 " static void testMain() {\n" 1466 " static void testMain() {\n"
1568 " }\n" 1467 " }\n"
1569 "}\n"; 1468 "}\n";
1570 Dart_Handle result; 1469 Dart_Handle result;
1571 1470
1572 Dart_CreateIsolate(NULL, NULL); 1471 TestIsolateScope __test_isolate__;
1573 { 1472 {
1574 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1575
1576 // Create a test library and Load up a test script in it. 1473 // Create a test library and Load up a test script in it.
1577 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1474 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1578 1475
1579 // Invoke a function. 1476 // Invoke a function.
1580 result = Dart_InvokeStatic(lib, 1477 result = Dart_InvokeStatic(lib,
1581 Dart_NewString("TestClass"), 1478 Dart_NewString("TestClass"),
1582 Dart_NewString("testMain"), 1479 Dart_NewString("testMain"),
1583 0, 1480 0,
1584 NULL); 1481 NULL);
1585 1482
1586 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("TestClass")); 1483 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("TestClass"));
1587 EXPECT_VALID(cls); 1484 EXPECT_VALID(cls);
1588 1485
1589 result = Dart_GetStaticField(cls, Dart_NewString("not_found")); 1486 result = Dart_GetStaticField(cls, Dart_NewString("not_found"));
1590 EXPECT(Dart_IsError(result)); 1487 EXPECT(Dart_IsError(result));
1591 EXPECT_STREQ("Specified field is not found in the class", 1488 EXPECT_STREQ("Specified field is not found in the class",
1592 Dart_GetError(result)); 1489 Dart_GetError(result));
1593 1490
1594 result = Dart_SetStaticField(cls, 1491 result = Dart_SetStaticField(cls,
1595 Dart_NewString("not_found"), 1492 Dart_NewString("not_found"),
1596 Dart_NewInteger(13)); 1493 Dart_NewInteger(13));
1597 EXPECT(Dart_IsError(result)); 1494 EXPECT(Dart_IsError(result));
1598 EXPECT_STREQ("Specified field is not found in the class", 1495 EXPECT_STREQ("Specified field is not found in the class",
1599 Dart_GetError(result)); 1496 Dart_GetError(result));
1600
1601 Dart_ExitScope(); // Exit the Dart API scope.
1602 } 1497 }
1603 Dart_ShutdownIsolate();
1604 } 1498 }
1605 1499
1606 1500
1607 UNIT_TEST_CASE(InvokeDynamic) { 1501 UNIT_TEST_CASE(InvokeDynamic) {
1608 const char* kScriptChars = 1502 const char* kScriptChars =
1609 "class InvokeDynamic {\n" 1503 "class InvokeDynamic {\n"
1610 " InvokeDynamic(int i, int j) : fld1 = i, fld2 = j {}\n" 1504 " InvokeDynamic(int i, int j) : fld1 = i, fld2 = j {}\n"
1611 " int method1(int i) { return i + fld1 + fld2 + fld4; }\n" 1505 " int method1(int i) { return i + fld1 + fld2 + fld4; }\n"
1612 " static int method2(int i) { return i + fld4; }\n" 1506 " static int method2(int i) { return i + fld4; }\n"
1613 " int fld1;\n" 1507 " int fld1;\n"
1614 " final int fld2;\n" 1508 " final int fld2;\n"
1615 " static final int fld4 = 10;\n" 1509 " static final int fld4 = 10;\n"
1616 "}\n" 1510 "}\n"
1617 "class InvokeDynamicTest {\n" 1511 "class InvokeDynamicTest {\n"
1618 " static InvokeDynamic testMain() {\n" 1512 " static InvokeDynamic testMain() {\n"
1619 " InvokeDynamic obj = new InvokeDynamic(10, 20);\n" 1513 " InvokeDynamic obj = new InvokeDynamic(10, 20);\n"
1620 " return obj;\n" 1514 " return obj;\n"
1621 " }\n" 1515 " }\n"
1622 "}\n"; 1516 "}\n";
1623 Dart_Handle result; 1517 Dart_Handle result;
1624 1518
1625 Dart_CreateIsolate(NULL, NULL); 1519 TestIsolateScope __test_isolate__;
1626 { 1520 {
1627 DARTSCOPE(Isolate::Current()); 1521 DARTSCOPE(Isolate::Current());
1628 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1629 1522
1630 // Create a test library and Load up a test script in it. 1523 // Create a test library and Load up a test script in it.
1631 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1524 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1632 1525
1633 // Invoke a function which returns an object of type InvokeDynamic. 1526 // Invoke a function which returns an object of type InvokeDynamic.
1634 Dart_Handle retobj = Dart_InvokeStatic(lib, 1527 Dart_Handle retobj = Dart_InvokeStatic(lib,
1635 Dart_NewString("InvokeDynamicTest"), 1528 Dart_NewString("InvokeDynamicTest"),
1636 Dart_NewString("testMain"), 1529 Dart_NewString("testMain"),
1637 0, 1530 0,
1638 NULL); 1531 NULL);
(...skipping 11 matching lines...) Expand all
1650 EXPECT(Dart_IsInteger(result)); 1543 EXPECT(Dart_IsInteger(result));
1651 int64_t value = 0; 1544 int64_t value = 0;
1652 result = Dart_IntegerValue(result, &value); 1545 result = Dart_IntegerValue(result, &value);
1653 EXPECT_EQ(41, value); 1546 EXPECT_EQ(41, value);
1654 1547
1655 result = Dart_InvokeDynamic(retobj, Dart_NewString("method2"), 0, NULL); 1548 result = Dart_InvokeDynamic(retobj, Dart_NewString("method2"), 0, NULL);
1656 EXPECT(Dart_IsError(result)); 1549 EXPECT(Dart_IsError(result));
1657 1550
1658 result = Dart_InvokeDynamic(retobj, Dart_NewString("method1"), 0, NULL); 1551 result = Dart_InvokeDynamic(retobj, Dart_NewString("method1"), 0, NULL);
1659 EXPECT(Dart_IsError(result)); 1552 EXPECT(Dart_IsError(result));
1660
1661 Dart_ExitScope(); // Exit the Dart API scope.
1662 } 1553 }
1663 Dart_ShutdownIsolate();
1664 } 1554 }
1665 1555
1666 1556
1667 UNIT_TEST_CASE(InvokeClosure) { 1557 UNIT_TEST_CASE(InvokeClosure) {
1668 const char* kScriptChars = 1558 const char* kScriptChars =
1669 "class InvokeClosure {\n" 1559 "class InvokeClosure {\n"
1670 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" 1560 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n"
1671 " Function method1(int i) {\n" 1561 " Function method1(int i) {\n"
1672 " f(int j) => j + i + fld1 + fld2 + fld4; \n" 1562 " f(int j) => j + i + fld1 + fld2 + fld4; \n"
1673 " return f;\n" 1563 " return f;\n"
(...skipping 10 matching lines...) Expand all
1684 " static Function testMain1() {\n" 1574 " static Function testMain1() {\n"
1685 " InvokeClosure obj = new InvokeClosure(10, 20);\n" 1575 " InvokeClosure obj = new InvokeClosure(10, 20);\n"
1686 " return obj.method1(10);\n" 1576 " return obj.method1(10);\n"
1687 " }\n" 1577 " }\n"
1688 " static Function testMain2() {\n" 1578 " static Function testMain2() {\n"
1689 " return InvokeClosure.method2(10);\n" 1579 " return InvokeClosure.method2(10);\n"
1690 " }\n" 1580 " }\n"
1691 "}\n"; 1581 "}\n";
1692 Dart_Handle result; 1582 Dart_Handle result;
1693 1583
1694 Dart_CreateIsolate(NULL, NULL); 1584 TestIsolateScope __test_isolate__;
1695 { 1585 {
1696 DARTSCOPE(Isolate::Current()); 1586 DARTSCOPE(Isolate::Current());
1697 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1698 1587
1699 // Create a test library and Load up a test script in it. 1588 // Create a test library and Load up a test script in it.
1700 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1589 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1701 1590
1702 // Invoke a function which returns a closure. 1591 // Invoke a function which returns a closure.
1703 Dart_Handle retobj = Dart_InvokeStatic(lib, 1592 Dart_Handle retobj = Dart_InvokeStatic(lib,
1704 Dart_NewString("InvokeClosureTest"), 1593 Dart_NewString("InvokeClosureTest"),
1705 Dart_NewString("testMain1"), 1594 Dart_NewString("testMain1"),
1706 0, 1595 0,
1707 NULL); 1596 NULL);
(...skipping 26 matching lines...) Expand all
1734 EXPECT_VALID(retobj); 1623 EXPECT_VALID(retobj);
1735 1624
1736 EXPECT(Dart_IsClosure(retobj)); 1625 EXPECT(Dart_IsClosure(retobj));
1737 EXPECT(!Dart_IsClosure(Dart_NewString("abcdef"))); 1626 EXPECT(!Dart_IsClosure(Dart_NewString("abcdef")));
1738 1627
1739 // Now invoke the closure and check the result (should be an exception). 1628 // Now invoke the closure and check the result (should be an exception).
1740 dart_arguments[0] = Dart_NewInteger(1); 1629 dart_arguments[0] = Dart_NewInteger(1);
1741 result = Dart_InvokeClosure(retobj, 1, dart_arguments); 1630 result = Dart_InvokeClosure(retobj, 1, dart_arguments);
1742 EXPECT(Dart_IsError(result)); 1631 EXPECT(Dart_IsError(result));
1743 EXPECT(Dart_ErrorHasException(result)); 1632 EXPECT(Dart_ErrorHasException(result));
1744
1745 Dart_ExitScope(); // Exit the Dart API scope.
1746 } 1633 }
1747 Dart_ShutdownIsolate();
1748 } 1634 }
1749 1635
1750 1636
1751 void ExceptionNative(Dart_NativeArguments args) { 1637 void ExceptionNative(Dart_NativeArguments args) {
1752 Dart_Handle param = Dart_GetNativeArgument(args, 0); 1638 Dart_Handle param = Dart_GetNativeArgument(args, 0);
1753 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. 1639 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1754 char* str = reinterpret_cast<char*>(Api::Allocate(1024)); 1640 char* str = reinterpret_cast<char*>(Api::Allocate(1024));
1755 str[0] = 0; 1641 str[0] = 0;
1756 Dart_ThrowException(param); 1642 Dart_ThrowException(param);
1757 UNREACHABLE(); 1643 UNREACHABLE();
(...skipping 16 matching lines...) Expand all
1774 " int fld1;\n" 1660 " int fld1;\n"
1775 "}\n" 1661 "}\n"
1776 "class ThrowExceptionTest {\n" 1662 "class ThrowExceptionTest {\n"
1777 " static ThrowException testMain() {\n" 1663 " static ThrowException testMain() {\n"
1778 " ThrowException obj = new ThrowException(10);\n" 1664 " ThrowException obj = new ThrowException(10);\n"
1779 " return obj;\n" 1665 " return obj;\n"
1780 " }\n" 1666 " }\n"
1781 "}\n"; 1667 "}\n";
1782 Dart_Handle result; 1668 Dart_Handle result;
1783 1669
1784 Dart_CreateIsolate(NULL, NULL); 1670 TestIsolateScope __test_isolate__;
1671
1785 Isolate* isolate = Isolate::Current(); 1672 Isolate* isolate = Isolate::Current();
1786 EXPECT(isolate != NULL); 1673 EXPECT(isolate != NULL);
1787 ApiState* state = isolate->api_state(); 1674 ApiState* state = isolate->api_state();
1788 EXPECT(state != NULL); 1675 EXPECT(state != NULL);
1789 { 1676 {
1790 intptr_t size = state->ZoneSizeInBytes(); 1677 intptr_t size = state->ZoneSizeInBytes();
1791 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. 1678 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1792 1679
1793 // Load up a test script which extends the native wrapper class. 1680 // Load up a test script which extends the native wrapper class.
1794 Dart_Handle lib = TestCase::LoadTestScript( 1681 Dart_Handle lib = TestCase::LoadTestScript(
(...skipping 18 matching lines...) Expand all
1813 result = Dart_InvokeDynamic(retobj, Dart_NewString("method2"), 0, NULL); 1700 result = Dart_InvokeDynamic(retobj, Dart_NewString("method2"), 0, NULL);
1814 EXPECT_VALID(result); 1701 EXPECT_VALID(result);
1815 EXPECT(Dart_IsInteger(result)); 1702 EXPECT(Dart_IsInteger(result));
1816 int64_t value = 0; 1703 int64_t value = 0;
1817 result = Dart_IntegerValue(result, &value); 1704 result = Dart_IntegerValue(result, &value);
1818 EXPECT_EQ(5, value); 1705 EXPECT_EQ(5, value);
1819 1706
1820 Dart_ExitScope(); // Exit the Dart API scope. 1707 Dart_ExitScope(); // Exit the Dart API scope.
1821 EXPECT_EQ(size, state->ZoneSizeInBytes()); 1708 EXPECT_EQ(size, state->ZoneSizeInBytes());
1822 } 1709 }
1823 Dart_ShutdownIsolate();
1824 } 1710 }
1825 1711
1826 1712
1827 void NativeArgumentCounter(Dart_NativeArguments args) { 1713 void NativeArgumentCounter(Dart_NativeArguments args) {
1828 Dart_EnterScope(); 1714 Dart_EnterScope();
1829 int count = Dart_GetNativeArgumentCount(args); 1715 int count = Dart_GetNativeArgumentCount(args);
1830 Dart_SetReturnValue(args, Dart_NewInteger(count)); 1716 Dart_SetReturnValue(args, Dart_NewInteger(count));
1831 Dart_ExitScope(); 1717 Dart_ExitScope();
1832 } 1718 }
1833 1719
1834 1720
1835 static Dart_NativeFunction gnac_lookup(Dart_Handle name, int argument_count) { 1721 static Dart_NativeFunction gnac_lookup(Dart_Handle name, int argument_count) {
1836 return reinterpret_cast<Dart_NativeFunction>(&NativeArgumentCounter); 1722 return reinterpret_cast<Dart_NativeFunction>(&NativeArgumentCounter);
1837 } 1723 }
1838 1724
1839 1725
1840 UNIT_TEST_CASE(GetNativeArgumentCount) { 1726 UNIT_TEST_CASE(GetNativeArgumentCount) {
1841 const char* kScriptChars = 1727 const char* kScriptChars =
1842 "class MyObject {" 1728 "class MyObject {"
1843 " int method1(int i, int j) native 'Name_Does_Not_Matter';" 1729 " int method1(int i, int j) native 'Name_Does_Not_Matter';"
1844 "}" 1730 "}"
1845 "class Test {" 1731 "class Test {"
1846 " static testMain() {" 1732 " static testMain() {"
1847 " MyObject obj = new MyObject();" 1733 " MyObject obj = new MyObject();"
1848 " return obj.method1(77, 125);" 1734 " return obj.method1(77, 125);"
1849 " }" 1735 " }"
1850 "}"; 1736 "}";
1851 1737
1852 Dart_CreateIsolate(NULL, NULL); 1738 TestIsolateScope __test_isolate__;
1853 { 1739 {
1854 Dart_EnterScope();
1855 Dart_Handle lib = TestCase::LoadTestScript( 1740 Dart_Handle lib = TestCase::LoadTestScript(
1856 kScriptChars, 1741 kScriptChars,
1857 reinterpret_cast<Dart_NativeEntryResolver>(gnac_lookup)); 1742 reinterpret_cast<Dart_NativeEntryResolver>(gnac_lookup));
1858 1743
1859 Dart_Handle result = Dart_InvokeStatic(lib, 1744 Dart_Handle result = Dart_InvokeStatic(lib,
1860 Dart_NewString("Test"), 1745 Dart_NewString("Test"),
1861 Dart_NewString("testMain"), 1746 Dart_NewString("testMain"),
1862 0, 1747 0,
1863 NULL); 1748 NULL);
1864 EXPECT_VALID(result); 1749 EXPECT_VALID(result);
1865 EXPECT(Dart_IsInteger(result)); 1750 EXPECT(Dart_IsInteger(result));
1866 1751
1867 int64_t value = 0; 1752 int64_t value = 0;
1868 result = Dart_IntegerValue(result, &value); 1753 result = Dart_IntegerValue(result, &value);
1869 EXPECT_VALID(result); 1754 EXPECT_VALID(result);
1870 EXPECT_EQ(3, value); 1755 EXPECT_EQ(3, value);
1871
1872 Dart_ExitScope();
1873 } 1756 }
1874 Dart_ShutdownIsolate();
1875 } 1757 }
1876 1758
1877 1759
1878 UNIT_TEST_CASE(GetClass) { 1760 UNIT_TEST_CASE(GetClass) {
1879 const char* kScriptChars = 1761 const char* kScriptChars =
1880 "class DoesExist {" 1762 "class DoesExist {"
1881 "}"; 1763 "}";
1882 1764
1883 Dart_CreateIsolate(NULL, NULL); 1765 TestIsolateScope __test_isolate__;
1884 Dart_EnterScope(); 1766
1885 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1767 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1886 1768
1887 // Lookup a class that does exist. 1769 // Lookup a class that does exist.
1888 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("DoesExist")); 1770 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("DoesExist"));
1889 EXPECT_VALID(cls); 1771 EXPECT_VALID(cls);
1890 1772
1891 // Lookup a class that does not exist. 1773 // Lookup a class that does not exist.
1892 cls = Dart_GetClass(lib, Dart_NewString("DoesNotExist")); 1774 cls = Dart_GetClass(lib, Dart_NewString("DoesNotExist"));
1893 EXPECT(Dart_IsError(cls)); 1775 EXPECT(Dart_IsError(cls));
1894 EXPECT_STREQ("Class 'DoesNotExist' not found in library 'dart:test-lib'.", 1776 EXPECT_STREQ("Class 'DoesNotExist' not found in library 'dart:test-lib'.",
1895 Dart_GetError(cls)); 1777 Dart_GetError(cls));
1896
1897 Dart_ExitScope();
1898 Dart_ShutdownIsolate();
1899 } 1778 }
1900 1779
1901 1780
1902 UNIT_TEST_CASE(InstanceOf) { 1781 UNIT_TEST_CASE(InstanceOf) {
1903 const char* kScriptChars = 1782 const char* kScriptChars =
1904 "class OtherClass {\n" 1783 "class OtherClass {\n"
1905 " static returnNull() { return null; }\n" 1784 " static returnNull() { return null; }\n"
1906 "}\n" 1785 "}\n"
1907 "class InstanceOfTest {\n" 1786 "class InstanceOfTest {\n"
1908 " InstanceOfTest() {}\n" 1787 " InstanceOfTest() {}\n"
1909 " static InstanceOfTest testMain() {\n" 1788 " static InstanceOfTest testMain() {\n"
1910 " return new InstanceOfTest();\n" 1789 " return new InstanceOfTest();\n"
1911 " }\n" 1790 " }\n"
1912 "}\n"; 1791 "}\n";
1913 Dart_Handle result; 1792 Dart_Handle result;
1914 1793
1915 Dart_CreateIsolate(NULL, NULL); 1794 TestIsolateScope __test_isolate__;
1916 { 1795 {
1917 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
1918
1919 // Create a test library and Load up a test script in it. 1796 // Create a test library and Load up a test script in it.
1920 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1797 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1921 1798
1922 // Invoke a function which returns an object of type InstanceOf.. 1799 // Invoke a function which returns an object of type InstanceOf..
1923 Dart_Handle instanceOfTestObj = 1800 Dart_Handle instanceOfTestObj =
1924 Dart_InvokeStatic(lib, 1801 Dart_InvokeStatic(lib,
1925 Dart_NewString("InstanceOfTest"), 1802 Dart_NewString("InstanceOfTest"),
1926 Dart_NewString("testMain"), 1803 Dart_NewString("testMain"),
1927 0, 1804 0,
1928 NULL); 1805 NULL);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1969 NULL); 1846 NULL);
1970 EXPECT_VALID(null); 1847 EXPECT_VALID(null);
1971 1848
1972 result = Dart_ObjectIsType(null, otherClass, &is_instance); 1849 result = Dart_ObjectIsType(null, otherClass, &is_instance);
1973 EXPECT_VALID(result); 1850 EXPECT_VALID(result);
1974 EXPECT(!is_instance); 1851 EXPECT(!is_instance);
1975 1852
1976 // Check that error is returned if null is passed as a class argument. 1853 // Check that error is returned if null is passed as a class argument.
1977 result = Dart_ObjectIsType(null, null, &is_instance); 1854 result = Dart_ObjectIsType(null, null, &is_instance);
1978 EXPECT(Dart_IsError(result)); 1855 EXPECT(Dart_IsError(result));
1979
1980 Dart_ExitScope(); // Exit the Dart API scope.
1981 } 1856 }
1982 Dart_ShutdownIsolate();
1983 } 1857 }
1984 1858
1985 1859
1986 UNIT_TEST_CASE(NullReceiver) { 1860 UNIT_TEST_CASE(NullReceiver) {
1987 Dart_CreateIsolate(NULL, NULL); 1861 TestIsolateScope __test_isolate__;
1988 Dart_EnterScope(); // Enter a Dart API scope for the unit test.
1989 { 1862 {
1990 DARTSCOPE(Isolate::Current()); 1863 DARTSCOPE(Isolate::Current());
1991 1864
1992 Dart_Handle function_name = Dart_NewString("toString"); 1865 Dart_Handle function_name = Dart_NewString("toString");
1993 const int number_of_arguments = 0; 1866 const int number_of_arguments = 0;
1994 Dart_Handle null_receiver = Api::NewLocalHandle(Object::Handle()); 1867 Dart_Handle null_receiver = Api::NewLocalHandle(Object::Handle());
1995 Dart_Handle result = Dart_InvokeDynamic(null_receiver, 1868 Dart_Handle result = Dart_InvokeDynamic(null_receiver,
1996 function_name, 1869 function_name,
1997 number_of_arguments, 1870 number_of_arguments,
1998 NULL); 1871 NULL);
1999 EXPECT_VALID(result); 1872 EXPECT_VALID(result);
2000 EXPECT(Dart_IsString(result)); 1873 EXPECT(Dart_IsString(result));
2001 1874
2002 // Should throw a NullPointerException. Disabled due to bug 5415268. 1875 // Should throw a NullPointerException. Disabled due to bug 5415268.
2003 /* 1876 /*
2004 Dart_Handle function_name2 = Dart_NewString("NoNoNo"); 1877 Dart_Handle function_name2 = Dart_NewString("NoNoNo");
2005 result = Dart_InvokeDynamic(null_receiver, 1878 result = Dart_InvokeDynamic(null_receiver,
2006 function_name2, 1879 function_name2,
2007 number_of_arguments, 1880 number_of_arguments,
2008 dart_arguments); 1881 dart_arguments);
2009 EXPECT(Dart_IsError(result)); 1882 EXPECT(Dart_IsError(result));
2010 EXPECT(Dart_ErrorHasException(result)); */ 1883 EXPECT(Dart_ErrorHasException(result)); */
2011 } 1884 }
2012 Dart_ExitScope(); // Exit the Dart API scope.
2013 Dart_ShutdownIsolate();
2014 } 1885 }
2015 1886
2016 1887
2017 static Dart_Handle library_handler(Dart_LibraryTag tag, 1888 static Dart_Handle library_handler(Dart_LibraryTag tag,
2018 Dart_Handle library, 1889 Dart_Handle library,
2019 Dart_Handle url) { 1890 Dart_Handle url) {
2020 if (tag == kCanonicalizeUrl) { 1891 if (tag == kCanonicalizeUrl) {
2021 return url; 1892 return url;
2022 } 1893 }
2023 return Api::Success(); 1894 return Api::Success();
2024 } 1895 }
2025 1896
2026 1897
2027 UNIT_TEST_CASE(LoadScript) { 1898 UNIT_TEST_CASE(LoadScript) {
2028 const char* kScriptChars = 1899 const char* kScriptChars =
2029 "main() {" 1900 "main() {"
2030 " return 12345;" 1901 " return 12345;"
2031 "}"; 1902 "}";
2032 1903
2033 Dart_CreateIsolate(NULL, NULL); 1904 TestIsolateScope __test_isolate__;
2034 Dart_EnterScope();
2035 1905
2036 Dart_Handle url = Dart_NewString(TestCase::url()); 1906 Dart_Handle url = Dart_NewString(TestCase::url());
2037 Dart_Handle source = Dart_NewString(kScriptChars); 1907 Dart_Handle source = Dart_NewString(kScriptChars);
2038 Dart_Handle error = Dart_Error("incoming error"); 1908 Dart_Handle error = Dart_Error("incoming error");
2039 Dart_Handle result; 1909 Dart_Handle result;
2040 1910
2041 result = Dart_LoadScript(Dart_Null(), source, library_handler); 1911 result = Dart_LoadScript(Dart_Null(), source, library_handler);
2042 EXPECT(Dart_IsError(result)); 1912 EXPECT(Dart_IsError(result));
2043 EXPECT_STREQ("Dart_LoadScript expects argument 'url' to be non-null.", 1913 EXPECT_STREQ("Dart_LoadScript expects argument 'url' to be non-null.",
2044 Dart_GetError(result)); 1914 Dart_GetError(result));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2081 int64_t value = 0; 1951 int64_t value = 0;
2082 EXPECT_VALID(Dart_IntegerValue(result, &value)); 1952 EXPECT_VALID(Dart_IntegerValue(result, &value));
2083 EXPECT_EQ(12345, value); 1953 EXPECT_EQ(12345, value);
2084 1954
2085 // Further calls to LoadScript are errors. 1955 // Further calls to LoadScript are errors.
2086 result = Dart_LoadScript(url, source, library_handler); 1956 result = Dart_LoadScript(url, source, library_handler);
2087 EXPECT(Dart_IsError(result)); 1957 EXPECT(Dart_IsError(result));
2088 EXPECT_STREQ("Dart_LoadScript: " 1958 EXPECT_STREQ("Dart_LoadScript: "
2089 "A script has already been loaded from 'dart:test-lib'.", 1959 "A script has already been loaded from 'dart:test-lib'.",
2090 Dart_GetError(result)); 1960 Dart_GetError(result));
2091
2092 Dart_ExitScope();
2093 Dart_ShutdownIsolate();
2094 } 1961 }
2095 1962
2096 1963
2097 UNIT_TEST_CASE(LoadScript_CompileError) { 1964 UNIT_TEST_CASE(LoadScript_CompileError) {
2098 const char* kScriptChars = 1965 const char* kScriptChars =
2099 ")"; 1966 ")";
2100 1967
2101 Dart_CreateIsolate(NULL, NULL); 1968 TestIsolateScope __test_isolate__;
2102 Dart_EnterScope();
2103 1969
2104 Dart_Handle url = Dart_NewString(TestCase::url()); 1970 Dart_Handle url = Dart_NewString(TestCase::url());
2105 Dart_Handle source = Dart_NewString(kScriptChars); 1971 Dart_Handle source = Dart_NewString(kScriptChars);
2106 Dart_Handle result = Dart_LoadScript(url, source, library_handler); 1972 Dart_Handle result = Dart_LoadScript(url, source, library_handler);
2107 EXPECT(Dart_IsError(result)); 1973 EXPECT(Dart_IsError(result));
2108 EXPECT(strstr(Dart_GetError(result), "unexpected token ')'")); 1974 EXPECT(strstr(Dart_GetError(result), "unexpected token ')'"));
2109
2110 Dart_ExitScope();
2111 Dart_ShutdownIsolate();
2112 } 1975 }
2113 1976
2114 1977
2115 UNIT_TEST_CASE(LookupLibrary) { 1978 UNIT_TEST_CASE(LookupLibrary) {
2116 const char* kScriptChars = 1979 const char* kScriptChars =
2117 "#import('library1.dart');" 1980 "#import('library1.dart');"
2118 "main() {}"; 1981 "main() {}";
2119 const char* kLibrary1Chars = 1982 const char* kLibrary1Chars =
2120 "#library('library1.dart');" 1983 "#library('library1.dart');"
2121 "#import('library2.dart');"; 1984 "#import('library2.dart');";
2122 1985
2123 Dart_CreateIsolate(NULL, NULL); 1986 TestIsolateScope __test_isolate__;
2124 Dart_EnterScope();
2125 1987
2126 // Create a test library and Load up a test script in it. 1988 // Create a test library and Load up a test script in it.
2127 Dart_Handle url = Dart_NewString(TestCase::url()); 1989 Dart_Handle url = Dart_NewString(TestCase::url());
2128 Dart_Handle source = Dart_NewString(kScriptChars); 1990 Dart_Handle source = Dart_NewString(kScriptChars);
2129 Dart_Handle result = Dart_LoadScript(url, source, library_handler); 1991 Dart_Handle result = Dart_LoadScript(url, source, library_handler);
2130 EXPECT_VALID(result); 1992 EXPECT_VALID(result);
2131 1993
2132 url = Dart_NewString("library1.dart"); 1994 url = Dart_NewString("library1.dart");
2133 source = Dart_NewString(kLibrary1Chars); 1995 source = Dart_NewString(kLibrary1Chars);
2134 result = Dart_LoadLibrary(url, source); 1996 result = Dart_LoadLibrary(url, source);
(...skipping 15 matching lines...) Expand all
2150 2012
2151 result = Dart_LookupLibrary(Dart_Error("incoming error")); 2013 result = Dart_LookupLibrary(Dart_Error("incoming error"));
2152 EXPECT(Dart_IsError(result)); 2014 EXPECT(Dart_IsError(result));
2153 EXPECT_STREQ("incoming error", Dart_GetError(result)); 2015 EXPECT_STREQ("incoming error", Dart_GetError(result));
2154 2016
2155 url = Dart_NewString("noodles.dart"); 2017 url = Dart_NewString("noodles.dart");
2156 result = Dart_LookupLibrary(url); 2018 result = Dart_LookupLibrary(url);
2157 EXPECT(Dart_IsError(result)); 2019 EXPECT(Dart_IsError(result));
2158 EXPECT_STREQ("Dart_LookupLibrary: library 'noodles.dart' not found.", 2020 EXPECT_STREQ("Dart_LookupLibrary: library 'noodles.dart' not found.",
2159 Dart_GetError(result)); 2021 Dart_GetError(result));
2160
2161 Dart_ExitScope();
2162 Dart_ShutdownIsolate();
2163 } 2022 }
2164 2023
2165 2024
2166 UNIT_TEST_CASE(LibraryUrl) { 2025 UNIT_TEST_CASE(LibraryUrl) {
2167 const char* kLibrary1Chars = 2026 const char* kLibrary1Chars =
2168 "#library('library1_name');"; 2027 "#library('library1_name');";
2169 2028
2170 Dart_CreateIsolate(NULL, NULL); 2029 TestIsolateScope __test_isolate__;
2171 Dart_EnterScope();
2172 2030
2173 Dart_Handle url = Dart_NewString("library1_url"); 2031 Dart_Handle url = Dart_NewString("library1_url");
2174 Dart_Handle source = Dart_NewString(kLibrary1Chars); 2032 Dart_Handle source = Dart_NewString(kLibrary1Chars);
2175 Dart_Handle lib = Dart_LoadLibrary(url, source); 2033 Dart_Handle lib = Dart_LoadLibrary(url, source);
2176 Dart_Handle error = Dart_Error("incoming error"); 2034 Dart_Handle error = Dart_Error("incoming error");
2177 EXPECT_VALID(lib); 2035 EXPECT_VALID(lib);
2178 2036
2179 Dart_Handle result = Dart_LibraryUrl(Dart_Null()); 2037 Dart_Handle result = Dart_LibraryUrl(Dart_Null());
2180 EXPECT(Dart_IsError(result)); 2038 EXPECT(Dart_IsError(result));
2181 EXPECT_STREQ("Dart_LibraryUrl expects argument 'library' to be non-null.", 2039 EXPECT_STREQ("Dart_LibraryUrl expects argument 'library' to be non-null.",
2182 Dart_GetError(result)); 2040 Dart_GetError(result));
2183 2041
2184 result = Dart_LibraryUrl(Dart_True()); 2042 result = Dart_LibraryUrl(Dart_True());
2185 EXPECT(Dart_IsError(result)); 2043 EXPECT(Dart_IsError(result));
2186 EXPECT_STREQ( 2044 EXPECT_STREQ(
2187 "Dart_LibraryUrl expects argument 'library' to be of type Library.", 2045 "Dart_LibraryUrl expects argument 'library' to be of type Library.",
2188 Dart_GetError(result)); 2046 Dart_GetError(result));
2189 2047
2190 result = Dart_LibraryUrl(error); 2048 result = Dart_LibraryUrl(error);
2191 EXPECT(Dart_IsError(result)); 2049 EXPECT(Dart_IsError(result));
2192 EXPECT_STREQ("incoming error", Dart_GetError(result)); 2050 EXPECT_STREQ("incoming error", Dart_GetError(result));
2193 2051
2194 result = Dart_LibraryUrl(lib); 2052 result = Dart_LibraryUrl(lib);
2195 EXPECT_VALID(result); 2053 EXPECT_VALID(result);
2196 EXPECT(Dart_IsString(result)); 2054 EXPECT(Dart_IsString(result));
2197 const char* cstr = NULL; 2055 const char* cstr = NULL;
2198 EXPECT_VALID(Dart_StringToCString(result, &cstr)); 2056 EXPECT_VALID(Dart_StringToCString(result, &cstr));
2199 EXPECT_STREQ("library1_url", cstr); 2057 EXPECT_STREQ("library1_url", cstr);
2200
2201 Dart_ExitScope();
2202 Dart_ShutdownIsolate();
2203 } 2058 }
2204 2059
2205 2060
2206 UNIT_TEST_CASE(LibraryImportLibrary) { 2061 UNIT_TEST_CASE(LibraryImportLibrary) {
2207 const char* kLibrary1Chars = 2062 const char* kLibrary1Chars =
2208 "#library('library1_name');"; 2063 "#library('library1_name');";
2209 const char* kLibrary2Chars = 2064 const char* kLibrary2Chars =
2210 "#library('library2_name');"; 2065 "#library('library2_name');";
2211 2066
2212 Dart_CreateIsolate(NULL, NULL); 2067 TestIsolateScope __test_isolate__;
2213 Dart_EnterScope();
2214 2068
2215 Dart_Handle error = Dart_Error("incoming error"); 2069 Dart_Handle error = Dart_Error("incoming error");
2216 Dart_Handle result; 2070 Dart_Handle result;
2217 2071
2218 Dart_Handle url = Dart_NewString("library1_url"); 2072 Dart_Handle url = Dart_NewString("library1_url");
2219 Dart_Handle source = Dart_NewString(kLibrary1Chars); 2073 Dart_Handle source = Dart_NewString(kLibrary1Chars);
2220 Dart_Handle lib1 = Dart_LoadLibrary(url, source); 2074 Dart_Handle lib1 = Dart_LoadLibrary(url, source);
2221 EXPECT_VALID(lib1); 2075 EXPECT_VALID(lib1);
2222 2076
2223 url = Dart_NewString("library2_url"); 2077 url = Dart_NewString("library2_url");
(...skipping 28 matching lines...) Expand all
2252 EXPECT_STREQ("Dart_LibraryImportLibrary expects argument 'import' to be of " 2106 EXPECT_STREQ("Dart_LibraryImportLibrary expects argument 'import' to be of "
2253 "type Library.", 2107 "type Library.",
2254 Dart_GetError(result)); 2108 Dart_GetError(result));
2255 2109
2256 result = Dart_LibraryImportLibrary(lib1, error); 2110 result = Dart_LibraryImportLibrary(lib1, error);
2257 EXPECT(Dart_IsError(result)); 2111 EXPECT(Dart_IsError(result));
2258 EXPECT_STREQ("incoming error", Dart_GetError(result)); 2112 EXPECT_STREQ("incoming error", Dart_GetError(result));
2259 2113
2260 result = Dart_LibraryImportLibrary(lib1, lib2); 2114 result = Dart_LibraryImportLibrary(lib1, lib2);
2261 EXPECT_VALID(result); 2115 EXPECT_VALID(result);
2262
2263 Dart_ExitScope();
2264 Dart_ShutdownIsolate();
2265 } 2116 }
2266 2117
2267 2118
2268 2119
2269 UNIT_TEST_CASE(LoadLibrary) { 2120 UNIT_TEST_CASE(LoadLibrary) {
2270 const char* kLibrary1Chars = 2121 const char* kLibrary1Chars =
2271 "#library('library1_name');"; 2122 "#library('library1_name');";
2272 2123
2273 Dart_CreateIsolate(NULL, NULL); 2124 TestIsolateScope __test_isolate__;
2274 Dart_EnterScope();
2275 2125
2276 Dart_Handle error = Dart_Error("incoming error"); 2126 Dart_Handle error = Dart_Error("incoming error");
2277 Dart_Handle result; 2127 Dart_Handle result;
2278 2128
2279 Dart_Handle url = Dart_NewString("library1_url"); 2129 Dart_Handle url = Dart_NewString("library1_url");
2280 Dart_Handle source = Dart_NewString(kLibrary1Chars); 2130 Dart_Handle source = Dart_NewString(kLibrary1Chars);
2281 2131
2282 result = Dart_LoadLibrary(Dart_Null(), source); 2132 result = Dart_LoadLibrary(Dart_Null(), source);
2283 EXPECT(Dart_IsError(result)); 2133 EXPECT(Dart_IsError(result));
2284 EXPECT_STREQ("Dart_LoadLibrary expects argument 'url' to be non-null.", 2134 EXPECT_STREQ("Dart_LoadLibrary expects argument 'url' to be non-null.",
(...skipping 27 matching lines...) Expand all
2312 result = Dart_LoadLibrary(url, source); 2162 result = Dart_LoadLibrary(url, source);
2313 EXPECT_VALID(result); 2163 EXPECT_VALID(result);
2314 EXPECT(Dart_IsLibrary(result)); 2164 EXPECT(Dart_IsLibrary(result));
2315 2165
2316 // Duplicate library load fails. 2166 // Duplicate library load fails.
2317 result = Dart_LoadLibrary(url, source); 2167 result = Dart_LoadLibrary(url, source);
2318 EXPECT(Dart_IsError(result)); 2168 EXPECT(Dart_IsError(result));
2319 EXPECT_STREQ( 2169 EXPECT_STREQ(
2320 "Dart_LoadLibrary: library 'library1_url' has already been loaded.", 2170 "Dart_LoadLibrary: library 'library1_url' has already been loaded.",
2321 Dart_GetError(result)); 2171 Dart_GetError(result));
2322
2323 Dart_ExitScope();
2324 Dart_ShutdownIsolate();
2325 } 2172 }
2326 2173
2327 2174
2328 UNIT_TEST_CASE(LoadLibrary_CompileError) { 2175 UNIT_TEST_CASE(LoadLibrary_CompileError) {
2329 const char* kLibrary1Chars = 2176 const char* kLibrary1Chars =
2330 "#library('library1_name');" 2177 "#library('library1_name');"
2331 ")"; 2178 ")";
2332 2179
2333 Dart_CreateIsolate(NULL, NULL); 2180 TestIsolateScope __test_isolate__;
2334 Dart_EnterScope();
2335 2181
2336 Dart_Handle url = Dart_NewString("library1_url"); 2182 Dart_Handle url = Dart_NewString("library1_url");
2337 Dart_Handle source = Dart_NewString(kLibrary1Chars); 2183 Dart_Handle source = Dart_NewString(kLibrary1Chars);
2338 Dart_Handle result = Dart_LoadLibrary(url, source); 2184 Dart_Handle result = Dart_LoadLibrary(url, source);
2339 EXPECT(Dart_IsError(result)); 2185 EXPECT(Dart_IsError(result));
2340 EXPECT(strstr(Dart_GetError(result), "unexpected token ')'")); 2186 EXPECT(strstr(Dart_GetError(result), "unexpected token ')'"));
2341
2342 Dart_ExitScope();
2343 Dart_ShutdownIsolate();
2344 } 2187 }
2345 2188
2346 2189
2347 UNIT_TEST_CASE(LoadSource) { 2190 UNIT_TEST_CASE(LoadSource) {
2348 const char* kLibrary1Chars = 2191 const char* kLibrary1Chars =
2349 "#library('library1_name');"; 2192 "#library('library1_name');";
2350 const char* kSourceChars = 2193 const char* kSourceChars =
2351 "// Something innocuous"; 2194 "// Something innocuous";
2352 const char* kBadSourceChars = 2195 const char* kBadSourceChars =
2353 ")"; 2196 ")";
2354 2197
2355 Dart_CreateIsolate(NULL, NULL); 2198 TestIsolateScope __test_isolate__;
2356 Dart_EnterScope();
2357 2199
2358 Dart_Handle error = Dart_Error("incoming error"); 2200 Dart_Handle error = Dart_Error("incoming error");
2359 Dart_Handle result; 2201 Dart_Handle result;
2360 2202
2361 // Load up a library. 2203 // Load up a library.
2362 Dart_Handle url = Dart_NewString("library1_url"); 2204 Dart_Handle url = Dart_NewString("library1_url");
2363 Dart_Handle source = Dart_NewString(kLibrary1Chars); 2205 Dart_Handle source = Dart_NewString(kLibrary1Chars);
2364 Dart_Handle lib = Dart_LoadLibrary(url, source); 2206 Dart_Handle lib = Dart_LoadLibrary(url, source);
2365 EXPECT_VALID(lib); 2207 EXPECT_VALID(lib);
2366 EXPECT(Dart_IsLibrary(lib)); 2208 EXPECT(Dart_IsLibrary(lib));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2425 EXPECT_VALID(result); 2267 EXPECT_VALID(result);
2426 EXPECT(Dart_IsLibrary(result)); 2268 EXPECT(Dart_IsLibrary(result));
2427 same = false; 2269 same = false;
2428 EXPECT_VALID(Dart_IsSame(lib, result, &same)); 2270 EXPECT_VALID(Dart_IsSame(lib, result, &same));
2429 EXPECT(same); 2271 EXPECT(same);
2430 2272
2431 // Language errors are detected. 2273 // Language errors are detected.
2432 source = Dart_NewString(kBadSourceChars); 2274 source = Dart_NewString(kBadSourceChars);
2433 result = Dart_LoadSource(lib, url, source); 2275 result = Dart_LoadSource(lib, url, source);
2434 EXPECT(Dart_IsError(result)); 2276 EXPECT(Dart_IsError(result));
2435
2436 Dart_ExitScope();
2437 Dart_ShutdownIsolate();
2438 } 2277 }
2439 2278
2440 2279
2441 static void MyNativeFunction1(Dart_NativeArguments args) { 2280 static void MyNativeFunction1(Dart_NativeArguments args) {
2442 Dart_EnterScope(); 2281 Dart_EnterScope();
2443 Dart_SetReturnValue(args, Dart_NewInteger(654321)); 2282 Dart_SetReturnValue(args, Dart_NewInteger(654321));
2444 Dart_ExitScope(); 2283 Dart_ExitScope();
2445 } 2284 }
2446 2285
2447 2286
(...skipping 17 matching lines...) Expand all
2465 2304
2466 2305
2467 UNIT_TEST_CASE(SetNativeResolver) { 2306 UNIT_TEST_CASE(SetNativeResolver) {
2468 const char* kScriptChars = 2307 const char* kScriptChars =
2469 "class Test {" 2308 "class Test {"
2470 " static foo() native \"SomeNativeFunction\";" 2309 " static foo() native \"SomeNativeFunction\";"
2471 " static bar() native \"SomeNativeFunction2\";" 2310 " static bar() native \"SomeNativeFunction2\";"
2472 " static baz() native \"SomeNativeFunction3\";" 2311 " static baz() native \"SomeNativeFunction3\";"
2473 "}"; 2312 "}";
2474 2313
2475 Dart_CreateIsolate(NULL, NULL); 2314 TestIsolateScope __test_isolate__;
2476 Dart_EnterScope();
2477 2315
2478 Dart_Handle error = Dart_Error("incoming error"); 2316 Dart_Handle error = Dart_Error("incoming error");
2479 Dart_Handle result; 2317 Dart_Handle result;
2480 2318
2481 // Load a test script. 2319 // Load a test script.
2482 Dart_Handle url = Dart_NewString(TestCase::url()); 2320 Dart_Handle url = Dart_NewString(TestCase::url());
2483 Dart_Handle source = Dart_NewString(kScriptChars); 2321 Dart_Handle source = Dart_NewString(kScriptChars);
2484 Dart_Handle lib = Dart_LoadScript(url, source, library_handler); 2322 Dart_Handle lib = Dart_LoadScript(url, source, library_handler);
2485 EXPECT_VALID(lib); 2323 EXPECT_VALID(lib);
2486 EXPECT(Dart_IsLibrary(lib)); 2324 EXPECT(Dart_IsLibrary(lib));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2549 EXPECT_VALID(result); 2387 EXPECT_VALID(result);
2550 2388
2551 result = Dart_InvokeStatic(lib, 2389 result = Dart_InvokeStatic(lib,
2552 Dart_NewString("Test"), 2390 Dart_NewString("Test"),
2553 Dart_NewString("baz"), 2391 Dart_NewString("baz"),
2554 0, 2392 0,
2555 NULL); 2393 NULL);
2556 EXPECT(Dart_IsError(result)); 2394 EXPECT(Dart_IsError(result));
2557 EXPECT(strstr(Dart_GetError(result), 2395 EXPECT(strstr(Dart_GetError(result),
2558 "native function 'SomeNativeFunction3' cannot be found")); 2396 "native function 'SomeNativeFunction3' cannot be found"));
2559
2560 Dart_ExitScope();
2561 Dart_ShutdownIsolate();
2562 } 2397 }
2563 2398
2564 2399
2565 UNIT_TEST_CASE(ImportLibrary1) { 2400 UNIT_TEST_CASE(ImportLibrary1) {
2566 const char* kScriptChars = 2401 const char* kScriptChars =
2567 "#import('library1.dart');" 2402 "#import('library1.dart');"
2568 "#import('library2.dart');" 2403 "#import('library2.dart');"
2569 "var foo; // library2 defines foo, so should be error." 2404 "var foo; // library2 defines foo, so should be error."
2570 "main() {}"; 2405 "main() {}";
2571 const char* kLibrary1Chars = 2406 const char* kLibrary1Chars =
2572 "#library('library1.dart');" 2407 "#library('library1.dart');"
2573 "#import('library2.dart');" 2408 "#import('library2.dart');"
2574 "var foo1;"; 2409 "var foo1;";
2575 const char* kLibrary2Chars = 2410 const char* kLibrary2Chars =
2576 "#library('library2.dart');" 2411 "#library('library2.dart');"
2577 "var foo;"; 2412 "var foo;";
2578 Dart_Handle result; 2413 Dart_Handle result;
2579 2414
2580 Dart_CreateIsolate(NULL, NULL); 2415 TestIsolateScope __test_isolate__;
2581 { 2416 {
2582 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
2583
2584 // Create a test library and Load up a test script in it. 2417 // Create a test library and Load up a test script in it.
2585 Dart_Handle url = Dart_NewString(TestCase::url()); 2418 Dart_Handle url = Dart_NewString(TestCase::url());
2586 Dart_Handle source = Dart_NewString(kScriptChars); 2419 Dart_Handle source = Dart_NewString(kScriptChars);
2587 result = Dart_LoadScript(url, source, library_handler); 2420 result = Dart_LoadScript(url, source, library_handler);
2588 2421
2589 url = Dart_NewString("library1.dart"); 2422 url = Dart_NewString("library1.dart");
2590 source = Dart_NewString(kLibrary1Chars); 2423 source = Dart_NewString(kLibrary1Chars);
2591 Dart_LoadLibrary(url, source); 2424 Dart_LoadLibrary(url, source);
2592 2425
2593 url = Dart_NewString("library2.dart"); 2426 url = Dart_NewString("library2.dart");
2594 source = Dart_NewString(kLibrary2Chars); 2427 source = Dart_NewString(kLibrary2Chars);
2595 Dart_LoadLibrary(url, source); 2428 Dart_LoadLibrary(url, source);
2596 2429
2597 result = Dart_InvokeStatic(result, 2430 result = Dart_InvokeStatic(result,
2598 Dart_NewString(""), 2431 Dart_NewString(""),
2599 Dart_NewString("main"), 2432 Dart_NewString("main"),
2600 0, 2433 0,
2601 NULL); 2434 NULL);
2602 EXPECT(Dart_IsError(result)); 2435 EXPECT(Dart_IsError(result));
2603 EXPECT_STREQ("Duplicate definition : 'foo' is defined in" 2436 EXPECT_STREQ("Duplicate definition : 'foo' is defined in"
2604 " 'library2.dart' and 'dart:test-lib'\n", 2437 " 'library2.dart' and 'dart:test-lib'\n",
2605 Dart_GetError(result)); 2438 Dart_GetError(result));
2606
2607 Dart_ExitScope(); // Exit the Dart API scope.
2608 } 2439 }
2609 Dart_ShutdownIsolate();
2610 } 2440 }
2611 2441
2612 2442
2613 UNIT_TEST_CASE(ImportLibrary2) { 2443 UNIT_TEST_CASE(ImportLibrary2) {
2614 const char* kScriptChars = 2444 const char* kScriptChars =
2615 "#import('library1.dart');" 2445 "#import('library1.dart');"
2616 "var foo;" 2446 "var foo;"
2617 "main() {}"; 2447 "main() {}";
2618 const char* kLibrary1Chars = 2448 const char* kLibrary1Chars =
2619 "#library('library1.dart');" 2449 "#library('library1.dart');"
2620 "#import('library2.dart');" 2450 "#import('library2.dart');"
2621 "var foo1;"; 2451 "var foo1;";
2622 const char* kLibrary2Chars = 2452 const char* kLibrary2Chars =
2623 "#library('library2.dart');" 2453 "#library('library2.dart');"
2624 "#import('library1.dart');" 2454 "#import('library1.dart');"
2625 "var foo;"; 2455 "var foo;";
2626 Dart_Handle result; 2456 Dart_Handle result;
2627 2457
2628 Dart_CreateIsolate(NULL, NULL); 2458 TestIsolateScope __test_isolate__;
2629 { 2459 {
2630 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
2631
2632 // Create a test library and Load up a test script in it. 2460 // Create a test library and Load up a test script in it.
2633 Dart_Handle url = Dart_NewString(TestCase::url()); 2461 Dart_Handle url = Dart_NewString(TestCase::url());
2634 Dart_Handle source = Dart_NewString(kScriptChars); 2462 Dart_Handle source = Dart_NewString(kScriptChars);
2635 result = Dart_LoadScript(url, source, library_handler); 2463 result = Dart_LoadScript(url, source, library_handler);
2636 2464
2637 url = Dart_NewString("library1.dart"); 2465 url = Dart_NewString("library1.dart");
2638 source = Dart_NewString(kLibrary1Chars); 2466 source = Dart_NewString(kLibrary1Chars);
2639 Dart_LoadLibrary(url, source); 2467 Dart_LoadLibrary(url, source);
2640 2468
2641 url = Dart_NewString("library2.dart"); 2469 url = Dart_NewString("library2.dart");
2642 source = Dart_NewString(kLibrary2Chars); 2470 source = Dart_NewString(kLibrary2Chars);
2643 Dart_LoadLibrary(url, source); 2471 Dart_LoadLibrary(url, source);
2644 2472
2645 result = Dart_InvokeStatic(result, 2473 result = Dart_InvokeStatic(result,
2646 Dart_NewString(""), 2474 Dart_NewString(""),
2647 Dart_NewString("main"), 2475 Dart_NewString("main"),
2648 0, 2476 0,
2649 NULL); 2477 NULL);
2650 EXPECT_VALID(result); 2478 EXPECT_VALID(result);
2651
2652 Dart_ExitScope(); // Exit the Dart API scope.
2653 } 2479 }
2654 Dart_ShutdownIsolate();
2655 } 2480 }
2656 2481
2657 2482
2658 UNIT_TEST_CASE(ImportLibrary3) { 2483 UNIT_TEST_CASE(ImportLibrary3) {
2659 const char* kScriptChars = 2484 const char* kScriptChars =
2660 "#import('library2.dart');" 2485 "#import('library2.dart');"
2661 "#import('library1.dart');" 2486 "#import('library1.dart');"
2662 "var foo_top = 10; // foo has dup def. So should be an error." 2487 "var foo_top = 10; // foo has dup def. So should be an error."
2663 "main() {}"; 2488 "main() {}";
2664 const char* kLibrary1Chars = 2489 const char* kLibrary1Chars =
2665 "#library('library1.dart');" 2490 "#library('library1.dart');"
2666 "var foo;"; 2491 "var foo;";
2667 const char* kLibrary2Chars = 2492 const char* kLibrary2Chars =
2668 "#library('library2.dart');" 2493 "#library('library2.dart');"
2669 "var foo;"; 2494 "var foo;";
2670 Dart_Handle result; 2495 Dart_Handle result;
2671 2496
2672 Dart_CreateIsolate(NULL, NULL); 2497 TestIsolateScope __test_isolate__;
2673 { 2498 {
2674 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
2675
2676 // Create a test library and Load up a test script in it. 2499 // Create a test library and Load up a test script in it.
2677 Dart_Handle url = Dart_NewString(TestCase::url()); 2500 Dart_Handle url = Dart_NewString(TestCase::url());
2678 Dart_Handle source = Dart_NewString(kScriptChars); 2501 Dart_Handle source = Dart_NewString(kScriptChars);
2679 result = Dart_LoadScript(url, source, library_handler); 2502 result = Dart_LoadScript(url, source, library_handler);
2680 2503
2681 url = Dart_NewString("library2.dart"); 2504 url = Dart_NewString("library2.dart");
2682 source = Dart_NewString(kLibrary2Chars); 2505 source = Dart_NewString(kLibrary2Chars);
2683 Dart_LoadLibrary(url, source); 2506 Dart_LoadLibrary(url, source);
2684 2507
2685 url = Dart_NewString("library1.dart"); 2508 url = Dart_NewString("library1.dart");
2686 source = Dart_NewString(kLibrary1Chars); 2509 source = Dart_NewString(kLibrary1Chars);
2687 Dart_LoadLibrary(url, source); 2510 Dart_LoadLibrary(url, source);
2688 2511
2689 result = Dart_InvokeStatic(result, 2512 result = Dart_InvokeStatic(result,
2690 Dart_NewString(""), 2513 Dart_NewString(""),
2691 Dart_NewString("main"), 2514 Dart_NewString("main"),
2692 0, 2515 0,
2693 NULL); 2516 NULL);
2694 EXPECT(Dart_IsError(result)); 2517 EXPECT(Dart_IsError(result));
2695 EXPECT_STREQ("Duplicate definition : 'foo' is defined in" 2518 EXPECT_STREQ("Duplicate definition : 'foo' is defined in"
2696 " 'library1.dart' and 'library2.dart'\n", 2519 " 'library1.dart' and 'library2.dart'\n",
2697 Dart_GetError(result)); 2520 Dart_GetError(result));
2698
2699 Dart_ExitScope(); // Exit the Dart API scope.
2700 } 2521 }
2701 Dart_ShutdownIsolate();
2702 } 2522 }
2703 2523
2704 2524
2705 UNIT_TEST_CASE(ImportLibrary4) { 2525 UNIT_TEST_CASE(ImportLibrary4) {
2706 const char* kScriptChars = 2526 const char* kScriptChars =
2707 "#import('libraryA.dart');" 2527 "#import('libraryA.dart');"
2708 "#import('libraryB.dart');" 2528 "#import('libraryB.dart');"
2709 "#import('libraryD.dart');" 2529 "#import('libraryD.dart');"
2710 "#import('libraryE.dart');" 2530 "#import('libraryE.dart');"
2711 "var fooApp;" 2531 "var fooApp;"
(...skipping 16 matching lines...) Expand all
2728 const char* kLibraryEChars = 2548 const char* kLibraryEChars =
2729 "#library('libraryE.dart');" 2549 "#library('libraryE.dart');"
2730 "#import('libraryC.dart');" 2550 "#import('libraryC.dart');"
2731 "#import('libraryF.dart');" 2551 "#import('libraryF.dart');"
2732 "var fooE = 10; //fooC has duplicate def. so should be an error."; 2552 "var fooE = 10; //fooC has duplicate def. so should be an error.";
2733 const char* kLibraryFChars = 2553 const char* kLibraryFChars =
2734 "#library('libraryF.dart');" 2554 "#library('libraryF.dart');"
2735 "var fooC;"; 2555 "var fooC;";
2736 Dart_Handle result; 2556 Dart_Handle result;
2737 2557
2738 Dart_CreateIsolate(NULL, NULL); 2558 TestIsolateScope __test_isolate__;
2739 { 2559 {
2740 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
2741
2742 // Create a test library and Load up a test script in it. 2560 // Create a test library and Load up a test script in it.
2743 Dart_Handle url = Dart_NewString(TestCase::url()); 2561 Dart_Handle url = Dart_NewString(TestCase::url());
2744 Dart_Handle source = Dart_NewString(kScriptChars); 2562 Dart_Handle source = Dart_NewString(kScriptChars);
2745 result = Dart_LoadScript(url, source, library_handler); 2563 result = Dart_LoadScript(url, source, library_handler);
2746 2564
2747 url = Dart_NewString("libraryA.dart"); 2565 url = Dart_NewString("libraryA.dart");
2748 source = Dart_NewString(kLibraryAChars); 2566 source = Dart_NewString(kLibraryAChars);
2749 Dart_LoadLibrary(url, source); 2567 Dart_LoadLibrary(url, source);
2750 2568
2751 url = Dart_NewString("libraryC.dart"); 2569 url = Dart_NewString("libraryC.dart");
(...skipping 18 matching lines...) Expand all
2770 2588
2771 result = Dart_InvokeStatic(result, 2589 result = Dart_InvokeStatic(result,
2772 Dart_NewString(""), 2590 Dart_NewString(""),
2773 Dart_NewString("main"), 2591 Dart_NewString("main"),
2774 0, 2592 0,
2775 NULL); 2593 NULL);
2776 EXPECT(Dart_IsError(result)); 2594 EXPECT(Dart_IsError(result));
2777 EXPECT_STREQ("Duplicate definition : 'fooC' is defined in" 2595 EXPECT_STREQ("Duplicate definition : 'fooC' is defined in"
2778 " 'libraryF.dart' and 'libraryC.dart'\n", 2596 " 'libraryF.dart' and 'libraryC.dart'\n",
2779 Dart_GetError(result)); 2597 Dart_GetError(result));
2780
2781 Dart_ExitScope(); // Exit the Dart API scope.
2782 } 2598 }
2783 Dart_ShutdownIsolate();
2784 } 2599 }
2785 2600
2786 2601
2787 UNIT_TEST_CASE(ImportLibrary5) { 2602 UNIT_TEST_CASE(ImportLibrary5) {
2788 const char* kScriptChars = 2603 const char* kScriptChars =
2789 "#import('lib.dart');" 2604 "#import('lib.dart');"
2790 "interface Y {" 2605 "interface Y {"
2791 " void set handler(void callback(List<int> x));" 2606 " void set handler(void callback(List<int> x));"
2792 "}" 2607 "}"
2793 "void main() {}"; 2608 "void main() {}";
2794 const char* kLibraryChars = 2609 const char* kLibraryChars =
2795 "#library('lib.dart');" 2610 "#library('lib.dart');"
2796 "interface X {" 2611 "interface X {"
2797 " void set handler(void callback(List<int> x));" 2612 " void set handler(void callback(List<int> x));"
2798 "}"; 2613 "}";
2799 Dart_Handle result; 2614 Dart_Handle result;
2800 2615
2801 Dart_CreateIsolate(NULL, NULL); 2616 TestIsolateScope __test_isolate__;
2802 { 2617 {
2803 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
2804
2805 // Create a test library and Load up a test script in it. 2618 // Create a test library and Load up a test script in it.
2806 Dart_Handle url = Dart_NewString(TestCase::url()); 2619 Dart_Handle url = Dart_NewString(TestCase::url());
2807 Dart_Handle source = Dart_NewString(kScriptChars); 2620 Dart_Handle source = Dart_NewString(kScriptChars);
2808 result = Dart_LoadScript(url, source, library_handler); 2621 result = Dart_LoadScript(url, source, library_handler);
2809 2622
2810 url = Dart_NewString("lib.dart"); 2623 url = Dart_NewString("lib.dart");
2811 source = Dart_NewString(kLibraryChars); 2624 source = Dart_NewString(kLibraryChars);
2812 Dart_LoadLibrary(url, source); 2625 Dart_LoadLibrary(url, source);
2813 2626
2814 result = Dart_InvokeStatic(result, 2627 result = Dart_InvokeStatic(result,
2815 Dart_NewString(""), 2628 Dart_NewString(""),
2816 Dart_NewString("main"), 2629 Dart_NewString("main"),
2817 0, 2630 0,
2818 NULL); 2631 NULL);
2819 EXPECT_VALID(result); 2632 EXPECT_VALID(result);
2820
2821 Dart_ExitScope(); // Exit the Dart API scope.
2822 } 2633 }
2823 Dart_ShutdownIsolate();
2824 } 2634 }
2825 2635
2826 2636
2827 static void* RunLoopTest_InitCallback(void* data) { 2637 static bool RunLoopTestCallback(void* data, char** error) {
2828 const char* kScriptChars = 2638 const char* kScriptChars =
2829 "#import('builtin');\n" 2639 "#import('builtin');\n"
2830 "class MyIsolate extends Isolate {\n" 2640 "class MyIsolate extends Isolate {\n"
2831 " MyIsolate() : super() { }\n" 2641 " MyIsolate() : super() { }\n"
2832 " void main() {\n" 2642 " void main() {\n"
2833 " port.receive((message, replyTo) {\n" 2643 " port.receive((message, replyTo) {\n"
2834 " if (message) {\n" 2644 " if (message) {\n"
2835 " throw new Exception('MakeVMExit');\n" 2645 " throw new Exception('MakeVMExit');\n"
2836 " } else {\n" 2646 " } else {\n"
2837 " replyTo.call('hello');\n" 2647 " replyTo.call('hello');\n"
2838 " port.close();\n" 2648 " port.close();\n"
2839 " }\n" 2649 " }\n"
2840 " });\n" 2650 " });\n"
2841 " }\n" 2651 " }\n"
2842 "}\n" 2652 "}\n"
2843 "\n" 2653 "\n"
2844 "void main(message) {\n" 2654 "void main(message) {\n"
2845 " new MyIsolate().spawn().then((port) {\n" 2655 " new MyIsolate().spawn().then((port) {\n"
2846 " port.call(message).receive((message, replyTo) {\n" 2656 " port.call(message).receive((message, replyTo) {\n"
2847 " if (message != 'hello') throw new Exception('ShouldNotHappen');\n" 2657 " if (message != 'hello') throw new Exception('ShouldNotHappen');\n"
2848 " });\n" 2658 " });\n"
2849 " });\n" 2659 " });\n"
2850 "}\n"; 2660 "}\n";
2851 2661
2662 Dart_Isolate isolate = TestCase::CreateTestIsolate();
2663 ASSERT(isolate != NULL);
2852 Dart_EnterScope(); 2664 Dart_EnterScope();
2853 Dart_Handle url = Dart_NewString(TestCase::url()); 2665 Dart_Handle url = Dart_NewString(TestCase::url());
2854 Dart_Handle source = Dart_NewString(kScriptChars); 2666 Dart_Handle source = Dart_NewString(kScriptChars);
2855 Dart_Handle lib = Dart_LoadScript(url, source, library_handler); 2667 Dart_Handle lib = Dart_LoadScript(url, source, TestCase::library_handler);
2856 EXPECT_VALID(lib); 2668 EXPECT_VALID(lib);
2857 Dart_ExitScope(); 2669 Dart_ExitScope();
2858 return NULL; 2670 return true;
2859 } 2671 }
2860 2672
2861 2673
2862 // Common code for RunLoop_Success/RunLoop_Failure. 2674 // Common code for RunLoop_Success/RunLoop_Failure.
2863 static void RunLoopTest(bool throw_exception) { 2675 static void RunLoopTest(bool throw_exception) {
2864 Dart_IsolateInitCallback saved = Isolate::InitCallback(); 2676 Dart_IsolateCreateCallback saved = Isolate::CreateCallback();
2865 Isolate::SetInitCallback(RunLoopTest_InitCallback); 2677 Isolate::SetCreateCallback(RunLoopTestCallback);
2866 Dart_CreateIsolate(NULL, NULL); 2678 RunLoopTestCallback(NULL, NULL);
2679
2867 Dart_EnterScope(); 2680 Dart_EnterScope();
2868
2869 Dart_Handle lib = Dart_LookupLibrary(Dart_NewString(TestCase::url())); 2681 Dart_Handle lib = Dart_LookupLibrary(Dart_NewString(TestCase::url()));
2870 EXPECT_VALID(lib); 2682 EXPECT_VALID(lib);
2683
2871 Dart_Handle result; 2684 Dart_Handle result;
2872 Dart_Handle args[1]; 2685 Dart_Handle args[1];
2873 args[0] = (throw_exception ? Dart_True() : Dart_False()); 2686 args[0] = (throw_exception ? Dart_True() : Dart_False());
2874 result = Dart_InvokeStatic(lib, 2687 result = Dart_InvokeStatic(lib,
2875 Dart_NewString(""), 2688 Dart_NewString(""),
2876 Dart_NewString("main"), 2689 Dart_NewString("main"),
2877 1, 2690 1,
2878 args); 2691 args);
2879 EXPECT_VALID(result); 2692 EXPECT_VALID(result);
2880 result = Dart_RunLoop(); 2693 result = Dart_RunLoop();
2881 EXPECT_VALID(result); 2694 EXPECT_VALID(result);
2882 2695
2883 Dart_ExitScope(); 2696 Dart_ExitScope();
2884 Dart_ShutdownIsolate(); 2697 Dart_ShutdownIsolate();
2885 2698
2886 Isolate::SetInitCallback(saved); 2699 Isolate::SetCreateCallback(saved);
2887 } 2700 }
2888 2701
2889 2702
2890 UNIT_TEST_CASE(RunLoop_Success) { 2703 UNIT_TEST_CASE(RunLoop_Success) {
2891 RunLoopTest(false); 2704 RunLoopTest(false);
2892 } 2705 }
2893 2706
2894 2707
2895 UNIT_TEST_CASE(RunLoop_Exception) { 2708 UNIT_TEST_CASE(RunLoop_Exception) {
2896 RunLoopTest(true); 2709 RunLoopTest(true);
2897 } 2710 }
2898 2711
2899 #endif // TARGET_ARCH_IA32. 2712 #endif // TARGET_ARCH_IA32.
2900 2713
2901 } // namespace dart 2714 } // namespace dart
OLDNEW
« no previous file with comments | « vm/dart_api_impl.cc ('k') | vm/isolate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698