| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 7 #include "platform/utils.h" | 7 #include "platform/utils.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
| 10 #include "vm/dart_api_state.h" | 10 #include "vm/dart_api_state.h" |
| (...skipping 3150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3161 | 3161 |
| 3162 result = Dart_SetStaticField(cls, | 3162 result = Dart_SetStaticField(cls, |
| 3163 Dart_NewString("not_found"), | 3163 Dart_NewString("not_found"), |
| 3164 Dart_NewInteger(13)); | 3164 Dart_NewInteger(13)); |
| 3165 EXPECT(Dart_IsError(result)); | 3165 EXPECT(Dart_IsError(result)); |
| 3166 EXPECT_STREQ("Specified field is not found in the class", | 3166 EXPECT_STREQ("Specified field is not found in the class", |
| 3167 Dart_GetError(result)); | 3167 Dart_GetError(result)); |
| 3168 } | 3168 } |
| 3169 | 3169 |
| 3170 | 3170 |
| 3171 TEST_CASE(New) { |
| 3172 const char* kScriptChars = |
| 3173 "class Test implements TestInterface {\n" |
| 3174 " Test() : foo = 7 {}\n" |
| 3175 " Test.named(value) : foo = value {}\n" |
| 3176 " Test._hidden(value) : foo = -value {}\n" |
| 3177 " Test.exception(value) : foo = value {\n" |
| 3178 " throw 'ConstructorDeath';\n" |
| 3179 " }\n" |
| 3180 " factory Test.multiply(value) {\n" |
| 3181 " return new Test.named(value * 100);\n" |
| 3182 " }\n" |
| 3183 " factory Test.nullo() {\n" |
| 3184 " return null;\n" |
| 3185 " }\n" |
| 3186 " var foo;\n" |
| 3187 "}\n" |
| 3188 "\n" |
| 3189 "interface TestInterface default Test {\n" |
| 3190 " TestInterface.named(value);\n" |
| 3191 " TestInterface.multiply(value);\n" |
| 3192 " TestInterface.notfound(value);\n" |
| 3193 "}\n"; |
| 3194 |
| 3195 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 3196 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Test")); |
| 3197 EXPECT_VALID(cls); |
| 3198 Dart_Handle intf = Dart_GetClass(lib, Dart_NewString("TestInterface")); |
| 3199 EXPECT_VALID(intf); |
| 3200 Dart_Handle args[1]; |
| 3201 args[0] = Dart_NewInteger(11); |
| 3202 Dart_Handle bad_args[1]; |
| 3203 bad_args[0] = Dart_Error("myerror"); |
| 3204 |
| 3205 // Invoke the unnamed constructor. |
| 3206 Dart_Handle result = Dart_New(cls, Dart_Null(), 0, NULL); |
| 3207 EXPECT_VALID(result); |
| 3208 bool instanceof = false; |
| 3209 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3210 EXPECT(instanceof); |
| 3211 int64_t int_value = 0; |
| 3212 Dart_Handle foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3213 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3214 EXPECT_EQ(7, int_value); |
| 3215 |
| 3216 // Invoke the unnamed constructor with an empty string. |
| 3217 result = Dart_New(cls, Dart_NewString(""), 0, NULL); |
| 3218 EXPECT_VALID(result); |
| 3219 instanceof = false; |
| 3220 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3221 EXPECT(instanceof); |
| 3222 int_value = 0; |
| 3223 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3224 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3225 EXPECT_EQ(7, int_value); |
| 3226 |
| 3227 // Invoke a named constructor. |
| 3228 result = Dart_New(cls, Dart_NewString("named"), 1, args); |
| 3229 EXPECT_VALID(result); |
| 3230 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3231 EXPECT(instanceof); |
| 3232 int_value = 0; |
| 3233 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3234 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3235 EXPECT_EQ(11, int_value); |
| 3236 |
| 3237 // Invoke a hidden named constructor. |
| 3238 result = Dart_New(cls, Dart_NewString("_hidden"), 1, args); |
| 3239 EXPECT_VALID(result); |
| 3240 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3241 EXPECT(instanceof); |
| 3242 int_value = 0; |
| 3243 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3244 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3245 EXPECT_EQ(-11, int_value); |
| 3246 |
| 3247 // Invoke a factory constructor. |
| 3248 result = Dart_New(cls, Dart_NewString("multiply"), 1, args); |
| 3249 EXPECT_VALID(result); |
| 3250 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3251 EXPECT(instanceof); |
| 3252 int_value = 0; |
| 3253 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3254 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3255 EXPECT_EQ(1100, int_value); |
| 3256 |
| 3257 // Invoke a factory constructor which returns null. |
| 3258 result = Dart_New(cls, Dart_NewString("nullo"), 0, NULL); |
| 3259 EXPECT_VALID(result); |
| 3260 EXPECT(Dart_IsNull(result)); |
| 3261 |
| 3262 // Pass an error class object. Error is passed through. |
| 3263 result = Dart_New(Dart_Error("myerror"), Dart_NewString("named"), 1, args); |
| 3264 EXPECT_ERROR(result, "myerror"); |
| 3265 |
| 3266 // Pass a bad class object. |
| 3267 result = Dart_New(Dart_Null(), Dart_NewString("named"), 1, args); |
| 3268 EXPECT_ERROR(result, "Dart_New expects argument 'clazz' to be non-null."); |
| 3269 |
| 3270 // Pass a negative arg count. |
| 3271 result = Dart_New(cls, Dart_NewString("named"), -1, args); |
| 3272 EXPECT_ERROR( |
| 3273 result, |
| 3274 "Dart_New expects argument 'number_of_arguments' to be non-negative."); |
| 3275 |
| 3276 // Pass the wrong arg count. |
| 3277 result = Dart_New(cls, Dart_NewString("named"), 0, NULL); |
| 3278 EXPECT_ERROR(result, |
| 3279 "Dart_New: wrong argument count for constructor 'Test.named': " |
| 3280 "expected 1 but saw 0."); |
| 3281 |
| 3282 // Pass a bad argument. Error is passed through. |
| 3283 result = Dart_New(cls, Dart_NewString("named"), 1, bad_args); |
| 3284 EXPECT_ERROR(result, "myerror"); |
| 3285 |
| 3286 // Pass a bad constructor name. |
| 3287 result = Dart_New(cls, Dart_NewInteger(55), 1, args); |
| 3288 EXPECT_ERROR( |
| 3289 result, |
| 3290 "Dart_New expects argument 'constructor_name' to be of type String."); |
| 3291 |
| 3292 // Invoke a missing constructor. |
| 3293 result = Dart_New(cls, Dart_NewString("missing"), 1, args); |
| 3294 EXPECT_ERROR(result, "Dart_New: could not find constructor 'Test.missing'."); |
| 3295 |
| 3296 // Invoke a constructor which throws an exception. |
| 3297 result = Dart_New(cls, Dart_NewString("exception"), 1, args); |
| 3298 EXPECT_ERROR(result, "ConstructorDeath"); |
| 3299 |
| 3300 // Invoke an interface constructor. |
| 3301 result = Dart_New(intf, Dart_NewString("named"), 1, args); |
| 3302 EXPECT_VALID(result); |
| 3303 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3304 EXPECT(instanceof); |
| 3305 int_value = 0; |
| 3306 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3307 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3308 EXPECT_EQ(11, int_value); |
| 3309 |
| 3310 // Invoke an interface constructor which in turn calls a factory |
| 3311 // constructor. Why not? |
| 3312 result = Dart_New(intf, Dart_NewString("multiply"), 1, args); |
| 3313 EXPECT_VALID(result); |
| 3314 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3315 EXPECT(instanceof); |
| 3316 int_value = 0; |
| 3317 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3318 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3319 EXPECT_EQ(1100, int_value); |
| 3320 |
| 3321 // Invoke a constructor that is missing in the interface but present |
| 3322 // in the default class. |
| 3323 result = Dart_New(intf, Dart_Null(), 0, NULL); |
| 3324 EXPECT_ERROR(result, |
| 3325 "Dart_New: could not find constructor 'TestInterface.'."); |
| 3326 |
| 3327 // Invoke a constructor that is present in the interface but missing |
| 3328 // in the default class. |
| 3329 result = Dart_New(intf, Dart_NewString("notfound"), 1, args); |
| 3330 EXPECT_ERROR(result, "Dart_New: could not find constructor 'Test.notfound'."); |
| 3331 } |
| 3332 |
| 3333 |
| 3171 TEST_CASE(Invoke) { | 3334 TEST_CASE(Invoke) { |
| 3172 const char* kScriptChars = | 3335 const char* kScriptChars = |
| 3173 "class BaseMethods {\n" | 3336 "class BaseMethods {\n" |
| 3174 " inheritedMethod(arg) => 'inherited $arg';\n" | 3337 " inheritedMethod(arg) => 'inherited $arg';\n" |
| 3175 " static nonInheritedMethod(arg) => 'noninherited $arg';\n" | 3338 " static nonInheritedMethod(arg) => 'noninherited $arg';\n" |
| 3176 "}\n" | 3339 "}\n" |
| 3177 "\n" | 3340 "\n" |
| 3178 "class Methods extends BaseMethods {\n" | 3341 "class Methods extends BaseMethods {\n" |
| 3179 " instanceMethod(arg) => 'instance $arg';\n" | 3342 " instanceMethod(arg) => 'instance $arg';\n" |
| 3180 " _instanceMethod(arg) => 'hidden instance $arg';\n" | 3343 " _instanceMethod(arg) => 'hidden instance $arg';\n" |
| (...skipping 1790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4971 EXPECT_VALID(result); | 5134 EXPECT_VALID(result); |
| 4972 EXPECT(Dart_IsDouble(result)); | 5135 EXPECT(Dart_IsDouble(result)); |
| 4973 double out; | 5136 double out; |
| 4974 result = Dart_DoubleValue(result, &out); | 5137 result = Dart_DoubleValue(result, &out); |
| 4975 fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out); | 5138 fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out); |
| 4976 } | 5139 } |
| 4977 | 5140 |
| 4978 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 5141 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 4979 | 5142 |
| 4980 } // namespace dart | 5143 } // namespace dart |
| OLD | NEW |