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
use crate::api;
use ::std::{ffi, ptr};
use ::std::default::Default;
pub struct VM {
raw: api::JSContextGroupRef,
}
impl VM {
pub fn new() -> VM {
unsafe { VM { raw: api::JSContextGroupCreate() } }
}
}
impl Drop for VM {
fn drop(&mut self) {
unsafe {
api::JSContextGroupRelease(self.raw);
}
}
}
pub struct String {
raw: api::JSStringRef,
}
impl String {
pub fn new(s: &str) -> String {
let cstr = ffi::CString::new(s.as_bytes()).unwrap();
unsafe {
String {
raw: api::JSStringCreateWithUTF8CString(cstr.as_ptr()),
}
}
}
pub fn length(&self) {
unsafe {
api::JSStringGetLength(self.raw);
}
}
}
impl Drop for String {
fn drop(&mut self) {
unsafe {
api::JSStringRelease(self.raw);
}
}
}
pub struct Context {
raw: api::JSGlobalContextRef,
}
impl Context {
pub fn new(vm: &VM) -> Context {
unsafe {
Context {
raw: api::JSGlobalContextCreateInGroup(vm.raw, ptr::null_mut()),
}
}
}
}
impl Drop for Context {
fn drop(&mut self) {
unsafe {
api::JSGlobalContextRelease(self.raw);
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct Value {
raw: api::JSValueRef,
}
pub type JSResult<T> = Result<T, Value>;
impl Value {
pub fn with_boolean(ctx: &Context, value: bool) -> Value {
unsafe {
Value {
raw: api::JSValueMakeBoolean(ctx.raw, value),
}
}
}
pub fn with_number(ctx: &Context, value: f64) -> Value {
unsafe {
Value {
raw: api::JSValueMakeNumber(ctx.raw, value),
}
}
}
pub fn with_string(ctx: &Context, value: &str) -> Value {
unsafe {
Value {
raw: api::JSValueMakeString(ctx.raw, String::new(value).raw),
}
}
}
pub fn null(ctx: &Context) -> Value {
unsafe { Value { raw: api::JSValueMakeNull(ctx.raw) } }
}
pub fn undefined(ctx: &Context) -> Value {
unsafe {
Value {
raw: api::JSValueMakeUndefined(ctx.raw),
}
}
}
pub fn is_boolean(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsBoolean(ctx.raw, self.raw) }
}
pub fn is_null(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsNull(ctx.raw, self.raw) }
}
pub fn is_undefined(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsUndefined(ctx.raw, self.raw) }
}
pub fn is_number(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsNumber(ctx.raw, self.raw) }
}
pub fn is_string(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsString(ctx.raw, self.raw) }
}
pub fn is_object(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsObject(ctx.raw, self.raw) }
}
pub fn is_array(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsArray(ctx.raw, self.raw) }
}
pub fn is_date(&self, ctx: &Context) -> bool {
unsafe { api::JSValueIsDate(ctx.raw, self.raw) }
}
pub fn is_empty(&self) -> bool {
self.raw == ptr::null()
}
pub fn to_number(&self, ctx: &Context) -> JSResult<f64> {
unsafe {
let mut exception: api::JSValueRef = ptr::null_mut();
let result = api::JSValueToNumber(ctx.raw, self.raw, &mut exception);
if exception == ptr::null() {
Ok(result)
} else {
Err(Value { raw: exception })
}
}
}
pub fn to_boolean(&self, ctx: &Context) -> bool {
unsafe { api::JSValueToBoolean(ctx.raw, self.raw) }
}
}
impl Default for Value {
fn default() -> Value {
Value { raw: ptr::null() }
}
}
#[derive(Copy, Clone, Debug)]
pub struct Object {
raw: api::JSObjectRef,
}
impl Object {
pub fn array(ctx: &Context, arguments: &[Value]) -> JSResult<Object> {
unsafe {
let mut exception: api::JSValueRef = ptr::null_mut();
let result = api::JSObjectMakeArray(
ctx.raw,
arguments.len() as usize,
arguments.as_ptr() as *mut api::JSValueRef,
&mut exception,
);
if exception == ptr::null_mut() {
Ok(Object { raw: result })
} else {
Err(Value { raw: exception })
}
}
}
pub fn is_constructor(&self, ctx: &Context) -> bool {
unsafe { api::JSObjectIsConstructor(ctx.raw, self.raw) }
}
}
impl Default for Object {
fn default() -> Object {
Object { raw: ptr::null_mut() }
}
}
impl Context {
pub fn evaluate_script(&self, script: &str, receiver: &Object, url: url::Url, starting_line_number: i32) -> JSResult<Value> {
let string = String::new(script);
let source = String::new(url.as_str());
unsafe {
let mut exception: api::JSValueRef = ptr::null_mut();
let result = api::JSEvaluateScript(self.raw, string.raw, receiver.raw, source.raw, starting_line_number, &mut exception);
if exception == ptr::null_mut() {
Ok(Value { raw: result })
} else {
Err(Value { raw: exception })
}
}
}
pub fn check_syntax(&self, script: &str, url: url::Url, starting_line_number: i32) -> JSResult<bool> {
let string = String::new(script);
let source = String::new(url.as_str());
unsafe {
let mut exception: api::JSValueRef = ptr::null_mut();
let result = api::JSCheckScriptSyntax(self.raw, string.raw, source.raw, starting_line_number, &mut exception);
if exception == ptr::null_mut() {
Ok(result)
} else {
Err(Value { raw: exception })
}
}
}
}
#[cfg(test)]
mod tests {
use super::api;
#[test]
#[ignore]
fn vm_context_ffi_smoke() {
unsafe {
let vm_raw = api::JSContextGroupCreate();
assert_eq!(api::JSContextGroupRelease(vm_raw), ());
}
}
}