OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/builtin.h" | 5 #include "bin/builtin.h" |
6 #include "include/dart_api.h" | 6 #include "include/dart_api.h" |
7 #include "include/dart_debugger_api.h" | 7 #include "include/dart_debugger_api.h" |
8 #include "include/dart_mirrors_api.h" | 8 #include "include/dart_mirrors_api.h" |
9 #include "include/dart_native_api.h" | 9 #include "include/dart_native_api.h" |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
(...skipping 5190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5201 // Invoke 'test' and check for an uncaught exception. | 5201 // Invoke 'test' and check for an uncaught exception. |
5202 result = Dart_Invoke(lib, NewString("test"), 0, NULL); | 5202 result = Dart_Invoke(lib, NewString("test"), 0, NULL); |
5203 EXPECT_ERROR(result, "Hello from ExceptionNative!"); | 5203 EXPECT_ERROR(result, "Hello from ExceptionNative!"); |
5204 EXPECT(Dart_ErrorHasException(result)); | 5204 EXPECT(Dart_ErrorHasException(result)); |
5205 | 5205 |
5206 Dart_ExitScope(); // Exit the Dart API scope. | 5206 Dart_ExitScope(); // Exit the Dart API scope. |
5207 EXPECT_EQ(size, state->ZoneSizeInBytes()); | 5207 EXPECT_EQ(size, state->ZoneSizeInBytes()); |
5208 } | 5208 } |
5209 | 5209 |
5210 | 5210 |
5211 void NativeArgumentCounter(Dart_NativeArguments args) { | 5211 static intptr_t kNativeArgumentNativeField1Value = 30; |
| 5212 static intptr_t kNativeArgumentNativeField2Value = 40; |
| 5213 static intptr_t native_arg_str_peer = 100; |
| 5214 static void NativeArgumentCreate(Dart_NativeArguments args) { |
| 5215 Dart_Handle lib = Dart_LookupLibrary(NewString(TestCase::url())); |
| 5216 Dart_Handle type = Dart_GetType(lib, NewString("MyObject"), 0, NULL); |
| 5217 EXPECT_VALID(type); |
| 5218 |
| 5219 // Allocate without a constructor. |
| 5220 Dart_Handle obj = Dart_Allocate(type); |
| 5221 EXPECT_VALID(obj); |
| 5222 |
| 5223 // Setup native fields. |
| 5224 Dart_SetNativeInstanceField(obj, 0, kNativeArgumentNativeField1Value); |
| 5225 Dart_SetNativeInstanceField(obj, 1, kNativeArgumentNativeField2Value); |
| 5226 kNativeArgumentNativeField1Value *= 2; |
| 5227 kNativeArgumentNativeField2Value *= 2; |
| 5228 Dart_SetReturnValue(args, obj); |
| 5229 } |
| 5230 |
| 5231 |
| 5232 static void NativeArgumentAccess(Dart_NativeArguments args) { |
| 5233 const int kNumNativeFields = 2; |
| 5234 |
| 5235 // Test different argument types with a valid descriptor set. |
| 5236 { |
| 5237 const char* cstr = NULL; |
| 5238 intptr_t native_fields1[kNumNativeFields]; |
| 5239 intptr_t native_fields2[kNumNativeFields]; |
| 5240 const Dart_NativeArgument_Descriptor arg_descriptors[9] = { |
| 5241 { Dart_NativeArgument_kNativeFields, 0 }, |
| 5242 { Dart_NativeArgument_kInt32, 1 }, |
| 5243 { Dart_NativeArgument_kUint64, 2 }, |
| 5244 { Dart_NativeArgument_kBool, 3 }, |
| 5245 { Dart_NativeArgument_kDouble, 4 }, |
| 5246 { Dart_NativeArgument_kString, 5 }, |
| 5247 { Dart_NativeArgument_kString, 6 }, |
| 5248 { Dart_NativeArgument_kNativeFields, 7 }, |
| 5249 { Dart_NativeArgument_kInstance, 7 }, |
| 5250 }; |
| 5251 Dart_NativeArgument_Value arg_values[9]; |
| 5252 arg_values[0].as_native_fields.num_fields = kNumNativeFields; |
| 5253 arg_values[0].as_native_fields.values = native_fields1; |
| 5254 arg_values[7].as_native_fields.num_fields = kNumNativeFields; |
| 5255 arg_values[7].as_native_fields.values = native_fields2; |
| 5256 Dart_Handle result = Dart_GetNativeArguments(args, |
| 5257 9, |
| 5258 arg_descriptors, |
| 5259 arg_values); |
| 5260 EXPECT_VALID(result); |
| 5261 |
| 5262 EXPECT(arg_values[0].as_native_fields.values[0] == 30); |
| 5263 EXPECT(arg_values[0].as_native_fields.values[1] == 40); |
| 5264 |
| 5265 EXPECT(arg_values[1].as_int32 == 77); |
| 5266 |
| 5267 EXPECT(arg_values[2].as_uint64 == 0xffffffffffffffff); |
| 5268 |
| 5269 EXPECT(arg_values[3].as_bool == true); |
| 5270 |
| 5271 EXPECT(arg_values[4].as_double == 3.14); |
| 5272 |
| 5273 EXPECT_VALID(arg_values[5].as_string.dart_str); |
| 5274 EXPECT(Dart_IsString(arg_values[5].as_string.dart_str)); |
| 5275 EXPECT_VALID(Dart_StringToCString(arg_values[5].as_string.dart_str, &cstr)); |
| 5276 EXPECT_STREQ("abcdefg", cstr); |
| 5277 EXPECT(arg_values[5].as_string.peer == NULL); |
| 5278 |
| 5279 EXPECT(arg_values[6].as_string.dart_str == NULL); |
| 5280 EXPECT(arg_values[6].as_string.peer == |
| 5281 reinterpret_cast<void*>(&native_arg_str_peer)); |
| 5282 |
| 5283 EXPECT(arg_values[7].as_native_fields.values[0] == 60); |
| 5284 EXPECT(arg_values[7].as_native_fields.values[1] == 80); |
| 5285 |
| 5286 EXPECT_VALID(arg_values[8].as_instance); |
| 5287 EXPECT(Dart_IsInstance(arg_values[8].as_instance)); |
| 5288 int field_count = 0; |
| 5289 EXPECT_VALID(Dart_GetNativeInstanceFieldCount( |
| 5290 arg_values[8].as_instance, &field_count)); |
| 5291 EXPECT(field_count == 2); |
| 5292 } |
| 5293 |
| 5294 // Test with an invalid descriptor set (invalid type). |
| 5295 { |
| 5296 const Dart_NativeArgument_Descriptor arg_descriptors[8] = { |
| 5297 { Dart_NativeArgument_kInt32, 1 }, |
| 5298 { Dart_NativeArgument_kUint64, 2 }, |
| 5299 { Dart_NativeArgument_kString, 3 }, |
| 5300 { Dart_NativeArgument_kDouble, 4 }, |
| 5301 { Dart_NativeArgument_kString, 5 }, |
| 5302 { Dart_NativeArgument_kString, 6 }, |
| 5303 { Dart_NativeArgument_kNativeFields, 0 }, |
| 5304 { Dart_NativeArgument_kNativeFields, 7 }, |
| 5305 }; |
| 5306 Dart_NativeArgument_Value arg_values[8]; |
| 5307 Dart_Handle result = Dart_GetNativeArguments(args, |
| 5308 8, |
| 5309 arg_descriptors, |
| 5310 arg_values); |
| 5311 EXPECT(Dart_IsError(result)); |
| 5312 } |
| 5313 |
| 5314 // Test with an invalid range error. |
| 5315 { |
| 5316 const Dart_NativeArgument_Descriptor arg_descriptors[8] = { |
| 5317 { Dart_NativeArgument_kInt32, 2 }, |
| 5318 { Dart_NativeArgument_kUint64, 2 }, |
| 5319 { Dart_NativeArgument_kBool, 3 }, |
| 5320 { Dart_NativeArgument_kDouble, 4 }, |
| 5321 { Dart_NativeArgument_kString, 5 }, |
| 5322 { Dart_NativeArgument_kString, 6 }, |
| 5323 { Dart_NativeArgument_kNativeFields, 0 }, |
| 5324 { Dart_NativeArgument_kNativeFields, 7 }, |
| 5325 }; |
| 5326 Dart_NativeArgument_Value arg_values[8]; |
| 5327 Dart_Handle result = Dart_GetNativeArguments(args, |
| 5328 8, |
| 5329 arg_descriptors, |
| 5330 arg_values); |
| 5331 EXPECT(Dart_IsError(result)); |
| 5332 } |
| 5333 |
| 5334 Dart_SetIntegerReturnValue(args, 0); |
| 5335 } |
| 5336 |
| 5337 |
| 5338 static Dart_NativeFunction native_args_lookup(Dart_Handle name, |
| 5339 int argument_count, |
| 5340 bool* auto_scope_setup) { |
| 5341 const Object& obj = Object::Handle(Api::UnwrapHandle(name)); |
| 5342 if (!obj.IsString()) { |
| 5343 return NULL; |
| 5344 } |
| 5345 ASSERT(auto_scope_setup != NULL); |
| 5346 *auto_scope_setup = true; |
| 5347 const char* function_name = obj.ToCString(); |
| 5348 ASSERT(function_name != NULL); |
| 5349 if (!strcmp(function_name, "NativeArgument_Create")) { |
| 5350 return reinterpret_cast<Dart_NativeFunction>(&NativeArgumentCreate); |
| 5351 } else if (!strcmp(function_name, "NativeArgument_Access")) { |
| 5352 return reinterpret_cast<Dart_NativeFunction>(&NativeArgumentAccess); |
| 5353 } |
| 5354 return NULL; |
| 5355 } |
| 5356 |
| 5357 |
| 5358 TEST_CASE(GetNativeArguments) { |
| 5359 const char* kScriptChars = |
| 5360 "import 'dart:nativewrappers';" |
| 5361 "class MyObject extends NativeFieldWrapperClass2 {" |
| 5362 " static MyObject createObject() native 'NativeArgument_Create';" |
| 5363 " int accessFields(int arg1," |
| 5364 " int arg2," |
| 5365 " bool arg3," |
| 5366 " double arg4," |
| 5367 " String arg5," |
| 5368 " String arg6," |
| 5369 " MyObject arg7) native 'NativeArgument_Access';" |
| 5370 "}" |
| 5371 "int testMain(String extstr) {" |
| 5372 " String str = 'abcdefg';" |
| 5373 " MyObject obj1 = MyObject.createObject();" |
| 5374 " MyObject obj2 = MyObject.createObject();" |
| 5375 " return obj1.accessFields(77," |
| 5376 " 0xffffffffffffffff," |
| 5377 " true," |
| 5378 " 3.14," |
| 5379 " str," |
| 5380 " extstr," |
| 5381 " obj2);" |
| 5382 "}"; |
| 5383 |
| 5384 Dart_Handle lib = TestCase::LoadTestScript( |
| 5385 kScriptChars, |
| 5386 reinterpret_cast<Dart_NativeEntryResolver>(native_args_lookup)); |
| 5387 |
| 5388 intptr_t size; |
| 5389 Dart_Handle ascii_str = NewString("string"); |
| 5390 EXPECT_VALID(ascii_str); |
| 5391 EXPECT_VALID(Dart_StringStorageSize(ascii_str, &size)); |
| 5392 uint8_t ext_ascii_str[10]; |
| 5393 Dart_Handle extstr = Dart_MakeExternalString( |
| 5394 ascii_str, |
| 5395 ext_ascii_str, |
| 5396 size, |
| 5397 reinterpret_cast<void*>(&native_arg_str_peer), |
| 5398 NULL); |
| 5399 |
| 5400 Dart_Handle args[1]; |
| 5401 args[0] = extstr; |
| 5402 Dart_Handle result = Dart_Invoke(lib, NewString("testMain"), 1, args); |
| 5403 EXPECT_VALID(result); |
| 5404 EXPECT(Dart_IsInteger(result)); |
| 5405 } |
| 5406 |
| 5407 |
| 5408 static void NativeArgumentCounter(Dart_NativeArguments args) { |
5212 Dart_EnterScope(); | 5409 Dart_EnterScope(); |
5213 int count = Dart_GetNativeArgumentCount(args); | 5410 int count = Dart_GetNativeArgumentCount(args); |
5214 Dart_SetReturnValue(args, Dart_NewInteger(count)); | 5411 Dart_SetReturnValue(args, Dart_NewInteger(count)); |
5215 Dart_ExitScope(); | 5412 Dart_ExitScope(); |
5216 } | 5413 } |
5217 | 5414 |
5218 | 5415 |
5219 static Dart_NativeFunction gnac_lookup(Dart_Handle name, | 5416 static Dart_NativeFunction gnac_lookup(Dart_Handle name, |
5220 int argument_count, | 5417 int argument_count, |
5221 bool* auto_setup_scope) { | 5418 bool* auto_setup_scope) { |
(...skipping 2989 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8211 NewString("main"), | 8408 NewString("main"), |
8212 1, | 8409 1, |
8213 dart_args); | 8410 dart_args); |
8214 int64_t value = 0; | 8411 int64_t value = 0; |
8215 result = Dart_IntegerToInt64(result, &value); | 8412 result = Dart_IntegerToInt64(result, &value); |
8216 EXPECT_VALID(result); | 8413 EXPECT_VALID(result); |
8217 EXPECT_EQ(6, value); | 8414 EXPECT_EQ(6, value); |
8218 } | 8415 } |
8219 | 8416 |
8220 } // namespace dart | 8417 } // namespace dart |
OLD | NEW |