runtime/globals/
microtasks.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use ion::flags::PropertyFlags;
8use ion::{Context, Error, Function, Object, Result, function_spec, js_fn};
9use mozjs::jsapi::JSFunctionSpec;
10
11use crate::ContextExt as _;
12use crate::event_loop::microtasks::Microtask;
13
14#[js_fn]
15fn queue_microtask(cx: &Context, callback: Function) -> Result<()> {
16	let event_loop = unsafe { &mut cx.get_private().event_loop };
17	if let Some(queue) = &mut event_loop.microtasks {
18		queue.enqueue(cx, Microtask::User(callback.get()));
19		Ok(())
20	} else {
21		Err(Error::new("Microtask Queue has not been initialised.", None))
22	}
23}
24
25const FUNCTION: JSFunctionSpec = function_spec!(queue_microtask, c"queueMicrotask", 0);
26
27pub fn define(cx: &Context, global: &Object) -> bool {
28	global.define_as(
29		cx,
30		"queueMicrotask",
31		&Function::from_spec(cx, &FUNCTION),
32		PropertyFlags::CONSTANT_ENUMERATED,
33	)
34}