Chromium Code Reviews| 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 GET_NON_NULL_NATIVE_ARGUMENT(Array, fun_arguments, arguments->NativeArgAt(0)); | |
| 18 GET_NON_NULL_NATIVE_ARGUMENT(Array, fun_arg_names, arguments->NativeArgAt(1)); | |
|
srdjan
2012/12/14 18:13:13
This can be CheckedHandles since it is an internal
regis
2012/12/14 18:49:49
Done.
| |
| 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 UNREACHABLE(); | |
| 48 } | |
| 49 } | |
| 50 // Set up arguments as GrowableArray. | |
| 51 const int num_arguments = fun_arguments.Length(); | |
| 52 GrowableArray<const Object*> args(num_arguments); | |
| 53 for (int i = 0; i < num_arguments; i++) { | |
| 54 args.Add(&Object::ZoneHandle(fun_arguments.At(i))); | |
| 55 } | |
| 56 // Now call the invoke stub which will invoke the closure. | |
| 57 DartEntry::invokestub entrypoint = reinterpret_cast<DartEntry::invokestub>( | |
| 58 StubCode::InvokeDartCodeEntryPoint()); | |
| 59 ASSERT(context.isolate() == Isolate::Current()); | |
| 60 const Code& code = Code::Handle(function.CurrentCode()); | |
| 61 ASSERT(!code.IsNull()); | |
| 62 const Array& fun_args_desc = | |
| 63 Array::Handle(ArgumentsDescriptor::New(num_arguments, fun_arg_names)); | |
| 64 const Object& result = Object::Handle( | |
| 65 entrypoint(code.EntryPoint(), fun_args_desc, args.data(), context)); | |
| 66 if (result.IsError()) { | |
| 67 Exceptions::PropagateError(Error::Cast(result)); | |
| 68 UNREACHABLE(); | |
| 69 } | |
| 70 return result.raw(); | |
| 71 } | |
| 72 | |
| 73 } // namespace dart | |
| OLD | NEW |