dart-sdk/tests/corelib/expando_test.dart
Daco Harkes 4558112105 Rereland "[vm/ffi] Disallow Pointers and structs in finalizers and expandos"
`Dart_NewWeakPersistentHandle` and `Dart_NewFinalizableHandle` in
`dart_api.h` do no longer accept `Pointer`s and subtypes of `Struct`s
as values passed in to the `object` parameter.

Expandos no longer accept `Pointer`s and subtypes of `Struct`s
as values passed in to the `object` parameter.

Cleans up unused object_store->ffi_struct_class.

Reland 1: Allow importing `dart:ffi` from `dart:core` with
          `--enable-ffi=false`.
Reland 2: Allow the new `_Compound` class to extend `NativeType`. (This
          got triggered in this CL on the build because of the import
          from `dart:core`.)

Closes: https://github.com/dart-lang/sdk/issues/45071
Breaking change: https://github.com/dart-lang/sdk/issues/45072

TEST=vm/cc/DartAPI_FinalizableHandleErrors
TEST=vm/cc/DartAPI_WeakPersistentHandleErrors
TEST=tests/ffi(_2)/expando_test.dart

Change-Id: I133278e16bd75cd2bb6234e7ddf042ffa0a54fd6
Cq-Include-Trybots: luci.dart.try:dart-sdk-linux-try,dart-sdk-mac-try,dart-sdk-win-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-mac-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-linux-debug-x64-try,vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-nnbd-linux-debug-ia32-try,vm-kernel-nnbd-mac-release-x64-try,vm-kernel-nnbd-win-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-linux-debug-simarm_x64-try,vm-kernel-precomp-nnbd-linux-debug-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-precomp-android-release-arm_x64-try,analyzer-analysis-server-linux-try,vm-kernel-precomp-linux-debug-x64c-try,vm-kernel-linux-debug-x64c-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195079
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2021-04-14 12:07:18 +00:00

110 lines
3 KiB
Dart

// 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.
//
// VMOptions=--enable-ffi=true
// VMOptions=--enable-ffi=false
import "package:expect/expect.dart";
class ExpandoTest {
static Expando<int> visits = Expando('visits');
static testMain() {
var legal = [
new Object(),
new List.filled(0, null),
[1, 2, 3],
const [1, 2, 3],
new Map(),
{'x': 1, 'y': 2},
const {'x': 1, 'y': 2},
new Expando(),
new Expando('horse')
];
for (var object in legal) {
testNamedExpando(object);
testUnnamedExpando(object);
}
for (var object in legal) {
Expect.equals(2, visits[object], "$object");
}
testIllegal();
testIdentity();
}
static visit(object) {
int? count = visits[object];
count = (count == null) ? 1 : count + 1;
visits[object] = count;
}
static testNamedExpando(object) {
Expando<int> expando = new Expando<int>('myexpando');
Expect.equals('myexpando', expando.name);
Expect.isTrue(expando.toString().startsWith('Expando:myexpando'));
testExpando(expando, object);
}
static testUnnamedExpando(object) {
Expando<int> expando = new Expando<int>();
Expect.isNull(expando.name);
Expect.isTrue(expando.toString().startsWith('Expando:'));
testExpando(expando, object);
}
static testExpando(Expando<int> expando, object) {
visit(object);
Expect.isNull(expando[object]);
expando[object] = 42;
Expect.equals(42, expando[object]);
expando[object] = null;
Expect.isNull(expando[object]);
Expando<int> alternative = new Expando('myexpando');
Expect.isNull(alternative[object]);
alternative[object] = 87;
Expect.isNull(expando[object]);
expando[object] = 99;
Expect.equals(99, expando[object]);
Expect.equals(87, alternative[object]);
}
static testIllegal() {
Expando<int> expando = new Expando<int>();
Expect.throwsArgumentError(() => expando['string'], "'string'");
Expect.throwsArgumentError(() => expando[42], "42");
Expect.throwsArgumentError(() => expando[42.87], "42.87");
Expect.throwsArgumentError(() => expando[true], "true");
Expect.throwsArgumentError(() => expando[false], "false");
}
static testIdentity() {
// Expando only depends on identity of object.
Expando<int> expando = new Expando<int>();
var m1 = new Mutable(1);
var m2 = new Mutable(7);
var m3 = new Mutable(13);
expando[m1] = 42;
Expect.equals(42, expando[m1]);
m1.id = 37;
Expect.equals(42, expando[m1]);
expando[m2] = 37;
expando[m3] = 10;
m3.id = 1;
Expect.equals(42, expando[m1]);
Expect.equals(37, expando[m2]);
Expect.equals(10, expando[m3]);
}
}
main() => ExpandoTest.testMain();
class Mutable {
int id;
Mutable(this.id);
int get hashCode => id;
bool operator ==(other) => other is Mutable && other.id == id;
}