Skip to content

Upgrade to Dart 2.6 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.0-dev.2

- A few more updates to new FFI API

## 0.4.0-dev.1

- Update to new FFI API
Expand Down
32 changes: 6 additions & 26 deletions example/ffi/ffi-win32.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart' as ffi;

const STD_INPUT_HANDLE = -10;
const STD_OUTPUT_HANDLE = -11;
Expand Down Expand Up @@ -30,7 +31,7 @@ typedef getConsoleScreenBufferInfoDart = int Function(int hConsoleOutput,
// SMALL_RECT srWindow;
// COORD dwMaximumWindowSize;
// } CONSOLE_SCREEN_BUFFER_INFO;
class CONSOLE_SCREEN_BUFFER_INFO extends Struct<CONSOLE_SCREEN_BUFFER_INFO> {
class CONSOLE_SCREEN_BUFFER_INFO extends Struct {
@Int16()
int dwSizeX;

Expand Down Expand Up @@ -58,33 +59,13 @@ class CONSOLE_SCREEN_BUFFER_INFO extends Struct<CONSOLE_SCREEN_BUFFER_INFO> {
int dwMaximumWindowSizeX;
@Int16()
int dwMaximumWindowSizeY;

factory CONSOLE_SCREEN_BUFFER_INFO.allocate(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you delete the factory on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't look like they were being used, and I was trying to be consistent with the previous changes in https://github.com/timsneath/dart_console/compare/new-ffi-model?expand=1. @timsneath ?

COORD dwSize,
COORD dwCursorPosition,
int wAttributes,
SMALL_RECT srWindow,
COORD dwMaximumWindowSize) =>
Pointer<CONSOLE_SCREEN_BUFFER_INFO>.allocate()
.load<CONSOLE_SCREEN_BUFFER_INFO>()
..dwSizeX = dwSize.X
..dwSizeY = dwSize.Y
..dwCursorPositionX = dwCursorPosition.X
..dwCursorPositionY = dwCursorPosition.Y
..wAttributes = wAttributes
..srWindowLeft = srWindow.Left
..srWindowTop = srWindow.Top
..srWindowRight = srWindow.Right
..srWindowBottom = srWindow.Bottom
..dwMaximumWindowSizeX = dwMaximumWindowSize.X
..dwMaximumWindowSizeY = dwMaximumWindowSize.Y;
}

// typedef struct _COORD {
// SHORT X;
// SHORT Y;
// } COORD, *PCOORD;
class COORD extends Struct<COORD> {
class COORD extends Struct {
@Int16()
int X;

Expand All @@ -98,7 +79,7 @@ class COORD extends Struct<COORD> {
// SHORT Right;
// SHORT Bottom;
// } SMALL_RECT;
class SMALL_RECT extends Struct<SMALL_RECT> {
class SMALL_RECT extends Struct {
@Int16()
int Left;

Expand All @@ -124,9 +105,8 @@ main() {
final outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
print(outputHandle);

Pointer<CONSOLE_SCREEN_BUFFER_INFO> pBufferInfo =
Pointer<CONSOLE_SCREEN_BUFFER_INFO>.allocate();
CONSOLE_SCREEN_BUFFER_INFO bufferInfo = pBufferInfo.load();
Pointer<CONSOLE_SCREEN_BUFFER_INFO> pBufferInfo = ffi.allocate();
CONSOLE_SCREEN_BUFFER_INFO bufferInfo = pBufferInfo.ref;
GetConsoleScreenBufferInfo(outputHandle, pBufferInfo);
print(bufferInfo.dwMaximumWindowSizeX);
print(bufferInfo.dwMaximumWindowSizeY);
Expand Down
19 changes: 6 additions & 13 deletions example/ffi/ffi_ioctl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart' as ffi;

// int ioctl(int, unsigned long, ...);
typedef ioctlVoidNative = Int32 Function(Int32, Int64, Pointer<Void>);
Expand All @@ -16,7 +17,7 @@ const STDERR_FILENO = 2;
// unsigned short ws_xpixel; /* horizontal size, pixels */
// unsigned short ws_ypixel; /* vertical size, pixels */
// };
class WinSize extends Struct<WinSize> {
class WinSize extends Struct {
@Int16()
int ws_row;

Expand All @@ -28,30 +29,22 @@ class WinSize extends Struct<WinSize> {

@Int16()
int ws_ypixel;

factory WinSize.allocate(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

int ws_row, int ws_col, int ws_xpixel, int ws_ypixel) =>
Pointer<WinSize>.allocate().load<WinSize>()
..ws_row = ws_row
..ws_col = ws_col
..ws_xpixel = ws_xpixel
..ws_ypixel = ws_ypixel;
}

main() {
final DynamicLibrary libc = Platform.isMacOS
? DynamicLibrary.open('libSystem.dylib')
? DynamicLibrary.open("/usr/lib/libSystem.dylib")
: DynamicLibrary.open("libc-2.28.so");

final ioctl = libc.lookupFunction<ioctlVoidNative, ioctlVoidDart>("ioctl");

Pointer<WinSize> winSizePointer = Pointer<WinSize>.allocate();
Pointer<WinSize> winSizePointer = ffi.allocate();
final result = ioctl(STDOUT_FILENO, TIOCGWINSZ, winSizePointer.cast());
print('result is $result');

final winSize = winSizePointer.load<WinSize>();
final winSize = winSizePointer.ref;
print('Per ioctl, this console window has ${winSize.ws_col} cols and '
'${winSize.ws_row} rows.');

winSizePointer.free();
ffi.free(winSizePointer);
}
17 changes: 9 additions & 8 deletions example/ffi/ffi_rawmode.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart' as ffi;

// int tcgetattr(int, struct termios *);
typedef tcgetattrNative = Int32 Function(
Expand Down Expand Up @@ -86,7 +87,7 @@ const int VTIME = 17; // time in 1/10s before returning
// speed_t c_ispeed; /* input speed */
// speed_t c_ospeed; /* output speed */
// };
class TermIOS extends Struct<TermIOS> {
class TermIOS extends Struct {
@Int64()
int c_iflag;
@Int64()
Expand Down Expand Up @@ -145,25 +146,25 @@ class TermIOS extends Struct<TermIOS> {

main() {
final DynamicLibrary libc = Platform.isMacOS
? DynamicLibrary.open('libSystem.dylib')
? DynamicLibrary.open("/usr/lib/libSystem.dylib")
: DynamicLibrary.open("libc-2.28.so");

final tcgetattr =
libc.lookupFunction<tcgetattrNative, tcgetattrDart>("tcgetattr");
final tcsetattr =
libc.lookupFunction<tcsetattrNative, tcsetattrDart>("tcsetattr");

Pointer<TermIOS> origTermIOSPointer = Pointer<TermIOS>.allocate();
Pointer<TermIOS> origTermIOSPointer = ffi.allocate();
var result = tcgetattr(STDIN_FILENO, origTermIOSPointer);
print('result is $result');

TermIOS origTermIOS = origTermIOSPointer.load();
TermIOS origTermIOS = origTermIOSPointer.ref;

print('origTermIOS.c_iflag: 0b${origTermIOS.c_iflag.toRadixString(2)}');
print('Copying and modifying...');

Pointer<TermIOS> newTermIOSPointer = Pointer<TermIOS>.allocate();
TermIOS newTermIOS = newTermIOSPointer.load();
Pointer<TermIOS> newTermIOSPointer = ffi.allocate();
TermIOS newTermIOS = newTermIOSPointer.ref;

newTermIOS.c_iflag =
origTermIOS.c_iflag & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
Expand Down Expand Up @@ -211,6 +212,6 @@ main() {

print('\nORIGINAL MODE: Here is some text.\nHere is some more text.');

origTermIOSPointer.free();
newTermIOSPointer.free();
ffi.free(origTermIOSPointer);
ffi.free(newTermIOSPointer);
}
6 changes: 0 additions & 6 deletions example/pubspec.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion lib/src/ffi/unix/termlib-unix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class TermLibUnix implements TermLib {

TermLibUnix() {
_stdlib = Platform.isMacOS
? DynamicLibrary.open('libSystem.dylib')
? DynamicLibrary.open("/usr/lib/libSystem.dylib")
: DynamicLibrary.open("libc.so.6");

ioctl = _stdlib.lookupFunction<ioctlNative, ioctlDart>("ioctl");
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name: dart_console
description:
A helper library for command-line applications that need more control over input/output than the standard library provides.

version: 0.4.0-dev.1
version: 0.4.0-dev.2
homepage: https://github.com/timsneath/dart_console
author: Tim Sneath <[email protected]>

environment:
sdk: '>=2.6.0-dev.6.0 <3.0.0'
sdk: '>=2.6.0-dev.8.2 <3.0.0'

dependencies:
ffi: ^0.1.3-dev.3
Expand Down