[go: nahoru, domu]

Skip to content

Commit

Permalink
Move to lints in package:lints (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
mit-mit committed Jun 2, 2021
1 parent b1e4524 commit 871a265
Show file tree
Hide file tree
Showing 31 changed files with 102 additions and 59 deletions.
2 changes: 1 addition & 1 deletion command_line/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:pedantic/analysis_options.yaml
include: package:lints/recommended.yaml
4 changes: 2 additions & 2 deletions command_line/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: >
publish_to: none

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=2.12.0 <3.0.0"

dependencies:
args: ^2.0.0
Expand All @@ -18,7 +18,7 @@ dependencies:
dev_dependencies:
build_cli: ^2.0.0
build_runner: ^1.0.0
pedantic: ^1.8.0
lints: ^1.0.0
test: ^1.6.0

executables:
Expand Down
2 changes: 1 addition & 1 deletion extension_methods/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:pedantic/analysis_options.yaml
include: package:lints/recommended.yaml
2 changes: 1 addition & 1 deletion extension_methods/example/core_type_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ extension Silliest on Object {
bool notAgain(Object other) => this != other;
}

extension ExtremelySilly on Null {
extension ExtremelySilly on Object? {
String stop(String x) => toString() + x;
}
4 changes: 2 additions & 2 deletions extension_methods/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ description: >
publish_to: none

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=2.12.0 <3.0.0"

dev_dependencies:
pedantic: ^1.8.0
lints: ^1.0.0
test: ^1.6.0
1 change: 1 addition & 0 deletions ffi/hello_world/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
11 changes: 7 additions & 4 deletions ffi/hello_world/hello.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@

import 'dart:ffi' as ffi;
import 'dart:io' show Platform, Directory;

import 'package:path/path.dart' as path;

// FFI signature of the hello_world C function
typedef hello_world_func = ffi.Void Function();
typedef HelloWorldFunc = ffi.Void Function();
// Dart type definition for calling the C foreign function
typedef HelloWorld = void Function();

main() {
// Open the dynamic library
var libraryPath =
path.join(Directory.current.path, 'hello_library', 'libhello.so');
if (Platform.isMacOS)
if (Platform.isMacOS) {
libraryPath =
path.join(Directory.current.path, 'hello_library', 'libhello.dylib');
if (Platform.isWindows)
}
if (Platform.isWindows) {
libraryPath = path.join(
Directory.current.path, 'hello_library', 'Debug', 'hello.dll');
}

final dylib = ffi.DynamicLibrary.open(libraryPath);

// Look up the C function 'hello_world'
final HelloWorld hello = dylib
.lookup<ffi.NativeFunction<hello_world_func>>('hello_world')
.lookup<ffi.NativeFunction<HelloWorldFunc>>('hello_world')
.asFunction();
// Call the function
hello();
Expand Down
3 changes: 2 additions & 1 deletion ffi/hello_world/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ description: >-
publish_to: none

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=2.12.0 <3.0.0"

dependencies:
path: ^1.7.0

dev_dependencies:
lints: ^1.0.0
test: ^1.16.0
test_utils:
path: ../test_utils
1 change: 1 addition & 0 deletions ffi/primitives/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
28 changes: 15 additions & 13 deletions ffi/primitives/primitives.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,60 @@

import 'dart:ffi';
import 'dart:io';
import 'package:path/path.dart' as path;

import 'package:ffi/ffi.dart';
import 'package:path/path.dart' as path;

// C sum function - int sum(int a, int b);
//
// Example of how to pass parameters into C and use the returned result
typedef sum_func = Int32 Function(Int32 a, Int32 b);
typedef SumFunc = Int32 Function(Int32 a, Int32 b);
typedef Sum = int Function(int a, int b);

// C subtract function - int subtract(int *a, int b);
//
// Example of how to create pointers in Dart, alloc them, and pass them as
// parameters
typedef subtract_func = Int32 Function(Pointer<Int32> a, Int32 b);
typedef SubtractFunc = Int32 Function(Pointer<Int32> a, Int32 b);
typedef Subtract = int Function(Pointer<Int32> a, int b);

// C multiply function - int *multiply(int a, int b);
//
// Example of how to receive pointers in Dart and access the data
typedef multiply_func = Pointer<Int32> Function(Int32 a, Int32 b);
typedef MultiplyFunc = Pointer<Int32> Function(Int32 a, Int32 b);
typedef Multiply = Pointer<Int32> Function(int a, int b);

// C multi sum function - int multi_sum(int nr_count, ...);
//
// Example of how to call C functions with varargs with a fixed arg count in
// Dart
typedef multi_sum_func = Int32 Function(
typedef MultiSumFunc = Int32 Function(
Int32 numCount, Int32 a, Int32 b, Int32 c);
typedef MultiSum = int Function(int numCount, int a, int b, int c);

// C free function - void free_pointer(int *int_pointer);
//
// Example of how to free pointers that were allocated in C.
typedef free_pointer_func = Void Function(Pointer<Int32> a);
typedef FreePointerFunc = Void Function(Pointer<Int32> a);
typedef FreePointer = void Function(Pointer<Int32> a);

main() {
// Open the dynamic library
var libraryPath = path.join(
Directory.current.path, 'primitives_library', 'libprimitives.so');
if (Platform.isMacOS)
if (Platform.isMacOS) {
libraryPath = path.join(
Directory.current.path, 'primitives_library', 'libprimitives.dylib');
if (Platform.isWindows)
}
if (Platform.isWindows) {
libraryPath = path.join(
Directory.current.path, 'primitives_library', 'Debug', 'primtives.dll');
}

final dylib = DynamicLibrary.open(libraryPath);

// calls int sum(int a, int b);
final sumPointer = dylib.lookup<NativeFunction<sum_func>>('sum');
final sumPointer = dylib.lookup<NativeFunction<SumFunc>>('sum');
final sum = sumPointer.asFunction<Sum>();
print('3 + 5 = ${sum(3, 5)}');

Expand All @@ -66,7 +68,7 @@ main() {
p.value = 3;

final subtractPointer =
dylib.lookup<NativeFunction<subtract_func>>('subtract');
dylib.lookup<NativeFunction<SubtractFunc>>('subtract');
final subtract = subtractPointer.asFunction<Subtract>();
print('3 - 5 = ${subtract(p, 5)}');

Expand All @@ -75,7 +77,7 @@ main() {

// calls int *multiply(int a, int b);
final multiplyPointer =
dylib.lookup<NativeFunction<multiply_func>>('multiply');
dylib.lookup<NativeFunction<MultiplyFunc>>('multiply');
final multiply = multiplyPointer.asFunction<Multiply>();
final resultPointer = multiply(3, 5);
// Fetch the result at the address pointed to
Expand All @@ -84,14 +86,14 @@ main() {

// Free up allocated memory. This time in C, because it was allocated in C.
final freePointerPointer =
dylib.lookup<NativeFunction<free_pointer_func>>('free_pointer');
dylib.lookup<NativeFunction<FreePointerFunc>>('free_pointer');
final freePointer = freePointerPointer.asFunction<FreePointer>();
freePointer(resultPointer);

// example calling a C function with varargs
// calls int multi_sum(int nr_count, ...);
final multiSumPointer =
dylib.lookup<NativeFunction<multi_sum_func>>('multi_sum');
dylib.lookup<NativeFunction<MultiSumFunc>>('multi_sum');
final multiSum = multiSumPointer.asFunction<MultiSum>();
print('3 + 7 + 11 = ${multiSum(3, 3, 7, 11)}');
}
1 change: 1 addition & 0 deletions ffi/primitives/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
path: ^1.8.0

dev_dependencies:
lints: ^1.0.0
test: ^1.16.0
test_utils:
path: ../test_utils
1 change: 1 addition & 0 deletions ffi/structs/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
3 changes: 2 additions & 1 deletion ffi/structs/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ description: >-
publish_to: none

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=2.12.0 <3.0.0"
dependencies:
ffi: ^1.0.0
path: ^1.8.0

dev_dependencies:
lints: ^1.0.0
test: ^1.16.0
test_utils:
path: ../test_utils
34 changes: 18 additions & 16 deletions ffi/structs/structs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// 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:io' show Directory, Platform;
import 'dart:ffi';
import 'package:path/path.dart' as path;
import 'dart:io' show Directory, Platform;

import 'package:ffi/ffi.dart';
import 'package:path/path.dart' as path;

// Example of handling a simple C struct
class Coordinate extends Struct {
Expand All @@ -26,25 +27,24 @@ class Place extends Struct {
// C function: char *hello_world();
// There's no need for two typedefs here, as both the
// C and Dart functions have the same signature
typedef hello_world = Pointer<Utf8> Function();
typedef HelloWorld = Pointer<Utf8> Function();

// C function: char *reverse(char *str, int length)
typedef reverse_native = Pointer<Utf8> Function(
Pointer<Utf8> str, Int32 length);
typedef ReverseNative = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length);
typedef Reverse = Pointer<Utf8> Function(Pointer<Utf8> str, int length);

// C function: void free_string(char *str)
typedef free_string_native = Void Function(Pointer<Utf8> str);
typedef FreeStringNative = Void Function(Pointer<Utf8> str);
typedef FreeString = void Function(Pointer<Utf8> str);

// C function: struct Coordinate create_coordinate(double latitude, double longitude)
typedef create_coordinate_native = Coordinate Function(
typedef CreateCoordinateNative = Coordinate Function(
Double latitude, Double longitude);
typedef CreateCoordinate = Coordinate Function(
double latitude, double longitude);

// C function: struct Place create_place(char *name, double latitude, double longitude)
typedef create_place_native = Place Function(
typedef CreatePlaceNative = Place Function(
Pointer<Utf8> name, Double latitude, Double longitude);
typedef CreatePlace = Place Function(
Pointer<Utf8> name, double latitude, double longitude);
Expand All @@ -53,20 +53,22 @@ main() {
// Open the dynamic library
var libraryPath =
path.join(Directory.current.path, 'structs_library', 'libstructs.so');
if (Platform.isMacOS)
if (Platform.isMacOS) {
libraryPath = path.join(
Directory.current.path, 'structs_library', 'libstructs.dylib');
if (Platform.isWindows)
}
if (Platform.isWindows) {
libraryPath = path.join(
Directory.current.path, 'structs_library', 'Debug', 'structs.dll');
}
final dylib = DynamicLibrary.open(libraryPath);

final helloWorld =
dylib.lookupFunction<hello_world, hello_world>('hello_world');
dylib.lookupFunction<HelloWorld, HelloWorld>('hello_world');
final message = helloWorld().toDartString();
print('$message');
print(message);

final reverse = dylib.lookupFunction<reverse_native, Reverse>('reverse');
final reverse = dylib.lookupFunction<ReverseNative, Reverse>('reverse');
final backwards = 'backwards';
final backwardsUtf8 = backwards.toNativeUtf8();
final reversedMessageUtf8 = reverse(backwardsUtf8, backwards.length);
Expand All @@ -75,19 +77,19 @@ main() {
print('$backwards reversed is $reversedMessage');

final freeString =
dylib.lookupFunction<free_string_native, FreeString>('free_string');
dylib.lookupFunction<FreeStringNative, FreeString>('free_string');
freeString(reversedMessageUtf8);

final createCoordinate =
dylib.lookupFunction<create_coordinate_native, CreateCoordinate>(
dylib.lookupFunction<CreateCoordinateNative, CreateCoordinate>(
'create_coordinate');
final coordinate = createCoordinate(3.5, 4.6);
print(
'Coordinate is lat ${coordinate.latitude}, long ${coordinate.longitude}');

final myHomeUtf8 = 'My Home'.toNativeUtf8();
final createPlace =
dylib.lookupFunction<create_place_native, CreatePlace>('create_place');
dylib.lookupFunction<CreatePlaceNative, CreatePlace>('create_place');
final place = createPlace(myHomeUtf8, 42.0, 24.0);
calloc.free(myHomeUtf8);
final name = place.name.toDartString();
Expand Down
5 changes: 5 additions & 0 deletions ffi/system-command/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include: package:lints/recommended.yaml

linter:
rules:
constant_identifier_names: false
4 changes: 2 additions & 2 deletions ffi/system-command/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ author: Michael Thomsen <mit@google.com>
publish_to: none

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=2.12.0 <3.0.0"

dependencies:
ffi: ^1.0.0

dev_dependencies:
lints: ^1.0.0
test: ^1.16.0

9 changes: 5 additions & 4 deletions ffi/system-command/win32ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';

import 'package:ffi/ffi.dart';

main() {
MessageBox('こんにちは窓', 'Hello Windows');
messageBox('こんにちは窓', 'Hello Windows');
}

/* MessageBoxW is the UTF16 (wchar_t) version of MessageBox.
Expand Down Expand Up @@ -48,12 +49,12 @@ const MB_ICONSTOP = 0x00000010;
const MB_ICONERROR = 0x00000010;
const MB_ICONHAND = 0x00000010;

int MessageBox(String message, String caption) {
int messageBox(String message, String caption) {
// Load user32.
final user32 = DynamicLibrary.open('user32.dll');

// Look up the `MessageBoxW` function.
final MessageBoxP =
final messageBoxP =
user32.lookupFunction<MessageBoxC, MessageBoxDart>('MessageBoxW');

// Allocate pointers to Utf16 arrays containing the command arguments.
Expand All @@ -62,7 +63,7 @@ int MessageBox(String message, String caption) {

// Invoke the command, and free the pointers.
final result =
MessageBoxP(nullptr, messageP, captionP, MB_OK | MB_ICONINFORMATION);
messageBoxP(nullptr, messageP, captionP, MB_OK | MB_ICONINFORMATION);
calloc.free(messageP);
calloc.free(captionP);

Expand Down
Loading

0 comments on commit 871a265

Please sign in to comment.