| Index: vm/dart_api_impl_test.cc
|
| ===================================================================
|
| --- vm/dart_api_impl_test.cc (revision 1624)
|
| +++ vm/dart_api_impl_test.cc (working copy)
|
| @@ -988,6 +988,17 @@
|
| }
|
|
|
|
|
| +void NativeFieldLookup(Dart_NativeArguments args) {
|
| + UNREACHABLE();
|
| +}
|
| +
|
| +
|
| +static Dart_NativeFunction native_field_lookup(Dart_Handle name,
|
| + int argument_count) {
|
| + return reinterpret_cast<Dart_NativeFunction>(&NativeFieldLookup);
|
| +}
|
| +
|
| +
|
| UNIT_TEST_CASE(InjectNativeFields1) {
|
| const char* kScriptChars =
|
| "class NativeFields extends NativeFieldsWrapper {\n"
|
| @@ -1007,12 +1018,12 @@
|
|
|
| Dart_CreateIsolate(NULL, NULL);
|
| {
|
| - DARTSCOPE(Isolate::Current());
|
| Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
|
| const int kNumNativeFields = 4;
|
|
|
| // Create a test library.
|
| - Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
|
| + Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
|
| + native_field_lookup);
|
|
|
| // Create a native wrapper class with native fields.
|
| result = Dart_CreateNativeWrapperClass(
|
| @@ -1029,6 +1040,7 @@
|
| 0,
|
| NULL);
|
| EXPECT_VALID(result);
|
| + DARTSCOPE(Isolate::Current());
|
| Instance& obj = Instance::Handle();
|
| obj ^= Api::UnwrapHandle(result);
|
| const Class& cls = Class::Handle(obj.clazz());
|
| @@ -1089,6 +1101,176 @@
|
| }
|
|
|
|
|
| +UNIT_TEST_CASE(InjectNativeFields3) {
|
| + const char* kScriptChars =
|
| + "#import('dart:nativewrappers');"
|
| + "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);
|
| + {
|
| + 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,
|
| + native_field_lookup);
|
| +
|
| + // 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);
|
| + DARTSCOPE(Isolate::Current());
|
| + 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();
|
| +}
|
| +
|
| +
|
| +UNIT_TEST_CASE(InjectNativeFields4) {
|
| + const char* kScriptChars =
|
| + "#import('dart:nativewrappers');"
|
| + "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);
|
| + {
|
| + 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.
|
| + result = Dart_InvokeStatic(lib,
|
| + Dart_NewString("NativeFieldsTest"),
|
| + Dart_NewString("testMain"),
|
| + 0,
|
| + NULL);
|
| + // We expect the test script to fail finalization with the error :
|
| + // "class 'NativeFields' is trying to extend a native fields class,"
|
| + // "but library 'TestCase::url()' has no native resolvers".
|
| + EXPECT(Dart_IsError(result));
|
| + Dart_Handle expected_error = Dart_Error(
|
| + "class 'NativeFields' is trying to extend a native fields class,"
|
| + "but library '%s' has no native resolvers", TestCase::url());
|
| + EXPECT_STREQ(Dart_GetError(expected_error), Dart_GetError(result));
|
| + 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_IsError(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_IsError(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_IsError(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_IsError(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"
|
| @@ -1112,7 +1294,8 @@
|
| const int kNumNativeFields = 4;
|
|
|
| // Create a test library.
|
| - Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
|
| + Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
|
| + native_field_lookup);
|
|
|
| // Create a native wrapper class with native fields.
|
| result = Dart_CreateNativeWrapperClass(
|
| @@ -1131,73 +1314,49 @@
|
| EXPECT_VALID(retobj);
|
|
|
| // Now access and set various instance fields of the returned object.
|
| - result = Dart_GetInstanceField(retobj, Dart_NewString("fld3"));
|
| - EXPECT(Dart_IsError(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_IsError(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_IsError(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_IsError(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:nativewrappers');"
|
| + "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,
|
| + native_field_lookup);
|
| +
|
| + // 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);
|
| +
|
| + // Now access and set various instance fields of the returned object.
|
| + TestNativeFields(retobj);
|
| +
|
| Dart_ExitScope(); // Exit the Dart API scope.
|
| }
|
| Dart_ShutdownIsolate();
|
|
|