Skip to content

Commit d312ef8

Browse files
authored
Avoid eval on web (#5313)
1 parent eeb60f7 commit d312ef8

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

packages/web/src/devtools.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ impl Display for ToastLevel {
213213
}
214214
}
215215

216+
#[wasm_bindgen(inline_js = r#"
217+
export function js_show_toast(header_text, message, level, as_ms) {
218+
if (typeof showDXToast !== "undefined") {{
219+
window.showDXToast(header_text, message, level, as_ms);
220+
}}
221+
}
222+
export function js_schedule_toast(header_text, message, level, as_ms) {
223+
if (typeof scheduleDXToast !== "undefined") {{
224+
window.scheduleDXToast(header_text, message, level, as_ms);
225+
}}
226+
}
227+
"#)]
228+
extern "C" {
229+
fn js_schedule_toast(header_text: &str, message: &str, level: String, as_ms: u32);
230+
fn js_show_toast(header_text: &str, message: &str, level: String, as_ms: u32);
231+
}
232+
216233
/// Displays a toast to the developer.
217234
pub(crate) fn show_toast(
218235
header_text: &str,
@@ -221,20 +238,12 @@ pub(crate) fn show_toast(
221238
duration: Duration,
222239
after_reload: bool,
223240
) {
224-
let as_ms = duration.as_millis();
225-
226-
let js_fn_name = match after_reload {
227-
true => "scheduleDXToast",
228-
false => "showDXToast",
229-
};
230-
231-
_ = js_sys::eval(&format!(
232-
r#"
233-
if (typeof {js_fn_name} !== "undefined") {{
234-
window.{js_fn_name}(`{header_text}`, `{message}`, `{level}`, {as_ms});
235-
}}
236-
"#,
237-
));
241+
let as_ms: u32 = duration.as_millis().try_into().unwrap();
242+
243+
match after_reload {
244+
true => js_schedule_toast(header_text, message, level.to_string(), as_ms),
245+
false => js_show_toast(header_text, message, level.to_string(), as_ms),
246+
}
238247
}
239248

240249
/// Force a hotreload of the assets on this page by walking them and changing their URLs to include

0 commit comments

Comments
 (0)