OLD | NEW |
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" |
(...skipping 970 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
981 EXPECT_VALID(result); | 981 EXPECT_VALID(result); |
982 result = Dart_IntegerValue(result, &value); | 982 result = Dart_IntegerValue(result, &value); |
983 EXPECT_EQ(40, value); | 983 EXPECT_EQ(40, value); |
984 | 984 |
985 Dart_ExitScope(); // Exit the Dart API scope. | 985 Dart_ExitScope(); // Exit the Dart API scope. |
986 } | 986 } |
987 Dart_ShutdownIsolate(); | 987 Dart_ShutdownIsolate(); |
988 } | 988 } |
989 | 989 |
990 | 990 |
| 991 void NativeFieldLookup(Dart_NativeArguments args) { |
| 992 UNREACHABLE(); |
| 993 } |
| 994 |
| 995 |
| 996 static Dart_NativeFunction native_field_lookup(Dart_Handle name, |
| 997 int argument_count) { |
| 998 return reinterpret_cast<Dart_NativeFunction>(&NativeFieldLookup); |
| 999 } |
| 1000 |
| 1001 |
991 UNIT_TEST_CASE(InjectNativeFields1) { | 1002 UNIT_TEST_CASE(InjectNativeFields1) { |
992 const char* kScriptChars = | 1003 const char* kScriptChars = |
993 "class NativeFields extends NativeFieldsWrapper {\n" | 1004 "class NativeFields extends NativeFieldsWrapper {\n" |
994 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | 1005 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
995 " int fld1;\n" | 1006 " int fld1;\n" |
996 " final int fld2;\n" | 1007 " final int fld2;\n" |
997 " static int fld3;\n" | 1008 " static int fld3;\n" |
998 " static final int fld4 = 10;\n" | 1009 " static final int fld4 = 10;\n" |
999 "}\n" | 1010 "}\n" |
1000 "class NativeFieldsTest {\n" | 1011 "class NativeFieldsTest {\n" |
1001 " static NativeFields testMain() {\n" | 1012 " static NativeFields testMain() {\n" |
1002 " NativeFields obj = new NativeFields(10, 20);\n" | 1013 " NativeFields obj = new NativeFields(10, 20);\n" |
1003 " return obj;\n" | 1014 " return obj;\n" |
1004 " }\n" | 1015 " }\n" |
1005 "}\n"; | 1016 "}\n"; |
1006 Dart_Handle result; | 1017 Dart_Handle result; |
1007 | 1018 |
1008 Dart_CreateIsolate(NULL, NULL); | 1019 Dart_CreateIsolate(NULL, NULL); |
1009 { | 1020 { |
1010 DARTSCOPE(Isolate::Current()); | |
1011 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 1021 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
1012 const int kNumNativeFields = 4; | 1022 const int kNumNativeFields = 4; |
1013 | 1023 |
1014 // Create a test library. | 1024 // Create a test library. |
1015 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 1025 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, |
| 1026 native_field_lookup); |
1016 | 1027 |
1017 // Create a native wrapper class with native fields. | 1028 // Create a native wrapper class with native fields. |
1018 result = Dart_CreateNativeWrapperClass( | 1029 result = Dart_CreateNativeWrapperClass( |
1019 lib, | 1030 lib, |
1020 Dart_NewString("NativeFieldsWrapper"), | 1031 Dart_NewString("NativeFieldsWrapper"), |
1021 kNumNativeFields); | 1032 kNumNativeFields); |
1022 | 1033 |
1023 // Load up a test script in the test library. | 1034 // Load up a test script in the test library. |
1024 | 1035 |
1025 // Invoke a function which returns an object of type NativeFields. | 1036 // Invoke a function which returns an object of type NativeFields. |
1026 result = Dart_InvokeStatic(lib, | 1037 result = Dart_InvokeStatic(lib, |
1027 Dart_NewString("NativeFieldsTest"), | 1038 Dart_NewString("NativeFieldsTest"), |
1028 Dart_NewString("testMain"), | 1039 Dart_NewString("testMain"), |
1029 0, | 1040 0, |
1030 NULL); | 1041 NULL); |
1031 EXPECT_VALID(result); | 1042 EXPECT_VALID(result); |
| 1043 DARTSCOPE(Isolate::Current()); |
1032 Instance& obj = Instance::Handle(); | 1044 Instance& obj = Instance::Handle(); |
1033 obj ^= Api::UnwrapHandle(result); | 1045 obj ^= Api::UnwrapHandle(result); |
1034 const Class& cls = Class::Handle(obj.clazz()); | 1046 const Class& cls = Class::Handle(obj.clazz()); |
1035 // We expect the newly created "NativeFields" object to have | 1047 // We expect the newly created "NativeFields" object to have |
1036 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. | 1048 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. |
1037 // Hence the size of an instance of "NativeFields" should be | 1049 // Hence the size of an instance of "NativeFields" should be |
1038 // (kNumNativeFields + 2) * kWordSize + sizeof the header word. | 1050 // (kNumNativeFields + 2) * kWordSize + sizeof the header word. |
1039 // We check to make sure the instance size computed by the VM matches | 1051 // We check to make sure the instance size computed by the VM matches |
1040 // our expectations. | 1052 // our expectations. |
1041 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize, | 1053 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1082 // "NativeFieldsWrapper" and there is no definition of it either | 1094 // "NativeFieldsWrapper" and there is no definition of it either |
1083 // in the dart code or through the native field injection mechanism. | 1095 // in the dart code or through the native field injection mechanism. |
1084 EXPECT(Dart_IsError(result)); | 1096 EXPECT(Dart_IsError(result)); |
1085 | 1097 |
1086 Dart_ExitScope(); // Exit the Dart API scope. | 1098 Dart_ExitScope(); // Exit the Dart API scope. |
1087 } | 1099 } |
1088 Dart_ShutdownIsolate(); | 1100 Dart_ShutdownIsolate(); |
1089 } | 1101 } |
1090 | 1102 |
1091 | 1103 |
| 1104 UNIT_TEST_CASE(InjectNativeFields3) { |
| 1105 const char* kScriptChars = |
| 1106 "#import('dart:nativewrappers');" |
| 1107 "class NativeFields extends NativeFieldWrapperClass2 {\n" |
| 1108 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 1109 " int fld1;\n" |
| 1110 " final int fld2;\n" |
| 1111 " static int fld3;\n" |
| 1112 " static final int fld4 = 10;\n" |
| 1113 "}\n" |
| 1114 "class NativeFieldsTest {\n" |
| 1115 " static NativeFields testMain() {\n" |
| 1116 " NativeFields obj = new NativeFields(10, 20);\n" |
| 1117 " return obj;\n" |
| 1118 " }\n" |
| 1119 "}\n"; |
| 1120 Dart_Handle result; |
| 1121 |
| 1122 Dart_CreateIsolate(NULL, NULL); |
| 1123 { |
| 1124 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 1125 const int kNumNativeFields = 2; |
| 1126 |
| 1127 // Load up a test script in the test library. |
| 1128 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, |
| 1129 native_field_lookup); |
| 1130 |
| 1131 // Invoke a function which returns an object of type NativeFields. |
| 1132 result = Dart_InvokeStatic(lib, |
| 1133 Dart_NewString("NativeFieldsTest"), |
| 1134 Dart_NewString("testMain"), |
| 1135 0, |
| 1136 NULL); |
| 1137 EXPECT_VALID(result); |
| 1138 DARTSCOPE(Isolate::Current()); |
| 1139 Instance& obj = Instance::Handle(); |
| 1140 obj ^= Api::UnwrapHandle(result); |
| 1141 const Class& cls = Class::Handle(obj.clazz()); |
| 1142 // We expect the newly created "NativeFields" object to have |
| 1143 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. |
| 1144 // Hence the size of an instance of "NativeFields" should be |
| 1145 // (kNumNativeFields + 2) * kWordSize + sizeof the header word. |
| 1146 // We check to make sure the instance size computed by the VM matches |
| 1147 // our expectations. |
| 1148 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize, |
| 1149 kObjectAlignment), |
| 1150 cls.instance_size()); |
| 1151 |
| 1152 Dart_ExitScope(); // Exit the Dart API scope. |
| 1153 } |
| 1154 Dart_ShutdownIsolate(); |
| 1155 } |
| 1156 |
| 1157 |
| 1158 UNIT_TEST_CASE(InjectNativeFields4) { |
| 1159 const char* kScriptChars = |
| 1160 "#import('dart:nativewrappers');" |
| 1161 "class NativeFields extends NativeFieldWrapperClass2 {\n" |
| 1162 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 1163 " int fld1;\n" |
| 1164 " final int fld2;\n" |
| 1165 " static int fld3;\n" |
| 1166 " static final int fld4 = 10;\n" |
| 1167 "}\n" |
| 1168 "class NativeFieldsTest {\n" |
| 1169 " static NativeFields testMain() {\n" |
| 1170 " NativeFields obj = new NativeFields(10, 20);\n" |
| 1171 " return obj;\n" |
| 1172 " }\n" |
| 1173 "}\n"; |
| 1174 Dart_Handle result; |
| 1175 |
| 1176 Dart_CreateIsolate(NULL, NULL); |
| 1177 { |
| 1178 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 1179 |
| 1180 // Load up a test script in the test library. |
| 1181 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 1182 |
| 1183 // Invoke a function which returns an object of type NativeFields. |
| 1184 result = Dart_InvokeStatic(lib, |
| 1185 Dart_NewString("NativeFieldsTest"), |
| 1186 Dart_NewString("testMain"), |
| 1187 0, |
| 1188 NULL); |
| 1189 // We expect the test script to fail finalization with the error : |
| 1190 // "class 'NativeFields' is trying to extend a native fields class," |
| 1191 // "but library 'TestCase::url()' has no native resolvers". |
| 1192 EXPECT(Dart_IsError(result)); |
| 1193 Dart_Handle expected_error = Dart_Error( |
| 1194 "class 'NativeFields' is trying to extend a native fields class," |
| 1195 "but library '%s' has no native resolvers", TestCase::url()); |
| 1196 EXPECT_STREQ(Dart_GetError(expected_error), Dart_GetError(result)); |
| 1197 Dart_ExitScope(); // Exit the Dart API scope. |
| 1198 } |
| 1199 Dart_ShutdownIsolate(); |
| 1200 } |
| 1201 |
| 1202 |
| 1203 static void TestNativeFields(Dart_Handle retobj) { |
| 1204 // Access and set various instance fields of the object. |
| 1205 Dart_Handle result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); |
| 1206 EXPECT(Dart_IsError(result)); |
| 1207 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
| 1208 EXPECT_VALID(result); |
| 1209 int64_t value = 0; |
| 1210 result = Dart_IntegerValue(result, &value); |
| 1211 EXPECT_EQ(10, value); |
| 1212 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); |
| 1213 EXPECT_VALID(result); |
| 1214 result = Dart_IntegerValue(result, &value); |
| 1215 EXPECT_EQ(20, value); |
| 1216 result = Dart_SetInstanceField(retobj, |
| 1217 Dart_NewString("fld2"), |
| 1218 Dart_NewInteger(40)); |
| 1219 EXPECT(Dart_IsError(result)); |
| 1220 result = Dart_SetInstanceField(retobj, |
| 1221 Dart_NewString("fld1"), |
| 1222 Dart_NewInteger(40)); |
| 1223 EXPECT_VALID(result); |
| 1224 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
| 1225 EXPECT_VALID(result); |
| 1226 result = Dart_IntegerValue(result, &value); |
| 1227 EXPECT_EQ(40, value); |
| 1228 |
| 1229 // Now access and set various native instance fields of the returned object. |
| 1230 const int kNativeFld0 = 0; |
| 1231 const int kNativeFld1 = 1; |
| 1232 const int kNativeFld2 = 2; |
| 1233 const int kNativeFld3 = 3; |
| 1234 const int kNativeFld4 = 4; |
| 1235 intptr_t field_value = 0; |
| 1236 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &field_value); |
| 1237 EXPECT(Dart_IsError(result)); |
| 1238 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &field_value); |
| 1239 EXPECT_VALID(result); |
| 1240 EXPECT_EQ(0, field_value); |
| 1241 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &field_value); |
| 1242 EXPECT_VALID(result); |
| 1243 EXPECT_EQ(0, field_value); |
| 1244 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &field_value); |
| 1245 EXPECT_VALID(result); |
| 1246 EXPECT_EQ(0, field_value); |
| 1247 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); |
| 1248 EXPECT(Dart_IsError(result)); |
| 1249 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 4); |
| 1250 EXPECT_VALID(result); |
| 1251 result = Dart_SetNativeInstanceField(retobj, kNativeFld1, 40); |
| 1252 EXPECT_VALID(result); |
| 1253 result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); |
| 1254 EXPECT_VALID(result); |
| 1255 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); |
| 1256 EXPECT_VALID(result); |
| 1257 result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); |
| 1258 EXPECT_VALID(result); |
| 1259 EXPECT_EQ(4000, field_value); |
| 1260 |
| 1261 // Now re-access various dart instance fields of the returned object |
| 1262 // to ensure that there was no corruption while setting native fields. |
| 1263 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); |
| 1264 EXPECT_VALID(result); |
| 1265 result = Dart_IntegerValue(result, &value); |
| 1266 EXPECT_EQ(40, value); |
| 1267 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); |
| 1268 EXPECT_VALID(result); |
| 1269 result = Dart_IntegerValue(result, &value); |
| 1270 EXPECT_EQ(20, value); |
| 1271 } |
| 1272 |
| 1273 |
1092 UNIT_TEST_CASE(NativeFieldAccess) { | 1274 UNIT_TEST_CASE(NativeFieldAccess) { |
1093 const char* kScriptChars = | 1275 const char* kScriptChars = |
1094 "class NativeFields extends NativeFieldsWrapper {\n" | 1276 "class NativeFields extends NativeFieldsWrapper {\n" |
1095 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | 1277 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
1096 " int fld1;\n" | 1278 " int fld1;\n" |
1097 " final int fld2;\n" | 1279 " final int fld2;\n" |
1098 " static int fld3;\n" | 1280 " static int fld3;\n" |
1099 " static final int fld4 = 10;\n" | 1281 " static final int fld4 = 10;\n" |
1100 "}\n" | 1282 "}\n" |
1101 "class NativeFieldsTest {\n" | 1283 "class NativeFieldsTest {\n" |
1102 " static NativeFields testMain() {\n" | 1284 " static NativeFields testMain() {\n" |
1103 " NativeFields obj = new NativeFields(10, 20);\n" | 1285 " NativeFields obj = new NativeFields(10, 20);\n" |
1104 " return obj;\n" | 1286 " return obj;\n" |
1105 " }\n" | 1287 " }\n" |
1106 "}\n"; | 1288 "}\n"; |
1107 Dart_Handle result; | 1289 Dart_Handle result; |
1108 | 1290 |
1109 Dart_CreateIsolate(NULL, NULL); | 1291 Dart_CreateIsolate(NULL, NULL); |
1110 { | 1292 { |
1111 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 1293 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
1112 const int kNumNativeFields = 4; | 1294 const int kNumNativeFields = 4; |
1113 | 1295 |
1114 // Create a test library. | 1296 // Create a test library. |
1115 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 1297 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, |
| 1298 native_field_lookup); |
1116 | 1299 |
1117 // Create a native wrapper class with native fields. | 1300 // Create a native wrapper class with native fields. |
1118 result = Dart_CreateNativeWrapperClass( | 1301 result = Dart_CreateNativeWrapperClass( |
1119 lib, | 1302 lib, |
1120 Dart_NewString("NativeFieldsWrapper"), | 1303 Dart_NewString("NativeFieldsWrapper"), |
1121 kNumNativeFields); | 1304 kNumNativeFields); |
1122 | 1305 |
1123 // Load up a test script in it. | 1306 // Load up a test script in it. |
1124 | 1307 |
1125 // Invoke a function which returns an object of type NativeFields. | 1308 // Invoke a function which returns an object of type NativeFields. |
1126 Dart_Handle retobj = Dart_InvokeStatic(lib, | 1309 Dart_Handle retobj = Dart_InvokeStatic(lib, |
1127 Dart_NewString("NativeFieldsTest"), | 1310 Dart_NewString("NativeFieldsTest"), |
1128 Dart_NewString("testMain"), | 1311 Dart_NewString("testMain"), |
1129 0, | 1312 0, |
1130 NULL); | 1313 NULL); |
1131 EXPECT_VALID(retobj); | 1314 EXPECT_VALID(retobj); |
1132 | 1315 |
1133 // Now access and set various instance fields of the returned object. | 1316 // Now access and set various instance fields of the returned object. |
1134 result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); | 1317 TestNativeFields(retobj); |
1135 EXPECT(Dart_IsError(result)); | |
1136 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
1137 EXPECT_VALID(result); | |
1138 int64_t value = 0; | |
1139 result = Dart_IntegerValue(result, &value); | |
1140 EXPECT_EQ(10, value); | |
1141 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
1142 EXPECT_VALID(result); | |
1143 result = Dart_IntegerValue(result, &value); | |
1144 EXPECT_EQ(20, value); | |
1145 result = Dart_SetInstanceField(retobj, | |
1146 Dart_NewString("fld2"), | |
1147 Dart_NewInteger(40)); | |
1148 EXPECT(Dart_IsError(result)); | |
1149 result = Dart_SetInstanceField(retobj, | |
1150 Dart_NewString("fld1"), | |
1151 Dart_NewInteger(40)); | |
1152 EXPECT_VALID(result); | |
1153 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
1154 EXPECT_VALID(result); | |
1155 result = Dart_IntegerValue(result, &value); | |
1156 EXPECT_EQ(40, value); | |
1157 | |
1158 // Now access and set various native instance fields of the returned object. | |
1159 const int kNativeFld0 = 0; | |
1160 const int kNativeFld1 = 1; | |
1161 const int kNativeFld2 = 2; | |
1162 const int kNativeFld3 = 3; | |
1163 const int kNativeFld4 = 4; | |
1164 intptr_t field_value = 0; | |
1165 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &field_value); | |
1166 EXPECT(Dart_IsError(result)); | |
1167 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &field_value); | |
1168 EXPECT_VALID(result); | |
1169 EXPECT_EQ(0, field_value); | |
1170 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &field_value); | |
1171 EXPECT_VALID(result); | |
1172 EXPECT_EQ(0, field_value); | |
1173 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &field_value); | |
1174 EXPECT_VALID(result); | |
1175 EXPECT_EQ(0, field_value); | |
1176 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); | |
1177 EXPECT(Dart_IsError(result)); | |
1178 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 4); | |
1179 EXPECT_VALID(result); | |
1180 result = Dart_SetNativeInstanceField(retobj, kNativeFld1, 40); | |
1181 EXPECT_VALID(result); | |
1182 result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); | |
1183 EXPECT_VALID(result); | |
1184 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); | |
1185 EXPECT_VALID(result); | |
1186 result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); | |
1187 EXPECT_VALID(result); | |
1188 EXPECT_EQ(4000, field_value); | |
1189 | |
1190 // Now re-access various dart instance fields of the returned object | |
1191 // to ensure that there was no corruption while setting native fields. | |
1192 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
1193 EXPECT_VALID(result); | |
1194 result = Dart_IntegerValue(result, &value); | |
1195 EXPECT_EQ(40, value); | |
1196 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
1197 EXPECT_VALID(result); | |
1198 result = Dart_IntegerValue(result, &value); | |
1199 EXPECT_EQ(20, value); | |
1200 | 1318 |
1201 Dart_ExitScope(); // Exit the Dart API scope. | 1319 Dart_ExitScope(); // Exit the Dart API scope. |
1202 } | 1320 } |
1203 Dart_ShutdownIsolate(); | 1321 Dart_ShutdownIsolate(); |
| 1322 } |
| 1323 |
| 1324 |
| 1325 UNIT_TEST_CASE(ImplicitNativeFieldAccess) { |
| 1326 const char* kScriptChars = |
| 1327 "#import('dart:nativewrappers');" |
| 1328 "class NativeFields extends NativeFieldWrapperClass4 {\n" |
| 1329 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 1330 " int fld1;\n" |
| 1331 " final int fld2;\n" |
| 1332 " static int fld3;\n" |
| 1333 " static final int fld4 = 10;\n" |
| 1334 "}\n" |
| 1335 "class NativeFieldsTest {\n" |
| 1336 " static NativeFields testMain() {\n" |
| 1337 " NativeFields obj = new NativeFields(10, 20);\n" |
| 1338 " return obj;\n" |
| 1339 " }\n" |
| 1340 "}\n"; |
| 1341 Dart_CreateIsolate(NULL, NULL); |
| 1342 { |
| 1343 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 1344 |
| 1345 // Load up a test script in the test library. |
| 1346 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, |
| 1347 native_field_lookup); |
| 1348 |
| 1349 // Invoke a function which returns an object of type NativeFields. |
| 1350 Dart_Handle retobj = Dart_InvokeStatic(lib, |
| 1351 Dart_NewString("NativeFieldsTest"), |
| 1352 Dart_NewString("testMain"), |
| 1353 0, |
| 1354 NULL); |
| 1355 EXPECT_VALID(retobj); |
| 1356 |
| 1357 // Now access and set various instance fields of the returned object. |
| 1358 TestNativeFields(retobj); |
| 1359 |
| 1360 Dart_ExitScope(); // Exit the Dart API scope. |
| 1361 } |
| 1362 Dart_ShutdownIsolate(); |
1204 } | 1363 } |
1205 | 1364 |
1206 | 1365 |
1207 UNIT_TEST_CASE(NegativeNativeFieldAccess) { | 1366 UNIT_TEST_CASE(NegativeNativeFieldAccess) { |
1208 const char* kScriptChars = | 1367 const char* kScriptChars = |
1209 "class NativeFields {\n" | 1368 "class NativeFields {\n" |
1210 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | 1369 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
1211 " int fld1;\n" | 1370 " int fld1;\n" |
1212 " final int fld2;\n" | 1371 " final int fld2;\n" |
1213 " static int fld3;\n" | 1372 " static int fld3;\n" |
(...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2119 NULL); | 2278 NULL); |
2120 EXPECT_VALID(result); | 2279 EXPECT_VALID(result); |
2121 | 2280 |
2122 Dart_ExitScope(); // Exit the Dart API scope. | 2281 Dart_ExitScope(); // Exit the Dart API scope. |
2123 } | 2282 } |
2124 Dart_ShutdownIsolate(); | 2283 Dart_ShutdownIsolate(); |
2125 } | 2284 } |
2126 #endif // TARGET_ARCH_IA32. | 2285 #endif // TARGET_ARCH_IA32. |
2127 | 2286 |
2128 } // namespace dart | 2287 } // namespace dart |
OLD | NEW |