-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfake_codegen.rs
441 lines (390 loc) · 13.7 KB
/
fake_codegen.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use js::jsapi;
use js::jsapi::CallArgs;
use js::jsapi::JSClass;
use js::jsapi::JSClassOps;
use js::jsapi::JSFunctionSpec;
use js::jsapi::JSNativeWrapper;
use js::jsapi::JSPropertySpec;
use js::jsapi::JS_GlobalObjectTraceHook;
use js::jsapi::JSPROP_ENUMERATE;
use js::jsapi::JSPROP_SHARED;
use js::jsval::JSVal;
use js::jsval::UndefinedValue;
use js::panic::wrap_panic;
use libc::c_char;
use libc::c_uint;
use josephine::CanAccess;
use josephine::CanAlloc;
use josephine::Compartment;
use josephine::JSContext;
use josephine::JSString;
use josephine::ffi::JSEvaluateErr;
use josephine::ffi::JSInitializer;
use josephine::ffi::finalize_jsobject_with_native_data;
use josephine::ffi::jsclass_global_flags_with_slots;
use josephine::ffi::jsclass_has_reserved_slots;
use josephine::ffi::jscontext_called_from_js;
use josephine::ffi::jsmanaged_called_from_js;
use josephine::ffi::jsstring_called_from_js;
use josephine::ffi::null_function;
use josephine::ffi::null_property;
use josephine::ffi::null_wrapper;
use josephine::ffi::trace_jsobject_with_native_data;
use minidom::Console;
use minidom::Document;
use minidom::Element;
use minidom::Window;
use std::panic;
use std::ptr;
// In a real example, this code would be produced by a codegen tool
// from webidl. For the moment, we do it by hand.
// Window
#[allow(non_snake_case)]
pub trait WindowMethods<'a, C> {
fn Console<S>(self, cx: &'a JSContext<S>) -> Console<'a, C> where S: CanAccess + CanAlloc, C: Compartment;
fn Document<S>(self, cx: &'a JSContext<S>) -> Document<'a, C> where S: CanAccess + CanAlloc, C: Compartment;
fn Window<S>(self, cx: &'a JSContext<S>) -> Window<'a, C> where S: CanAccess + CanAlloc, C: Compartment;
}
static WINDOW_CLASS: JSClass = JSClass {
name: b"Window\0" as *const u8 as *const c_char,
flags: jsclass_global_flags_with_slots(2),
cOps: &JSClassOps {
addProperty: None,
call: None,
construct: None,
delProperty: None,
enumerate: None,
finalize: Some(finalize_jsobject_with_native_data),
getProperty: None,
hasInstance: None,
mayResolve: None,
resolve: None,
setProperty: None,
trace: Some(JS_GlobalObjectTraceHook),
},
reserved: [0 as *mut _; 3],
};
const WINDOW_PROPERTIES: &[JSPropertySpec] = &[
JSPropertySpec {
name: b"window\0" as *const u8 as *const c_char,
flags: (JSPROP_ENUMERATE | JSPROP_SHARED) as u8,
getter: JSNativeWrapper {
op: Some(window_window_getter_op),
info: ptr::null(),
},
setter: null_wrapper(),
},
JSPropertySpec {
name: b"console\0" as *const u8 as *const c_char,
flags: (JSPROP_ENUMERATE | JSPROP_SHARED) as u8,
getter: JSNativeWrapper {
op: Some(window_console_getter_op),
info: ptr::null(),
},
setter: null_wrapper(),
},
JSPropertySpec {
name: b"document\0" as *const u8 as *const c_char,
flags: (JSPROP_ENUMERATE | JSPROP_SHARED) as u8,
getter: JSNativeWrapper {
op: Some(window_document_getter_op),
info: ptr::null(),
},
setter: null_wrapper(),
},
null_property(),
];
#[allow(unsafe_code)]
unsafe extern "C" fn window_window_getter_op(cx: *mut jsapi::JSContext, argc: c_uint, vp: *mut JSVal) -> bool {
wrap_panic(panic::AssertUnwindSafe(|| {
let args = CallArgs::from_vp(vp, argc);
match window_window_getter(cx, args) {
Ok(result) => { args.rval().set(result); true },
Err(_err) => { false } // TODO: set the exception
}
}), false)
}
#[allow(unsafe_code)]
unsafe fn window_window_getter(cx: *mut jsapi::JSContext, args: CallArgs) -> Result<JSVal, JSEvaluateErr> {
debug!("Getting window.");
let this = Window(jsmanaged_called_from_js(args.thisv())?);
let ref mut cx = jscontext_called_from_js(cx);
let result = this.Window(cx);
Ok(result.0.to_jsval())
}
#[allow(unsafe_code)]
unsafe extern "C" fn window_console_getter_op(cx: *mut jsapi::JSContext, argc: c_uint, vp: *mut JSVal) -> bool {
wrap_panic(panic::AssertUnwindSafe(|| {
let args = CallArgs::from_vp(vp, argc);
match window_console_getter(cx, args) {
Ok(result) => { args.rval().set(result); true },
Err(_err) => { false } // TODO: set the exception
}
}), false)
}
#[allow(unsafe_code)]
unsafe fn window_console_getter(cx: *mut jsapi::JSContext, args: CallArgs) -> Result<JSVal, JSEvaluateErr> {
debug!("Getting console.");
let this = Window(jsmanaged_called_from_js(args.thisv())?);
let ref mut cx = jscontext_called_from_js(cx);
let result = this.Console(cx);
Ok(result.0.to_jsval())
}
#[allow(unsafe_code)]
unsafe extern "C" fn window_document_getter_op(cx: *mut jsapi::JSContext, argc: c_uint, vp: *mut JSVal) -> bool {
wrap_panic(panic::AssertUnwindSafe(|| {
let args = CallArgs::from_vp(vp, argc);
match window_document_getter(cx, args) {
Ok(result) => { args.rval().set(result); true },
Err(_err) => { false } // TODO: set the exception
}
}), false)
}
#[allow(unsafe_code)]
unsafe fn window_document_getter(cx: *mut jsapi::JSContext, args: CallArgs) -> Result<JSVal, JSEvaluateErr> {
debug!("Getting document.");
let this = Window(jsmanaged_called_from_js(args.thisv())?);
let ref mut cx = jscontext_called_from_js(cx);
let result = this.Document(cx);
Ok(result.0.to_jsval())
}
pub struct WindowInitializer;
impl JSInitializer for WindowInitializer {
#[allow(unsafe_code)]
unsafe fn global_classp() -> *const JSClass {
&WINDOW_CLASS
}
#[allow(unsafe_code)]
unsafe fn properties() -> *const JSPropertySpec {
&WINDOW_PROPERTIES[0]
}
}
// Console
#[allow(non_snake_case)]
pub trait ConsoleMethods<'a, C> {
fn Log<S>(self, cx: &'a mut JSContext<S>, arg: JSString<'a, C>) where S: CanAccess + CanAlloc, C: Compartment;
}
static CONSOLE_CLASS: JSClass = JSClass {
name: b"Console\0" as *const u8 as *const c_char,
flags: jsclass_has_reserved_slots(2),
cOps: &JSClassOps {
addProperty: None,
call: None,
construct: None,
delProperty: None,
enumerate: None,
finalize: Some(finalize_jsobject_with_native_data),
getProperty: None,
hasInstance: None,
mayResolve: None,
resolve: None,
setProperty: None,
trace: Some(trace_jsobject_with_native_data),
},
reserved: [0 as *mut _; 3],
};
const CONSOLE_FUNCTIONS: &[JSFunctionSpec] = &[
JSFunctionSpec {
name: b"log\0" as *const u8 as *const c_char,
selfHostedName: ptr::null(),
flags: JSPROP_ENUMERATE as u16,
nargs: 1,
call: JSNativeWrapper {
op: Some(console_log_op),
info: ptr::null(),
},
},
null_function(),
];
pub struct ConsoleInitializer;
impl JSInitializer for ConsoleInitializer {
#[allow(unsafe_code)]
unsafe fn classp() -> *const JSClass {
&CONSOLE_CLASS
}
#[allow(unsafe_code)]
unsafe fn functions() -> *const JSFunctionSpec {
&CONSOLE_FUNCTIONS[0]
}
}
#[allow(unsafe_code)]
unsafe extern "C" fn console_log_op(cx: *mut jsapi::JSContext, argc: c_uint, vp: *mut JSVal) -> bool {
wrap_panic(panic::AssertUnwindSafe(|| {
let args = CallArgs::from_vp(vp, argc);
match console_log(cx, args) {
Ok(result) => { args.rval().set(result); true },
Err(_err) => { false } // TODO: set the exception
}
}), false)
}
#[allow(unsafe_code)]
unsafe fn console_log(cx: *mut jsapi::JSContext, args: CallArgs) -> Result<JSVal, JSEvaluateErr> {
debug!("Logging.");
let this = Console(jsmanaged_called_from_js(args.thisv())?);
let arg = jsstring_called_from_js(cx, args.get(0))?;
let ref mut cx = jscontext_called_from_js(cx);
this.Log(cx, arg);
Ok(UndefinedValue())
}
// Document
#[allow(non_snake_case)]
pub trait DocumentMethods<'a, C> {
fn Body<S>(self, cx: &'a mut JSContext<S>) -> Element<'a, C> where S: CanAccess + CanAlloc, C: Compartment;
}
static DOCUMENT_CLASS: JSClass = JSClass {
name: b"Document\0" as *const u8 as *const c_char,
flags: jsclass_has_reserved_slots(2),
cOps: &JSClassOps {
addProperty: None,
call: None,
construct: None,
delProperty: None,
enumerate: None,
finalize: Some(finalize_jsobject_with_native_data),
getProperty: None,
hasInstance: None,
mayResolve: None,
resolve: None,
setProperty: None,
trace: Some(trace_jsobject_with_native_data),
},
reserved: [0 as *mut _; 3],
};
const DOCUMENT_PROPERTIES: &[JSPropertySpec] = &[
JSPropertySpec {
name: b"body\0" as *const u8 as *const c_char,
flags: (JSPROP_ENUMERATE | JSPROP_SHARED) as u8,
getter: JSNativeWrapper {
op: Some(document_body_getter_op),
info: ptr::null(),
},
setter: null_wrapper(),
},
null_property(),
];
pub struct DocumentInitializer;
impl JSInitializer for DocumentInitializer {
#[allow(unsafe_code)]
unsafe fn classp() -> *const JSClass {
&DOCUMENT_CLASS
}
#[allow(unsafe_code)]
unsafe fn properties() -> *const JSPropertySpec {
&DOCUMENT_PROPERTIES[0]
}
}
#[allow(unsafe_code)]
unsafe extern "C" fn document_body_getter_op(cx: *mut jsapi::JSContext, argc: c_uint, vp: *mut JSVal) -> bool {
wrap_panic(panic::AssertUnwindSafe(|| {
let args = CallArgs::from_vp(vp, argc);
match document_body_getter(cx, args) {
Ok(result) => { args.rval().set(result); true },
Err(_err) => { false } // TODO: set the exception
}
}), false)
}
#[allow(unsafe_code)]
unsafe fn document_body_getter(cx: *mut jsapi::JSContext, args: CallArgs) -> Result<JSVal, JSEvaluateErr> {
debug!("Getting body.");
let this = Document(jsmanaged_called_from_js(args.thisv())?);
let ref mut cx = jscontext_called_from_js(cx);
let result = this.Body(cx);
Ok(result.0.to_jsval())
}
// Element
#[allow(non_snake_case)]
pub trait ElementMethods<'a, C> {
fn Parent<S>(self, cx: &'a mut JSContext<S>) -> Option<Element<'a, C>> where S: CanAccess + CanAlloc, C: Compartment;
fn TagName<S>(self, cx: &'a mut JSContext<S>) -> JSString<'a, C> where S: CanAccess + CanAlloc, C: Compartment;
fn Append<S, D>(self, cx: &'a mut JSContext<S>, child: Element<'a, D>) where S: CanAccess + CanAlloc, C: Compartment, D: Compartment;
}
static ELEMENT_CLASS: JSClass = JSClass {
name: b"Element\0" as *const u8 as *const c_char,
flags: jsclass_has_reserved_slots(2),
cOps: &JSClassOps {
addProperty: None,
call: None,
construct: None,
delProperty: None,
enumerate: None,
finalize: Some(finalize_jsobject_with_native_data),
getProperty: None,
hasInstance: None,
mayResolve: None,
resolve: None,
setProperty: None,
trace: Some(trace_jsobject_with_native_data),
},
reserved: [0 as *mut _; 3],
};
const ELEMENT_PROPERTIES: &[JSPropertySpec] = &[
JSPropertySpec {
name: b"parent\0" as *const u8 as *const c_char,
flags: (JSPROP_ENUMERATE | JSPROP_SHARED) as u8,
getter: JSNativeWrapper {
op: Some(element_parent_getter_op),
info: ptr::null(),
},
setter: null_wrapper(),
},
JSPropertySpec {
name: b"tagName\0" as *const u8 as *const c_char,
flags: (JSPROP_ENUMERATE | JSPROP_SHARED) as u8,
getter: JSNativeWrapper {
op: Some(element_tagName_getter_op),
info: ptr::null(),
},
setter: null_wrapper(),
},
null_property(),
];
pub struct ElementInitializer;
impl JSInitializer for ElementInitializer {
#[allow(unsafe_code)]
unsafe fn classp() -> *const JSClass {
&ELEMENT_CLASS
}
#[allow(unsafe_code)]
unsafe fn properties() -> *const JSPropertySpec {
&ELEMENT_PROPERTIES[0]
}
}
#[allow(unsafe_code)]
unsafe extern "C" fn element_parent_getter_op(cx: *mut jsapi::JSContext, argc: c_uint, vp: *mut JSVal) -> bool {
wrap_panic(panic::AssertUnwindSafe(|| {
let args = CallArgs::from_vp(vp, argc);
match element_parent_getter(cx, args) {
Ok(result) => { args.rval().set(result); true },
Err(_err) => { false } // TODO: set the exception
}
}), false)
}
#[allow(unsafe_code)]
unsafe fn element_parent_getter(cx: *mut jsapi::JSContext, args: CallArgs) -> Result<JSVal, JSEvaluateErr> {
debug!("Getting parent.");
let this = Element(jsmanaged_called_from_js(args.thisv())?);
let ref mut cx = jscontext_called_from_js(cx);
let result = this.Parent(cx);
Ok(result.map(|result| result.0.to_jsval()).unwrap_or(UndefinedValue()))
}
#[allow(unsafe_code,non_snake_case)]
unsafe extern "C" fn element_tagName_getter_op(cx: *mut jsapi::JSContext, argc: c_uint, vp: *mut JSVal) -> bool {
wrap_panic(panic::AssertUnwindSafe(|| {
let args = CallArgs::from_vp(vp, argc);
match element_tagName_getter(cx, args) {
Ok(result) => { args.rval().set(result); true },
Err(_err) => { false } // TODO: set the exception
}
}), false)
}
#[allow(unsafe_code,non_snake_case)]
unsafe fn element_tagName_getter(cx: *mut jsapi::JSContext, args: CallArgs) -> Result<JSVal, JSEvaluateErr> {
debug!("Getting tagName.");
let this = Element(jsmanaged_called_from_js(args.thisv())?);
let ref mut cx = jscontext_called_from_js(cx);
let result = this.TagName(cx);
Ok(result.to_jsval())
}