From 081f2fdc65b3bd7693c82cf8f019cd87a2990203 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 27 Jul 2018 13:21:04 -0700 Subject: [PATCH] Add `Function` construtor to `js-sys` --- crates/js-sys/src/lib.rs | 11 +++++++++++ crates/test/src/rt/detect.rs | 5 +---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index b68bad5c..5af6e4c8 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -722,6 +722,17 @@ extern "C" { #[derive(Clone, Debug)] pub type Function; + /// The `Function` constructor creates a new `Function` object. Calling the + /// constructor directly can create functions dynamically, but suffers from + /// security and similar (but far less significant) performance issues + /// similar to `eval`. However, unlike `eval`, the `Function` constructor + /// allows executing code in the global scope, prompting better programming + /// habits and allowing for more efficient code minification. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function + #[wasm_bindgen(constructor)] + pub fn new_no_args(body: &str) -> Function; + /// The apply() method calls a function with a given this value, and arguments provided as an array /// (or an array-like object). /// diff --git a/crates/test/src/rt/detect.rs b/crates/test/src/rt/detect.rs index 490d24c9..c29f3724 100644 --- a/crates/test/src/rt/detect.rs +++ b/crates/test/src/rt/detect.rs @@ -5,9 +5,6 @@ use js_sys::{Array, Function}; #[wasm_bindgen] extern { - #[wasm_bindgen(js_name = Function)] - fn new_function(s: &str) -> Function; - type This; #[wasm_bindgen(method, getter, structural, js_name = self)] fn self_(me: &This) -> JsValue; @@ -56,7 +53,7 @@ pub fn is_browser() -> bool { // * Last but not least, test whether `self` is defined or not. // // Whew! - let this = new_function("return this") + let this = Function::new_no_args("return this") .apply(&JsValue::undefined(), &Array::new()) .unwrap(); assert!(this != JsValue::undefined());