/
ffi.dart
893 lines (762 loc) · 29.7 KB
/
ffi.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
// 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
/// Foreign Function Interface for interoperability with the C programming language.
///
/// For further details, please see: https://dart.dev/server/c-interop.
///
/// {@category VM}
@Since('2.6')
library dart.ffi;
import 'dart:_internal' show Since;
import 'dart:isolate';
import 'dart:typed_data';
part 'abi.dart';
part 'abi_specific.dart';
part 'native_type.dart';
part 'native_finalizer.dart';
part 'allocation.dart';
part 'annotations.dart';
part 'c_type.dart';
part 'dynamic_library.dart';
part 'struct.dart';
part 'union.dart';
/// Number of bytes used by native type T.
///
/// Includes padding and alignment of structs.
///
/// This function must be invoked with a compile-time constant [T].
external int sizeOf<T extends NativeType>();
/// Represents a pointer into the native C memory corresponding to 'NULL', e.g.
/// a pointer with address 0.
final Pointer<Never> nullptr = Pointer.fromAddress(0);
/// Represents a pointer into the native C memory. Cannot be extended.
@pragma('vm:entry-point')
@pragma("wasm:entry-point")
class Pointer<T extends NativeType> extends NativeType {
/// Construction from raw integer.
external factory Pointer.fromAddress(int ptr);
/// Convert Dart function to a C function pointer, automatically marshalling
/// the arguments and return value
///
/// If an exception is thrown while calling `f()`, the native function will
/// return `exceptionalReturn`, which must be assignable to return type of `f`.
///
/// The returned function address can only be invoked on the mutator (main)
/// thread of the current isolate. It will abort the process if invoked on any
/// other thread.
///
/// The pointer returned will remain alive for the duration of the current
/// isolate's lifetime. After the isolate it was created in is terminated,
/// invoking it from native code will cause undefined behavior.
///
/// Does not accept dynamic invocations -- where the type of the receiver is
/// [dynamic].
external static Pointer<NativeFunction<T>> fromFunction<T extends Function>(
@DartRepresentationOf('T') Function f,
[Object? exceptionalReturn]);
/// Access to the raw pointer value.
/// On 32-bit systems, the upper 32-bits of the result are 0.
external int get address;
/// 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].
external Pointer<T> elementAt(int index);
/// Cast Pointer<T> to a Pointer<V>.
external Pointer<U> cast<U extends NativeType>();
/// Equality for Pointers only depends on their address.
bool operator ==(Object other) {
if (other is! Pointer) return false;
Pointer otherPointer = other;
return address == otherPointer.address;
}
/// The hash code for a Pointer only depends on its address.
int get hashCode {
return address.hashCode;
}
}
/// A fixed-sized array of [T]s.
@Since('2.13')
class Array<T extends NativeType> extends NativeType {
/// Const constructor to specify [Array] dimensions in [Struct]s.
///
/// ```dart
/// class MyStruct extends Struct {
/// @Array(8)
/// external Array<Uint8> inlineArray;
///
/// @Array(2, 2, 2)
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
/// }
/// ```
///
/// Do not invoke in normal code.
const factory Array(int dimension1,
[int dimension2,
int dimension3,
int dimension4,
int dimension5]) = _ArraySize<T>;
/// Const constructor to specify [Array] dimensions in [Struct]s.
///
/// ```dart
/// class MyStruct extends Struct {
/// @Array.multi([2, 2, 2])
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
///
/// @Array.multi([2, 2, 2, 2, 2, 2, 2, 2])
/// external Array<Array<Array<Array<Array<Array<Array<Array<Uint8>>>>>>>> eightDimensionalInlineArray;
/// }
/// ```
///
/// Do not invoke in normal code.
const factory Array.multi(List<int> dimensions) = _ArraySize<T>.multi;
}
class _ArraySize<T extends NativeType> implements Array<T> {
final int? dimension1;
final int? dimension2;
final int? dimension3;
final int? dimension4;
final int? dimension5;
final List<int>? dimensions;
const _ArraySize(this.dimension1,
[this.dimension2, this.dimension3, this.dimension4, this.dimension5])
: dimensions = null;
const _ArraySize.multi(this.dimensions)
: dimension1 = null,
dimension2 = null,
dimension3 = null,
dimension4 = null,
dimension5 = null;
}
/// Extension on [Pointer] specialized for the type argument [NativeFunction].
extension NativeFunctionPointer<NF extends Function>
on Pointer<NativeFunction<NF>> {
/// Convert to Dart function, automatically marshalling the arguments
/// and return value.
external DF asFunction<@DartRepresentationOf('NF') DF extends Function>(
{bool isLeaf: false});
}
//
// The following code is generated, do not edit by hand.
//
// Code generated by `runtime/tools/ffi/sdk_lib_ffi_generator.dart`.
//
/// Extension on [Pointer] specialized for the type argument [Int8].
extension Int8Pointer on Pointer<Int8> {
/// The 8-bit two's complement integer at [address].
///
/// A Dart integer is truncated to 8 bits (as if by `.toSigned(8)`) before
/// being stored, and the 8-bit value is sign-extended when it is loaded.
external int get value;
external void set value(int value);
/// The 8-bit two's complement integer at `address + index`.
///
/// A Dart integer is truncated to 8 bits (as if by `.toSigned(8)`) before
/// being stored, and the 8-bit value is sign-extended when it is loaded.
external int operator [](int index);
/// The 8-bit two's complement integer at `address + index`.
///
/// A Dart integer is truncated to 8 bits (as if by `.toSigned(8)`) before
/// being stored, and the 8-bit value is sign-extended when it is loaded.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
external Int8List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Int16].
extension Int16Pointer on Pointer<Int16> {
/// The 16-bit two's complement integer at [address].
///
/// A Dart integer is truncated to 16 bits (as if by `.toSigned(16)`) before
/// being stored, and the 16-bit value is sign-extended when it is loaded.
///
/// The [address] must be 2-byte aligned.
external int get value;
external void set value(int value);
/// The 16-bit two's complement integer at `address + 2 * index`.
///
/// A Dart integer is truncated to 16 bits (as if by `.toSigned(16)`) before
/// being stored, and the 16-bit value is sign-extended when it is loaded.
///
/// The [address] must be 2-byte aligned.
external int operator [](int index);
/// The 16-bit two's complement integer at `address + 2 * index`.
///
/// A Dart integer is truncated to 16 bits (as if by `.toSigned(16)`) before
/// being stored, and the 16-bit value is sign-extended when it is loaded.
///
/// The [address] must be 2-byte aligned.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 2 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 2-byte aligned.
external Int16List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Int32].
extension Int32Pointer on Pointer<Int32> {
/// The 32-bit two's complement integer at [address].
///
/// A Dart integer is truncated to 32 bits (as if by `.toSigned(32)`) before
/// being stored, and the 32-bit value is sign-extended when it is loaded.
///
/// The [address] must be 4-byte aligned.
external int get value;
external void set value(int value);
/// The 32-bit two's complement integer at `address + 4 * index`.
///
/// A Dart integer is truncated to 32 bits (as if by `.toSigned(32)`) before
/// being stored, and the 32-bit value is sign-extended when it is loaded.
///
/// The [address] must be 4-byte aligned.
external int operator [](int index);
/// The 32-bit two's complement integer at `address + 4 * index`.
///
/// A Dart integer is truncated to 32 bits (as if by `.toSigned(32)`) before
/// being stored, and the 32-bit value is sign-extended when it is loaded.
///
/// The [address] must be 4-byte aligned.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 4 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 4-byte aligned.
external Int32List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Int64].
extension Int64Pointer on Pointer<Int64> {
/// The 64-bit two's complement integer at [address].
///
/// The [address] must be 8-byte aligned.
external int get value;
external void set value(int value);
/// The 64-bit two's complement integer at `address + 8 * index`.
///
/// The [address] must be 8-byte aligned.
external int operator [](int index);
/// The 64-bit two's complement integer at `address + 8 * index`.
///
/// The [address] must be 8-byte aligned.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 8 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 8-byte aligned.
external Int64List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Uint8].
extension Uint8Pointer on Pointer<Uint8> {
/// The 8-bit unsigned integer at [address].
///
/// A Dart integer is truncated to 8 bits (as if by `.toUnsigned(8)`) before
/// being stored, and the 8-bit value is zero-extended when it is loaded.
external int get value;
external void set value(int value);
/// The 8-bit unsigned integer at `address + index`.
///
/// A Dart integer is truncated to 8 bits (as if by `.toUnsigned(8)`) before
/// being stored, and the 8-bit value is zero-extended when it is loaded.
external int operator [](int index);
/// The 8-bit unsigned integer at `address + index`.
///
/// A Dart integer is truncated to 8 bits (as if by `.toUnsigned(8)`) before
/// being stored, and the 8-bit value is zero-extended when it is loaded.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
external Uint8List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Uint16].
extension Uint16Pointer on Pointer<Uint16> {
/// The 16-bit unsigned integer at [address].
///
/// A Dart integer is truncated to 16 bits (as if by `.toUnsigned(16)`) before
/// being stored, and the 16-bit value is zero-extended when it is loaded.
///
/// The [address] must be 2-byte aligned.
external int get value;
external void set value(int value);
/// The 16-bit unsigned integer at `address + 2 * index`.
///
/// A Dart integer is truncated to 16 bits (as if by `.toUnsigned(16)`) before
/// being stored, and the 16-bit value is zero-extended when it is loaded.
///
/// The [address] must be 2-byte aligned.
external int operator [](int index);
/// The 16-bit unsigned integer at `address + 2 * index`.
///
/// A Dart integer is truncated to 16 bits (as if by `.toUnsigned(16)`) before
/// being stored, and the 16-bit value is zero-extended when it is loaded.
///
/// The [address] must be 2-byte aligned.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 2 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 2-byte aligned.
external Uint16List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Uint32].
extension Uint32Pointer on Pointer<Uint32> {
/// The 32-bit unsigned integer at [address].
///
/// A Dart integer is truncated to 32 bits (as if by `.toUnsigned(32)`) before
/// being stored, and the 32-bit value is zero-extended when it is loaded.
///
/// The [address] must be 4-byte aligned.
external int get value;
external void set value(int value);
/// The 32-bit unsigned integer at `address + 4 * index`.
///
/// A Dart integer is truncated to 32 bits (as if by `.toUnsigned(32)`) before
/// being stored, and the 32-bit value is zero-extended when it is loaded.
///
/// The [address] must be 4-byte aligned.
external int operator [](int index);
/// The 32-bit unsigned integer at `address + 4 * index`.
///
/// A Dart integer is truncated to 32 bits (as if by `.toUnsigned(32)`) before
/// being stored, and the 32-bit value is zero-extended when it is loaded.
///
/// The [address] must be 4-byte aligned.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 4 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 4-byte aligned.
external Uint32List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Uint64].
extension Uint64Pointer on Pointer<Uint64> {
/// The 64-bit unsigned integer at [address].
///
/// The [address] must be 8-byte aligned.
external int get value;
external void set value(int value);
/// The 64-bit unsigned integer at `address + 8 * index`.
///
/// The [address] must be 8-byte aligned.
external int operator [](int index);
/// The 64-bit unsigned integer at `address + 8 * index`.
///
/// The [address] must be 8-byte aligned.
external void operator []=(int index, int value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 8 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 8-byte aligned.
external Uint64List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Float].
extension FloatPointer on Pointer<Float> {
/// The float at [address].
///
/// A Dart double loses precision before being stored, and the float value is
/// converted to a double when it is loaded.
///
/// The [address] must be 4-byte aligned.
external double get value;
external void set value(double value);
/// The float at `address + 4 * index`.
///
/// A Dart double loses precision before being stored, and the float value is
/// converted to a double when it is loaded.
///
/// The [address] must be 4-byte aligned.
external double operator [](int index);
/// The float at `address + 4 * index`.
///
/// A Dart double loses precision before being stored, and the float value is
/// converted to a double when it is loaded.
///
/// The [address] must be 4-byte aligned.
external void operator []=(int index, double value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 4 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 4-byte aligned.
external Float32List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Double].
extension DoublePointer on Pointer<Double> {
/// The double at [address].
///
/// The [address] must be 8-byte aligned.
external double get value;
external void set value(double value);
/// The double at `address + 8 * index`.
///
/// The [address] must be 8-byte aligned.
external double operator [](int index);
/// The double at `address + 8 * index`.
///
/// The [address] must be 8-byte aligned.
external void operator []=(int index, double value);
/// Creates a typed list view backed by memory in the address space.
///
/// The returned view will allow access to the memory range from [address]
/// to `address + 8 * length`.
///
/// The user has to ensure the memory range is accessible while using the
/// returned list.
///
/// The [address] must be 8-byte aligned.
external Float64List asTypedList(int length);
}
/// Extension on [Pointer] specialized for the type argument [Bool].
@Since('2.15')
extension BoolPointer on Pointer<Bool> {
/// The bool at [address].
external bool get value;
external void set value(bool value);
/// The bool at `address + index`.
external bool operator [](int index);
/// The bool at `address + index`.
external void operator []=(int index, bool value);
}
/// Bounds checking indexing methods on [Array]s of [Int8].
@Since('2.13')
extension Int8Array on Array<Int8> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Int16].
@Since('2.13')
extension Int16Array on Array<Int16> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Int32].
@Since('2.13')
extension Int32Array on Array<Int32> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Int64].
@Since('2.13')
extension Int64Array on Array<Int64> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Uint8].
@Since('2.13')
extension Uint8Array on Array<Uint8> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Uint16].
@Since('2.13')
extension Uint16Array on Array<Uint16> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Uint32].
@Since('2.13')
extension Uint32Array on Array<Uint32> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Uint64].
@Since('2.13')
extension Uint64Array on Array<Uint64> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Float].
@Since('2.13')
extension FloatArray on Array<Float> {
external double operator [](int index);
external void operator []=(int index, double value);
}
/// Bounds checking indexing methods on [Array]s of [Double].
@Since('2.13')
extension DoubleArray on Array<Double> {
external double operator [](int index);
external void operator []=(int index, double value);
}
/// Bounds checking indexing methods on [Array]s of [Bool].
@Since('2.15')
extension BoolArray on Array<Bool> {
external bool operator [](int index);
external void operator []=(int index, bool value);
}
//
// End of generated code.
//
/// Extension on [Pointer] specialized for the type argument [Pointer].
extension PointerPointer<T extends NativeType> on Pointer<Pointer<T>> {
/// The pointer at [address].
///
/// A [Pointer] is unboxed before being stored (as if by `.address`), and the
/// pointer is boxed (as if by `Pointer.fromAddress`) when loaded.
///
/// On 32-bit platforms the [address] must be 4-byte aligned, and on 64-bit
/// platforms the [address] must be 8-byte aligned.
external Pointer<T> get value;
external void set value(Pointer<T> value);
/// Load a Dart value from this location offset by [index].
///
/// A [Pointer] is unboxed before being stored (as if by `.address`), and the
/// pointer is boxed (as if by `Pointer.fromAddress`) when loaded.
///
/// On 32-bit platforms the [address] must be 4-byte aligned, and on 64-bit
/// platforms the [address] must be 8-byte aligned.
external Pointer<T> operator [](int index);
/// Store a Dart value into this location offset by [index].
///
/// A [Pointer] is unboxed before being stored (as if by `.address`), and the
/// pointer is boxed (as if by [Pointer.fromAddress]) when loaded.
///
/// On 32-bit platforms the [address] must be 4-byte aligned, and on 64-bit
/// platforms the [address] must be 8-byte aligned.
external void operator []=(int index, Pointer<T> value);
}
/// Extension on [Pointer] specialized for the type argument [Struct].
@Since('2.12')
extension StructPointer<T extends Struct> on Pointer<T> {
/// A Dart view of the struct referenced by this pointer.
///
/// Reading [ref] creates a reference accessing the fields of this struct
/// backed by native memory at [address].
/// The [address] must be aligned according to the struct alignment rules of
/// the platform.
///
/// Assigning to [ref] copies contents of the struct into the native memory
/// starting at [address].
///
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external T get ref;
external set ref(T value);
/// Creates a reference to access the fields of this struct backed by native
/// memory at `address + sizeOf<T>() * index`.
///
/// The [address] must be aligned according to the struct alignment rules of
/// the platform.
///
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external T operator [](int index);
/// Copies the [value] struct into native memory, starting at
/// `address * sizeOf<T>() * index`.
///
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external void operator []=(int index, T value);
}
/// Extension on [Pointer] specialized for the type argument [Union].
@Since('2.14')
extension UnionPointer<T extends Union> on Pointer<T> {
/// A Dart view of the union referenced by this pointer.
///
/// Reading [ref] creates a reference accessing the fields of this union
/// backed by native memory at [address].
/// The [address] must be aligned according to the union alignment rules of
/// the platform.
///
/// Assigning to [ref] copies contents of the union into the native memory
/// starting at [address].
///
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external T get ref;
external set ref(T value);
/// Creates a reference to access the fields of this union backed by native
/// memory at `address + sizeOf<T>() * index`.
///
/// The [address] must be aligned according to the union alignment rules of
/// the platform.
///
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external T operator [](int index);
/// Copies the [value] union into native memory, starting at
/// `address * sizeOf<T>() * index`.
///
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external void operator []=(int index, T value);
}
/// Extension on [Pointer] specialized for the type argument
/// [AbiSpecificInteger].
@Since('2.16')
extension AbiSpecificIntegerPointer<T extends AbiSpecificInteger>
on Pointer<T> {
/// The integer at [address].
external int get value;
external void set value(int value);
/// The integer at `address + sizeOf<T>() * index`.
external int operator [](int index);
/// The integer at `address + sizeOf<T>() * index`.
external void operator []=(int index, int value);
}
/// Bounds checking indexing methods on [Array]s of [Pointer].
@Since('2.13')
extension PointerArray<T extends NativeType> on Array<Pointer<T>> {
external Pointer<T> operator [](int index);
external void operator []=(int index, Pointer<T> value);
}
/// Bounds checking indexing methods on [Array]s of [Struct].
@Since('2.13')
extension StructArray<T extends Struct> on Array<T> {
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external T operator [](int index);
}
/// Bounds checking indexing methods on [Array]s of [Union].
@Since('2.14')
extension UnionArray<T extends Union> on Array<T> {
/// This extension method must be invoked on a receiver of type `Pointer<T>`
/// where `T` is a compile-time constant type.
external T operator [](int index);
}
/// Bounds checking indexing methods on [Array]s of [Array].
@Since('2.13')
extension ArrayArray<T extends NativeType> on Array<Array<T>> {
external Array<T> operator [](int index);
external void operator []=(int index, Array<T> value);
}
/// Bounds checking indexing methods on [Array]s of [AbiSpecificInteger].
@Since('2.16')
extension AbiSpecificIntegerArray on Array<AbiSpecificInteger> {
external int operator [](int index);
external void operator []=(int index, int value);
}
/// Extension to retrieve the native `Dart_Port` from a [SendPort].
@Since('2.7')
extension NativePort on SendPort {
/// The native port of this [SendPort].
///
/// The returned native port can for example be used by C code to post
/// messages to the connected [ReceivePort] via `Dart_PostCObject()` - see
/// `dart_native_api.h`.
///
/// Only the send ports from the platform classes [ReceivePort] and
/// [RawReceivePort] are supported. User-defined implementations of
/// [SendPort] are not supported.
external int get nativePort;
}
/// Opaque, not exposing it's members.
@Since('2.8')
class Dart_CObject extends Opaque {}
typedef Dart_NativeMessageHandler = Void Function(Int64, Pointer<Dart_CObject>);
/// Utilities for accessing the Dart VM API from Dart code or
/// from C code via `dart_api_dl.h`.
@Since('2.8')
abstract class NativeApi {
/// On breaking changes the major version is increased.
///
/// The versioning covers the API surface in `dart_api_dl.h`.
@Since('2.9')
external static int get majorVersion;
/// On backwards compatible changes the minor version is increased.
///
/// The versioning covers the API surface in `dart_api_dl.h`.
@Since('2.9')
external static int get minorVersion;
/// A function pointer to
/// `bool Dart_PostCObject(Dart_Port port_id, Dart_CObject* message)`
/// in `dart_native_api.h`.
external static Pointer<
NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>>
get postCObject;
/// A function pointer to
/// ```c
/// Dart_Port Dart_NewNativePort(const char* name,
/// Dart_NativeMessageHandler handler,
/// bool handle_concurrently)
/// ```
/// in `dart_native_api.h`.
external static Pointer<
NativeFunction<
Int64 Function(
Pointer<Uint8>,
Pointer<NativeFunction<Dart_NativeMessageHandler>>,
Int8)>> get newNativePort;
/// A function pointer to
/// `bool Dart_CloseNativePort(Dart_Port native_port_id)`
/// in `dart_native_api.h`.
external static Pointer<NativeFunction<Int8 Function(Int64)>>
get closeNativePort;
/// Pass this to `Dart_InitializeApiDL` in your native code to enable using the
/// symbols in `dart_api_dl.h`.
@Since('2.9')
external static Pointer<Void> get initializeApiDLData;
}
/// Annotation to be used for marking an external function as FFI native.
///
/// Example:
///```dart template:top
/// @FfiNative<Int64 Function(Int64, Int64)>('FfiNative_Sum', isLeaf:true)
/// external int sum(int a, int b);
///```
/// Calling such functions will throw an exception if no resolver
/// was set on the library or the resolver failed to resolve the name.
///
/// See `Dart_SetFfiNativeResolver` in `dart_api.h`
///
/// NOTE: This is an experimental feature and may change in the future.
@Since('2.14')
class FfiNative<T> {
final String nativeName;
final bool isLeaf;
const FfiNative(this.nativeName, {this.isLeaf: false});
}
// Bootstrapping native for getting the FFI native C function pointer to look
// up the FFI resolver.
@pragma('vm:external-name', 'Ffi_GetFfiNativeResolver')
external Pointer<NativeFunction<IntPtr Function(Handle, Handle, IntPtr)>>
_get_ffi_native_resolver<T extends NativeFunction>();
// Resolver for FFI Native C function pointers.
@pragma('vm:entry-point')
final _ffi_resolver = _get_ffi_native_resolver<
NativeFunction<IntPtr Function(Handle, Handle, IntPtr)>>()
.asFunction<int Function(Object, Object, int)>();