2012-06-12 05:22:16 +00:00
|
|
|
// 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.
|
Rereland "[vm/ffi] Disallow `Pointer`s 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
|
|
|
//
|
|
|
|
// VMOptions=--enable-ffi=true
|
|
|
|
// VMOptions=--enable-ffi=false
|
2012-06-12 05:22:16 +00:00
|
|
|
|
2021-04-26 17:58:57 +00:00
|
|
|
// @dart = 2.9
|
|
|
|
|
2013-04-05 19:43:16 +00:00
|
|
|
import "package:expect/expect.dart";
|
|
|
|
|
2012-09-13 21:26:01 +00:00
|
|
|
class ExpandoTest {
|
|
|
|
static Expando<int> visits;
|
|
|
|
|
|
|
|
static testMain() {
|
|
|
|
visits = new Expando<int>('visits');
|
2017-04-17 21:52:57 +00:00
|
|
|
var legal = [
|
|
|
|
new Object(),
|
2022-12-15 11:36:22 +00:00
|
|
|
[],
|
2017-04-17 21:52:57 +00:00
|
|
|
[1, 2, 3],
|
|
|
|
const [1, 2, 3],
|
|
|
|
new Map(),
|
|
|
|
{'x': 1, 'y': 2},
|
|
|
|
const {'x': 1, 'y': 2},
|
|
|
|
new Expando(),
|
|
|
|
new Expando('horse')
|
|
|
|
];
|
2012-09-13 21:26:01 +00:00
|
|
|
for (var object in legal) {
|
|
|
|
testNamedExpando(object);
|
|
|
|
testUnnamedExpando(object);
|
|
|
|
}
|
|
|
|
for (var object in legal) {
|
2016-01-25 11:06:06 +00:00
|
|
|
Expect.equals(2, visits[object], "$object");
|
2012-09-13 21:26:01 +00:00
|
|
|
}
|
|
|
|
testIllegal();
|
2013-09-25 10:47:35 +00:00
|
|
|
testIdentity();
|
2012-06-12 05:22:16 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 21:26:01 +00:00
|
|
|
static visit(object) {
|
|
|
|
int count = visits[object];
|
2012-11-12 17:19:58 +00:00
|
|
|
count = (count == null) ? 1 : count + 1;
|
2012-09-13 21:26:01 +00:00
|
|
|
visits[object] = count;
|
|
|
|
}
|
2012-06-12 05:22:16 +00:00
|
|
|
|
2012-09-13 21:26:01 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-06-12 05:22:16 +00:00
|
|
|
|
2012-09-13 21:26:01 +00:00
|
|
|
static testUnnamedExpando(object) {
|
|
|
|
Expando<int> expando = new Expando<int>();
|
|
|
|
Expect.isNull(expando.name);
|
|
|
|
Expect.isTrue(expando.toString().startsWith('Expando:'));
|
|
|
|
testExpando(expando, object);
|
|
|
|
}
|
2012-06-12 05:22:16 +00:00
|
|
|
|
2012-09-13 21:26:01 +00:00
|
|
|
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]);
|
|
|
|
}
|
2012-06-12 05:22:16 +00:00
|
|
|
|
2012-09-13 21:26:01 +00:00
|
|
|
static testIllegal() {
|
|
|
|
Expando<int> expando = new Expando<int>();
|
2017-10-17 23:37:15 +00:00
|
|
|
Expect.throwsArgumentError(() => expando[null], "null");
|
|
|
|
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");
|
2012-09-13 21:26:01 +00:00
|
|
|
}
|
2013-09-25 10:47:35 +00:00
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
2012-06-12 05:22:16 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 21:26:01 +00:00
|
|
|
main() => ExpandoTest.testMain();
|
2013-09-25 10:47:35 +00:00
|
|
|
|
|
|
|
class Mutable {
|
|
|
|
int id;
|
|
|
|
Mutable(this.id);
|
|
|
|
int get hashCode => id;
|
2017-04-17 21:52:57 +00:00
|
|
|
bool operator ==(other) => other is Mutable && other.id == id;
|
2013-09-25 10:47:35 +00:00
|
|
|
}
|