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
use Size;
use crate::;
use ;
/// Sets the document title.
/// Open a URL in a new tab or the current tab.
/// Returns `true` if the screen is a mobile device.
/// Returns the number of characters that can fit in the window (viewport of the browser or terminal).
/// Returns the number of characters that can fit in the screen (entire physical display).
/// Calls a global JavaScript function by name, with a custom `this` context and an arbitrary number of arguments.
///
/// This function looks up the property `window[name]` on the global window, checks that it is a JavaScript
/// function, and then calls it using `Function.prototype.apply`.
///
/// # Parameters
///
/// * `name` - The name of the JavaScript function (as a property on the global `window` object).
/// * `this` - The value to use as the `this` binding during the function invocation.
/// * `args` - An iterable list of arguments to pass to the function.
///
/// # Type Parameters
///
/// * `S: AsRef<str>` – A type that can be converted to a string slice; used for the name of the function.
/// * `T: Into<JsValue>` – A type that can be converted into a `JsValue`, representing the `this` binding.
/// * `I: IntoIterator` – An iterable collection of function arguments.
/// * `I::Item: Into<JsValue>` – Each argument can be converted into a `JsValue`.
///
/// # Errors
///
/// * `Error::UnableToRetrieveWindow` if the global `window` object cannot be retrieved.
/// * `Error::JsValue(JsValue)` if any of the following JavaScript operations throw:
/// - retrieving the property via `Reflect::get`
/// - converting the property to a [`Function`] via `.dyn_into::<Function>()`
/// - invoking the function via `Function::apply`
///
/// # Examples
///
/// Calling a global JS function with a custom context:
///
/// ```no_run
/// # use web_sys::wasm_bindgen::JsValue;
/// # use ratzilla::utils::call_js_function_with_context;
/// # use ratzilla::error::Error;
/// use web_sys::js_sys::Object;
/// # fn example() -> Result<(), Error> {
/// // Suppose `myObj` is a JS object you want to be the `this` value.
/// let my_obj = JsValue::from(Object::new());
/// let result = call_js_function_with_context(
/// "myJsFunction", // JavaScript global function name
/// my_obj, // custom `this` context
/// [JsValue::from(42), JsValue::from("hello")] // arguments
/// )?;
/// # Ok(())
/// # }
/// ```
///
/// Calling a function without caring about `this`:
///
/// ```no_run
/// # use ratzilla::utils::call_js_function_with_context;
/// # use ratzilla::error::Error;
/// # use ratzilla::utils::call_js_function;
/// # use web_sys::wasm_bindgen::JsValue;
/// # fn example() -> Result<(), Error> {
/// // This will set `this` to `null` which in non-strict mode defaults to the global object.
/// let result = call_js_function_with_context("alert", JsValue::NULL, ["Hello from Rust"])?;
/// //Note that we also have a `call_js_function` function that defaults `this` to `null`.
/// let result = call_js_function("alert", ["Hello from Rust"])?;
/// // This is equivalent to the above call.
/// # Ok(())
/// # }
/// ```
/// Calls a global JavaScript function by name, defaulting the `this` context to `null`.
///
/// This is a convenience wrapper around [`call_js_function_with_context`]. It allows callers to
/// simply provide the function name and an iterable of arguments without worrying about the `this`
/// binding. Passing `null` as `this` means that in non‑strict mode, JavaScript will default to
/// using the global `window` object.
///
/// # Parameters
///
/// * `name` - The name of the JavaScript function (a property on the global `window` object).
/// * `args` - An iterable list of arguments to pass to the function call.
///
/// # Type Parameters
///
/// * `S: AsRef<str>` – Type for representing the name of the function.
/// * `I: IntoIterator` – An iterable collection of function arguments.
/// * `I::Item: Into<JsValue>` – Each argument can be converted into a `JsValue`.
///
/// # Errors
/// See [`call_js_function_with_context`] for error details.
///
/// # Examples
///
/// Calling a global function like `alert`:
///
/// ```no_run
/// # use ratzilla::utils::call_js_function;
/// # use ratzilla::error::Error;
/// # fn example() -> Result<(), Error> {
/// // Calls alert("Hello World!") on the global window.
/// call_js_function("alert", ["Hello World!"])?;
/// # Ok(())
/// # }
/// ```