Chromium Code Reviews| 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) { | |
|
Ivan Posva
2011/11/16 19:19:53
?
siva
2011/11/17 00:04:28
See below.
On 2011/11/16 19:19:53, Ivan Posva wro
| |
| 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>(&native_field_lookup); | |
|
Ivan Posva
2011/11/16 19:19:53
This points to itself?
siva
2011/11/17 00:04:28
Changed native_field_lookup to &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:core-native-fields');" | |
| 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:core-native-fields');" | |
| 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 EXPECT(Dart_IsError(result)); | |
|
Ivan Posva
2011/11/16 19:19:53
Please specify at least what kind of error you exp
siva
2011/11/17 00:04:28
Done.
| |
| 1190 Dart_ExitScope(); // Exit the Dart API scope. | |
| 1191 } | |
| 1192 Dart_ShutdownIsolate(); | |
| 1193 } | |
| 1194 | |
| 1195 | |
| 1196 static void TestNativeFields(Dart_Handle retobj) { | |
| 1197 // Access and set various instance fields of the object. | |
| 1198 Dart_Handle result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); | |
| 1199 EXPECT(Dart_IsError(result)); | |
| 1200 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1201 EXPECT_VALID(result); | |
| 1202 int64_t value = 0; | |
| 1203 result = Dart_IntegerValue(result, &value); | |
| 1204 EXPECT_EQ(10, value); | |
| 1205 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
| 1206 EXPECT_VALID(result); | |
| 1207 result = Dart_IntegerValue(result, &value); | |
| 1208 EXPECT_EQ(20, value); | |
| 1209 result = Dart_SetInstanceField(retobj, | |
| 1210 Dart_NewString("fld2"), | |
| 1211 Dart_NewInteger(40)); | |
| 1212 EXPECT(Dart_IsError(result)); | |
| 1213 result = Dart_SetInstanceField(retobj, | |
| 1214 Dart_NewString("fld1"), | |
| 1215 Dart_NewInteger(40)); | |
| 1216 EXPECT_VALID(result); | |
| 1217 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1218 EXPECT_VALID(result); | |
| 1219 result = Dart_IntegerValue(result, &value); | |
| 1220 EXPECT_EQ(40, value); | |
| 1221 | |
| 1222 // Now access and set various native instance fields of the returned object. | |
| 1223 const int kNativeFld0 = 0; | |
| 1224 const int kNativeFld1 = 1; | |
| 1225 const int kNativeFld2 = 2; | |
| 1226 const int kNativeFld3 = 3; | |
| 1227 const int kNativeFld4 = 4; | |
| 1228 intptr_t field_value = 0; | |
| 1229 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &field_value); | |
| 1230 EXPECT(Dart_IsError(result)); | |
| 1231 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &field_value); | |
| 1232 EXPECT_VALID(result); | |
| 1233 EXPECT_EQ(0, field_value); | |
| 1234 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &field_value); | |
| 1235 EXPECT_VALID(result); | |
| 1236 EXPECT_EQ(0, field_value); | |
| 1237 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &field_value); | |
| 1238 EXPECT_VALID(result); | |
| 1239 EXPECT_EQ(0, field_value); | |
| 1240 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); | |
| 1241 EXPECT(Dart_IsError(result)); | |
| 1242 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 4); | |
| 1243 EXPECT_VALID(result); | |
| 1244 result = Dart_SetNativeInstanceField(retobj, kNativeFld1, 40); | |
| 1245 EXPECT_VALID(result); | |
| 1246 result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); | |
| 1247 EXPECT_VALID(result); | |
| 1248 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); | |
| 1249 EXPECT_VALID(result); | |
| 1250 result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); | |
| 1251 EXPECT_VALID(result); | |
| 1252 EXPECT_EQ(4000, field_value); | |
| 1253 | |
| 1254 // Now re-access various dart instance fields of the returned object | |
| 1255 // to ensure that there was no corruption while setting native fields. | |
| 1256 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1257 EXPECT_VALID(result); | |
| 1258 result = Dart_IntegerValue(result, &value); | |
| 1259 EXPECT_EQ(40, value); | |
| 1260 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
| 1261 EXPECT_VALID(result); | |
| 1262 result = Dart_IntegerValue(result, &value); | |
| 1263 EXPECT_EQ(20, value); | |
| 1264 } | |
| 1265 | |
| 1266 | |
| 1092 UNIT_TEST_CASE(NativeFieldAccess) { | 1267 UNIT_TEST_CASE(NativeFieldAccess) { |
| 1093 const char* kScriptChars = | 1268 const char* kScriptChars = |
| 1094 "class NativeFields extends NativeFieldsWrapper {\n" | 1269 "class NativeFields extends NativeFieldsWrapper {\n" |
| 1095 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | 1270 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 1096 " int fld1;\n" | 1271 " int fld1;\n" |
| 1097 " final int fld2;\n" | 1272 " final int fld2;\n" |
| 1098 " static int fld3;\n" | 1273 " static int fld3;\n" |
| 1099 " static final int fld4 = 10;\n" | 1274 " static final int fld4 = 10;\n" |
| 1100 "}\n" | 1275 "}\n" |
| 1101 "class NativeFieldsTest {\n" | 1276 "class NativeFieldsTest {\n" |
| 1102 " static NativeFields testMain() {\n" | 1277 " static NativeFields testMain() {\n" |
| 1103 " NativeFields obj = new NativeFields(10, 20);\n" | 1278 " NativeFields obj = new NativeFields(10, 20);\n" |
| 1104 " return obj;\n" | 1279 " return obj;\n" |
| 1105 " }\n" | 1280 " }\n" |
| 1106 "}\n"; | 1281 "}\n"; |
| 1107 Dart_Handle result; | 1282 Dart_Handle result; |
| 1108 | 1283 |
| 1109 Dart_CreateIsolate(NULL, NULL); | 1284 Dart_CreateIsolate(NULL, NULL); |
| 1110 { | 1285 { |
| 1111 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 1286 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 1112 const int kNumNativeFields = 4; | 1287 const int kNumNativeFields = 4; |
| 1113 | 1288 |
| 1114 // Create a test library. | 1289 // Create a test library. |
| 1115 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 1290 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, |
| 1291 native_field_lookup); | |
| 1116 | 1292 |
| 1117 // Create a native wrapper class with native fields. | 1293 // Create a native wrapper class with native fields. |
| 1118 result = Dart_CreateNativeWrapperClass( | 1294 result = Dart_CreateNativeWrapperClass( |
| 1119 lib, | 1295 lib, |
| 1120 Dart_NewString("NativeFieldsWrapper"), | 1296 Dart_NewString("NativeFieldsWrapper"), |
| 1121 kNumNativeFields); | 1297 kNumNativeFields); |
| 1122 | 1298 |
| 1123 // Load up a test script in it. | 1299 // Load up a test script in it. |
| 1124 | 1300 |
| 1125 // Invoke a function which returns an object of type NativeFields. | 1301 // Invoke a function which returns an object of type NativeFields. |
| 1126 Dart_Handle retobj = Dart_InvokeStatic(lib, | 1302 Dart_Handle retobj = Dart_InvokeStatic(lib, |
| 1127 Dart_NewString("NativeFieldsTest"), | 1303 Dart_NewString("NativeFieldsTest"), |
| 1128 Dart_NewString("testMain"), | 1304 Dart_NewString("testMain"), |
| 1129 0, | 1305 0, |
| 1130 NULL); | 1306 NULL); |
| 1131 EXPECT_VALID(retobj); | 1307 EXPECT_VALID(retobj); |
| 1132 | 1308 |
| 1133 // Now access and set various instance fields of the returned object. | 1309 // Now access and set various instance fields of the returned object. |
| 1134 result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); | 1310 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 | 1311 |
| 1201 Dart_ExitScope(); // Exit the Dart API scope. | 1312 Dart_ExitScope(); // Exit the Dart API scope. |
| 1202 } | 1313 } |
| 1203 Dart_ShutdownIsolate(); | 1314 Dart_ShutdownIsolate(); |
| 1315 } | |
| 1316 | |
| 1317 | |
| 1318 UNIT_TEST_CASE(ImplicitNativeFieldAccess) { | |
| 1319 const char* kScriptChars = | |
| 1320 "#import('dart:core-native-fields');" | |
| 1321 "class NativeFields extends NativeFieldWrapperClass4 {\n" | |
| 1322 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | |
| 1323 " int fld1;\n" | |
| 1324 " final int fld2;\n" | |
| 1325 " static int fld3;\n" | |
| 1326 " static final int fld4 = 10;\n" | |
| 1327 "}\n" | |
| 1328 "class NativeFieldsTest {\n" | |
| 1329 " static NativeFields testMain() {\n" | |
| 1330 " NativeFields obj = new NativeFields(10, 20);\n" | |
| 1331 " return obj;\n" | |
| 1332 " }\n" | |
| 1333 "}\n"; | |
| 1334 Dart_CreateIsolate(NULL, NULL); | |
| 1335 { | |
| 1336 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | |
| 1337 | |
| 1338 // Load up a test script in the test library. | |
| 1339 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, | |
| 1340 native_field_lookup); | |
| 1341 | |
| 1342 // Invoke a function which returns an object of type NativeFields. | |
| 1343 Dart_Handle retobj = Dart_InvokeStatic(lib, | |
| 1344 Dart_NewString("NativeFieldsTest"), | |
| 1345 Dart_NewString("testMain"), | |
| 1346 0, | |
| 1347 NULL); | |
| 1348 EXPECT_VALID(retobj); | |
| 1349 | |
| 1350 // Now access and set various instance fields of the returned object. | |
| 1351 TestNativeFields(retobj); | |
| 1352 | |
| 1353 Dart_ExitScope(); // Exit the Dart API scope. | |
| 1354 } | |
| 1355 Dart_ShutdownIsolate(); | |
| 1204 } | 1356 } |
| 1205 | 1357 |
| 1206 | 1358 |
| 1207 UNIT_TEST_CASE(NegativeNativeFieldAccess) { | 1359 UNIT_TEST_CASE(NegativeNativeFieldAccess) { |
| 1208 const char* kScriptChars = | 1360 const char* kScriptChars = |
| 1209 "class NativeFields {\n" | 1361 "class NativeFields {\n" |
| 1210 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | 1362 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 1211 " int fld1;\n" | 1363 " int fld1;\n" |
| 1212 " final int fld2;\n" | 1364 " final int fld2;\n" |
| 1213 " static int fld3;\n" | 1365 " static int fld3;\n" |
| (...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2119 NULL); | 2271 NULL); |
| 2120 EXPECT_VALID(result); | 2272 EXPECT_VALID(result); |
| 2121 | 2273 |
| 2122 Dart_ExitScope(); // Exit the Dart API scope. | 2274 Dart_ExitScope(); // Exit the Dart API scope. |
| 2123 } | 2275 } |
| 2124 Dart_ShutdownIsolate(); | 2276 Dart_ShutdownIsolate(); |
| 2125 } | 2277 } |
| 2126 #endif // TARGET_ARCH_IA32. | 2278 #endif // TARGET_ARCH_IA32. |
| 2127 | 2279 |
| 2128 } // namespace dart | 2280 } // namespace dart |
| OLD | NEW |