dart-sdk/tests/ffi/vmspecific_handle_dynamically_linked_test.dart
blagoev 50e18b8160 [vm/ffi] Add Dart_IsNull in dart_api_dl.h
TEST=tests/ffi/vmspecific_handle_dynamically_linked_test.dart

Closes: https://github.com/dart-lang/sdk/pull/50466
Closes: https://github.com/dart-lang/sdk/issues/48331

GitOrigin-RevId: ce49a64788310a755cd9bb5c7ff6d281830bcc33
Change-Id: Ia8e23c57c76767d9e3db799b8fe6b172071582a2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/269742
Reviewed-by: Tess Strickland <sstrickl@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
2022-11-15 10:32:16 +00:00

54 lines
1.5 KiB
Dart

// Copyright (c) 2020, 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.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import 'package:expect/expect.dart';
import 'dylib_utils.dart';
void main() {
doDynamicLinking();
testHandle();
testNativeAPIs();
}
void doDynamicLinking() {
Expect.isTrue(NativeApi.majorVersion == 2);
Expect.isTrue(NativeApi.minorVersion >= 1);
final initializeApi = testLibrary.lookupFunction<
IntPtr Function(Pointer<Void>),
int Function(Pointer<Void>)>("InitDartApiDL");
Expect.isTrue(initializeApi(NativeApi.initializeApiDLData) == 0);
}
void testHandle() {
final s = SomeClass(123);
print("passObjectToC($s)");
final result = passObjectToC(s);
print("result = $result");
Expect.isTrue(identical(s, result));
}
void testNativeAPIs() {
// No need to expect here, `lookupFunction` throws an argument error if lookup fails.
testLibrary.lookupFunction<
Bool Function(Handle),
bool Function(Object)>("Dart_IsNull_DL");
}
class SomeClass {
// We use this getter in the native api, don't tree shake it.
@pragma("vm:entry-point")
final int a;
SomeClass(this.a);
}
final testLibrary = dlopenPlatformSpecific("ffi_test_functions");
final passObjectToC = testLibrary.lookupFunction<Handle Function(Handle),
Object Function(Object)>("PassObjectToCUseDynamicLinking");