1use std::mem;
2use std::os::raw::c_void;
3use std::ptr::null_mut;
4
5#[repr(C)]
10#[derive(Debug, Clone, Copy)]
11pub struct Local {
12    pub handle: *mut c_void,
13}
14
15pub type FunctionCallbackInfo = *const c_void;
20
21#[repr(C)]
22#[derive(Debug, Copy, Clone)]
23pub struct Isolate__ {
24    _unused: [u8; 0],
25}
26
27pub type Isolate = *mut Isolate__;
29
30const HANDLE_SCOPE_SIZE: usize = 24;
31
32#[repr(C)]
38#[derive(Clone, Copy)]
39pub struct HandleScope {
40    pub align_to_pointer: [*mut c_void; 0],
41    pub fields: [u8; HANDLE_SCOPE_SIZE],
42}
43
44impl HandleScope {
45    pub fn new() -> HandleScope {
46        unsafe { mem::zeroed() }
47    }
48}
49
50impl Default for HandleScope {
51    fn default() -> Self {
52        Self::new()
53    }
54}
55
56const ESCAPABLE_HANDLE_SCOPE_SIZE: usize = 32;
57
58#[repr(C)]
64#[derive(Clone, Copy)]
65pub struct EscapableHandleScope {
66    pub align_to_pointer: [*mut c_void; 0],
67    pub fields: [u8; ESCAPABLE_HANDLE_SCOPE_SIZE],
68}
69
70impl EscapableHandleScope {
71    pub fn new() -> EscapableHandleScope {
72        unsafe { mem::zeroed() }
73    }
74}
75
76impl Default for EscapableHandleScope {
77    fn default() -> Self {
78        Self::new()
79    }
80}
81
82#[repr(C)]
83pub struct CCallback {
84    pub static_callback: *mut c_void,
85    pub dynamic_callback: *mut c_void,
86}
87
88impl Default for CCallback {
89    fn default() -> Self {
90        CCallback {
91            static_callback: null_mut(),
92            dynamic_callback: null_mut(),
93        }
94    }
95}
96
97#[repr(u8)]
98pub enum TryCatchControl {
99    Returned = 0,
100    Threw = 1,
101    Panicked = 2,
102    UnexpectedErr = 3,
103}
104
105#[derive(Clone, Copy)]
106pub struct InheritedHandleScope;
107
108pub type TryCatchGlue = extern "C" fn(
115    rust_thunk: *mut c_void,
116    cx: *mut c_void,
117    result: *mut c_void,
118    unwind_value: *mut *mut c_void,
119) -> TryCatchControl;
120
121extern "C" {
122
123    pub fn Neon_Array_New(out: &mut Local, isolate: Isolate, length: u32);
124    pub fn Neon_Array_Length(isolate: Isolate, array: Local) -> u32;
125
126    pub fn Neon_ArrayBuffer_New(out: &mut Local, isolate: Isolate, size: u32) -> bool;
127    pub fn Neon_ArrayBuffer_Data<'a, 'b>(
128        isolate: Isolate,
129        base_out: &'a mut *mut c_void,
130        obj: Local,
131    ) -> usize;
132
133    pub fn Neon_Buffer_New(isolate: Isolate, out: &mut Local, size: u32) -> bool;
134    pub fn Neon_Buffer_Uninitialized(isolate: Isolate, out: &mut Local, size: u32) -> bool;
135    pub fn Neon_Buffer_Data<'a, 'b>(
136        isolate: Isolate,
137        base_out: &'a mut *mut c_void,
138        obj: Local,
139    ) -> usize;
140
141    pub fn Neon_Call_SetReturn(info: FunctionCallbackInfo, value: Local);
142    pub fn Neon_Call_GetIsolate(info: FunctionCallbackInfo) -> Isolate;
143    pub fn Neon_Call_CurrentIsolate() -> Isolate;
144    pub fn Neon_Call_IsConstruct(info: FunctionCallbackInfo) -> bool;
145    pub fn Neon_Call_This(isolate: Isolate, info: FunctionCallbackInfo, out: &mut Local);
146    pub fn Neon_Call_Data(isolate: Isolate, info: FunctionCallbackInfo, out: &mut *mut c_void);
147    pub fn Neon_Call_Length(isolate: Isolate, info: FunctionCallbackInfo) -> i32;
148    pub fn Neon_Call_Get(isolate: Isolate, info: FunctionCallbackInfo, i: i32, out: &mut Local);
149
150    pub fn Neon_Class_GetClassMap(isolate: Isolate) -> *mut c_void;
151    pub fn Neon_Class_SetClassMap(isolate: Isolate, map: *mut c_void, free_map: *mut c_void);
152    pub fn Neon_Class_CreateBase(
153        isolate: Isolate,
154        allocate: CCallback,
155        construct: CCallback,
156        call: CCallback,
157        drop: extern "C" fn(*mut c_void),
158    ) -> *mut c_void;
159    pub fn Neon_Class_GetName<'a>(
160        base_out: &'a mut *mut u8,
161        isolate: Isolate,
162        metadata: *const c_void,
163    ) -> usize;
164    pub fn Neon_Class_SetName(
165        isolate: Isolate,
166        metadata: *mut c_void,
167        name: *const u8,
168        byte_length: u32,
169    ) -> bool;
170    pub fn Neon_Class_ThrowCallError(isolate: Isolate, metadata: *mut c_void);
171    pub fn Neon_Class_ThrowThisError(isolate: Isolate, metadata: *mut c_void);
172    pub fn Neon_Class_AddMethod(
173        isolate: Isolate,
174        metadata: *mut c_void,
175        name: *const u8,
176        byte_length: u32,
177        method: Local,
178    ) -> bool;
179    pub fn Neon_Class_MetadataToConstructor(
180        out: &mut Local,
181        isolate: Isolate,
182        metadata: *mut c_void,
183    ) -> bool;
184    pub fn Neon_Class_GetAllocateKernel(data: *mut c_void) -> *mut c_void;
185    pub fn Neon_Class_GetConstructKernel(data: *mut c_void) -> *mut c_void;
186    pub fn Neon_Class_GetCallKernel(data: *mut c_void) -> *mut c_void;
187    pub fn Neon_Class_Constructor(out: &mut Local, ft: Local) -> bool;
188    pub fn Neon_Class_HasInstance(metadata: *mut c_void, v: Local) -> bool;
189    pub fn Neon_Class_GetInstanceInternals(obj: Local) -> *mut c_void;
190
191    pub fn Neon_Convert_ToObject(out: &mut Local, isolate: Isolate, value: Local) -> bool;
192    pub fn Neon_Convert_ToString(out: &mut Local, isolate: Isolate, value: Local) -> bool;
193
194    pub fn Neon_Error_Throw(val: Local);
195    pub fn Neon_Error_NewError(out: &mut Local, msg: Local);
196    pub fn Neon_Error_NewTypeError(out: &mut Local, msg: Local);
197    pub fn Neon_Error_NewRangeError(out: &mut Local, msg: Local);
198    pub fn Neon_Error_ThrowErrorFromUtf8(msg: *const u8, len: i32);
199
200    pub fn Neon_Fun_New(out: &mut Local, isolate: Isolate, callback: CCallback) -> bool;
201    pub fn Neon_Fun_Template_New(out: &mut Local, isolate: Isolate, callback: CCallback) -> bool;
202    pub fn Neon_Fun_GetDynamicCallback(isolate: Isolate, data: *mut c_void) -> *mut c_void;
203    pub fn Neon_Fun_Call(
204        out: &mut Local,
205        isolate: Isolate,
206        fun: Local,
207        this: Local,
208        argc: i32,
209        argv: *const c_void,
210    ) -> bool;
211    pub fn Neon_Fun_Construct(
212        out: &mut Local,
213        isolate: Isolate,
214        fun: Local,
215        argc: i32,
216        argv: *const c_void,
217    ) -> bool;
218
219    pub fn Neon_Mem_SameHandle(h1: Local, h2: Local) -> bool;
220
221    pub fn Neon_Module_ExecKernel(
222        kernel: *mut c_void,
223        callback: extern "C" fn(*mut c_void, *mut c_void, *mut c_void, *mut c_void),
224        exports: Local,
225        scope: *mut c_void,
226        vm: *mut c_void,
227    );
228    pub fn Neon_Module_ExecCallback(callback: CCallback, exports: Local, vm: *mut c_void);
229    pub fn Neon_Module_GetVersion() -> i32;
230
231    pub fn Neon_Object_New(out: &mut Local, isolate: Isolate);
232    pub fn Neon_Object_GetOwnPropertyNames(
233        out: &mut Local,
234        isolate: Isolate,
235        object: Local,
236    ) -> bool;
237    pub fn Neon_Object_GetIsolate(obj: Local) -> Isolate;
238    pub fn Neon_Object_Get_Index(out: &mut Local, object: Local, index: u32) -> bool;
239    pub fn Neon_Object_Set_Index(out: &mut bool, object: Local, index: u32, val: Local) -> bool;
240    pub fn Neon_Object_Get_String(out: &mut Local, object: Local, key: *const u8, len: i32)
241        -> bool;
242    pub fn Neon_Object_Set_String(
243        out: &mut bool,
244        object: Local,
245        key: *const u8,
246        len: i32,
247        val: Local,
248    ) -> bool;
249    pub fn Neon_Object_Get(out: &mut Local, object: Local, key: Local) -> bool;
250    pub fn Neon_Object_Set(out: &mut bool, object: Local, key: Local, val: Local) -> bool;
251
252    pub fn Neon_Primitive_Undefined(out: &mut Local, isolate: Isolate);
253    pub fn Neon_Primitive_Null(out: &mut Local, isolate: Isolate);
254    pub fn Neon_Primitive_Boolean(out: &mut Local, isolate: Isolate, b: bool);
255    pub fn Neon_Primitive_BooleanValue(p: Local) -> bool;
256    pub fn Neon_Primitive_Integer(out: &mut Local, isolate: Isolate, x: i32);
257    pub fn Neon_Primitive_IsUint32(p: Local) -> bool;
258    pub fn Neon_Primitive_IsInt32(p: Local) -> bool;
259    pub fn Neon_Primitive_IntegerValue(p: Local) -> i64;
260    pub fn Neon_Primitive_Number(out: &mut Local, isolate: Isolate, v: f64);
261    pub fn Neon_Primitive_NumberValue(p: Local) -> f64;
262
263    pub fn Neon_Scope_Escape(
264        isolate: Isolate,
265        out: &mut Local,
266        scope: *mut EscapableHandleScope,
267        value: Local,
268    );
269    pub fn Neon_Scope_Chained(
270        out: *mut c_void,
271        closure: *mut c_void,
272        callback: extern "C" fn(&mut c_void, *mut c_void, *mut c_void, *mut c_void),
273        parent_scope: *mut c_void,
274    );
275    pub fn Neon_Scope_Nested(
276        out: *mut c_void,
277        closure: *mut c_void,
278        callback: extern "C" fn(&mut c_void, *mut c_void, *mut c_void),
279        realm: *mut c_void,
280    );
281    pub fn Neon_Scope_Enter(scope: &mut HandleScope, isolate: Isolate);
282    pub fn Neon_Scope_Exit(scope: &mut HandleScope);
283    pub fn Neon_Scope_Enter_Escapable(scope: &mut EscapableHandleScope, isolate: Isolate);
284    pub fn Neon_Scope_Exit_Escapable(scope: &mut EscapableHandleScope);
285    pub fn Neon_Scope_Sizeof() -> usize;
286    pub fn Neon_Scope_Alignof() -> usize;
287    pub fn Neon_Scope_SizeofEscapable() -> usize;
288    pub fn Neon_Scope_AlignofEscapable() -> usize;
289    pub fn Neon_Scope_GetGlobal(isolate: Isolate, out: &mut Local);
290
291    pub fn Neon_String_New(out: &mut Local, isolate: Isolate, data: *const u8, len: i32) -> bool;
292    pub fn Neon_String_Utf8Length(str: Local) -> isize;
293    pub fn Neon_String_Data(out: *mut u8, len: isize, str: Local) -> isize;
294
295    pub fn Neon_Tag_IsUndefined(isolate: Isolate, val: Local) -> bool;
296    pub fn Neon_Tag_IsNull(isolate: Isolate, val: Local) -> bool;
297    pub fn Neon_Tag_IsNumber(isolate: Isolate, val: Local) -> bool;
298    pub fn Neon_Tag_IsBoolean(isolate: Isolate, val: Local) -> bool;
299    pub fn Neon_Tag_IsString(isolate: Isolate, val: Local) -> bool;
300    pub fn Neon_Tag_IsObject(isolate: Isolate, val: Local) -> bool;
301    pub fn Neon_Tag_IsArray(isolate: Isolate, val: Local) -> bool;
302    pub fn Neon_Tag_IsFunction(isolate: Isolate, val: Local) -> bool;
303    pub fn Neon_Tag_IsError(isolate: Isolate, val: Local) -> bool;
304    pub fn Neon_Tag_IsBuffer(isolate: Isolate, obj: Local) -> bool;
305    pub fn Neon_Tag_IsArrayBuffer(isolate: Isolate, obj: Local) -> bool;
306
307    pub fn Neon_Task_Schedule(
308        task: *mut c_void,
309        perform: unsafe extern "C" fn(*mut c_void) -> *mut c_void,
310        complete: unsafe extern "C" fn(*mut c_void, *mut c_void, &mut Local),
311        callback: Local,
312    );
313
314    pub fn Neon_EventHandler_New(isolate: Isolate, this: Local, callback: Local) -> *mut c_void;
315    pub fn Neon_EventHandler_Schedule(
316        thread_safe_cb: *mut c_void,
317        rust_callback: *mut c_void,
318        complete: unsafe extern "C" fn(Local, Local, *mut c_void),
319    );
320    pub fn Neon_EventHandler_Delete(thread_safe_cb: *mut c_void);
321
322    pub fn Neon_TryCatch_With(
328        glue: TryCatchGlue,
329        rust_thunk: *mut c_void,
330        cx: *mut c_void,
331        ok: *mut c_void,
332        err: *mut Local,
333        unwind_value: *mut *mut c_void,
334    ) -> TryCatchControl;
335}