Add bindings for new Function(args, body)

We don't support variadic args in front, but, luckily for us, `new Function` accepts comma-separated args as a single string as well (see updated JSON test for an example).
This commit is contained in:
Ingvar Stepanyan 2019-04-26 17:42:23 +01:00
parent 26f9d86f62
commit 814f576b1d
2 changed files with 13 additions and 2 deletions

View File

@ -987,6 +987,17 @@ extern "C" {
#[derive(Clone, Debug, PartialEq, Eq)]
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.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
#[wasm_bindgen(constructor)]
pub fn new_with_args(args: &str, body: &str) -> 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

View File

@ -101,7 +101,7 @@ fn stringify_with_replacer() {
assert_eq!(output1, "{\"hello\":\"world\"}");
let replacer_func =
Function::new_no_args("return arguments[0] === 'hello' ? undefined : arguments[1]");
Function::new_with_args("key, value", "return key === 'hello' ? undefined : value");
let output2: String =
JSON::stringify_with_replacer(&JsValue::from(obj), &JsValue::from(replacer_func))
.unwrap()
@ -164,7 +164,7 @@ fn stringify_with_replacer_and_space() {
assert_eq!(output2, "{\n \"hello\": \"world\"\n}");
let replacer_func =
Function::new_no_args("return arguments[0] === 'hello' ? undefined : arguments[1]");
Function::new_with_args("key, value", "return key === 'hello' ? undefined : value");
let output3: String = JSON::stringify_with_replacer_and_space(
&JsValue::from(obj),
&JsValue::from(replacer_func),