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
#![allow(missing_docs)]
use std::{
ffi::{c_void, CString},
os::raw,
};
use DartCObjectType::*;
use crate::{
dart_array::DartArray,
into_dart::{
visit_dart_typed_data_type, DartTypedDataTypeTrait,
DartTypedDataTypeVisitor,
},
};
pub type DartPort = i64;
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum DartTypedDataType {
ByteData = 0,
Int8 = 1,
Uint8 = 2,
Uint8Clamped = 3,
Int16 = 4,
Uint16 = 5,
Int32 = 6,
Uint32 = 7,
Int64 = 8,
Uint64 = 9,
Float32 = 10,
Float64 = 11,
Float32x4 = 12,
Invalid = 13,
}
#[repr(i32)]
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum DartCObjectType {
DartNull = 0,
DartBool = 1,
DartInt32 = 2,
DartInt64 = 3,
DartDouble = 4,
DartString = 5,
DartArray = 6,
DartTypedData = 7,
DartExternalTypedData = 8,
DartSendPort = 9,
DartCapability = 10,
DartUnsupported = 11,
DartNumberOfTypes = 12,
}
#[allow(missing_debug_implementations)]
#[repr(C)]
pub struct DartCObject {
pub ty: DartCObjectType,
pub value: DartCObjectValue,
}
#[allow(missing_debug_implementations)]
#[repr(C)]
#[derive(Clone, Copy)]
pub union DartCObjectValue {
pub as_bool: bool,
pub as_int32: i32,
pub as_int64: i64,
pub as_double: f64,
pub as_string: *mut raw::c_char,
pub as_send_port: DartNativeSendPort,
pub as_capability: DartNativeCapability,
pub as_array: DartNativeArray,
pub as_typed_data: DartNativeTypedData,
pub as_external_typed_data: DartNativeExternalTypedData,
_bindgen_union_align: [u64; 5usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DartNativeSendPort {
pub id: DartPort,
pub origin_id: DartPort,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DartNativeCapability {
pub id: i64,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DartNativeArray {
pub length: isize,
pub values: *mut *mut DartCObject,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DartNativeTypedData {
pub ty: DartTypedDataType,
pub length: isize,
pub values: *mut u8,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DartNativeExternalTypedData {
pub ty: DartTypedDataType,
pub length: isize,
pub data: *mut u8,
pub peer: *mut c_void,
pub callback: DartHandleFinalizer,
}
pub type DartHandleFinalizer =
unsafe extern "C" fn(isolate_callback_data: *mut c_void, peer: *mut c_void);
#[derive(Debug, Clone)]
pub struct ZeroCopyBuffer<T>(pub T);
pub type DartPostCObjectFnType =
unsafe extern "C" fn(port_id: DartPort, message: *mut DartCObject) -> bool;
impl Drop for DartCObject {
fn drop(&mut self) {
match self.ty {
DartString => {
let _ = unsafe { CString::from_raw(self.value.as_string) };
},
DartArray => {
let _ = DartArray::from(unsafe { self.value.as_array });
},
DartTypedData => {
struct MyVisitor<'a>(&'a DartNativeTypedData);
impl DartTypedDataTypeVisitor for MyVisitor<'_> {
fn visit<T: DartTypedDataTypeTrait>(&self) {
let _ = unsafe {
Vec::from_raw_parts(
self.0.values as *mut T,
self.0.length as usize,
self.0.length as usize,
)
};
}
}
let v = unsafe { self.value.as_typed_data };
visit_dart_typed_data_type(v.ty, &MyVisitor(&v));
},
DartNull | DartBool | DartInt32 | DartInt64 | DartDouble => {
},
DartExternalTypedData => {
},
DartSendPort | DartCapability | DartUnsupported
| DartNumberOfTypes => {
},
}
}
}