Skip to content

Commit

Permalink
Merge pull request #1492 from RReverser/function-new-with-args
Browse files Browse the repository at this point in the history
Add bindings for `new Function(args, body)`
  • Loading branch information
alexcrichton authored Apr 26, 2019
2 parents 4865b08 + 814f576 commit 36c0a13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/js-sys/tests/wasm/JSON.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 36c0a13

Please sign in to comment.