-
Notifications
You must be signed in to change notification settings - Fork 1
/
cxxbridge.rs
421 lines (302 loc) · 19.9 KB
/
cxxbridge.rs
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
/// Rust FFI bindings to JavaScriptCore's CXX runtime API
#[cxx::bridge]
mod ffi {
// Shared structs with fields visible to both languages.
{
// ---
}
// C++ types and signatures exposed to Rust.
unsafe extern "C++" {
//include!("WebKit/Source/JavaScriptCore/API/JavaScript.h");
//// Structs
/// A structure that contains properties and callbacks that define a type of object.
type JSClassDefinition;
type JSStaticFunction;
type JSStaticValue;
type OpaqueJSClass;
type OpaqueJSContext;
type OpaqueJSContextGroup;
type OpaqueJSPropertyNameAccumulator;
type OpaqueJSPropertyNameArray;
type OpaqueJSString;
type OpaqueJSValue;
//// Enums
/// Constants that identify the type of a JavaScript value.
type JSType;
/// The type of a JavaScript typed array object.
type JSTypedArrayType;
//// Typedefs
/// A Unicode character.
type JSChar;
type JSClassAttributes;
/// A JavaScript class.
type JSClassRef;
/// A group that associates JavaScript execution contexts with one another.
type JSContextGroupRef;
/// A JavaScript execution context.
type JSContextRef;
/// A global JavaScript execution context.
type JSGlobalContextRef;
type JSObjectCallAsConstructorCallback;
type JSObjectCallAsFunctionCallback;
type JSObjectConvertToTypeCallback;
type JSObjectDeletePropertyCallback;
type JSObjectFinalizeCallback;
type JSObjectGetPropertyCallback;
type JSObjectGetPropertyNamesCallback;
type JSObjectHasInstanceCallback;
type JSObjectHasPropertyCallback;
type JSObjectInitializeCallback;
/// A JavaScript object.
type JSObjectRef;
type JSObjectSetPropertyCallback;
/// A JavaScript property attribute.
type JSPropertyAttributes;
/// An ordered set of the names of a JavaScript object’s properties.
type JSPropertyNameAccumulatorRef;
/// An array of JavaScript property names.
type JSPropertyNameArrayRef;
/// A UTF-16 character buffer.
type JSStringRef;
/// A defntion that deallocates bytes that pass to a typed array constructor.;
type JSTypedArrayBytesDeallocator;
/// A JavaScript value.
type JSValueRef;
///// Script Functions
/// Checks for syntax errors in a string of JavaScript.
fn JSCheckScriptSyntax(
ctx: JSContextRef,
script: JSStringRef,
sourceURL: JSStringRef,
startingLineNumber: i32,
exception: &mut JSValueRef,
) -> bool;
/// Evaluates a string of JavaScript.
fn JSEvaluateScript(
// The execution context to use.
ctx: JSContextRef,
// Contains the script to evaluate.
script: JSStringRef,
// The object to use as this or NULL to use the global object as this.
thisObject: JSObjectRef,
// A JSStringRef that contains a URL for the script's source file.
// The system only uses this when reporting exceptions.
// Pass NULL to omit source file information in exceptions.
sourceURL: JSStringRef,
// An integer value that specifies the script's starting line number in the file at
// sourceURL. The system only uses this when reporting exceptions.
startingLineNumber: i32,
// A pointer to a JSValueRef to store an exception in, if any.
// Pass NULL to discard any exception.
exception: &mut JSValueRef,
) -> JSValueRef
//// Performs a JavaScript garbage collection.
fn JSGarbageCollect(ctx: JSContextRef);
///// Impls
/// Returns a JavaScript value’s type.
fn JSValueGetType(JSContextRef, JSValueRef) -> JSType;
/// Tests whether a JavaScript value’s type is the undefined type.
fn JSValueIsUndefined(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value’s type is the null type.
fn JSValueIsNull(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value is Boolean.
fn JSValueIsBoolean(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value’s type is the number type.
fn JSValueIsNumber(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value’s type is the string type.
fn JSValueIsString(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value's type is the symbol type.
fn JSValueIsSymbol(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value’s type is the object type.
fn JSValueIsObject(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value is an object with a specified class in its class chain.
fn JSValueIsObjectOfClass(JSContextRef, JSValueRef, JSClassRef) -> Bool;
/// Tests whether a JavaScript value is an array.
fn JSValueIsArray(JSContextRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value is a date.
fn JSValueIsDate(JSContextRef, JSValueRef) -> Bool;
/// Returns a JavaScript value’s typed array type.
fn JSValueGetTypedArrayType(JSContextRef, JSValueRef, &mut JSValueRef) -> JSTypedArrayType;
/// Creates a JavaScript value of the undefined type.
fn JSValueMakeUndefined(JSContextRef) -> JSValueRef;
/// Creates a JavaScript value of the null type.
fn JSValueMakeNull(JSContextRef) -> JSValueRef;
/// Creates a JavaScript Boolean value.
fn JSValueMakeBoolean(JSContextRef, Bool) -> JSValueRef;
/// Creates a JavaScript value of the number type.
fn JSValueMakeNumber(JSContextRef, Double) -> JSValueRef;
/// Creates a JavaScript value of the string type.
fn JSValueMakeString(JSContextRef, JSStringRef) -> JSValueRef;
/// Creates a JavaScript value of the symbol type.
fn JSValueMakeSymbol(JSContextRef, JSStringRef) -> JSValueRef;
/// Converts a JavaScript value to a Boolean and returns the resulting Boolean.
fn JSValueToBoolean(JSContextRef, JSValueRef) -> Bool;
/// Converts a JavaScript value to a number and returns the resulting number.
fn JSValueToNumber(JSContextRef, JSValueRef, &mut JSValueRef) -> Double;
/// Converts a JavaScript value to a string and copies the result into a JavaScript string.
fn JSValueToStringCopy(JSContextRef, JSValueRef, &mut JSValueRef) -> JSStringRef;
/// Converts a JavaScript value to an object and returns the resulting object.
fn JSValueToObject(JSContextRef, JSValueRef, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript value from a JSON-formatted string.
fn JSValueMakeFromJSONString(JSContextRef, JSStringRef) -> JSValueRef;
/// Creates a JavaScript string that contains the JSON-serialized representation of a JavaScript value.
fn JSValueCreateJSONString(JSContextRef, JSValueRef, UInt32, &mut JSValueRef) -> JSStringRef;
/// Tests whether two JavaScript values are equal.
fn JSValueIsEqual(JSContextRef, JSValueRef, JSValueRef, &mut JSValueRef) -> Bool;
/// Tests whether two JavaScript values are strict equal.
fn JSValueIsStrictEqual(JSContextRef, JSValueRef, JSValueRef) -> Bool;
/// Tests whether a JavaScript value is an object that the specified constructor creates.
fn JSValueIsInstanceOfConstructor(JSContextRef, JSValueRef, JSObjectRef, &mut JSValueRef) -> Bool;
/// Protects a JavaScript value from garbage collection.
fn JSValueProtect(JSContextRef, JSValueRef);
/// Unprotects a JavaScript value from garbage collection.
fn JSValueUnprotect(JSContextRef, JSValueRef);
/// Gets the global object of a JavaScript execution context.
fn JSContextGetGlobalObject(JSContextRef) -> JSObjectRef;
/// Calls an object as a constructor.
fn JSObjectCallAsConstructor(JSContextRef, JSObjectRef, Int, &JSValueRef, &mut JSValueRef) -> JSObjectRef;
/// Calls an object as a fntion.;
fn JSObjectCallAsFunction(JSContextRef, JSObjectRef, JSObjectRef, Int, &JSValueRef, &mut JSValueRef) -> JSValueRef;
/// Gets the names of an object’s enumerable properties.
fn JSObjectCopyPropertyNames(JSContextRef, JSObjectRef) -> JSPropertyNameArrayRef;
/// Deletes a property from an object.
fn JSObjectDeleteProperty(JSContextRef, JSObjectRef, JSStringRef, &mut JSValueRef) -> Bool;
/// Gets an object’s private data.
fn JSObjectGetPrivate(JSObjectRef) -> UnsafeMutableRawPointer;
/// Gets a property from an object.
fn JSObjectGetProperty(JSContextRef, JSObjectRef, JSStringRef, &mut JSValueRef) -> JSValueRef;
/// Gets a property from an object by numeric index.
fn JSObjectGetPropertyAtIndex(JSContextRef, JSObjectRef, UInt32, &mut JSValueRef) -> JSValueRef;
/// Gets an object’s prototype.
fn JSObjectGetPrototype(JSContextRef, JSObjectRef) -> JSValueRef;
/// Tests whether an object has a specified property.
fn JSObjectHasProperty(JSContextRef, JSObjectRef, JSStringRef) -> Bool;
/// Tests whether you can call an object as a constructor.
fn JSObjectIsConstructor(JSContextRef, JSObjectRef) -> Bool;
/// Tests whether you can call an object as a fntion.;
fn JSObjectIsFunction(JSContextRef, JSObjectRef) -> Bool;
/// Creates a JavaScript object.
fn JSObjectMake(JSContextRef, JSClassRef, Box<_>) -> JSObjectRef;
/// Creates a JavaScript array object.
fn JSObjectMakeArray(JSContextRef, Int, &JSValueRef, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript constructor.
fn JSObjectMakeConstructor(JSContextRef, JSClassRef, JSObjectCallAsConstructorCallback) -> JSObjectRef;
/// Creates a JavaScript date object as though invoking the built-in date constructor.
fn JSObjectMakeDate(JSContextRef, Int, &JSValueRef, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript error object as though invoking the built-in error constructor.
fn JSObjectMakeError(JSContextRef, Int, &JSValueRef, &mut JSValueRef) -> JSObjectRef;
/// Creates a fntion with a specified script as its body.;
fn JSObjectMakeFunction(JSContextRef, JSStringRef, UInt32, &JSStringRef, JSStringRef, JSStringRef, Int32, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript fntion with a specified callback as its implementation.;
fn JSObjectMakeFunctionWithCallback(JSContextRef, JSStringRef, JSObjectCallAsFunctionCallback) -> JSObjectRef;
/// Creates a JavaScript regular expression object as though invoking the built-in regular expression constructor.
fn JSObjectMakeRegExp(JSContextRef, Int, &JSValueRef, &mut JSValueRef) -> JSObjectRef;
/// Sets a pointer to private data on an object.
fn JSObjectSetPrivate(JSObjectRef, Box<_>) -> Bool;
/// Sets a property on an object.
fn JSObjectSetProperty(JSContextRef, JSObjectRef, JSStringRef, JSValueRef, JSPropertyAttributes, &mut JSValueRef);
/// Sets a property on an object by numeric index.
fn JSObjectSetPropertyAtIndex(JSContextRef, JSObjectRef, UInt32, JSValueRef, &mut JSValueRef);
/// Gets a property from an object using a JavaScript value as the property key.
fn JSObjectGetPropertyForKey(JSContextRef, JSObjectRef, JSValueRef, &mut JSValueRef) -> JSValueRef;
/// Sets an object’s prototype.
fn JSObjectSetPrototype(JSContextRef, JSObjectRef, JSValueRef);
/// Deletes a property from an object using a JavaScript value as the property key.
fn JSObjectDeletePropertyForKey(JSContextRef, JSObjectRef, JSValueRef, &mut JSValueRef) -> Bool;
/// Tests whether an object has the specified property using a JavaScript value as the property key.
fn JSObjectHasPropertyForKey(JSContextRef, JSObjectRef, JSValueRef, &mut JSValueRef) -> Bool;
/// Sets a property on an object using a JavaScript value as the property key.
fn JSObjectSetPropertyForKey(JSContextRef, JSObjectRef, JSValueRef, JSValueRef, JSPropertyAttributes, &mut JSValueRef);
/// Creates a JavaScript promise object by invoking the provided executor.
fn JSObjectMakeDeferredPromise(JSContextRef, &mut JSObjectRef, &mut JSObjectRef, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript class.
fn JSClassCreate(&JSClassDefinition) -> JSClassRef;
/// Releases a JavaScript class.
fn JSClassRelease(JSClassRef);
/// Retains a JavaScript class.
fn JSClassRetain(JSClassRef) -> JSClassRef;
/// Adds a property name to a JavaScript property name accumulator.
fn JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef, JSStringRef);
/// Gets a count of the number of items in a JavaScript property name array.
fn JSPropertyNameArrayGetCount(JSPropertyNameArrayRef) -> Int;
/// Gets a property name at a specified index in a JavaScript property name array.
fn JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef, Int) -> JSStringRef;
/// Releases a JavaScript property name array.
fn JSPropertyNameArrayRelease(JSPropertyNameArrayRef);
/// Retains a JavaScript property name array.
fn JSPropertyNameArrayRetain(JSPropertyNameArrayRef) -> JSPropertyNameArrayRef;
/// Creates a JavaScript typed array object with the specified number of elements.
fn JSObjectMakeTypedArray(JSContextRef, JSTypedArrayType, Int, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript typed array object from an existing pointer.
fn JSObjectMakeTypedArrayWithBytesNoCopy(JSContextRef, JSTypedArrayType, UnsafeMutableRawPointer, Int, JSTypedArrayBytesDeallocator, UnsafeMutableRawPointer, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript typed array object from an existing JavaScript array buffer object.
fn JSObjectMakeTypedArrayWithArrayBuffer(JSContextRef, JSTypedArrayType, JSObjectRef, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript typed array object from an existing JavaScript array buffer object with the specified offset and length.
fn JSObjectMakeTypedArrayWithArrayBufferAndOffset(JSContextRef, JSTypedArrayType, JSObjectRef, Int, Int, &mut JSValueRef) -> JSObjectRef;
/// Returns a temporary pointer to the backing store of a JavaScript typed array object.
fn JSObjectGetTypedArrayBytesPtr(JSContextRef, JSObjectRef, &mut JSValueRef) -> UnsafeMutableRawPointer;
/// Returns the length of a JavaScript typed array object.
fn JSObjectGetTypedArrayLength(JSContextRef, JSObjectRef, &mut JSValueRef) -> Int;
/// Returns the byte length of a JavaScript typed array object.
fn JSObjectGetTypedArrayByteLength(JSContextRef, JSObjectRef, &mut JSValueRef) -> Int;
/// Returns the byte offset of a JavaScript typed array object.
fn JSObjectGetTypedArrayByteOffset(JSContextRef, JSObjectRef, &mut JSValueRef) -> Int;
/// Returns the JavaScript array buffer object to use as the backing of a JavaScript typed array object.
fn JSObjectGetTypedArrayBuffer(JSContextRef, JSObjectRef, &mut JSValueRef) -> JSObjectRef;
/// Creates a JavaScript array buffer object from an existing pointer.
fn JSObjectMakeArrayBufferWithBytesNoCopy(
JSContextRef,
UnsafeMutableRawPointer,
i32,
JSTypedArrayBytesDeallocator,
UnsafeMutableRawPointer,
&mut JSValueRef
) -> JSObjectRef;
/// Returns the number of bytes in a JavaScript data object.
fn JSObjectGetArrayBufferByteLength(JSContextRef, JSObjectRef, &mut JSValueRef) -> Int;
/// Returns a pointer to the data buffer that serves as the backing store for a JavaScript typed array object.
fn JSObjectGetArrayBufferBytesPtr(JSContextRef, JSObjectRef, &mut JSValueRef) -> UnsafeMutableRawPointer;
/// Creates a JavaScript string from a buffer of Unicode characters.
fn JSStringCreateWithCharacters(&JSChar, i32) -> JSStringRef;
/// Creates a JavaScript string from a null-terminated UTF-8 string.
fn JSStringCreateWithUTF8CString(&[u8]) -> JSStringRef;
/// Retains a JavaScript string.
fn JSStringRetain(JSStringRef) -> JSStringRef;
/// Releases a JavaScript string.
fn JSStringRelease(JSStringRef);
/// Returns the number of Unicode characters in a JavaScript string.
fn JSStringGetLength(JSStringRef) -> u32;
/// Returns a pointer to the Unicode character buffer that serves as the backing store for a JavaScript string.
fn JSStringGetCharactersPtr(JSStringRef) -> &JSChar;
/// Returns the maximum number of bytes a JavaScript string uses when you convert it into a null-terminated UTF-8 string.
fn JSStringGetMaximumUTF8CStringSize(JSStringRef) -> i32;
/// Converts a JavaScript string into a null-terminated UTF-8 string, and copies the result into an external byte buffer.
fn JSStringGetUTF8CString(JSStringRef, &[u8], i32) -> i32;
/// Tests whether two JavaScript strings match.
fn JSStringIsEqual(JSStringRef, JSStringRef) -> bool;
/// Tests whether a JavaScript string matches a null-terminated UTF-8 string.
fn JSStringIsEqualToUTF8CString(JSStringRef, &[u8]) -> bool;
/// Creates a global JavaScript execution context.
fn JSGlobalContextCreate(JSClassRef) -> JSGlobalContextRef;
/// Creates a global JavaScript execution context in the provided context group.
fn JSGlobalContextCreateInGroup(JSContextGroupRef, JSClassRef) -> JSGlobalContextRef;
/// Retains a global JavaScript execution context.
fn JSGlobalContextRetain(JSGlobalContextRef) -> JSGlobalContextRef;
/// Releases a global JavaScript execution context.
fn JSGlobalContextRelease(JSGlobalContextRef);
/// Gets a copy of the name of a context.
fn JSGlobalContextCopyName(JSGlobalContextRef) -> JSStringRef;
/// Sets the remote debugging name for a context.
fn JSGlobalContextSetName(JSGlobalContextRef, JSStringRef);
/// Creates a JavaScript context group.
fn JSContextGroupCreate() -> JSContextGroupRef;
/// Retains a JavaScript context group.
fn JSContextGroupRetain(JSContextGroupRef) -> JSContextGroupRef;
/// Releases a JavaScript context group.
fn JSContextGroupRelease(JSContextGroupRef);
/// Gets the global context of a JavaScript execution context.
fn JSContextGetGlobalContext(JSContextRef) -> JSGlobalContextRef;
/// Gets the context group that a JavaScript execution context belongs to.
fn JSContextGetGroup(JSContextRef) -> JSContextGroupRef;
}
}