// 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 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> callbackPointer; switch (arguments.single) { case "1": callbackPointer = Pointer.fromFunction( simpleAddition, ); case "2": callbackPointer = NativeCallable.isolateLocal( simpleAddition, ).nativeFunction; case "3": callbackPointer = NativeCallable.listener( simpleAddition, ).nativeFunction; default: throw "Unknown"; } final function = callbackPointer.asFunction( isLeaf: true, ); function(3, 4); print('We should have crashed by now.'); }