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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate::;
use ptr;
/// Evaluates a string of JavaScript.
///
/// * `ctx`: The execution context to use.
/// * `script`: A string containing the script to evaluate.
/// * `this_object`: The optional object to use as `this`, or `None` to
/// use the global object as `this`.
/// * `source_url`: An optional string containing a URL for the script's
/// source file. This is used by debuggers and when reporting
/// exceptions. Pass `None` if you do not care to include source
/// file information.
/// * `starting_line_number`: An integer value specifying the script's
/// starting line number in the file located at `source_url`. This
/// is only used when reporting exceptions. The value is one-based,
/// so the first line is line `1` and invalid values are clamped
/// to `1`.
///
/// Returns either the [`JSValue`] that results from evaluating the script or
/// the exception that occurred.
///
/// ```
/// use javascriptcore::*;
///
/// let ctx = JSContext::default();
/// let r = evaluate_script(&ctx, "2 + 2", None, "test.js", 1);
/// assert_eq!(r.unwrap().as_number().unwrap(), 4.0);
/// ```
/// Checks for syntax errors in a string of JavaScript.
///
/// * `ctx`: The execution context to use.
/// * `script`: A string containing the script to check for
/// syntax errors.
/// * `source_url`: An optional string containing a URL for the script's
/// source file. This is only used when reporting exceptions. Pass
/// `None` if you do not care to include source file information in
/// exceptions.
/// * `starting_line_number`: An integer value specifying the script's
/// starting line number in the file located at `source_url`. This
/// is only used when reporting exceptions. The value is one-based,
/// so the first line is line `1` and invalid values are clamped
/// to `1`.
///
/// Returns `Ok` if the script is syntactically correct, otherwise
/// returns an [exception](JSException).
///
/// ```
/// use javascriptcore::*;
///
/// let ctx = JSContext::default();
/// let r = check_script_syntax(&ctx, "alert('abc');", "test.js", 1);
/// assert!(r.is_ok());
/// ```
/// Performs a JavaScript garbage collection.
///
/// JavaScript values that are on the machine stack, in a register,
/// protected by [`JSValue::protect()`], set as the global object of an
/// execution context, or reachable from any such value will not
/// be collected.
///
/// During JavaScript execution, you are not required to call this
/// function; the JavaScript engine will garbage collect as needed.
/// JavaScript values created within a context group are automatically
/// destroyed when the last reference to the context group is released.
///
/// * `ctx`: The execution context to use.
///
/// ```
/// use javascriptcore::*;
///
/// let ctx = JSContext::default();
/// // ... Do things ...
/// garbage_collect(&ctx);
/// ```
///
/// # See also
///
/// * [`JSValue::protect()`]
/// * [`JSValue::unprotect()`]