Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
function.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
6
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, 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));
26 zone, DartEntry::InvokeClosure(thread, fun_arguments, fun_args_desc));
27 if (result.IsError()) {
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(other);
43 const auto& func_a = Function::Handle(zone, receiver.function());
44 const auto& func_b = Function::Handle(zone, 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 =
67 const auto& type_args_b =
68 TypeArguments::Handle(zone, 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(type_args_b, TypeEquality::kSyntactical)) {
72 return false;
73 }
74 }
75 if (func_a.IsImplicitClosureFunction()) {
76 ASSERT(func_b.IsImplicitClosureFunction());
77 if (!func_a.is_static()) {
78 ASSERT(!func_b.is_static());
79 // Check that the both receiver instances are the same.
80 const Instance& receiver_a =
82 const Instance& receiver_b =
83 Instance::Handle(zone, other_closure.GetImplicitClosureReceiver());
84 return receiver_a.ptr() == receiver_b.ptr();
85 }
86 } else {
87 ASSERT(!func_b.IsImplicitClosureFunction());
88 if (func_a.IsGeneric()) {
89 // Additional constraints for closures of generic functions:
90 // (1) Different instantiations of the same generic closure
91 // with the same type arguments should be equal.
92 // This means that instantiated generic closures are not unique
93 // and equality of instantiated generic closures should not be
94 // based on identity.
95 // (2) Instantiations of non-equal generic closures should be non-equal.
96 // This means that equality of non-instantiated generic closures
97 // should not be based on identity too as it won't match equality
98 // after instantiation.
99 if ((receiver.GetContext() != other_closure.GetContext()) ||
100 (receiver.instantiator_type_arguments() !=
101 other_closure.instantiator_type_arguments()) ||
102 (receiver.function_type_arguments() !=
103 other_closure.function_type_arguments())) {
104 return false;
105 }
106 } else {
107 // Closures of non-generic functions are unique.
108 return false;
109 }
110 }
111 return true;
112}
113
114DEFINE_NATIVE_ENTRY(Closure_equals, 0, 2) {
115 const Closure& receiver =
116 Closure::CheckedHandle(zone, arguments->NativeArgAt(0));
117 GET_NATIVE_ARGUMENT(Instance, other, arguments->NativeArgAt(1));
118 ASSERT(!other.IsNull());
119 return Bool::Get(ClosureEqualsHelper(zone, receiver, other)).ptr();
120}
121
122DEFINE_NATIVE_ENTRY(Closure_computeHash, 0, 1) {
123 const Closure& receiver =
124 Closure::CheckedHandle(zone, arguments->NativeArgAt(0));
125 return Smi::New(receiver.ComputeHash());
126}
127
128} // namespace dart
static ArrayPtr NewBoxed(intptr_t type_args_len, intptr_t num_arguments, const Array &optional_arguments_names, Heap::Space space=Heap::kOld)
Definition dart_entry.h:83
intptr_t Length() const
Definition object.h:10808
static const Bool & Get(bool value)
Definition object.h:10780
ContextPtr GetContext() const
Definition object.h:12334
TypeArgumentsPtr instantiator_type_arguments() const
Definition object.h:12294
TypeArgumentsPtr delayed_type_arguments() const
Definition object.h:12314
TypeArgumentsPtr function_type_arguments() const
Definition object.h:12304
InstancePtr GetImplicitClosureReceiver() const
Definition object.h:12339
FunctionPtr function() const
Definition object.h:12324
uword ComputeHash() const
Definition object.cc:25988
static ObjectPtr InvokeClosure(Thread *thread, const Array &arguments)
static DART_NORETURN void PropagateError(const Error &error)
ObjectPtr ptr() const
Definition object.h:332
static Object & Handle()
Definition object.h:407
static SmiPtr New(intptr_t value)
Definition object.h:9985
#define ASSERT(E)
GAsyncResult * result
static bool ClosureEqualsHelper(Zone *zone, const Closure &receiver, const Object &other)
Definition function.cc:33
#define DEFINE_NATIVE_ENTRY(name, type_argument_count, argument_count)
#define GET_NATIVE_ARGUMENT(type, name, value)