logo
  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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Macros

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

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


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

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


/// 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)
		}
	};
}

/// Pack arguments into a `[Value]` array to call Sciter script functions.
///
/// Used in [`Element.call_function()`](dom/struct.Element.html#method.call_function),
/// [`Element.call_method()`](dom/struct.Element.html#method.call_method),
/// [`Host.call_function()`](host/struct.Host.html#method.call_function),
/// [`Value.call()`](value/struct.Value.html#method.call).
///
/// ## Example:
///
/// ```rust,ignore
/// # #![doc(test(no_crate_inject))]
/// # #[macro_use] extern crate sciter;
/// let value = sciter::Value::new();
/// let result = value.call(None, &make_args!(1, "2", 3.0), Some(file!())).unwrap();
/// ```
#[macro_export]
macro_rules! make_args {
	() => { { let args : [$crate::value::Value; 0] = []; args } };

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

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

/// Dispatch script calls to native code. Used in [`dom::EventHandler`](dom/event/trait.EventHandler.html) implementations.
///
/// This macro generates a new function which dispatches incoming script calls to the corresponding native functions
/// 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: $crate::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;
		}
	};
}


/// Create a `sciter::Value` (of map type) from a list of key-value pairs.
///
/// # Example
///
/// ```rust
/// # #[macro_use] extern crate sciter;
/// # fn main() {
/// let v: sciter::Value = vmap! {
///   "one" => 1,
///   "two" => 2.0,
///   "three" => "",
/// };
/// assert!(v.is_map());
/// assert_eq!(v.len(), 3);
/// # }
/// ```
#[macro_export]
macro_rules! vmap {
  ( $($key:expr => $value:expr,)+ ) => { vmap!($($key => $value),+)  };
  ( $($key:expr => $value:expr),* ) => {
    {
      let mut _v = $crate::Value::map();
      $(
        _v.set_item($key, $value);
      )*
      _v
    }
  };
}

/// Creates a `sciter::Value` (of array type) containing the arguments.
///
/// # Example
///
/// ```rust
/// # #[macro_use] extern crate sciter;
/// # fn main() {
/// let v: sciter::Value = varray![1, 2.0, "three"];
/// assert!(v.is_array());
/// assert_eq!(v.len(), 3);
/// # }
/// ```
#[macro_export]
macro_rules! varray {
  ( $($value:expr,)+ ) => { varray!($($value),+) };
  ( $($value:expr),* ) => {
    {
      // args count
      let mut _i = 0;
      $(
        let _ = &$value;
        _i += 1;
      )*
      let argc = _i;
      let mut _v = $crate::Value::array(argc);
      let mut _i = 0;
      $(
        _v.set(_i, $value);
        _i += 1;
      )*
      _v
    }
  };
}