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

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 25675009: Allow invocation of constructors using Dart_Invoke. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 "include/dart_mirrors_api.h" 6 #include "include/dart_mirrors_api.h"
7 #include "include/dart_native_api.h" 7 #include "include/dart_native_api.h"
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "vm/bigint_operations.h" 10 #include "vm/bigint_operations.h"
(...skipping 2969 matching lines...) Expand 10 before | Expand all | Expand 10 after
2980 if (type_obj.IsNull()) { 2980 if (type_obj.IsNull()) {
2981 RETURN_TYPE_ERROR(isolate, type, Type); 2981 RETURN_TYPE_ERROR(isolate, type, Type);
2982 } 2982 }
2983 const Class& cls = Class::Handle(isolate, type_obj.type_class()); 2983 const Class& cls = Class::Handle(isolate, type_obj.type_class());
2984 2984
2985 // Allocate an object for the given class. 2985 // Allocate an object for the given class.
2986 return Api::NewHandle(isolate, Instance::New(cls)); 2986 return Api::NewHandle(isolate, Instance::New(cls));
2987 } 2987 }
2988 2988
2989 2989
2990 static Dart_Handle SetupArguments(Isolate* isolate,
2991 int num_args,
2992 Dart_Handle* arguments,
2993 int extra_args,
2994 Array* args) {
2995 // Check for malformed arguments in the arguments list.
2996 *args = Array::New(num_args + extra_args);
2997 Object& arg = Object::Handle(isolate);
2998 for (int i = 0; i < num_args; i++) {
2999 arg = Api::UnwrapHandle(arguments[i]);
3000 if (!arg.IsNull() && !arg.IsInstance()) {
3001 *args = Array::null();
3002 if (arg.IsError()) {
3003 return Api::NewHandle(isolate, arg.raw());
3004 } else {
3005 return Api::NewError(
3006 "%s expects arguments[%d] to be an Instance handle.",
3007 "Dart_Invoke", i);
3008 }
3009 }
3010 args->SetAt((i + extra_args), arg);
3011 }
3012 return Api::Success();
3013 }
3014
3015
3016 DART_EXPORT Dart_Handle Dart_InvokeConstructor(Dart_Handle object,
3017 Dart_Handle name,
3018 int number_of_arguments,
3019 Dart_Handle* arguments) {
3020 Isolate* isolate = Isolate::Current();
3021 DARTSCOPE(isolate);
3022 CHECK_CALLBACK_STATE(isolate);
3023
3024 if (number_of_arguments < 0) {
3025 return Api::NewError(
3026 "%s expects argument 'number_of_arguments' to be non-negative.",
3027 CURRENT_FUNC);
3028 }
3029 const String& constructor_name = Api::UnwrapStringHandle(isolate, name);
3030 if (constructor_name.IsNull()) {
3031 RETURN_TYPE_ERROR(isolate, name, String);
3032 }
3033 const Instance& instance = Api::UnwrapInstanceHandle(isolate, object);
3034 if (instance.IsNull()) {
3035 RETURN_TYPE_ERROR(isolate, object, Instance);
3036 }
3037
3038 // Since we have allocated an object it would mean that all classes
3039 // are finalized and hence it is not necessary to call
3040 // Api::CheckIsolateState.
3041 // TODO(asiva): How do we ensure that a constructor is not called more than
3042 // once for the same object.
3043
3044 // Construct name of the constructor to invoke.
3045 const Type& type_obj = Type::Handle(isolate, instance.GetType());
3046 const Class& cls = Class::Handle(isolate, type_obj.type_class());
3047 const String& class_name = String::Handle(isolate, cls.Name());
3048 const Array& strings = Array::Handle(Array::New(3));
3049 strings.SetAt(0, class_name);
3050 strings.SetAt(1, Symbols::Dot());
3051 strings.SetAt(2, constructor_name);
3052 const String& dot_name = String::Handle(isolate, String::ConcatAll(strings));
3053 const AbstractTypeArguments& type_arguments =
3054 AbstractTypeArguments::Handle(isolate, type_obj.arguments());
3055 const Function& constructor =
3056 Function::Handle(isolate, cls.LookupFunctionAllowPrivate(dot_name));
3057 const int extra_args = 2;
3058 if (!constructor.IsNull() &&
3059 constructor.IsConstructor() &&
3060 constructor.AreValidArgumentCounts(number_of_arguments + extra_args,
3061 0,
3062 NULL)) {
3063 // Create the argument list.
3064 // Constructors get the uninitialized object and a constructor phase.
3065 if (!type_arguments.IsNull()) {
3066 // The type arguments will be null if the class has no type
3067 // parameters, in which case the following call would fail
3068 // because there is no slot reserved in the object for the
3069 // type vector.
3070 instance.SetTypeArguments(type_arguments);
3071 }
3072 Dart_Handle result;
3073 Array& args = Array::Handle(isolate);
3074 result = SetupArguments(isolate,
3075 number_of_arguments,
3076 arguments,
3077 extra_args,
3078 &args);
3079 if (!::Dart_IsError(result)) {
3080 args.SetAt(0, instance);
3081 args.SetAt(1, Smi::Handle(isolate, Smi::New(Function::kCtorPhaseAll)));
3082 const Object& retval = Object::Handle(
3083 isolate,
3084 DartEntry::InvokeFunction(constructor, args));
3085 if (retval.IsError()) {
3086 result = Api::NewHandle(isolate, retval.raw());
3087 } else {
3088 result = Api::NewHandle(isolate, instance.raw());
3089 }
3090 }
3091 return result;
3092 }
3093 return Api::NewError(
3094 "%s expects argument 'name' to be a valid constructor.",
3095 CURRENT_FUNC);
3096 }
3097
3098
2990 DART_EXPORT Dart_Handle Dart_Invoke(Dart_Handle target, 3099 DART_EXPORT Dart_Handle Dart_Invoke(Dart_Handle target,
2991 Dart_Handle name, 3100 Dart_Handle name,
2992 int number_of_arguments, 3101 int number_of_arguments,
2993 Dart_Handle* arguments) { 3102 Dart_Handle* arguments) {
2994 Isolate* isolate = Isolate::Current(); 3103 Isolate* isolate = Isolate::Current();
2995 DARTSCOPE(isolate); 3104 DARTSCOPE(isolate);
2996 CHECK_CALLBACK_STATE(isolate); 3105 CHECK_CALLBACK_STATE(isolate);
2997 3106
2998 const String& function_name = Api::UnwrapStringHandle(isolate, name); 3107 const String& function_name = Api::UnwrapStringHandle(isolate, name);
2999 if (function_name.IsNull()) { 3108 if (function_name.IsNull()) {
3000 RETURN_TYPE_ERROR(isolate, name, String); 3109 RETURN_TYPE_ERROR(isolate, name, String);
3001 } 3110 }
3002 if (number_of_arguments < 0) { 3111 if (number_of_arguments < 0) {
3003 return Api::NewError( 3112 return Api::NewError(
3004 "%s expects argument 'number_of_arguments' to be non-negative.", 3113 "%s expects argument 'number_of_arguments' to be non-negative.",
3005 CURRENT_FUNC); 3114 CURRENT_FUNC);
3006 } 3115 }
3007 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(target)); 3116 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(target));
3008 if (obj.IsError()) { 3117 if (obj.IsError()) {
3009 return target; 3118 return target;
3010 } 3119 }
3011 3120 Dart_Handle result;
3012 // Check for malformed arguments in the arguments list. 3121 Array& args = Array::Handle(isolate);
3013 intptr_t num_receiver =
3014 (obj.IsNull() || (obj.IsInstance() && !obj.IsType())) ? 1 : 0;
3015 const Array& args =
3016 Array::Handle(isolate, Array::New(number_of_arguments + num_receiver));
3017 Object& arg = Object::Handle(isolate);
3018 for (int i = 0; i < number_of_arguments; i++) {
3019 arg = Api::UnwrapHandle(arguments[i]);
3020 if (!arg.IsNull() && !arg.IsInstance()) {
3021 if (arg.IsError()) {
3022 return Api::NewHandle(isolate, arg.raw());
3023 } else {
3024 return Api::NewError(
3025 "%s expects arguments[%d] to be an Instance handle.",
3026 CURRENT_FUNC, i);
3027 }
3028 }
3029 args.SetAt((i + num_receiver), arg);
3030 }
3031
3032 if (obj.IsType()) { 3122 if (obj.IsType()) {
3033 // Finalize all classes. 3123 // Finalize all classes.
3034 Dart_Handle state = Api::CheckIsolateState(isolate); 3124 Dart_Handle state = Api::CheckIsolateState(isolate);
3035 if (::Dart_IsError(state)) { 3125 if (::Dart_IsError(state)) {
3036 return state; 3126 return state;
3037 } 3127 }
3038 3128
3039 const Class& cls = Class::Handle(isolate, Type::Cast(obj).type_class()); 3129 const Class& cls = Class::Handle(isolate, Type::Cast(obj).type_class());
3040 const Function& function = Function::Handle( 3130 const Function& function = Function::Handle(
3041 isolate, 3131 isolate,
3042 Resolver::ResolveStatic(cls, 3132 Resolver::ResolveStatic(cls,
3043 function_name, 3133 function_name,
3044 number_of_arguments, 3134 number_of_arguments,
3045 Object::empty_array(), 3135 Object::empty_array(),
3046 Resolver::kIsQualified)); 3136 Resolver::kIsQualified));
3047 if (function.IsNull()) { 3137 if (function.IsNull()) {
3048 const String& cls_name = String::Handle(isolate, cls.Name()); 3138 const String& cls_name = String::Handle(isolate, cls.Name());
3049 return Api::NewError("%s: did not find static method '%s.%s'.", 3139 return Api::NewError("%s: did not find static method '%s.%s'.",
3050 CURRENT_FUNC, 3140 CURRENT_FUNC,
3051 cls_name.ToCString(), 3141 cls_name.ToCString(),
3052 function_name.ToCString()); 3142 function_name.ToCString());
3053 } 3143 }
3054 return Api::NewHandle(isolate, DartEntry::InvokeFunction(function, args)); 3144 // Setup args and check for malformed arguments in the arguments list.
3055 3145 result = SetupArguments(isolate, number_of_arguments, arguments, 0, &args);
3146 if (!::Dart_IsError(result)) {
3147 result = Api::NewHandle(isolate,
3148 DartEntry::InvokeFunction(function, args));
3149 }
3150 return result;
3056 } else if (obj.IsNull() || obj.IsInstance()) { 3151 } else if (obj.IsNull() || obj.IsInstance()) {
3152 // Since we have allocated an object it would mean that all classes
3153 // are finalized and hence it is not necessary to call
3154 // Api::CheckIsolateState.
3057 Instance& instance = Instance::Handle(isolate); 3155 Instance& instance = Instance::Handle(isolate);
3058 instance ^= obj.raw(); 3156 instance ^= obj.raw();
3059 ArgumentsDescriptor args_desc( 3157 ArgumentsDescriptor args_desc(
3060 Array::Handle(ArgumentsDescriptor::New(number_of_arguments + 1))); 3158 Array::Handle(ArgumentsDescriptor::New(number_of_arguments + 1)));
3061 const Function& function = Function::Handle( 3159 const Function& function = Function::Handle(
3062 isolate, 3160 isolate,
3063 Resolver::ResolveDynamic(instance, function_name, args_desc)); 3161 Resolver::ResolveDynamic(instance, function_name, args_desc));
3064 args.SetAt(0, instance);
3065 if (function.IsNull()) { 3162 if (function.IsNull()) {
3066 const Array& args_descriptor = 3163 // Setup args and check for malformed arguments in the arguments list.
3164 result = SetupArguments(isolate,
3165 number_of_arguments,
3166 arguments,
3167 1,
3168 &args);
3169 if (!::Dart_IsError(result)) {
3170 args.SetAt(0, instance);
3171 const Array& args_descriptor =
3067 Array::Handle(ArgumentsDescriptor::New(args.Length())); 3172 Array::Handle(ArgumentsDescriptor::New(args.Length()));
3068 return Api::NewHandle(isolate, 3173 result = Api::NewHandle(isolate,
3069 DartEntry::InvokeNoSuchMethod(instance, 3174 DartEntry::InvokeNoSuchMethod(instance,
3070 function_name, 3175 function_name,
3071 args, 3176 args,
3072 args_descriptor)); 3177 args_descriptor));
3178 }
3179 return result;
3073 } 3180 }
3074 return Api::NewHandle(isolate, DartEntry::InvokeFunction(function, args)); 3181 // Setup args and check for malformed arguments in the arguments list.
3075 3182 result = SetupArguments(isolate, number_of_arguments, arguments, 1, &args);
3183 if (!::Dart_IsError(result)) {
3184 args.SetAt(0, instance);
3185 result = Api::NewHandle(isolate,
3186 DartEntry::InvokeFunction(function, args));
3187 }
3188 return result;
3076 } else if (obj.IsLibrary()) { 3189 } else if (obj.IsLibrary()) {
3077 // Check whether class finalization is needed. 3190 // Check whether class finalization is needed.
3078 const Library& lib = Library::Cast(obj); 3191 const Library& lib = Library::Cast(obj);
3079 3192
3080 // Finalize all classes if needed. 3193 // Finalize all classes if needed.
3081 Dart_Handle state = Api::CheckIsolateState(isolate); 3194 Dart_Handle state = Api::CheckIsolateState(isolate);
3082 if (::Dart_IsError(state)) { 3195 if (::Dart_IsError(state)) {
3083 return state; 3196 return state;
3084 } 3197 }
3085 3198
3086 const Function& function = 3199 const Function& function =
3087 Function::Handle(isolate, 3200 Function::Handle(isolate,
3088 lib.LookupFunctionAllowPrivate(function_name)); 3201 lib.LookupFunctionAllowPrivate(function_name));
3089 if (function.IsNull()) { 3202 if (function.IsNull()) {
3090 return Api::NewError("%s: did not find top-level function '%s'.", 3203 return Api::NewError("%s: did not find top-level function '%s'.",
3091 CURRENT_FUNC, 3204 CURRENT_FUNC,
3092 function_name.ToCString()); 3205 function_name.ToCString());
3093 } 3206 }
3094 // LookupFunctionAllowPrivate does not check argument arity, so we 3207 // LookupFunctionAllowPrivate does not check argument arity, so we
3095 // do it here. 3208 // do it here.
3096 String& error_message = String::Handle(); 3209 String& error_message = String::Handle();
3097 if (!function.AreValidArgumentCounts(number_of_arguments, 3210 if (!function.AreValidArgumentCounts(number_of_arguments,
3098 0, 3211 0,
3099 &error_message)) { 3212 &error_message)) {
3100 return Api::NewError("%s: wrong argument count for function '%s': %s.", 3213 return Api::NewError("%s: wrong argument count for function '%s': %s.",
3101 CURRENT_FUNC, 3214 CURRENT_FUNC,
3102 function_name.ToCString(), 3215 function_name.ToCString(),
3103 error_message.ToCString()); 3216 error_message.ToCString());
3104 } 3217 }
3105 return Api::NewHandle(isolate, DartEntry::InvokeFunction(function, args)); 3218 // Setup args and check for malformed arguments in the arguments list.
3106 3219 result = SetupArguments(isolate, number_of_arguments, arguments, 0, &args);
3220 if (!::Dart_IsError(result)) {
3221 result = Api::NewHandle(isolate,
3222 DartEntry::InvokeFunction(function, args));
3223 }
3224 return result;
3107 } else { 3225 } else {
3108 return Api::NewError( 3226 return Api::NewError(
3109 "%s expects argument 'target' to be an object, type, or library.", 3227 "%s expects argument 'target' to be an object, type, or library.",
3110 CURRENT_FUNC); 3228 CURRENT_FUNC);
3111 } 3229 }
3112 } 3230 }
3113 3231
3114 3232
3115 DART_EXPORT Dart_Handle Dart_InvokeClosure(Dart_Handle closure, 3233 DART_EXPORT Dart_Handle Dart_InvokeClosure(Dart_Handle closure,
3116 int number_of_arguments, 3234 int number_of_arguments,
(...skipping 1197 matching lines...) Expand 10 before | Expand all | Expand 10 after
4314 } 4432 }
4315 { 4433 {
4316 NoGCScope no_gc; 4434 NoGCScope no_gc;
4317 RawObject* raw_obj = obj.raw(); 4435 RawObject* raw_obj = obj.raw();
4318 isolate->heap()->SetPeer(raw_obj, peer); 4436 isolate->heap()->SetPeer(raw_obj, peer);
4319 } 4437 }
4320 return Api::Success(); 4438 return Api::Success();
4321 } 4439 }
4322 4440
4323 } // namespace dart 4441 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698