OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "vm/bootstrap_natives.h" |
| 6 |
| 7 #include "vm/compiler.h" |
| 8 #include "vm/dart_entry.h" |
| 9 #include "vm/exceptions.h" |
| 10 #include "vm/native_entry.h" |
| 11 #include "vm/object.h" |
| 12 #include "vm/symbols.h" |
| 13 |
| 14 namespace dart { |
| 15 |
| 16 DEFINE_NATIVE_ENTRY(Function_apply, 2) { |
| 17 const Array& fun_arguments = Array::CheckedHandle(arguments->NativeArgAt(0)); |
| 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(); |
| 22 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 = |
| 62 Array::Handle(ArgumentsDescriptor::New(num_arguments, fun_arg_names)); |
| 63 const Object& result = Object::Handle( |
| 64 entrypoint(code.EntryPoint(), fun_args_desc, args.data(), context)); |
| 65 if (result.IsError()) { |
| 66 Exceptions::PropagateError(Error::Cast(result)); |
| 67 } |
| 68 return result.raw(); |
| 69 } |
| 70 |
| 71 } // namespace dart |
OLD | NEW |