javascriptcore/auto/
context.rs1use crate::{CheckSyntaxMode, CheckSyntaxResult, Exception, Value, VirtualMachine};
6use glib::{prelude::*, translate::*};
7use std::boxed::Box as Box_;
8
9glib::wrapper! {
10 #[doc(alias = "JSCContext")]
11 pub struct Context(Object<ffi::JSCContext, ffi::JSCContextClass>);
12
13 match fn {
14 type_ => || ffi::jsc_context_get_type(),
15 }
16}
17
18impl Context {
19 pub const NONE: Option<&'static Context> = None;
20
21 #[doc(alias = "jsc_context_new")]
22 pub fn new() -> Context {
23 unsafe { from_glib_full(ffi::jsc_context_new()) }
24 }
25
26 #[doc(alias = "jsc_context_new_with_virtual_machine")]
27 #[doc(alias = "new_with_virtual_machine")]
28 pub fn with_virtual_machine(vm: &impl IsA<VirtualMachine>) -> Context {
29 unsafe {
30 from_glib_full(ffi::jsc_context_new_with_virtual_machine(
31 vm.as_ref().to_glib_none().0,
32 ))
33 }
34 }
35
36 pub fn builder() -> ContextBuilder {
41 ContextBuilder::new()
42 }
43
44 #[doc(alias = "jsc_context_get_current")]
45 #[doc(alias = "get_current")]
46 pub fn current() -> Option<Context> {
47 unsafe { from_glib_none(ffi::jsc_context_get_current()) }
48 }
49}
50
51impl Default for Context {
52 fn default() -> Self {
53 Self::new()
54 }
55}
56
57#[must_use = "The builder must be built to be used"]
62pub struct ContextBuilder {
63 builder: glib::object::ObjectBuilder<'static, Context>,
64}
65
66impl ContextBuilder {
67 fn new() -> Self {
68 Self {
69 builder: glib::object::Object::builder(),
70 }
71 }
72
73 pub fn virtual_machine(self, virtual_machine: &impl IsA<VirtualMachine>) -> Self {
74 Self {
75 builder: self
76 .builder
77 .property("virtual-machine", virtual_machine.clone().upcast()),
78 }
79 }
80
81 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
84 pub fn build(self) -> Context {
85 self.builder.build()
86 }
87}
88
89mod sealed {
90 pub trait Sealed {}
91 impl<T: super::IsA<super::Context>> Sealed for T {}
92}
93
94pub trait ContextExt: IsA<Context> + sealed::Sealed + 'static {
95 #[doc(alias = "jsc_context_check_syntax")]
96 fn check_syntax(
97 &self,
98 code: &str,
99 mode: CheckSyntaxMode,
100 uri: &str,
101 line_number: u32,
102 ) -> (CheckSyntaxResult, Exception) {
103 let length = code.len() as _;
104 unsafe {
105 let mut exception = std::ptr::null_mut();
106 let ret = from_glib(ffi::jsc_context_check_syntax(
107 self.as_ref().to_glib_none().0,
108 code.to_glib_none().0,
109 length,
110 mode.into_glib(),
111 uri.to_glib_none().0,
112 line_number,
113 &mut exception,
114 ));
115 (ret, from_glib_full(exception))
116 }
117 }
118
119 #[doc(alias = "jsc_context_clear_exception")]
120 fn clear_exception(&self) {
121 unsafe {
122 ffi::jsc_context_clear_exception(self.as_ref().to_glib_none().0);
123 }
124 }
125
126 #[doc(alias = "jsc_context_evaluate")]
127 fn evaluate(&self, code: &str) -> Option<Value> {
128 let length = code.len() as _;
129 unsafe {
130 from_glib_full(ffi::jsc_context_evaluate(
131 self.as_ref().to_glib_none().0,
132 code.to_glib_none().0,
133 length,
134 ))
135 }
136 }
137
138 #[doc(alias = "jsc_context_evaluate_with_source_uri")]
144 fn evaluate_with_source_uri(&self, code: &str, uri: &str, line_number: u32) -> Option<Value> {
145 let length = code.len() as _;
146 unsafe {
147 from_glib_full(ffi::jsc_context_evaluate_with_source_uri(
148 self.as_ref().to_glib_none().0,
149 code.to_glib_none().0,
150 length,
151 uri.to_glib_none().0,
152 line_number,
153 ))
154 }
155 }
156
157 #[doc(alias = "jsc_context_get_exception")]
158 #[doc(alias = "get_exception")]
159 fn exception(&self) -> Option<Exception> {
160 unsafe {
161 from_glib_none(ffi::jsc_context_get_exception(
162 self.as_ref().to_glib_none().0,
163 ))
164 }
165 }
166
167 #[doc(alias = "jsc_context_get_global_object")]
168 #[doc(alias = "get_global_object")]
169 fn global_object(&self) -> Option<Value> {
170 unsafe {
171 from_glib_full(ffi::jsc_context_get_global_object(
172 self.as_ref().to_glib_none().0,
173 ))
174 }
175 }
176
177 #[doc(alias = "jsc_context_get_value")]
178 #[doc(alias = "get_value")]
179 fn value(&self, name: &str) -> Option<Value> {
180 unsafe {
181 from_glib_full(ffi::jsc_context_get_value(
182 self.as_ref().to_glib_none().0,
183 name.to_glib_none().0,
184 ))
185 }
186 }
187
188 #[doc(alias = "jsc_context_get_virtual_machine")]
189 #[doc(alias = "get_virtual_machine")]
190 fn virtual_machine(&self) -> Option<VirtualMachine> {
191 unsafe {
192 from_glib_none(ffi::jsc_context_get_virtual_machine(
193 self.as_ref().to_glib_none().0,
194 ))
195 }
196 }
197
198 #[doc(alias = "jsc_context_pop_exception_handler")]
199 fn pop_exception_handler(&self) {
200 unsafe {
201 ffi::jsc_context_pop_exception_handler(self.as_ref().to_glib_none().0);
202 }
203 }
204
205 #[doc(alias = "jsc_context_push_exception_handler")]
206 fn push_exception_handler<P: Fn(&Context, &Exception) + 'static>(&self, handler: P) {
207 let handler_data: Box_<P> = Box_::new(handler);
208 unsafe extern "C" fn handler_func<P: Fn(&Context, &Exception) + 'static>(
209 context: *mut ffi::JSCContext,
210 exception: *mut ffi::JSCException,
211 user_data: glib::ffi::gpointer,
212 ) {
213 let context = from_glib_borrow(context);
214 let exception = from_glib_borrow(exception);
215 let callback: &P = &*(user_data as *mut _);
216 (*callback)(&context, &exception)
217 }
218 let handler = Some(handler_func::<P> as _);
219 unsafe extern "C" fn destroy_notify_func<P: Fn(&Context, &Exception) + 'static>(
220 data: glib::ffi::gpointer,
221 ) {
222 let _callback: Box_<P> = Box_::from_raw(data as *mut _);
223 }
224 let destroy_call3 = Some(destroy_notify_func::<P> as _);
225 let super_callback0: Box_<P> = handler_data;
226 unsafe {
227 ffi::jsc_context_push_exception_handler(
228 self.as_ref().to_glib_none().0,
229 handler,
230 Box_::into_raw(super_callback0) as *mut _,
231 destroy_call3,
232 );
233 }
234 }
235
236 #[doc(alias = "jsc_context_set_value")]
242 fn set_value(&self, name: &str, value: &impl IsA<Value>) {
243 unsafe {
244 ffi::jsc_context_set_value(
245 self.as_ref().to_glib_none().0,
246 name.to_glib_none().0,
247 value.as_ref().to_glib_none().0,
248 );
249 }
250 }
251
252 #[doc(alias = "jsc_context_throw")]
253 fn throw(&self, error_message: &str) {
254 unsafe {
255 ffi::jsc_context_throw(
256 self.as_ref().to_glib_none().0,
257 error_message.to_glib_none().0,
258 );
259 }
260 }
261
262 #[doc(alias = "jsc_context_throw_exception")]
263 fn throw_exception(&self, exception: &impl IsA<Exception>) {
264 unsafe {
265 ffi::jsc_context_throw_exception(
266 self.as_ref().to_glib_none().0,
267 exception.as_ref().to_glib_none().0,
268 );
269 }
270 }
271
272 #[doc(alias = "jsc_context_throw_with_name")]
278 fn throw_with_name(&self, error_name: &str, error_message: &str) {
279 unsafe {
280 ffi::jsc_context_throw_with_name(
281 self.as_ref().to_glib_none().0,
282 error_name.to_glib_none().0,
283 error_message.to_glib_none().0,
284 );
285 }
286 }
287
288 }
293
294impl<O: IsA<Context>> ContextExt for O {}