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
//! Macros

/// Rust string to UTF-8 conversion. See also `utf::u2s`.
///
/// # Example:
///
/// ```macros
/// let (cstr, len) = s2u!("hello"); // ffi::CString
/// libc::printf("%.*hs", len, cstr.as_ptr());
/// ```
///
#[macro_export]
macro_rules! s2u {
	($s:expr) => ( $crate::utf::s2un($s) )
}


/// Rust string to UTF-16 conversion. See also `utf::w2s`.
///
/// # Example:
///
/// ```macros
/// let (cwstr, len) = s2w!("hello"); // Vec<u16>
/// libc::printf("%.*ws", len, cwstr.as_ptr());
/// ```
///
#[macro_export]
macro_rules! s2w {
	($s:expr) => ( $crate::utf::s2vecn($s) )
}


/// UTF-16 to `String` conversion.
#[macro_export]
macro_rules! w2s {
	($s:expr) => ( $crate::utf::w2s($s) )
}


/// UTF-8 to `String` conversion.
#[macro_export]
macro_rules! u2s {
	($s:expr) => ( $crate::utf::u2s($s) )
}


/// Pack arguments to call the sciter script function.
#[doc(hidden)]
#[macro_export]
macro_rules! pack_args {
	() => { $crate::value::Value::pack_args(&[]) };

	( $($s:expr),* ) => {
		{
			let args = [
			$(
				$crate::value::Value::from($s)
			 ),*
			];
			$crate::value::Value::pack_args(&args)
		}
	};
}

/// Make `[Value]` sequence to call the sciter script function. Used in `call_function()` and `call_method()` calls.
#[macro_export]
macro_rules! make_args {
	() => { { let args : [$crate::value::Value; 0] = []; args } };

	( $($s:expr),* ) => {
		{
			let args = [
			$(
				$crate::value::Value::from($s)
			 ),*
			];
			args
		}
	};
}

/// Declare handle type (native pointer).
#[doc(hidden)]
#[macro_export]
macro_rules! MAKE_HANDLE {
	($name:ident, $inner:ident) => {
		#[repr(C)]
		pub struct $inner { _unused: usize }
		pub type $name = *mut $inner;
	};
}

/// Dispatch script calls to native code. Used in [dom::EventHandler](dom/event/trait.EventHandler.html) implementations.
///
/// This macro generates new function which dispatchs incoming script call to native function
/// with arguments unpacking and type checking.
///
/// Note: unstable, will be improved.
#[macro_export]
macro_rules! dispatch_script_call {

	(
		$(
			fn $name:ident ( $( $argt:ident ),* );
		 )*
	) => {

		fn dispatch_script_call(&mut self, root: sciter::HELEMENT, name: &str, argv: &[$crate::Value]) -> Option<$crate::Value>
		{
			match name {
				$(
					stringify!($name) => {

						// args count
						let mut _i = 0;
						$(
							let _: $argt;
							_i += 1;
						)*
						let argc = _i;

						if argv.len() != argc {
							return Some($crate::Value::error(&format!("{} error: {} of {} arguments provided.", stringify!($name), argv.len(), argc)));
						}

						// call function
						let mut _i = 0;
						let rv = self.$name(
							$(
								{
									match $crate::FromValue::from_value(&argv[_i]) {
										Some(arg) => { _i += 1; arg },
										None => {
											// invalid type
											return Some($crate::Value::error(&format!("{} error: invalid type of {} argument ({} expected, {:?} provided).",
												stringify!($name), _i, stringify!($argt), argv[_i])));
										},
									}
								}
							 ),*
						);

						// return result value
						return Some($crate::Value::from(rv));
					},
				 )*

				_ => ()
			};

			// script call not handled
			return None;
		}
	};
}