Files
sdk/runtime/lib/function.cc
T
Tess Strickland 0920ac883c [vm] Ensure only generic closures have empty delayed type arguments.
Closures created via Closure::New() set the delayed type arguments to
the null type argument vector for non-generic closure functions and to
the empty type argument vector for generic closure functions.  However,
closures created via the method extractor stub would always have the
empty type argument vector for delayed type arguments.  Make the two
consistent by creating two method extractor stubs, one for generic
extracted methods and one for non-generic ones.

With this change, Closure::IsGeneric simply compares the delayed type
arguments against the empty type argument vector. This change also makes
method extraction for non-generic functions slightly faster, since the
delayed type arguments field is not changed post-allocation.

The old version of Closure::IsGeneric was the only use of
Closure::NumTypeParameters, so remove the latter method.

Related cleanups:

* Use similar checks for Class::IsGeneric and FunctionType::IsGeneric,
  since they only have non-null type_parameters fields when generic.
  For Class::IsGeneric, this avoids having to fetch the current thread
  if the class declaration has been loaded.

* Refactor methods that read packed fields in FunctionType into two
  methods, a static method that takes a FunctionTypePtr and an instance
  method that calls the static method. Replace the methods in Function
  that also directly read FunctionType packed fields with delegations to
  the static methods.

* Rework Closure_equals to avoid unneeded handle allocation in certain
  cases for non-equal closures.

* Replace cases that allocates a handle for the closure function only
  for retrieving the signature with calls to Closure::signature().

TEST=language_2/closure/tearoff_dynamic_test
     language_2/regress/regress45890_test

Cq-Include-Trybots: luci.dart.try:vm-kernel-linux-debug-x64-try,vm-kernel-linux-debug-x64c-try,vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64c-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-nnbd-linux-release-simarm-try,vm-kernel-nnbd-linux-release-simarm64-try,vm-kernel-precomp-linux-release-simarm-try,vm-kernel-precomp-linux-release-simarm64-try,vm-kernel-precomp-nnbd-linux-release-simarm64-try,vm-kernel-linux-debug-simarm64c-try,vm-kernel-precomp-linux-debug-simarm_x64-try,vm-kernel-precomp-nnbd-linux-debug-simarm_x64-try
Change-Id: I29c8859c3350ed7b3f1a8f71d82a4393496b7c2b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/206820
Commit-Queue: Tess Strickland <sstrickl@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
2021-07-15 22:51:57 +00:00

88 lines
3.2 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/bootstrap_natives.h"
#include "vm/compiler/jit/compiler.h"
#include "vm/dart_entry.h"
#include "vm/exceptions.h"
#include "vm/native_entry.h"
#include "vm/object.h"
#include "vm/symbols.h"
namespace dart {
DEFINE_NATIVE_ENTRY(Function_apply, 0, 2) {
const int kTypeArgsLen = 0; // TODO(regis): Add support for generic function.
const Array& fun_arguments =
Array::CheckedHandle(zone, arguments->NativeArgAt(0));
const Array& fun_arg_names =
Array::CheckedHandle(zone, arguments->NativeArgAt(1));
const Array& fun_args_desc = Array::Handle(
zone, ArgumentsDescriptor::NewBoxed(kTypeArgsLen, fun_arguments.Length(),
fun_arg_names));
const Object& result = Object::Handle(
zone, DartEntry::InvokeClosure(thread, fun_arguments, fun_args_desc));
if (result.IsError()) {
Exceptions::PropagateError(Error::Cast(result));
}
return result.ptr();
}
static bool ClosureEqualsHelper(Zone* zone,
const Closure& receiver,
const Object& other) {
if (receiver.ptr() == other.ptr()) {
return true;
}
if (!other.IsClosure()) {
return false;
}
const auto& other_closure = Closure::Cast(other);
// Check that the delayed type argument vectors match.
if (receiver.delayed_type_arguments() !=
other_closure.delayed_type_arguments()) {
// Mismatches should only happen when a generic function is involved.
ASSERT(Function::Handle(receiver.function()).IsGeneric() ||
Function::Handle(other_closure.function()).IsGeneric());
return false;
}
// Closures that are not implicit instance closures are unique.
const auto& func_a = Function::Handle(zone, receiver.function());
if (!func_a.IsImplicitInstanceClosureFunction()) {
return false;
}
const auto& func_b = Function::Handle(zone, other_closure.function());
if (!func_b.IsImplicitInstanceClosureFunction()) {
return false;
}
// If the closure functions are not the same, check the function's name and
// owner, as multiple function objects could exist for the same function due
// to hot reload.
if (func_a.ptr() != func_b.ptr() &&
(func_a.name() != func_b.name() || func_a.Owner() != func_b.Owner())) {
return false;
}
// Check that the both receiver instances are the same.
const Context& context_a = Context::Handle(zone, receiver.context());
const Context& context_b = Context::Handle(zone, other_closure.context());
return context_a.At(0) == context_b.At(0);
}
DEFINE_NATIVE_ENTRY(Closure_equals, 0, 2) {
const Closure& receiver =
Closure::CheckedHandle(zone, arguments->NativeArgAt(0));
GET_NATIVE_ARGUMENT(Instance, other, arguments->NativeArgAt(1));
ASSERT(!other.IsNull());
return Bool::Get(ClosureEqualsHelper(zone, receiver, other)).ptr();
}
DEFINE_NATIVE_ENTRY(Closure_computeHash, 0, 1) {
const Closure& receiver =
Closure::CheckedHandle(zone, arguments->NativeArgAt(0));
return Smi::New(receiver.ComputeHash());
}
} // namespace dart