ion_proc/class/
constructor.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 proc_macro2::TokenStream;
8use quote::format_ident;
9use syn::{ItemFn, Result, Type, parse_quote};
10
11use crate::class::method::{Method, MethodReceiver};
12use crate::function::wrapper::impl_wrapper_fn;
13use crate::function::{check_abi, set_signature};
14
15pub(super) fn impl_constructor(ion: &TokenStream, mut constructor: ItemFn, ty: &Type, class: &str) -> Result<Method> {
16	let (wrapper, parameters) = impl_wrapper_fn(
17		ion,
18		constructor.clone(),
19		Some(ty),
20		true,
21		&format!("{class} constructor"),
22	)?;
23
24	check_abi(&mut constructor)?;
25	set_signature(&mut constructor);
26	constructor.attrs.clear();
27
28	let body = parse_quote!({
29		#wrapper
30
31		let cx = &#ion::Context::new_unchecked(cx);
32		let args = &mut #ion::Arguments::new(cx, argc, vp);
33
34		unsafe {
35			#ion::class::construct_native_object::<#ty, _>(
36				cx, args, |cx, args, this| unsafe { wrapper(cx, args, this) }
37			)
38		}
39	});
40	constructor.block = body;
41	constructor.sig.ident = format_ident!("__ion_bindings_constructor", span = constructor.sig.ident.span());
42
43	let method = Method {
44		receiver: MethodReceiver::Static,
45		method: constructor,
46		nargs: parameters.nargs,
47		names: vec![],
48	};
49	Ok(method)
50}