Index: vm/dart_api_impl_test.cc |
=================================================================== |
--- vm/dart_api_impl_test.cc (revision 1465) |
+++ vm/dart_api_impl_test.cc (working copy) |
@@ -1049,6 +1049,132 @@ |
} |
+UNIT_TEST_CASE(InjectNativeFields3) { |
+ const char* kScriptChars = |
+ "#import('dart:core_native_fields');" |
+ "class NativeFields extends NativeFieldWrapperClass2 {\n" |
+ " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
+ " int fld1;\n" |
+ " final int fld2;\n" |
+ " static int fld3;\n" |
+ " static final int fld4 = 10;\n" |
+ "}\n" |
+ "class NativeFieldsTest {\n" |
+ " static NativeFields testMain() {\n" |
+ " NativeFields obj = new NativeFields(10, 20);\n" |
+ " return obj;\n" |
+ " }\n" |
+ "}\n"; |
+ Dart_Handle result; |
+ |
+ Dart_CreateIsolate(NULL, NULL); |
+ { |
+ Zone zone; |
+ HandleScope scope; |
Anton Muhin
2011/11/13 16:19:32
may you defer VM Zone and HandleScope creation til
siva
2011/11/15 02:16:52
Done.
|
+ Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
+ const int kNumNativeFields = 2; |
+ |
+ // Load up a test script in the test library. |
+ Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
+ |
+ // Invoke a function which returns an object of type NativeFields. |
+ result = Dart_InvokeStatic(lib, |
+ Dart_NewString("NativeFieldsTest"), |
+ Dart_NewString("testMain"), |
+ 0, |
+ NULL); |
+ EXPECT_VALID(result); |
+ EXPECT(!Dart_ExceptionOccurred(result)); |
+ Instance& obj = Instance::Handle(); |
+ obj ^= Api::UnwrapHandle(result); |
+ const Class& cls = Class::Handle(obj.clazz()); |
+ // We expect the newly created "NativeFields" object to have |
+ // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. |
+ // Hence the size of an instance of "NativeFields" should be |
+ // (kNumNativeFields + 2) * kWordSize + sizeof the header word. |
+ // We check to make sure the instance size computed by the VM matches |
+ // our expectations. |
+ EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize, |
+ kObjectAlignment), |
+ cls.instance_size()); |
+ |
+ Dart_ExitScope(); // Exit the Dart API scope. |
+ } |
+ Dart_ShutdownIsolate(); |
+} |
+ |
+ |
+static void TestNativeFields(Dart_Handle retobj) { |
+ // Access and set various instance fields of the object. |
+ Dart_Handle result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); |
+ EXPECT(!Dart_IsValid(result)); |
+ result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
+ EXPECT_VALID(result); |
+ int64_t value = 0; |
+ result = Dart_IntegerValue(result, &value); |
+ EXPECT_EQ(10, value); |
+ result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); |
+ EXPECT_VALID(result); |
+ result = Dart_IntegerValue(result, &value); |
+ EXPECT_EQ(20, value); |
+ result = Dart_SetInstanceField(retobj, |
+ Dart_NewString("fld2"), |
+ Dart_NewInteger(40)); |
+ EXPECT(!Dart_IsValid(result)); |
+ result = Dart_SetInstanceField(retobj, |
+ Dart_NewString("fld1"), |
+ Dart_NewInteger(40)); |
+ EXPECT_VALID(result); |
+ result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
+ EXPECT_VALID(result); |
+ result = Dart_IntegerValue(result, &value); |
+ EXPECT_EQ(40, value); |
+ |
+ // Now access and set various native instance fields of the returned object. |
+ const int kNativeFld0 = 0; |
+ const int kNativeFld1 = 1; |
+ const int kNativeFld2 = 2; |
+ const int kNativeFld3 = 3; |
+ const int kNativeFld4 = 4; |
+ intptr_t field_value = 0; |
+ result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &field_value); |
+ EXPECT(!Dart_IsValid(result)); |
+ result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &field_value); |
+ EXPECT_VALID(result); |
+ EXPECT_EQ(0, field_value); |
+ result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &field_value); |
+ EXPECT_VALID(result); |
+ EXPECT_EQ(0, field_value); |
+ result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &field_value); |
+ EXPECT_VALID(result); |
+ EXPECT_EQ(0, field_value); |
+ result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); |
+ EXPECT(!Dart_IsValid(result)); |
+ result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 4); |
+ EXPECT_VALID(result); |
+ result = Dart_SetNativeInstanceField(retobj, kNativeFld1, 40); |
+ EXPECT_VALID(result); |
+ result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); |
+ EXPECT_VALID(result); |
+ result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); |
+ EXPECT_VALID(result); |
+ result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); |
+ EXPECT_VALID(result); |
+ EXPECT_EQ(4000, field_value); |
+ |
+ // Now re-access various dart instance fields of the returned object |
+ // to ensure that there was no corruption while setting native fields. |
+ result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
+ EXPECT_VALID(result); |
+ result = Dart_IntegerValue(result, &value); |
+ EXPECT_EQ(40, value); |
+ result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); |
+ EXPECT_VALID(result); |
+ result = Dart_IntegerValue(result, &value); |
+ EXPECT_EQ(20, value); |
+} |
+ |
+ |
UNIT_TEST_CASE(NativeFieldAccess) { |
const char* kScriptChars = |
"class NativeFields extends NativeFieldsWrapper {\n" |
@@ -1092,73 +1218,49 @@ |
EXPECT(!Dart_ExceptionOccurred(retobj)); |
// Now access and set various instance fields of the returned object. |
- result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); |
- EXPECT(!Dart_IsValid(result)); |
- result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
- EXPECT_VALID(result); |
- int64_t value = 0; |
- result = Dart_IntegerValue(result, &value); |
- EXPECT_EQ(10, value); |
- result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); |
- EXPECT_VALID(result); |
- result = Dart_IntegerValue(result, &value); |
- EXPECT_EQ(20, value); |
- result = Dart_SetInstanceField(retobj, |
- Dart_NewString("fld2"), |
- Dart_NewInteger(40)); |
- EXPECT(!Dart_IsValid(result)); |
- result = Dart_SetInstanceField(retobj, |
- Dart_NewString("fld1"), |
- Dart_NewInteger(40)); |
- EXPECT_VALID(result); |
- result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
- EXPECT_VALID(result); |
- result = Dart_IntegerValue(result, &value); |
- EXPECT_EQ(40, value); |
+ TestNativeFields(retobj); |
- // Now access and set various native instance fields of the returned object. |
- const int kNativeFld0 = 0; |
- const int kNativeFld1 = 1; |
- const int kNativeFld2 = 2; |
- const int kNativeFld3 = 3; |
- const int kNativeFld4 = 4; |
- intptr_t field_value = 0; |
- result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &field_value); |
- EXPECT(!Dart_IsValid(result)); |
- result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &field_value); |
- EXPECT_VALID(result); |
- EXPECT_EQ(0, field_value); |
- result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &field_value); |
- EXPECT_VALID(result); |
- EXPECT_EQ(0, field_value); |
- result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &field_value); |
- EXPECT_VALID(result); |
- EXPECT_EQ(0, field_value); |
- result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); |
- EXPECT(!Dart_IsValid(result)); |
- result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 4); |
- EXPECT_VALID(result); |
- result = Dart_SetNativeInstanceField(retobj, kNativeFld1, 40); |
- EXPECT_VALID(result); |
- result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); |
- EXPECT_VALID(result); |
- result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); |
- EXPECT_VALID(result); |
- result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); |
- EXPECT_VALID(result); |
- EXPECT_EQ(4000, field_value); |
+ Dart_ExitScope(); // Exit the Dart API scope. |
+ } |
+ Dart_ShutdownIsolate(); |
+} |
- // Now re-access various dart instance fields of the returned object |
- // to ensure that there was no corruption while setting native fields. |
- result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
- EXPECT_VALID(result); |
- result = Dart_IntegerValue(result, &value); |
- EXPECT_EQ(40, value); |
- result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); |
- EXPECT_VALID(result); |
- result = Dart_IntegerValue(result, &value); |
- EXPECT_EQ(20, value); |
+UNIT_TEST_CASE(ImplicitNativeFieldAccess) { |
+ const char* kScriptChars = |
+ "#import('dart:core_native_fields');" |
+ "class NativeFields extends NativeFieldWrapperClass4 {\n" |
+ " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
+ " int fld1;\n" |
+ " final int fld2;\n" |
+ " static int fld3;\n" |
+ " static final int fld4 = 10;\n" |
+ "}\n" |
+ "class NativeFieldsTest {\n" |
+ " static NativeFields testMain() {\n" |
+ " NativeFields obj = new NativeFields(10, 20);\n" |
+ " return obj;\n" |
+ " }\n" |
+ "}\n"; |
+ Dart_CreateIsolate(NULL, NULL); |
+ { |
+ Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
+ |
+ // Load up a test script in the test library. |
+ Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
+ |
+ // Invoke a function which returns an object of type NativeFields. |
+ Dart_Handle retobj = Dart_InvokeStatic(lib, |
+ Dart_NewString("NativeFieldsTest"), |
+ Dart_NewString("testMain"), |
+ 0, |
+ NULL); |
+ EXPECT_VALID(retobj); |
+ EXPECT(!Dart_ExceptionOccurred(retobj)); |
+ |
+ // Now access and set various instance fields of the returned object. |
+ TestNativeFields(retobj); |
+ |
Dart_ExitScope(); // Exit the Dart API scope. |
} |
Dart_ShutdownIsolate(); |