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/jit/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, 0, 2) { |
17 | const int kTypeArgsLen = 0; // TODO(regis): Add support for generic function. |
18 | const Array& fun_arguments = |
19 | Array::CheckedHandle(zone, arguments->NativeArgAt(0)); |
20 | const Array& fun_arg_names = |
21 | Array::CheckedHandle(zone, arguments->NativeArgAt(1)); |
22 | const Array& fun_args_desc = Array::Handle( |
23 | zone, ArgumentsDescriptor::NewBoxed(kTypeArgsLen, fun_arguments.Length(), |
24 | fun_arg_names)); |
25 | const Object& result = Object::Handle( |
26 | zone, DartEntry::InvokeClosure(fun_arguments, fun_args_desc)); |
27 | if (result.IsError()) { |
28 | Exceptions::PropagateError(Error::Cast(result)); |
29 | } |
30 | return result.raw(); |
31 | } |
32 | |
33 | DEFINE_NATIVE_ENTRY(Closure_equals, 0, 2) { |
34 | const Closure& receiver = |
35 | Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); |
36 | GET_NATIVE_ARGUMENT(Instance, other, arguments->NativeArgAt(1)); |
37 | ASSERT(!other.IsNull()); |
38 | // For implicit instance closures compare receiver instance and function's |
39 | // name and owner (multiple function objects could exist for the same |
40 | // function due to hot reload). |
41 | // Objects of other closure kinds are unique, so use identity comparison. |
42 | if (receiver.raw() == other.raw()) { |
43 | return Bool::True().raw(); |
44 | } |
45 | if (other.IsClosure()) { |
46 | const Function& func_a = Function::Handle(zone, receiver.function()); |
47 | if (func_a.IsImplicitInstanceClosureFunction()) { |
48 | const Closure& other_closure = Closure::Cast(other); |
49 | const Function& func_b = Function::Handle(zone, other_closure.function()); |
50 | if (func_b.IsImplicitInstanceClosureFunction()) { |
51 | const Context& context_a = Context::Handle(zone, receiver.context()); |
52 | const Context& context_b = |
53 | Context::Handle(zone, other_closure.context()); |
54 | ObjectPtr receiver_a = context_a.At(0); |
55 | ObjectPtr receiver_b = context_b.At(0); |
56 | if ((receiver_a == receiver_b) && |
57 | ((func_a.raw() == func_b.raw()) || |
58 | ((func_a.name() == func_b.name()) && |
59 | (func_a.Owner() == func_b.Owner())))) { |
60 | return Bool::True().raw(); |
61 | } |
62 | } |
63 | } |
64 | } |
65 | return Bool::False().raw(); |
66 | } |
67 | |
68 | DEFINE_NATIVE_ENTRY(Closure_computeHash, 0, 1) { |
69 | const Closure& receiver = |
70 | Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); |
71 | return Smi::New(receiver.ComputeHash()); |
72 | } |
73 | |
74 | DEFINE_NATIVE_ENTRY(Closure_clone, 0, 1) { |
75 | const Closure& receiver = |
76 | Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); |
77 | const TypeArguments& instantiator_type_arguments = |
78 | TypeArguments::Handle(zone, receiver.instantiator_type_arguments()); |
79 | const TypeArguments& function_type_arguments = |
80 | TypeArguments::Handle(zone, receiver.function_type_arguments()); |
81 | const Function& function = Function::Handle(zone, receiver.function()); |
82 | const Context& context = Context::Handle(zone, receiver.context()); |
83 | Context& cloned_context = Context::Handle(zone); |
84 | if (!context.IsNull()) { |
85 | cloned_context = Context::New(context.num_variables()); |
86 | cloned_context.set_parent(Context::Handle(zone, context.parent())); |
87 | Object& instance = Object::Handle(zone); |
88 | for (int i = 0; i < context.num_variables(); i++) { |
89 | instance = context.At(i); |
90 | cloned_context.SetAt(i, instance); |
91 | } |
92 | } |
93 | return Closure::New(instantiator_type_arguments, function_type_arguments, |
94 | function, cloned_context); |
95 | } |
96 | |
97 | } // namespace dart |
98 | |