mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-17 15:01:23 +00:00
Merge pull request #1492 from RReverser/function-new-with-args
Add bindings for `new Function(args, body)`
This commit is contained in:
@ -987,6 +987,17 @@ extern "C" {
|
|||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub type Function;
|
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
|
/// The `Function` constructor creates a new `Function` object. Calling the
|
||||||
/// constructor directly can create functions dynamically, but suffers from
|
/// constructor directly can create functions dynamically, but suffers from
|
||||||
/// security and similar (but far less significant) performance issues
|
/// security and similar (but far less significant) performance issues
|
||||||
|
@ -101,7 +101,7 @@ fn stringify_with_replacer() {
|
|||||||
assert_eq!(output1, "{\"hello\":\"world\"}");
|
assert_eq!(output1, "{\"hello\":\"world\"}");
|
||||||
|
|
||||||
let replacer_func =
|
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 =
|
let output2: String =
|
||||||
JSON::stringify_with_replacer(&JsValue::from(obj), &JsValue::from(replacer_func))
|
JSON::stringify_with_replacer(&JsValue::from(obj), &JsValue::from(replacer_func))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -164,7 +164,7 @@ fn stringify_with_replacer_and_space() {
|
|||||||
assert_eq!(output2, "{\n \"hello\": \"world\"\n}");
|
assert_eq!(output2, "{\n \"hello\": \"world\"\n}");
|
||||||
|
|
||||||
let replacer_func =
|
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(
|
let output3: String = JSON::stringify_with_replacer_and_space(
|
||||||
&JsValue::from(obj),
|
&JsValue::from(obj),
|
||||||
&JsValue::from(replacer_func),
|
&JsValue::from(replacer_func),
|
||||||
|
Reference in New Issue
Block a user