mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 08:24:39 +00:00
![Daco Harkes](/assets/img/avatar_default.png)
This is a reland of commit 1755f89092
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>
124 lines
3.8 KiB
Dart
124 lines
3.8 KiB
Dart
// 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;
|
|
final class ScreenCellAttrs extends Struct {
|
|
@Int16()
|
|
external int bits;
|
|
|
|
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 {
|
|
static int _bitMask(int offset, int length) => ((1 << length) - 1) << offset;
|
|
|
|
/// 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() {
|
|
final p = calloc<ScreenCellAttrs>(3);
|
|
|
|
// 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);
|
|
|
|
calloc.free(p);
|
|
}
|