-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sgrekhov
committed
Mar 1, 2021
1 parent
ba0d762
commit c3ffd4d
Showing
8 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion Pointer<T extends NativeType>.fromAddress(int ptr) | ||
* Construction from raw integer. | ||
* | ||
* @description Checks that this constructor constructs pointer from a raw | ||
* integer | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
import '../ffi_utils.dart'; | ||
import '../../../Utils/expect.dart'; | ||
|
||
void main() { | ||
Pointer<Int8> p1 = calloc<Int8>(); | ||
p1.value = 42; | ||
Pointer<Int8> p2 = new Pointer.fromAddress(p1.address); | ||
Expect.equals(42, p2.value); | ||
Expect.equals(p1.address, p2.address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion Pointer<T extends NativeType> | ||
* Represents a pointer into the native C memory. Cannot be extended. | ||
* | ||
* @description Checks that this class cannot be extended | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
|
||
abstract class C1<T extends NativeType> extends Pointer<T> { | ||
// ^^^^^^^^^^ | ||
// [cfe] unspecified | ||
// [analyzer] unspecified | ||
} | ||
|
||
abstract class C2 extends Pointer<Int8> { | ||
// ^^^^^^^^^^^^^ | ||
// [cfe] unspecified | ||
// [analyzer] unspecified | ||
} | ||
|
||
abstract class C3 implements Pointer<Int8> { | ||
// ^^^^^^^^^^^^^ | ||
// [cfe] unspecified | ||
// [analyzer] unspecified | ||
} | ||
|
||
void main() { | ||
C1? c1; | ||
C2? c2; | ||
C3? c3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion int address | ||
* Access to the raw pointer value. On 32-bit systems, the upper 32-bits of the | ||
* result are 0. | ||
* | ||
* @description Checks that this getter returns the raw pointer value | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
import '../ffi_utils.dart'; | ||
import '../../../Utils/expect.dart'; | ||
|
||
void main() { | ||
Pointer<Int8> p1 = calloc<Int8>(); | ||
try { | ||
Expect.isTrue(p1.address != 0); | ||
Pointer<Int8> p2 = calloc<Int8>(); | ||
Expect.notEquals(p1.address, p2.address); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion Pointer<T> elementAt(int index) | ||
* Pointer arithmetic (takes element size into account). | ||
* This method must be invoked with a compile-time constant T. | ||
* Does not accept dynamic invocations -- where the type of the receiver is | ||
* dynamic. | ||
* | ||
* @description Checks that this method implements pointer arithmetic | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
import '../ffi_utils.dart'; | ||
import '../../../Utils/expect.dart'; | ||
|
||
void main() { | ||
Pointer<Int16> p1 = calloc<Int16>(3); | ||
try { | ||
Expect.equals(2, p1.elementAt(1).address - p1.address); | ||
Expect.equals(2, p1.elementAt(2).address - p1.elementAt(1).address); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion Pointer<T> elementAt(int index) | ||
* Pointer arithmetic (takes element size into account). | ||
* This method must be invoked with a compile-time constant T. | ||
* Does not accept dynamic invocations -- where the type of the receiver is | ||
* dynamic. | ||
* | ||
* @description Checks that this method must be invoked with a compile-time | ||
* constant T | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
import '../ffi_utils.dart'; | ||
import '../../../Utils/expect.dart'; | ||
|
||
void main() { | ||
Pointer<Int64> p1 = calloc<Int64>(3); | ||
try { | ||
Pointer<Void> p2 = new Pointer.fromAddress(p1.address); | ||
Expect.throws(() { | ||
p2.elementAt(1); | ||
}); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion Pointer<T> elementAt(int index) | ||
* Pointer arithmetic (takes element size into account). | ||
* This method must be invoked with a compile-time constant T. | ||
* Does not accept dynamic invocations -- where the type of the receiver is | ||
* dynamic. | ||
* | ||
* @description Checks that this method does not accept dynamic invocations | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
import '../ffi_utils.dart'; | ||
|
||
void main() { | ||
Pointer p1 = calloc<Int64>(3); | ||
p1.elementAt(1); | ||
//^^^^^^^^^^^^^^^ | ||
// [cfe] unspecified | ||
// [analyzer] unspecified | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion Pointer<T> elementAt(int index) | ||
* Pointer arithmetic (takes element size into account). | ||
* This method must be invoked with a compile-time constant T. | ||
* Does not accept dynamic invocations -- where the type of the receiver is | ||
* dynamic. | ||
* | ||
* @description Checks negative argument value | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
import '../ffi_utils.dart'; | ||
import '../../../Utils/expect.dart'; | ||
|
||
void main() { | ||
Pointer<Int8> p1 = calloc<Int8>(3); | ||
try { | ||
p1.elementAt(0).value = 42; | ||
Pointer<Int8> p2 = new Pointer.fromAddress(p1.elementAt(1).address); | ||
Expect.equals(42, p2.elementAt(-1).value); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2021, 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. | ||
*/ | ||
/** | ||
* @assertion Pointer<T> elementAt(int index) | ||
* Pointer arithmetic (takes element size into account). | ||
* This method must be invoked with a compile-time constant T. | ||
* Does not accept dynamic invocations -- where the type of the receiver is | ||
* dynamic. | ||
* | ||
* @description Checks that it is allowed to read memory outside the border | ||
* @author sgrekhov@unipro.ru | ||
*/ | ||
import "dart:ffi"; | ||
import '../ffi_utils.dart'; | ||
|
||
void main() { | ||
Pointer<Int8> p1 = calloc<Int8>(3); | ||
try { | ||
p1.elementAt(42); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |