| 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 "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
| 6 | 6 |
| 7 #include "vm/compiler.h" | 7 #include "vm/compiler.h" |
| 8 #include "vm/dart_entry.h" | 8 #include "vm/dart_entry.h" |
| 9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
| 10 #include "vm/native_entry.h" | 10 #include "vm/native_entry.h" |
| 11 #include "vm/object.h" | 11 #include "vm/object.h" |
| 12 #include "vm/symbols.h" | 12 #include "vm/symbols.h" |
| 13 | 13 |
| 14 namespace dart { | 14 namespace dart { |
| 15 | 15 |
| 16 DEFINE_NATIVE_ENTRY(Function_apply, 2) { | 16 DEFINE_NATIVE_ENTRY(Function_apply, 2) { |
| 17 const Array& fun_arguments = Array::CheckedHandle(arguments->NativeArgAt(0)); | 17 const Array& fun_arguments = Array::CheckedHandle(arguments->NativeArgAt(0)); |
| 18 const Array& fun_arg_names = Array::CheckedHandle(arguments->NativeArgAt(1)); | 18 const Array& fun_arg_names = Array::CheckedHandle(arguments->NativeArgAt(1)); |
| 19 // TODO(regis): Simply call DartEntry::InvokeClosure once it is modified to | |
| 20 // take an arguments descriptor array and an arguments array. | |
| 21 Instance& instance = Instance::Handle(); | 19 Instance& instance = Instance::Handle(); |
| 22 instance ^= fun_arguments.At(0); | 20 instance ^= fun_arguments.At(0); |
| 23 // Get the entrypoint corresponding to the closure function or to the call | |
| 24 // method of the instance. This will result in a compilation of the function | |
| 25 // if it is not already compiled. | |
| 26 Function& function = Function::Handle(); | |
| 27 Context& context = Context::Handle(); | |
| 28 if (!instance.IsCallable(&function, &context)) { | |
| 29 const String& call_symbol = String::Handle(Symbols::Call()); | |
| 30 const Object& null_object = Object::Handle(); | |
| 31 GrowableArray<const Object*> dart_arguments(5); | |
| 32 dart_arguments.Add(&instance); | |
| 33 dart_arguments.Add(&call_symbol); | |
| 34 dart_arguments.Add(&fun_arguments); // Including instance. | |
| 35 dart_arguments.Add(&null_object); // TODO(regis): Provide names. | |
| 36 // If a function "call" with different arguments exists, it will have been | |
| 37 // invoked above, so no need to handle this case here. | |
| 38 Exceptions::ThrowByType(Exceptions::kNoSuchMethod, dart_arguments); | |
| 39 UNREACHABLE(); | |
| 40 return Object::null(); | |
| 41 } | |
| 42 ASSERT(!function.IsNull()); | |
| 43 if (!function.HasCode()) { | |
| 44 const Error& error = Error::Handle(Compiler::CompileFunction(function)); | |
| 45 if (!error.IsNull()) { | |
| 46 Exceptions::PropagateError(error); | |
| 47 } | |
| 48 } | |
| 49 // Set up arguments as GrowableArray. | |
| 50 const int num_arguments = fun_arguments.Length(); | |
| 51 GrowableArray<const Object*> args(num_arguments); | |
| 52 for (int i = 0; i < num_arguments; i++) { | |
| 53 args.Add(&Object::ZoneHandle(fun_arguments.At(i))); | |
| 54 } | |
| 55 // Now call the invoke stub which will invoke the closure. | |
| 56 DartEntry::invokestub entrypoint = reinterpret_cast<DartEntry::invokestub>( | |
| 57 StubCode::InvokeDartCodeEntryPoint()); | |
| 58 ASSERT(context.isolate() == Isolate::Current()); | |
| 59 const Code& code = Code::Handle(function.CurrentCode()); | |
| 60 ASSERT(!code.IsNull()); | |
| 61 const Array& fun_args_desc = | 21 const Array& fun_args_desc = |
| 62 Array::Handle(ArgumentsDescriptor::New(num_arguments, fun_arg_names)); | 22 Array::Handle(ArgumentsDescriptor::New(fun_arguments.Length(), |
| 63 const Object& result = Object::Handle( | 23 fun_arg_names)); |
| 64 entrypoint(code.EntryPoint(), fun_args_desc, args.data(), context)); | 24 const Object& result = |
| 25 Object::Handle(DartEntry::InvokeClosure(instance, |
| 26 fun_arguments, |
| 27 fun_args_desc)); |
| 65 if (result.IsError()) { | 28 if (result.IsError()) { |
| 66 Exceptions::PropagateError(Error::Cast(result)); | 29 Exceptions::PropagateError(Error::Cast(result)); |
| 67 } | 30 } |
| 68 return result.raw(); | 31 return result.raw(); |
| 69 } | 32 } |
| 70 | 33 |
| 71 } // namespace dart | 34 } // namespace dart |
| OLD | NEW |