546ddc645e
Change-Id: Iaa29e2df68875fbb363ab2ac5cd59cc99e65532b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422102 Auto-Submit: Daco Harkes <dacoharkes@google.com> Commit-Queue: Bob Nystrom <rnystrom@google.com> Reviewed-by: Bob Nystrom <rnystrom@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com>
59 lines
1.7 KiB
Dart
59 lines
1.7 KiB
Dart
// Copyright (c) 2025, 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.
|
|
|
|
import 'dart:ffi';
|
|
import 'dart:io';
|
|
|
|
import 'dylib_utils.dart';
|
|
|
|
final ffiTestFunctions = dlopenPlatformSpecific('ffi_test_functions');
|
|
|
|
typedef SimpleAdditionType = Void Function(Int32, Int32);
|
|
|
|
void simpleAddition(int x, int y) {
|
|
print("simpleAddition($x, $y)");
|
|
}
|
|
|
|
void main(List<String> arguments) async {
|
|
if (arguments.isEmpty) {
|
|
for (int i in [1, 2, 3]) {
|
|
final result = Process.runSync(Platform.resolvedExecutable, [
|
|
Platform.script.toFilePath(),
|
|
'$i',
|
|
]);
|
|
if (result.exitCode == 0) {
|
|
throw 'Expected non-0 exit code: ${result.exitCode}';
|
|
}
|
|
if (!result.stderr.contains(
|
|
'Cannot invoke native callback from a leaf call.',
|
|
)) {
|
|
throw "Expected stderr to contain 'Cannot invoke native callback from a leaf call.': ${result.stderr}";
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
final Pointer<NativeFunction<SimpleAdditionType>> callbackPointer;
|
|
switch (arguments.single) {
|
|
case "1":
|
|
callbackPointer = Pointer.fromFunction<SimpleAdditionType>(
|
|
simpleAddition,
|
|
);
|
|
case "2":
|
|
callbackPointer = NativeCallable<SimpleAdditionType>.isolateLocal(
|
|
simpleAddition,
|
|
).nativeFunction;
|
|
case "3":
|
|
callbackPointer = NativeCallable<SimpleAdditionType>.listener(
|
|
simpleAddition,
|
|
).nativeFunction;
|
|
default:
|
|
throw "Unknown";
|
|
}
|
|
final function = callbackPointer.asFunction<void Function(int, int)>(
|
|
isLeaf: true,
|
|
);
|
|
function(3, 4);
|
|
print('We should have crashed by now.');
|
|
}
|