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

Side by Side Diff: vm/dart_api_impl_test.cc

Issue 10153002: Add Dart_New. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 8 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 2920 matching lines...) Expand 10 before | Expand all | Expand 10 after
2931 EXPECT_VALID(result); 2931 EXPECT_VALID(result);
2932 2932
2933 // We now get the new value for fld2, not the initializer 2933 // We now get the new value for fld2, not the initializer
2934 result = Dart_GetField(cls, Dart_NewString("fld2")); 2934 result = Dart_GetField(cls, Dart_NewString("fld2"));
2935 EXPECT_VALID(result); 2935 EXPECT_VALID(result);
2936 result = Dart_IntegerToInt64(result, &value); 2936 result = Dart_IntegerToInt64(result, &value);
2937 EXPECT_EQ(13, value); 2937 EXPECT_EQ(13, value);
2938 } 2938 }
2939 2939
2940 2940
2941 TEST_CASE(New) {
2942 const char* kScriptChars =
2943 "class Test implements TestInterface {\n"
2944 " Test() : foo = 7 {}\n"
2945 " Test.named(value) : foo = value {}\n"
2946 " Test._hidden(value) : foo = -value {}\n"
2947 " Test.exception(value) : foo = value {\n"
2948 " throw 'ConstructorDeath';\n"
2949 " }\n"
2950 " factory Test.multiply(value) {\n"
2951 " return new Test.named(value * 100);\n"
2952 " }\n"
2953 " factory Test.nullo() {\n"
2954 " return null;\n"
2955 " }\n"
2956 " var foo;\n"
2957 "}\n"
2958 "\n"
2959 "interface TestInterface default Test {\n"
2960 " TestInterface.named(value);\n"
2961 " TestInterface.multiply(value);\n"
2962 " TestInterface.notfound(value);\n"
2963 "}\n";
2964
2965 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
2966 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Test"));
2967 EXPECT_VALID(cls);
2968 Dart_Handle intf = Dart_GetClass(lib, Dart_NewString("TestInterface"));
2969 EXPECT_VALID(intf);
2970 Dart_Handle args[1];
2971 args[0] = Dart_NewInteger(11);
2972 Dart_Handle bad_args[1];
2973 bad_args[0] = Dart_Error("myerror");
2974
2975 // Invoke the unnamed constructor.
2976 Dart_Handle result = Dart_New(cls, Dart_Null(), 0, NULL);
2977 EXPECT_VALID(result);
2978 bool instanceof = false;
2979 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
2980 EXPECT(instanceof);
2981 int64_t int_value = 0;
2982 Dart_Handle foo = Dart_GetField(result, Dart_NewString("foo"));
2983 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
2984 EXPECT_EQ(7, int_value);
2985
2986 // Invoke the unnamed constructor with an empty string.
2987 result = Dart_New(cls, Dart_NewString(""), 0, NULL);
2988 EXPECT_VALID(result);
2989 instanceof = false;
2990 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
2991 EXPECT(instanceof);
2992 int_value = 0;
2993 foo = Dart_GetField(result, Dart_NewString("foo"));
2994 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
2995 EXPECT_EQ(7, int_value);
2996
2997 // Invoke a named constructor.
2998 result = Dart_New(cls, Dart_NewString("named"), 1, args);
2999 EXPECT_VALID(result);
3000 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3001 EXPECT(instanceof);
3002 int_value = 0;
3003 foo = Dart_GetField(result, Dart_NewString("foo"));
3004 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
3005 EXPECT_EQ(11, int_value);
3006
3007 // Invoke a hidden named constructor.
3008 result = Dart_New(cls, Dart_NewString("_hidden"), 1, args);
3009 EXPECT_VALID(result);
3010 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3011 EXPECT(instanceof);
3012 int_value = 0;
3013 foo = Dart_GetField(result, Dart_NewString("foo"));
3014 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
3015 EXPECT_EQ(-11, int_value);
3016
3017 // Invoke a factory constructor.
3018 result = Dart_New(cls, Dart_NewString("multiply"), 1, args);
3019 EXPECT_VALID(result);
3020 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3021 EXPECT(instanceof);
3022 int_value = 0;
3023 foo = Dart_GetField(result, Dart_NewString("foo"));
3024 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
3025 EXPECT_EQ(1100, int_value);
3026
3027 // Invoke a factory constructor which returns null.
3028 result = Dart_New(cls, Dart_NewString("nullo"), 0, NULL);
3029 EXPECT_VALID(result);
3030 EXPECT(Dart_IsNull(result));
3031
3032 // Pass an error class object. Error is passed through.
3033 result = Dart_New(Dart_Error("myerror"), Dart_NewString("named"), 1, args);
3034 EXPECT_ERROR(result, "myerror");
3035
3036 // Pass a bad class object.
3037 result = Dart_New(Dart_Null(), Dart_NewString("named"), 1, args);
3038 EXPECT_ERROR(result, "Dart_New expects argument 'clazz' to be non-null.");
3039
3040 // Pass a negative arg count.
3041 result = Dart_New(cls, Dart_NewString("named"), -1, args);
3042 EXPECT_ERROR(
3043 result,
3044 "Dart_New expects argument 'number_of_arguments' to be non-negative.");
3045
3046 // Pass the wrong arg count.
3047 result = Dart_New(cls, Dart_NewString("named"), 0, NULL);
3048 EXPECT_ERROR(result,
3049 "Dart_New: wrong argument count for constructor 'Test.named': "
3050 "expected 1 but saw 0.");
3051
3052 // Pass a bad argument. Error is passed through.
3053 result = Dart_New(cls, Dart_NewString("named"), 1, bad_args);
3054 EXPECT_ERROR(result, "myerror");
3055
3056 // Pass a bad constructor name.
3057 result = Dart_New(cls, Dart_NewInteger(55), 1, args);
3058 EXPECT_ERROR(
3059 result,
3060 "Dart_New expects argument 'constructor_name' to be of type String.");
3061
3062 // Invoke a missing constructor.
3063 result = Dart_New(cls, Dart_NewString("missing"), 1, args);
3064 EXPECT_ERROR(result, "Dart_New: could not find constructor 'Test.missing'.");
3065
3066 // Invoke a constructor which throws an exception.
3067 result = Dart_New(cls, Dart_NewString("exception"), 1, args);
3068 EXPECT_ERROR(result, "ConstructorDeath");
3069
3070 // Invoke an interface constructor.
3071 result = Dart_New(intf, Dart_NewString("named"), 1, args);
3072 EXPECT_VALID(result);
3073 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3074 EXPECT(instanceof);
3075 int_value = 0;
3076 foo = Dart_GetField(result, Dart_NewString("foo"));
3077 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
3078 EXPECT_EQ(11, int_value);
3079
3080 // Invoke an interface constructor which in turn calls a factory
3081 // constructor. Why not?
3082 result = Dart_New(intf, Dart_NewString("multiply"), 1, args);
3083 EXPECT_VALID(result);
3084 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof));
3085 EXPECT(instanceof);
3086 int_value = 0;
3087 foo = Dart_GetField(result, Dart_NewString("foo"));
3088 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value));
3089 EXPECT_EQ(1100, int_value);
3090
3091 // Invoke a constructor that is missing in the interface but present
3092 // in the default class.
3093 result = Dart_New(intf, Dart_Null(), 0, NULL);
3094 EXPECT_ERROR(result,
3095 "Dart_New: could not find constructor 'TestInterface.'.");
3096
3097 // Invoke a constructor that is present in the interface but missing
3098 // in the default class.
3099 result = Dart_New(intf, Dart_NewString("notfound"), 1, args);
3100 EXPECT_ERROR(result, "Dart_New: could not find constructor 'Test.notfound'.");
3101 }
3102
3103
2941 TEST_CASE(Invoke) { 3104 TEST_CASE(Invoke) {
2942 const char* kScriptChars = 3105 const char* kScriptChars =
2943 "class BaseMethods {\n" 3106 "class BaseMethods {\n"
2944 " inheritedMethod(arg) => 'inherited $arg';\n" 3107 " inheritedMethod(arg) => 'inherited $arg';\n"
2945 " static nonInheritedMethod(arg) => 'noninherited $arg';\n" 3108 " static nonInheritedMethod(arg) => 'noninherited $arg';\n"
2946 "}\n" 3109 "}\n"
2947 "\n" 3110 "\n"
2948 "class Methods extends BaseMethods {\n" 3111 "class Methods extends BaseMethods {\n"
2949 " instanceMethod(arg) => 'instance $arg';\n" 3112 " instanceMethod(arg) => 'instance $arg';\n"
2950 " _instanceMethod(arg) => 'hidden instance $arg';\n" 3113 " _instanceMethod(arg) => 'hidden instance $arg';\n"
(...skipping 1692 matching lines...) Expand 10 before | Expand all | Expand 10 after
4643 EXPECT_VALID(result); 4806 EXPECT_VALID(result);
4644 EXPECT(Dart_IsDouble(result)); 4807 EXPECT(Dart_IsDouble(result));
4645 double out; 4808 double out;
4646 result = Dart_DoubleValue(result, &out); 4809 result = Dart_DoubleValue(result, &out);
4647 fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out); 4810 fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out);
4648 } 4811 }
4649 4812
4650 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 4813 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
4651 4814
4652 } // namespace dart 4815 } // 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