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 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1042 // "NativeFieldsWrapper" and there is no definition of it either | 1042 // "NativeFieldsWrapper" and there is no definition of it either |
| 1043 // in the dart code or through the native field injection mechanism. | 1043 // in the dart code or through the native field injection mechanism. |
| 1044 EXPECT(!Dart_IsValid(result)); | 1044 EXPECT(!Dart_IsValid(result)); |
| 1045 | 1045 |
| 1046 Dart_ExitScope(); // Exit the Dart API scope. | 1046 Dart_ExitScope(); // Exit the Dart API scope. |
| 1047 } | 1047 } |
| 1048 Dart_ShutdownIsolate(); | 1048 Dart_ShutdownIsolate(); |
| 1049 } | 1049 } |
| 1050 | 1050 |
| 1051 | 1051 |
| 1052 UNIT_TEST_CASE(InjectNativeFields3) { | |
| 1053 const char* kScriptChars = | |
| 1054 "#import('dart:core_native_fields');" | |
| 1055 "class NativeFields extends NativeFieldWrapperClass2 {\n" | |
| 1056 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | |
| 1057 " int fld1;\n" | |
| 1058 " final int fld2;\n" | |
| 1059 " static int fld3;\n" | |
| 1060 " static final int fld4 = 10;\n" | |
| 1061 "}\n" | |
| 1062 "class NativeFieldsTest {\n" | |
| 1063 " static NativeFields testMain() {\n" | |
| 1064 " NativeFields obj = new NativeFields(10, 20);\n" | |
| 1065 " return obj;\n" | |
| 1066 " }\n" | |
| 1067 "}\n"; | |
| 1068 Dart_Handle result; | |
| 1069 | |
| 1070 Dart_CreateIsolate(NULL, NULL); | |
| 1071 { | |
| 1072 Zone zone; | |
| 1073 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.
| |
| 1074 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | |
| 1075 const int kNumNativeFields = 2; | |
| 1076 | |
| 1077 // Load up a test script in the test library. | |
| 1078 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 1079 | |
| 1080 // Invoke a function which returns an object of type NativeFields. | |
| 1081 result = Dart_InvokeStatic(lib, | |
| 1082 Dart_NewString("NativeFieldsTest"), | |
| 1083 Dart_NewString("testMain"), | |
| 1084 0, | |
| 1085 NULL); | |
| 1086 EXPECT_VALID(result); | |
| 1087 EXPECT(!Dart_ExceptionOccurred(result)); | |
| 1088 Instance& obj = Instance::Handle(); | |
| 1089 obj ^= Api::UnwrapHandle(result); | |
| 1090 const Class& cls = Class::Handle(obj.clazz()); | |
| 1091 // We expect the newly created "NativeFields" object to have | |
| 1092 // 2 dart instance fields (fld1, fld2) and kNumNativeFields native fields. | |
| 1093 // Hence the size of an instance of "NativeFields" should be | |
| 1094 // (kNumNativeFields + 2) * kWordSize + sizeof the header word. | |
| 1095 // We check to make sure the instance size computed by the VM matches | |
| 1096 // our expectations. | |
| 1097 EXPECT_EQ(Utils::RoundUp(((kNumNativeFields + 2) * kWordSize) + kWordSize, | |
| 1098 kObjectAlignment), | |
| 1099 cls.instance_size()); | |
| 1100 | |
| 1101 Dart_ExitScope(); // Exit the Dart API scope. | |
| 1102 } | |
| 1103 Dart_ShutdownIsolate(); | |
| 1104 } | |
| 1105 | |
| 1106 | |
| 1107 static void TestNativeFields(Dart_Handle retobj) { | |
| 1108 // Access and set various instance fields of the object. | |
| 1109 Dart_Handle result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); | |
| 1110 EXPECT(!Dart_IsValid(result)); | |
| 1111 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1112 EXPECT_VALID(result); | |
| 1113 int64_t value = 0; | |
| 1114 result = Dart_IntegerValue(result, &value); | |
| 1115 EXPECT_EQ(10, value); | |
| 1116 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
| 1117 EXPECT_VALID(result); | |
| 1118 result = Dart_IntegerValue(result, &value); | |
| 1119 EXPECT_EQ(20, value); | |
| 1120 result = Dart_SetInstanceField(retobj, | |
| 1121 Dart_NewString("fld2"), | |
| 1122 Dart_NewInteger(40)); | |
| 1123 EXPECT(!Dart_IsValid(result)); | |
| 1124 result = Dart_SetInstanceField(retobj, | |
| 1125 Dart_NewString("fld1"), | |
| 1126 Dart_NewInteger(40)); | |
| 1127 EXPECT_VALID(result); | |
| 1128 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1129 EXPECT_VALID(result); | |
| 1130 result = Dart_IntegerValue(result, &value); | |
| 1131 EXPECT_EQ(40, value); | |
| 1132 | |
| 1133 // Now access and set various native instance fields of the returned object. | |
| 1134 const int kNativeFld0 = 0; | |
| 1135 const int kNativeFld1 = 1; | |
| 1136 const int kNativeFld2 = 2; | |
| 1137 const int kNativeFld3 = 3; | |
| 1138 const int kNativeFld4 = 4; | |
| 1139 intptr_t field_value = 0; | |
| 1140 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &field_value); | |
| 1141 EXPECT(!Dart_IsValid(result)); | |
| 1142 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &field_value); | |
| 1143 EXPECT_VALID(result); | |
| 1144 EXPECT_EQ(0, field_value); | |
| 1145 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &field_value); | |
| 1146 EXPECT_VALID(result); | |
| 1147 EXPECT_EQ(0, field_value); | |
| 1148 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &field_value); | |
| 1149 EXPECT_VALID(result); | |
| 1150 EXPECT_EQ(0, field_value); | |
| 1151 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); | |
| 1152 EXPECT(!Dart_IsValid(result)); | |
| 1153 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 4); | |
| 1154 EXPECT_VALID(result); | |
| 1155 result = Dart_SetNativeInstanceField(retobj, kNativeFld1, 40); | |
| 1156 EXPECT_VALID(result); | |
| 1157 result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); | |
| 1158 EXPECT_VALID(result); | |
| 1159 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); | |
| 1160 EXPECT_VALID(result); | |
| 1161 result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); | |
| 1162 EXPECT_VALID(result); | |
| 1163 EXPECT_EQ(4000, field_value); | |
| 1164 | |
| 1165 // Now re-access various dart instance fields of the returned object | |
| 1166 // to ensure that there was no corruption while setting native fields. | |
| 1167 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1168 EXPECT_VALID(result); | |
| 1169 result = Dart_IntegerValue(result, &value); | |
| 1170 EXPECT_EQ(40, value); | |
| 1171 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
| 1172 EXPECT_VALID(result); | |
| 1173 result = Dart_IntegerValue(result, &value); | |
| 1174 EXPECT_EQ(20, value); | |
| 1175 } | |
| 1176 | |
| 1177 | |
| 1052 UNIT_TEST_CASE(NativeFieldAccess) { | 1178 UNIT_TEST_CASE(NativeFieldAccess) { |
| 1053 const char* kScriptChars = | 1179 const char* kScriptChars = |
| 1054 "class NativeFields extends NativeFieldsWrapper {\n" | 1180 "class NativeFields extends NativeFieldsWrapper {\n" |
| 1055 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | 1181 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 1056 " int fld1;\n" | 1182 " int fld1;\n" |
| 1057 " final int fld2;\n" | 1183 " final int fld2;\n" |
| 1058 " static int fld3;\n" | 1184 " static int fld3;\n" |
| 1059 " static final int fld4 = 10;\n" | 1185 " static final int fld4 = 10;\n" |
| 1060 "}\n" | 1186 "}\n" |
| 1061 "class NativeFieldsTest {\n" | 1187 "class NativeFieldsTest {\n" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1085 // Invoke a function which returns an object of type NativeFields. | 1211 // Invoke a function which returns an object of type NativeFields. |
| 1086 Dart_Handle retobj = Dart_InvokeStatic(lib, | 1212 Dart_Handle retobj = Dart_InvokeStatic(lib, |
| 1087 Dart_NewString("NativeFieldsTest"), | 1213 Dart_NewString("NativeFieldsTest"), |
| 1088 Dart_NewString("testMain"), | 1214 Dart_NewString("testMain"), |
| 1089 0, | 1215 0, |
| 1090 NULL); | 1216 NULL); |
| 1091 EXPECT_VALID(retobj); | 1217 EXPECT_VALID(retobj); |
| 1092 EXPECT(!Dart_ExceptionOccurred(retobj)); | 1218 EXPECT(!Dart_ExceptionOccurred(retobj)); |
| 1093 | 1219 |
| 1094 // Now access and set various instance fields of the returned object. | 1220 // Now access and set various instance fields of the returned object. |
| 1095 result = Dart_GetInstanceField(retobj, Dart_NewString("fld3")); | 1221 TestNativeFields(retobj); |
| 1096 EXPECT(!Dart_IsValid(result)); | |
| 1097 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1098 EXPECT_VALID(result); | |
| 1099 int64_t value = 0; | |
| 1100 result = Dart_IntegerValue(result, &value); | |
| 1101 EXPECT_EQ(10, value); | |
| 1102 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
| 1103 EXPECT_VALID(result); | |
| 1104 result = Dart_IntegerValue(result, &value); | |
| 1105 EXPECT_EQ(20, value); | |
| 1106 result = Dart_SetInstanceField(retobj, | |
| 1107 Dart_NewString("fld2"), | |
| 1108 Dart_NewInteger(40)); | |
| 1109 EXPECT(!Dart_IsValid(result)); | |
| 1110 result = Dart_SetInstanceField(retobj, | |
| 1111 Dart_NewString("fld1"), | |
| 1112 Dart_NewInteger(40)); | |
| 1113 EXPECT_VALID(result); | |
| 1114 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1115 EXPECT_VALID(result); | |
| 1116 result = Dart_IntegerValue(result, &value); | |
| 1117 EXPECT_EQ(40, value); | |
| 1118 | |
| 1119 // Now access and set various native instance fields of the returned object. | |
| 1120 const int kNativeFld0 = 0; | |
| 1121 const int kNativeFld1 = 1; | |
| 1122 const int kNativeFld2 = 2; | |
| 1123 const int kNativeFld3 = 3; | |
| 1124 const int kNativeFld4 = 4; | |
| 1125 intptr_t field_value = 0; | |
| 1126 result = Dart_GetNativeInstanceField(retobj, kNativeFld4, &field_value); | |
| 1127 EXPECT(!Dart_IsValid(result)); | |
| 1128 result = Dart_GetNativeInstanceField(retobj, kNativeFld0, &field_value); | |
| 1129 EXPECT_VALID(result); | |
| 1130 EXPECT_EQ(0, field_value); | |
| 1131 result = Dart_GetNativeInstanceField(retobj, kNativeFld1, &field_value); | |
| 1132 EXPECT_VALID(result); | |
| 1133 EXPECT_EQ(0, field_value); | |
| 1134 result = Dart_GetNativeInstanceField(retobj, kNativeFld2, &field_value); | |
| 1135 EXPECT_VALID(result); | |
| 1136 EXPECT_EQ(0, field_value); | |
| 1137 result = Dart_SetNativeInstanceField(retobj, kNativeFld4, 40); | |
| 1138 EXPECT(!Dart_IsValid(result)); | |
| 1139 result = Dart_SetNativeInstanceField(retobj, kNativeFld0, 4); | |
| 1140 EXPECT_VALID(result); | |
| 1141 result = Dart_SetNativeInstanceField(retobj, kNativeFld1, 40); | |
| 1142 EXPECT_VALID(result); | |
| 1143 result = Dart_SetNativeInstanceField(retobj, kNativeFld2, 400); | |
| 1144 EXPECT_VALID(result); | |
| 1145 result = Dart_SetNativeInstanceField(retobj, kNativeFld3, 4000); | |
| 1146 EXPECT_VALID(result); | |
| 1147 result = Dart_GetNativeInstanceField(retobj, kNativeFld3, &field_value); | |
| 1148 EXPECT_VALID(result); | |
| 1149 EXPECT_EQ(4000, field_value); | |
| 1150 | |
| 1151 // Now re-access various dart instance fields of the returned object | |
| 1152 // to ensure that there was no corruption while setting native fields. | |
| 1153 result = Dart_GetInstanceField(retobj, Dart_NewString("fld1")); | |
| 1154 EXPECT_VALID(result); | |
| 1155 result = Dart_IntegerValue(result, &value); | |
| 1156 EXPECT_EQ(40, value); | |
| 1157 result = Dart_GetInstanceField(retobj, Dart_NewString("fld2")); | |
| 1158 EXPECT_VALID(result); | |
| 1159 result = Dart_IntegerValue(result, &value); | |
| 1160 EXPECT_EQ(20, value); | |
| 1161 | 1222 |
| 1162 Dart_ExitScope(); // Exit the Dart API scope. | 1223 Dart_ExitScope(); // Exit the Dart API scope. |
| 1163 } | 1224 } |
| 1164 Dart_ShutdownIsolate(); | 1225 Dart_ShutdownIsolate(); |
| 1226 } | |
| 1227 | |
| 1228 | |
| 1229 UNIT_TEST_CASE(ImplicitNativeFieldAccess) { | |
| 1230 const char* kScriptChars = | |
| 1231 "#import('dart:core_native_fields');" | |
| 1232 "class NativeFields extends NativeFieldWrapperClass4 {\n" | |
| 1233 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | |
| 1234 " int fld1;\n" | |
| 1235 " final int fld2;\n" | |
| 1236 " static int fld3;\n" | |
| 1237 " static final int fld4 = 10;\n" | |
| 1238 "}\n" | |
| 1239 "class NativeFieldsTest {\n" | |
| 1240 " static NativeFields testMain() {\n" | |
| 1241 " NativeFields obj = new NativeFields(10, 20);\n" | |
| 1242 " return obj;\n" | |
| 1243 " }\n" | |
| 1244 "}\n"; | |
| 1245 Dart_CreateIsolate(NULL, NULL); | |
| 1246 { | |
| 1247 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | |
| 1248 | |
| 1249 // Load up a test script in the test library. | |
| 1250 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 1251 | |
| 1252 // Invoke a function which returns an object of type NativeFields. | |
| 1253 Dart_Handle retobj = Dart_InvokeStatic(lib, | |
| 1254 Dart_NewString("NativeFieldsTest"), | |
| 1255 Dart_NewString("testMain"), | |
| 1256 0, | |
| 1257 NULL); | |
| 1258 EXPECT_VALID(retobj); | |
| 1259 EXPECT(!Dart_ExceptionOccurred(retobj)); | |
| 1260 | |
| 1261 // Now access and set various instance fields of the returned object. | |
| 1262 TestNativeFields(retobj); | |
| 1263 | |
| 1264 Dart_ExitScope(); // Exit the Dart API scope. | |
| 1265 } | |
| 1266 Dart_ShutdownIsolate(); | |
| 1165 } | 1267 } |
| 1166 | 1268 |
| 1167 | 1269 |
| 1168 UNIT_TEST_CASE(NegativeNativeFieldAccess) { | 1270 UNIT_TEST_CASE(NegativeNativeFieldAccess) { |
| 1169 const char* kScriptChars = | 1271 const char* kScriptChars = |
| 1170 "class NativeFields {\n" | 1272 "class NativeFields {\n" |
| 1171 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" | 1273 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 1172 " int fld1;\n" | 1274 " int fld1;\n" |
| 1173 " final int fld2;\n" | 1275 " final int fld2;\n" |
| 1174 " static int fld3;\n" | 1276 " static int fld3;\n" |
| (...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2096 NULL); | 2198 NULL); |
| 2097 EXPECT_VALID(result); | 2199 EXPECT_VALID(result); |
| 2098 | 2200 |
| 2099 Dart_ExitScope(); // Exit the Dart API scope. | 2201 Dart_ExitScope(); // Exit the Dart API scope. |
| 2100 } | 2202 } |
| 2101 Dart_ShutdownIsolate(); | 2203 Dart_ShutdownIsolate(); |
| 2102 } | 2204 } |
| 2103 #endif // TARGET_ARCH_IA32. | 2205 #endif // TARGET_ARCH_IA32. |
| 2104 | 2206 |
| 2105 } // namespace dart | 2207 } // namespace dart |
| OLD | NEW |