| 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 2905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2916 // We now get the new value for fld2, not the initializer | 2916 // We now get the new value for fld2, not the initializer |
| 2917 result = Dart_GetField(cls, Dart_NewString("fld2")); | 2917 result = Dart_GetField(cls, Dart_NewString("fld2")); |
| 2918 EXPECT_VALID(result); | 2918 EXPECT_VALID(result); |
| 2919 result = Dart_IntegerToInt64(result, &value); | 2919 result = Dart_IntegerToInt64(result, &value); |
| 2920 EXPECT_EQ(13, value); | 2920 EXPECT_EQ(13, value); |
| 2921 } | 2921 } |
| 2922 | 2922 |
| 2923 | 2923 |
| 2924 TEST_CASE(New) { | 2924 TEST_CASE(New) { |
| 2925 const char* kScriptChars = | 2925 const char* kScriptChars = |
| 2926 "class Test implements TestInterface {\n" | 2926 "class MyClass implements MyInterface {\n" |
| 2927 " Test() : foo = 7 {}\n" | 2927 " MyClass() : foo = 7 {}\n" |
| 2928 " Test.named(value) : foo = value {}\n" | 2928 " MyClass.named(value) : foo = value {}\n" |
| 2929 " Test._hidden(value) : foo = -value {}\n" | 2929 " MyClass._hidden(value) : foo = -value {}\n" |
| 2930 " Test.exception(value) : foo = value {\n" | 2930 " MyClass.exception(value) : foo = value {\n" |
| 2931 " throw 'ConstructorDeath';\n" | 2931 " throw 'ConstructorDeath';\n" |
| 2932 " }\n" | 2932 " }\n" |
| 2933 " factory Test.multiply(value) {\n" | 2933 " factory MyClass.multiply(value) {\n" |
| 2934 " return new Test.named(value * 100);\n" | 2934 " return new MyClass.named(value * 100);\n" |
| 2935 " }\n" | 2935 " }\n" |
| 2936 " factory Test.nullo() {\n" | 2936 " factory MyClass.nullo() {\n" |
| 2937 " return null;\n" | 2937 " return null;\n" |
| 2938 " }\n" | 2938 " }\n" |
| 2939 " factory MyInterface.multiply(value) { // won't get called.\n" |
| 2940 " return new MyClass.named(value * 1000);\n" |
| 2941 " }\n" |
| 2942 " factory MyInterface2.unused(value) {\n" |
| 2943 " return new MyClass2(-value);\n" |
| 2944 " }\n" |
| 2945 " factory MyInterface2.multiply(value) {\n" |
| 2946 " return new MyClass2(value * 10000);\n" |
| 2947 " }\n" |
| 2939 " var foo;\n" | 2948 " var foo;\n" |
| 2940 "}\n" | 2949 "}\n" |
| 2941 "\n" | 2950 "\n" |
| 2942 "interface TestInterface default Test {\n" | 2951 "class MyClass2 implements MyInterface2 {\n" |
| 2943 " TestInterface.named(value);\n" | 2952 " MyClass2(value) : bar = value {}\n" |
| 2944 " TestInterface.multiply(value);\n" | 2953 " var bar;\n" |
| 2945 " TestInterface.notfound(value);\n" | 2954 "}\n" |
| 2955 "\n" |
| 2956 "interface MyInterface default MyClass {\n" |
| 2957 " MyInterface.named(value);\n" |
| 2958 " MyInterface.multiply(value);\n" |
| 2959 " MyInterface.notfound(value);\n" |
| 2960 "}\n" |
| 2961 "\n" |
| 2962 "interface MyInterface2 default MyClass {\n" |
| 2963 " MyInterface2.multiply(value);\n" |
| 2964 " MyInterface2.notfound(value);\n" |
| 2946 "}\n"; | 2965 "}\n"; |
| 2947 | 2966 |
| 2948 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 2967 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 2949 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Test")); | 2968 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("MyClass")); |
| 2950 EXPECT_VALID(cls); | 2969 EXPECT_VALID(cls); |
| 2951 Dart_Handle intf = Dart_GetClass(lib, Dart_NewString("TestInterface")); | 2970 Dart_Handle cls2 = Dart_GetClass(lib, Dart_NewString("MyClass2")); |
| 2971 EXPECT_VALID(cls2); |
| 2972 Dart_Handle intf = Dart_GetClass(lib, Dart_NewString("MyInterface")); |
| 2952 EXPECT_VALID(intf); | 2973 EXPECT_VALID(intf); |
| 2974 Dart_Handle intf2 = Dart_GetClass(lib, Dart_NewString("MyInterface2")); |
| 2975 EXPECT_VALID(intf2); |
| 2953 Dart_Handle args[1]; | 2976 Dart_Handle args[1]; |
| 2954 args[0] = Dart_NewInteger(11); | 2977 args[0] = Dart_NewInteger(11); |
| 2955 Dart_Handle bad_args[1]; | 2978 Dart_Handle bad_args[1]; |
| 2956 bad_args[0] = Dart_Error("myerror"); | 2979 bad_args[0] = Dart_Error("myerror"); |
| 2957 | 2980 |
| 2958 // Invoke the unnamed constructor. | 2981 // Invoke the unnamed constructor. |
| 2959 Dart_Handle result = Dart_New(cls, Dart_Null(), 0, NULL); | 2982 Dart_Handle result = Dart_New(cls, Dart_Null(), 0, NULL); |
| 2960 EXPECT_VALID(result); | 2983 EXPECT_VALID(result); |
| 2961 bool instanceof = false; | 2984 bool instanceof = false; |
| 2962 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); | 2985 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3021 EXPECT_ERROR(result, "Dart_New expects argument 'clazz' to be non-null."); | 3044 EXPECT_ERROR(result, "Dart_New expects argument 'clazz' to be non-null."); |
| 3022 | 3045 |
| 3023 // Pass a negative arg count. | 3046 // Pass a negative arg count. |
| 3024 result = Dart_New(cls, Dart_NewString("named"), -1, args); | 3047 result = Dart_New(cls, Dart_NewString("named"), -1, args); |
| 3025 EXPECT_ERROR( | 3048 EXPECT_ERROR( |
| 3026 result, | 3049 result, |
| 3027 "Dart_New expects argument 'number_of_arguments' to be non-negative."); | 3050 "Dart_New expects argument 'number_of_arguments' to be non-negative."); |
| 3028 | 3051 |
| 3029 // Pass the wrong arg count. | 3052 // Pass the wrong arg count. |
| 3030 result = Dart_New(cls, Dart_NewString("named"), 0, NULL); | 3053 result = Dart_New(cls, Dart_NewString("named"), 0, NULL); |
| 3031 EXPECT_ERROR(result, | 3054 EXPECT_ERROR( |
| 3032 "Dart_New: wrong argument count for constructor 'Test.named': " | 3055 result, |
| 3033 "expected 1 but saw 0."); | 3056 "Dart_New: wrong argument count for constructor 'MyClass.named': " |
| 3057 "expected 1 but saw 0."); |
| 3034 | 3058 |
| 3035 // Pass a bad argument. Error is passed through. | 3059 // Pass a bad argument. Error is passed through. |
| 3036 result = Dart_New(cls, Dart_NewString("named"), 1, bad_args); | 3060 result = Dart_New(cls, Dart_NewString("named"), 1, bad_args); |
| 3037 EXPECT_ERROR(result, "myerror"); | 3061 EXPECT_ERROR(result, "myerror"); |
| 3038 | 3062 |
| 3039 // Pass a bad constructor name. | 3063 // Pass a bad constructor name. |
| 3040 result = Dart_New(cls, Dart_NewInteger(55), 1, args); | 3064 result = Dart_New(cls, Dart_NewInteger(55), 1, args); |
| 3041 EXPECT_ERROR( | 3065 EXPECT_ERROR( |
| 3042 result, | 3066 result, |
| 3043 "Dart_New expects argument 'constructor_name' to be of type String."); | 3067 "Dart_New expects argument 'constructor_name' to be of type String."); |
| 3044 | 3068 |
| 3045 // Invoke a missing constructor. | 3069 // Invoke a missing constructor. |
| 3046 result = Dart_New(cls, Dart_NewString("missing"), 1, args); | 3070 result = Dart_New(cls, Dart_NewString("missing"), 1, args); |
| 3047 EXPECT_ERROR(result, "Dart_New: could not find constructor 'Test.missing'."); | 3071 EXPECT_ERROR(result, |
| 3072 "Dart_New: could not find constructor 'MyClass.missing'."); |
| 3048 | 3073 |
| 3049 // Invoke a constructor which throws an exception. | 3074 // Invoke a constructor which throws an exception. |
| 3050 result = Dart_New(cls, Dart_NewString("exception"), 1, args); | 3075 result = Dart_New(cls, Dart_NewString("exception"), 1, args); |
| 3051 EXPECT_ERROR(result, "ConstructorDeath"); | 3076 EXPECT_ERROR(result, "ConstructorDeath"); |
| 3052 | 3077 |
| 3078 // MyInterface has default class MyClass. |
| 3079 // |
| 3080 // MyClass *implements* MyInterface. |
| 3081 // |
| 3082 // Therefore the constructor call: |
| 3083 // |
| 3084 // MyInterface.foo() |
| 3085 // |
| 3086 // Becomes: |
| 3087 // |
| 3088 // MyClass.foo() from the class MyClass. |
| 3089 |
| 3053 // Invoke an interface constructor. | 3090 // Invoke an interface constructor. |
| 3054 result = Dart_New(intf, Dart_NewString("named"), 1, args); | 3091 result = Dart_New(intf, Dart_NewString("named"), 1, args); |
| 3055 EXPECT_VALID(result); | 3092 EXPECT_VALID(result); |
| 3056 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); | 3093 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3057 EXPECT(instanceof); | 3094 EXPECT(instanceof); |
| 3058 int_value = 0; | 3095 int_value = 0; |
| 3059 foo = Dart_GetField(result, Dart_NewString("foo")); | 3096 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3060 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); | 3097 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3061 EXPECT_EQ(11, int_value); | 3098 EXPECT_EQ(11, int_value); |
| 3062 | 3099 |
| 3063 // Invoke an interface constructor which in turn calls a factory | 3100 // Invoke an interface constructor which in turn calls a factory |
| 3064 // constructor. Why not? | 3101 // constructor. |
| 3065 result = Dart_New(intf, Dart_NewString("multiply"), 1, args); | 3102 result = Dart_New(intf, Dart_NewString("multiply"), 1, args); |
| 3066 EXPECT_VALID(result); | 3103 EXPECT_VALID(result); |
| 3067 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); | 3104 EXPECT_VALID(Dart_ObjectIsType(result, cls, &instanceof)); |
| 3068 EXPECT(instanceof); | 3105 EXPECT(instanceof); |
| 3069 int_value = 0; | 3106 int_value = 0; |
| 3070 foo = Dart_GetField(result, Dart_NewString("foo")); | 3107 foo = Dart_GetField(result, Dart_NewString("foo")); |
| 3071 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); | 3108 EXPECT_VALID(Dart_IntegerToInt64(foo, &int_value)); |
| 3072 EXPECT_EQ(1100, int_value); | 3109 EXPECT_EQ(1100, int_value); |
| 3073 | 3110 |
| 3074 // Invoke a constructor that is missing in the interface but present | 3111 // Invoke a constructor that is missing in the interface but present |
| 3075 // in the default class. | 3112 // in the default class. |
| 3076 result = Dart_New(intf, Dart_Null(), 0, NULL); | 3113 result = Dart_New(intf, Dart_Null(), 0, NULL); |
| 3077 EXPECT_ERROR(result, | 3114 EXPECT_ERROR(result, |
| 3078 "Dart_New: could not find constructor 'TestInterface.'."); | 3115 "Dart_New: could not find constructor 'MyInterface.'."); |
| 3079 | 3116 |
| 3080 // Invoke a constructor that is present in the interface but missing | 3117 // Invoke a constructor that is present in the interface but missing |
| 3081 // in the default class. | 3118 // in the default class. |
| 3082 result = Dart_New(intf, Dart_NewString("notfound"), 1, args); | 3119 result = Dart_New(intf, Dart_NewString("notfound"), 1, args); |
| 3083 EXPECT_ERROR(result, "Dart_New: could not find constructor 'Test.notfound'."); | 3120 EXPECT_ERROR(result, |
| 3121 "Dart_New: could not find constructor 'MyClass.notfound'."); |
| 3122 |
| 3123 // MyInterface2 has default class MyClass. |
| 3124 // |
| 3125 // MyClass *does not implement* MyInterface2. |
| 3126 // |
| 3127 // Therefore the constructor call: |
| 3128 // |
| 3129 // new MyInterface2.foo() |
| 3130 // |
| 3131 // Becomes: |
| 3132 // |
| 3133 // new MyInterface2.foo() from the class MyClass. |
| 3134 |
| 3135 // Invoke an interface constructor which in turn calls a factory |
| 3136 // constructor. |
| 3137 result = Dart_New(intf2, Dart_NewString("multiply"), 1, args); |
| 3138 EXPECT_VALID(result); |
| 3139 EXPECT_VALID(Dart_ObjectIsType(result, cls2, &instanceof)); |
| 3140 EXPECT(instanceof); |
| 3141 int_value = 0; |
| 3142 Dart_Handle bar = Dart_GetField(result, Dart_NewString("bar")); |
| 3143 EXPECT_VALID(Dart_IntegerToInt64(bar, &int_value)); |
| 3144 EXPECT_EQ(110000, int_value); |
| 3145 |
| 3146 // Invoke a constructor that is missing in the interface but present |
| 3147 // in the default class. |
| 3148 result = Dart_New(intf2, Dart_NewString("unused"), 1, args); |
| 3149 EXPECT_ERROR(result, |
| 3150 "Dart_New: could not find constructor 'MyInterface2.unused'."); |
| 3151 |
| 3152 // Invoke a constructor that is present in the interface but missing |
| 3153 // in the default class. |
| 3154 result = Dart_New(intf2, Dart_NewString("notfound"), 1, args); |
| 3155 EXPECT_ERROR(result, |
| 3156 "Dart_New: could not find factory 'MyInterface2.notfound' " |
| 3157 "in class 'MyClass'."); |
| 3158 } |
| 3159 |
| 3160 |
| 3161 TEST_CASE(New_Issue2971) { |
| 3162 // Issue 2971: We were unable to use Dart_New to construct an |
| 3163 // instance of List, due to problems implementing interface |
| 3164 // factories. |
| 3165 Dart_Handle core_lib = Dart_LookupLibrary(Dart_NewString("dart:core")); |
| 3166 EXPECT_VALID(core_lib); |
| 3167 Dart_Handle list_class = Dart_GetClass(core_lib, Dart_NewString("List")); |
| 3168 EXPECT_VALID(list_class); |
| 3169 |
| 3170 const int kNumArgs = 1; |
| 3171 Dart_Handle args[kNumArgs]; |
| 3172 args[0] = Dart_NewInteger(1); |
| 3173 Dart_Handle list_obj = Dart_New(list_class, Dart_Null(), kNumArgs, args); |
| 3174 EXPECT_VALID(list_obj); |
| 3175 EXPECT(Dart_IsList(list_obj)); |
| 3084 } | 3176 } |
| 3085 | 3177 |
| 3086 | 3178 |
| 3087 TEST_CASE(Invoke) { | 3179 TEST_CASE(Invoke) { |
| 3088 const char* kScriptChars = | 3180 const char* kScriptChars = |
| 3089 "class BaseMethods {\n" | 3181 "class BaseMethods {\n" |
| 3090 " inheritedMethod(arg) => 'inherited $arg';\n" | 3182 " inheritedMethod(arg) => 'inherited $arg';\n" |
| 3091 " static nonInheritedMethod(arg) => 'noninherited $arg';\n" | 3183 " static nonInheritedMethod(arg) => 'noninherited $arg';\n" |
| 3092 "}\n" | 3184 "}\n" |
| 3093 "\n" | 3185 "\n" |
| (...skipping 1613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4707 // We should have received the expected number of interrupts. | 4799 // We should have received the expected number of interrupts. |
| 4708 EXPECT_EQ(kInterruptCount, interrupt_count); | 4800 EXPECT_EQ(kInterruptCount, interrupt_count); |
| 4709 | 4801 |
| 4710 // Give the spawned thread enough time to properly exit. | 4802 // Give the spawned thread enough time to properly exit. |
| 4711 Isolate::SetInterruptCallback(saved); | 4803 Isolate::SetInterruptCallback(saved); |
| 4712 } | 4804 } |
| 4713 | 4805 |
| 4714 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 4806 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 4715 | 4807 |
| 4716 } // namespace dart | 4808 } // namespace dart |
| OLD | NEW |