Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: vm/dart_api_impl_test.cc

Issue 9608024: Add Dart_Invoke, which will replace Dart_InvokeDynamic and Dart_InvokeStatic. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 2285 matching lines...) Expand 10 before | Expand all | Expand 10 after
2296 TestFieldOk(lib, name, false, "top getset"); 2296 TestFieldOk(lib, name, false, "top getset");
2297 2297
2298 // Hidden top-level get/set field. 2298 // Hidden top-level get/set field.
2299 name = Dart_NewString("_top_getset_fld"); 2299 name = Dart_NewString("_top_getset_fld");
2300 TestFieldNotFound(cls, name); 2300 TestFieldNotFound(cls, name);
2301 TestFieldNotFound(instance, name); 2301 TestFieldNotFound(instance, name);
2302 TestFieldOk(lib, name, false, "hidden top getset"); 2302 TestFieldOk(lib, name, false, "hidden top getset");
2303 } 2303 }
2304 2304
2305 2305
2306 TEST_CASE(SetField_FunnyValue) {
2307 const char* kScriptChars =
2308 "var top;\n";
2309
2310 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2311 Dart_Handle name = Dart_NewString("top");
2312
2313 Dart_Handle args[1];
2314 bool value;
2315
2316 // Test that you can set the field to a good value.
2317 EXPECT_VALID(Dart_SetField(lib, name, Dart_True()));
2318 Dart_Handle result = Dart_GetField(lib, name);
2319 EXPECT_VALID(result);
2320 EXPECT(Dart_IsBoolean(result));
2321 EXPECT_VALID(Dart_BooleanValue(result, &value));
2322 EXPECT(value);
2323
2324 // Test that you can set the field to null
2325 EXPECT_VALID(Dart_SetField(lib, name, Dart_Null()));
2326 result = Dart_GetField(lib, name);
2327 EXPECT_VALID(result);
2328 EXPECT(Dart_IsNull(result));
2329
2330 // Pass a non-instance handle.
2331 result = Dart_SetField(lib, name, lib);
2332 EXPECT(Dart_IsError(result));
2333 EXPECT_STREQ("Dart_SetField expects argument 'value' to be of type Instance.",
2334 Dart_GetError(result));
2335
2336 // Pass an error handle. The error is contagious.
2337 result = Dart_SetField(lib, name, Api::NewError("myerror"));
2338 EXPECT(Dart_IsError(result));
2339 EXPECT_STREQ("myerror", Dart_GetError(result));
2340 }
2341
2342
2306 TEST_CASE(FieldAccessOld) { 2343 TEST_CASE(FieldAccessOld) {
2307 const char* kScriptChars = 2344 const char* kScriptChars =
2308 "class Fields {\n" 2345 "class Fields {\n"
2309 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n" 2346 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n"
2310 " int fld1;\n" 2347 " int fld1;\n"
2311 " final int fld2;\n" 2348 " final int fld2;\n"
2312 " static int fld3;\n" 2349 " static int fld3;\n"
2313 " static final int fld4 = 10;\n" 2350 " static final int fld4 = 10;\n"
2314 "}\n" 2351 "}\n"
2315 "class FieldsTest {\n" 2352 "class FieldsTest {\n"
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after
2951 2988
2952 result = Dart_SetStaticField(cls, 2989 result = Dart_SetStaticField(cls,
2953 Dart_NewString("not_found"), 2990 Dart_NewString("not_found"),
2954 Dart_NewInteger(13)); 2991 Dart_NewInteger(13));
2955 EXPECT(Dart_IsError(result)); 2992 EXPECT(Dart_IsError(result));
2956 EXPECT_STREQ("Specified field is not found in the class", 2993 EXPECT_STREQ("Specified field is not found in the class",
2957 Dart_GetError(result)); 2994 Dart_GetError(result));
2958 } 2995 }
2959 2996
2960 2997
2998 TEST_CASE(Invoke) {
2999 const char* kScriptChars =
3000 "class BaseMethods {\n"
3001 " inheritedMethod(arg) => 'inherited $arg';\n"
3002 " static nonInheritedMethod(arg) => 'noninherited $arg';\n"
3003 "}\n"
3004 "\n"
3005 "class Methods extends BaseMethods {\n"
3006 " instanceMethod(arg) => 'instance $arg';\n"
3007 " _instanceMethod(arg) => 'hidden instance $arg';\n"
3008 " static staticMethod(arg) => 'static $arg';\n"
3009 " static _staticMethod(arg) => 'hidden static $arg';\n"
3010 "}\n"
3011 "\n"
3012 "topMethod(arg) => 'top $arg';\n"
3013 "_topMethod(arg) => 'hidden top $arg';\n"
3014 "\n"
3015 "Methods test() {\n"
3016 " return new Methods();\n"
3017 "}\n";
3018
3019 // Shared setup.
3020 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3021 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Methods"));
3022 EXPECT_VALID(cls);
3023 Dart_Handle instance = Dart_Invoke(lib, Dart_NewString("test"), 0, NULL);
3024 EXPECT_VALID(instance);
3025 Dart_Handle args[1];
3026 args[0] = Dart_NewString("!!!");
3027 Dart_Handle result;
3028 Dart_Handle name;
3029 const char* str;
3030
3031 // Instance method.
3032 name = Dart_NewString("instanceMethod");
3033 EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args)));
3034 EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args)));
3035 result = Dart_Invoke(instance, name, 1, args);
3036 EXPECT_VALID(result);
3037 result = Dart_StringToCString(result, &str);
3038 EXPECT_STREQ("instance !!!", str);
3039
3040 // Hidden instance method.
3041 name = Dart_NewString("_instanceMethod");
3042 EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args)));
3043 EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args)));
3044 result = Dart_Invoke(instance, name, 1, args);
3045 EXPECT_VALID(result);
3046 result = Dart_StringToCString(result, &str);
3047 EXPECT_STREQ("hidden instance !!!", str);
3048
3049 // Inherited method.
3050 name = Dart_NewString("inheritedMethod");
3051 EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args)));
3052 EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args)));
3053 result = Dart_Invoke(instance, name, 1, args);
3054 EXPECT_VALID(result);
3055 result = Dart_StringToCString(result, &str);
3056 EXPECT_STREQ("inherited !!!", str);
3057
3058 // Static method.
3059 name = Dart_NewString("staticMethod");
3060 EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args)));
3061 EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args)));
3062 result = Dart_Invoke(cls, name, 1, args);
3063 EXPECT_VALID(result);
3064 result = Dart_StringToCString(result, &str);
3065 EXPECT_STREQ("static !!!", str);
3066
3067 // Hidden static method.
3068 name = Dart_NewString("_staticMethod");
3069 EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args)));
3070 EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args)));
3071 result = Dart_Invoke(cls, name, 1, args);
3072 EXPECT_VALID(result);
3073 result = Dart_StringToCString(result, &str);
3074 EXPECT_STREQ("hidden static !!!", str);
3075
3076 // Static non-inherited method. Not found at any level.
3077 name = Dart_NewString("non_inheritedMethod");
3078 EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args)));
3079 EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args)));
3080 EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args)));
3081
3082 // Top-Level method.
3083 name = Dart_NewString("topMethod");
3084 EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args)));
3085 EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args)));
3086 result = Dart_Invoke(lib, name, 1, args);
3087 EXPECT_VALID(result);
3088 result = Dart_StringToCString(result, &str);
3089 EXPECT_STREQ("top !!!", str);
3090
3091 // Hidden top-level method.
3092 name = Dart_NewString("_topMethod");
3093 EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args)));
3094 EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args)));
3095 result = Dart_Invoke(lib, name, 1, args);
3096 EXPECT_VALID(result);
3097 result = Dart_StringToCString(result, &str);
3098 EXPECT_STREQ("hidden top !!!", str);
3099 }
3100
3101
3102 TEST_CASE(Invoke_FunnyArgs) {
3103 const char* kScriptChars =
3104 "test(arg) => 'hello $arg';\n";
3105
3106 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3107 Dart_Handle name = Dart_NewString("test");
3108
3109 Dart_Handle args[1];
3110 const char* str;
3111
3112 // Make sure that valid args yield valid results.
3113 args[0] = Dart_NewString("!!!");
3114 Dart_Handle result = Dart_Invoke(lib, Dart_NewString("test"), 1, args);
3115 EXPECT_VALID(result);
3116 result = Dart_StringToCString(result, &str);
3117 EXPECT_STREQ("hello !!!", str);
3118
3119 // Make sure that null is legal.
3120 args[0] = Dart_Null();
3121 result = Dart_Invoke(lib, Dart_NewString("test"), 1, args);
3122 EXPECT_VALID(result);
3123 result = Dart_StringToCString(result, &str);
3124 EXPECT_STREQ("hello null", str);
3125
3126 // Pass a non-instance handle.
3127 args[0] = lib;
3128 result = Dart_Invoke(lib, Dart_NewString("test"), 1, args);
3129 EXPECT(Dart_IsError(result));
3130 EXPECT_STREQ("Dart_Invoke expects argument 0 to be an instance of Object.",
3131 Dart_GetError(result));
3132
3133 // Pass an error handle. The error is contagious.
3134 args[0] = Api::NewError("myerror");
3135 result = Dart_Invoke(lib, Dart_NewString("test"), 1, args);
3136 EXPECT(Dart_IsError(result));
3137 EXPECT_STREQ("myerror", Dart_GetError(result));
3138 }
3139
3140
2961 TEST_CASE(InvokeDynamic) { 3141 TEST_CASE(InvokeDynamic) {
2962 const char* kScriptChars = 3142 const char* kScriptChars =
2963 "class InvokeDynamic {\n" 3143 "class InvokeDynamic {\n"
2964 " InvokeDynamic(int i, int j) : fld1 = i, fld2 = j {}\n" 3144 " InvokeDynamic(int i, int j) : fld1 = i, fld2 = j {}\n"
2965 " int method1(int i) { return i + fld1 + fld2 + fld4; }\n" 3145 " int method1(int i) { return i + fld1 + fld2 + fld4; }\n"
2966 " static int method2(int i) { return i + fld4; }\n" 3146 " static int method2(int i) { return i + fld4; }\n"
2967 " int fld1;\n" 3147 " int fld1;\n"
2968 " final int fld2;\n" 3148 " final int fld2;\n"
2969 " static final int fld4 = 10;\n" 3149 " static final int fld4 = 10;\n"
2970 "}\n" 3150 "}\n"
(...skipping 1531 matching lines...) Expand 10 before | Expand all | Expand 10 after
4502 // We should have received the expected number of interrupts. 4682 // We should have received the expected number of interrupts.
4503 EXPECT_EQ(kInterruptCount, interrupt_count); 4683 EXPECT_EQ(kInterruptCount, interrupt_count);
4504 4684
4505 // Give the spawned thread enough time to properly exit. 4685 // Give the spawned thread enough time to properly exit.
4506 Isolate::SetInterruptCallback(saved); 4686 Isolate::SetInterruptCallback(saved);
4507 } 4687 }
4508 4688
4509 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 4689 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
4510 4690
4511 } // namespace dart 4691 } // namespace dart
OLDNEW
« no previous file with comments | « vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698