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
14namespace dart {
15
16DEFINE_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, ptr: arguments->NativeArgAt(index: 0));
20 const Array& fun_arg_names =
21 Array::CheckedHandle(zone, ptr: arguments->NativeArgAt(index: 1));
22 const Array& fun_args_desc = Array::Handle(
23 zone, ptr: ArgumentsDescriptor::NewBoxed(type_args_len: kTypeArgsLen, num_arguments: fun_arguments.Length(),
24 optional_arguments_names: fun_arg_names));
25 const Object& result = Object::Handle(
26 zone, ptr: DartEntry::InvokeClosure(thread, arguments: fun_arguments, arguments_descriptor: fun_args_desc));
27 if (result.IsError()) {
28 Exceptions::PropagateError(error: Error::Cast(obj: result));
29 }
30 return result.ptr();
31}
32
33static bool ClosureEqualsHelper(Zone* zone,
34 const Closure& receiver,
35 const Object& other) {
36 if (receiver.ptr() == other.ptr()) {
37 return true;
38 }
39 if (!other.IsClosure()) {
40 return false;
41 }
42 const auto& other_closure = Closure::Cast(obj: other);
43 const auto& func_a = Function::Handle(zone, ptr: receiver.function());
44 const auto& func_b = Function::Handle(zone, ptr: other_closure.function());
45 // Check that functions match.
46 if (func_a.ptr() != func_b.ptr()) {
47 // Non-implicit closures taken from different functions are not equal.
48 if (!func_a.IsImplicitClosureFunction() ||
49 !func_b.IsImplicitClosureFunction()) {
50 return false;
51 }
52 // If the closure functions are not the same, check the function's name and
53 // owner, as multiple function objects could exist for the same function due
54 // to hot reload.
55 if ((func_a.name() != func_b.name() || func_a.Owner() != func_b.Owner() ||
56 func_a.is_static() != func_b.is_static())) {
57 return false;
58 }
59 }
60 // Check that the delayed type argument vectors match.
61 if (receiver.delayed_type_arguments() !=
62 other_closure.delayed_type_arguments()) {
63 // Mismatches should only happen when a generic function is involved.
64 ASSERT(func_a.IsGeneric() || func_b.IsGeneric());
65 const auto& type_args_a =
66 TypeArguments::Handle(zone, ptr: receiver.delayed_type_arguments());
67 const auto& type_args_b =
68 TypeArguments::Handle(zone, ptr: other_closure.delayed_type_arguments());
69 if (type_args_a.IsNull() || type_args_b.IsNull() ||
70 (type_args_a.Length() != type_args_b.Length()) ||
71 !type_args_a.IsEquivalent(other: type_args_b, kind: TypeEquality::kSyntactical)) {
72 return false;
73 }
74 }
75 if (func_a.IsImplicitClosureFunction() &&
76 func_b.IsImplicitClosureFunction()) {
77 if (!func_a.is_static()) {
78 // Check that the both receiver instances are the same.
79 const Context& context_a = Context::Handle(zone, ptr: receiver.context());
80 const Context& context_b = Context::Handle(zone, ptr: other_closure.context());
81 return context_a.At(context_index: 0) == context_b.At(context_index: 0);
82 }
83 } else if (func_a.IsGeneric()) {
84 // Additional constraints for closures of generic functions:
85 // (1) Different instantiations of the same generic closure
86 // with the same type arguments should be equal.
87 // This means that instantiated generic closures are not unique
88 // and equality of instantiated generic closures should not be
89 // based on identity.
90 // (2) Instantiations of non-equal generic closures should be non-equal.
91 // This means that equality of non-instantiated generic closures
92 // should not be based on identity too as it won't match equality
93 // after instantiation.
94 if ((receiver.context() != other_closure.context()) ||
95 (receiver.instantiator_type_arguments() !=
96 other_closure.instantiator_type_arguments()) ||
97 (receiver.function_type_arguments() !=
98 other_closure.function_type_arguments())) {
99 return false;
100 }
101 } else {
102 // Closures of non-generic functions are unique.
103 return false;
104 }
105 return true;
106}
107
108DEFINE_NATIVE_ENTRY(Closure_equals, 0, 2) {
109 const Closure& receiver =
110 Closure::CheckedHandle(zone, ptr: arguments->NativeArgAt(index: 0));
111 GET_NATIVE_ARGUMENT(Instance, other, arguments->NativeArgAt(1));
112 ASSERT(!other.IsNull());
113 return Bool::Get(value: ClosureEqualsHelper(zone, receiver, other)).ptr();
114}
115
116DEFINE_NATIVE_ENTRY(Closure_computeHash, 0, 1) {
117 const Closure& receiver =
118 Closure::CheckedHandle(zone, ptr: arguments->NativeArgAt(index: 0));
119 return Smi::New(value: receiver.ComputeHash());
120}
121
122} // namespace dart
123

source code of flutter_engine/third_party/dart/runtime/lib/function.cc