2019-10-24 17:40:27 +00:00
|
|
|
// Copyright (c) 2019, 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 'package:ffi/ffi.dart';
|
|
|
|
import 'package:expect/expect.dart';
|
|
|
|
|
|
|
|
/// typedef struct {
|
|
|
|
/// unsigned int bold : 1;
|
|
|
|
/// unsigned int underline : 2;
|
|
|
|
/// unsigned int italic : 1;
|
|
|
|
/// unsigned int blink : 1;
|
|
|
|
/// unsigned int reverse : 1;
|
|
|
|
/// unsigned int strike : 1;
|
|
|
|
/// unsigned int font : 4;
|
|
|
|
/// } ScreenCellAttrs;
|
Reland "[vm/ffi] Add class modifiers"
This is a reland of commit 1755f8909264330a207a0e0e44d31f5fec9c489a
Can land after (or with) the Flutter PR:
https://github.com/flutter/engine/pull/40434
Original change's description:
> [vm/ffi] Add class modifiers
>
> Adds class modifiers to `dart:ffi`.
>
> Migrates all user-defined subclasses of `Struct`, `Union`, `Opaque`,
> and `AbiSpecificInteger` to be `final class`es.
>
> Does not remove the manual error checking, so some errors will show up
> twice now in language version 3.0. In language version <3.0, only the
> FFI-specific error will show up.
>
> In a follow-up CL, we will try to make the language-errors to show up
> also <3.0 so that we can remove the FFI-specific errors.
>
> Examples of duplicated errors:
> pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart
>
> TEST=pkg/analyzer/test/ (for the analyzer)
> TEST=pkg/front_end/testcases/ (for the CFE)
> TEST=test/ffi/ (for the VM)
>
> CoreLibraryReviewExempt: No need for dart2js to review.
> Bug: https://github.com/dart-lang/sdk/issues/51683
> Change-Id: I2964ceccb7db59fbdaf6be5319f5e4ec2dabe0f3
> Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-win-release-try,pkg-mac-release-try,vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,vm-reload-rollback-linux-debug-x64-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289223
> Reviewed-by: Johnni Winther <johnniwinther@google.com>
> Reviewed-by: Devon Carew <devoncarew@google.com>
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
> Reviewed-by: Jackson Gardner <jacksongardner@google.com>
> Reviewed-by: Lasse Nielsen <lrn@google.com>
> Commit-Queue: Daco Harkes <dacoharkes@google.com>
TEST=pkg/analyzer/test/ (for the analyzer)
TEST=pkg/front_end/testcases/ (for the CFE)
TEST=test/ffi/ (for the VM)
CoreLibraryReviewExempt: No need for dart2js to review.
Bug: https://github.com/dart-lang/sdk/issues/51683
Change-Id: I2ee3f0ac31d4162068a2346a06320029b2263ee2
Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-win-release-try,pkg-mac-release-try,vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,vm-reload-rollback-linux-debug-x64-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289781
Reviewed-by: Devon Carew <devoncarew@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-03-21 15:25:10 +00:00
|
|
|
final class ScreenCellAttrs extends Struct {
|
2019-10-24 17:40:27 +00:00
|
|
|
@Int16()
|
[samples] Migrate ffi samples to NNBD
Closes: https://github.com/dart-lang/sdk/issues/43600.
TEST=samples/ffi
Change-Id: I620e6a73c8e37dd72f8f3921fa37dd4f8966e45c
Cq-Include-Trybots: luci.dart.try:vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-precomp-nnbd-linux-debug-x64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,dart-sdk-linux-try,analyzer-nnbd-linux-release-try,analyzer-linux-release-try,front-end-nnbd-linux-release-x64-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/173542
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2020-11-24 18:50:28 +00:00
|
|
|
external int bits;
|
2019-10-24 17:40:27 +00:00
|
|
|
|
|
|
|
int get bold => getBits(kBoldFieldOffset, kBoldFieldLength);
|
|
|
|
void set bold(int value) =>
|
|
|
|
setBits(kBoldFieldOffset, kBoldFieldLength, value);
|
|
|
|
|
|
|
|
int get underline => getBits(kUnderlineFieldOffset, kUnderlineFieldLength);
|
|
|
|
void set underline(int value) =>
|
|
|
|
setBits(kUnderlineFieldOffset, kUnderlineFieldLength, value);
|
|
|
|
|
|
|
|
int get italic => getBits(kItalicFieldOffset, kItalicFieldLength);
|
|
|
|
void set italic(int value) =>
|
|
|
|
setBits(kItalicFieldOffset, kItalicFieldLength, value);
|
|
|
|
|
|
|
|
int get blink => getBits(kBlinkFieldOffset, kBlinkFieldLength);
|
|
|
|
void set blink(int value) =>
|
|
|
|
setBits(kBlinkFieldOffset, kBlinkFieldLength, value);
|
|
|
|
|
|
|
|
int get reverse => getBits(kReverseFieldOffset, kReverseFieldLength);
|
|
|
|
void set reverse(int value) =>
|
|
|
|
setBits(kReverseFieldOffset, kReverseFieldLength, value);
|
|
|
|
|
|
|
|
int get strike => getBits(kStrikeFieldOffset, kStrikeFieldLength);
|
|
|
|
void set strike(int value) =>
|
|
|
|
setBits(kStrikeFieldOffset, kStrikeFieldLength, value);
|
|
|
|
|
|
|
|
int get font => getBits(kFontFieldOffset, kFontFieldLength);
|
|
|
|
void set font(int value) =>
|
|
|
|
setBits(kFontFieldOffset, kFontFieldLength, value);
|
|
|
|
|
|
|
|
int getBits(int offset, int length) => bits.getBits(offset, length);
|
|
|
|
|
|
|
|
void setBits(int offset, int length, int value) {
|
|
|
|
bits = bits.setBits(offset, length, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int kBoldFieldOffset = 0;
|
|
|
|
const int kBoldFieldLength = 1;
|
|
|
|
const int kUnderlineFieldOffset = kBoldFieldOffset + kBoldFieldLength;
|
|
|
|
const int kUnderlineFieldLength = 2;
|
|
|
|
const int kItalicFieldOffset = kUnderlineFieldOffset + kUnderlineFieldLength;
|
|
|
|
const int kItalicFieldLength = 1;
|
|
|
|
const int kBlinkFieldOffset = kItalicFieldOffset + kItalicFieldLength;
|
|
|
|
const int kBlinkFieldLength = 1;
|
|
|
|
const int kReverseFieldOffset = kBlinkFieldOffset + kBlinkFieldLength;
|
|
|
|
const int kReverseFieldLength = 1;
|
|
|
|
const int kStrikeFieldOffset = kReverseFieldOffset + kReverseFieldLength;
|
|
|
|
const int kStrikeFieldLength = 1;
|
|
|
|
const int kFontFieldOffset = kStrikeFieldOffset + kStrikeFieldLength;
|
|
|
|
const int kFontFieldLength = 4;
|
|
|
|
|
|
|
|
/// Extension to use a 64-bit integer as bit field.
|
|
|
|
extension IntBitField on int {
|
2023-01-09 17:44:29 +00:00
|
|
|
static int _bitMask(int offset, int length) => ((1 << length) - 1) << offset;
|
2019-10-24 17:40:27 +00:00
|
|
|
|
|
|
|
/// Read `length` bits at `offset`.
|
|
|
|
///
|
|
|
|
/// Truncates everything.
|
|
|
|
int getBits(int offset, int length) {
|
|
|
|
final mask = _bitMask(offset, length);
|
|
|
|
return (this & mask) >> offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new integer value in which `length` bits are overwritten at
|
|
|
|
/// `offset`.
|
|
|
|
///
|
|
|
|
/// Truncates everything.
|
|
|
|
int setBits(int offset, int length, int value) {
|
|
|
|
final mask = _bitMask(offset, length);
|
|
|
|
return (this & ~mask) | ((value << offset) & mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2021-01-14 13:57:03 +00:00
|
|
|
final p = calloc<ScreenCellAttrs>(3);
|
2019-10-24 17:40:27 +00:00
|
|
|
|
|
|
|
// Zeroes out all fields.
|
|
|
|
p.ref.bits = 0;
|
|
|
|
|
|
|
|
// Set individual fields.
|
|
|
|
p.ref.blink = 0;
|
|
|
|
p.ref.bold = 1;
|
|
|
|
p.ref.font = 15;
|
|
|
|
p.ref.italic = 1;
|
|
|
|
p.ref.reverse = 0;
|
|
|
|
p.ref.strike = 0;
|
|
|
|
p.ref.underline = 2;
|
|
|
|
|
|
|
|
// Read individual fields.
|
|
|
|
print(p.ref.blink);
|
|
|
|
print(p.ref.bold);
|
|
|
|
print(p.ref.font);
|
|
|
|
print(p.ref.italic);
|
|
|
|
print(p.ref.reverse);
|
|
|
|
print(p.ref.strike);
|
|
|
|
print(p.ref.underline);
|
|
|
|
|
|
|
|
// A check for automated testing.
|
|
|
|
Expect.equals(1933, p.ref.bits);
|
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
2019-10-24 17:40:27 +00:00
|
|
|
}
|