Flutter Engine
The Flutter Engine
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:10829
static const Bool & Get(bool value)
Definition: object.h:10801
ContextPtr GetContext() const
Definition: object.h:12360
TypeArgumentsPtr instantiator_type_arguments() const
Definition: object.h:12320
TypeArgumentsPtr delayed_type_arguments() const
Definition: object.h:12340
TypeArgumentsPtr function_type_arguments() const
Definition: object.h:12330
InstancePtr GetImplicitClosureReceiver() const
Definition: object.h:12365
FunctionPtr function() const
Definition: object.h:12350
uword ComputeHash() const
Definition: object.cc:25909
static ObjectPtr InvokeClosure(Thread *thread, const Array &arguments)
Definition: dart_entry.cc:282
static DART_NORETURN void PropagateError(const Error &error)
Definition: exceptions.cc:1003
ObjectPtr ptr() const
Definition: object.h:332
static Object & Handle()
Definition: object.h:407
static SmiPtr New(intptr_t value)
Definition: object.h:10006
#define ASSERT(E)
GAsyncResult * result
Definition: dart_vm.cc:33
static bool ClosureEqualsHelper(Zone *zone, const Closure &receiver, const Object &other)
Definition: function.cc:33
DEFINE_NATIVE_ENTRY(List_allocate, 0, 2)
Definition: array.cc:13
#define GET_NATIVE_ARGUMENT(type, name, value)
Definition: native_entry.h:84