Add new &Module parameter to native functions.

This commit is contained in:
Stephen Chung
2020-07-06 12:04:02 +08:00
parent 495d202af4
commit ea86888638
10 changed files with 107 additions and 99 deletions

View File

@@ -56,19 +56,20 @@ let result: () = engine.call_fn(&mut scope, &ast, "hidden", ())?;
```
`Engine::call_fn_dynamic`
------------------------
Low-Level API - `Engine::call_fn_dynamic`
----------------------------------------
For more control, construct all arguments as `Dynamic` values and use `Engine::call_fn_dynamic`, passing it
anything that implements `IntoIterator<Item = Dynamic>` (such as a simple `Vec<Dynamic>`):
anything that implements `AsMut<Dynamic>` (such as a simple array or a `Vec<Dynamic>`):
```rust
let result: Dynamic = engine.call_fn_dynamic(
&mut scope, // scope to use
&ast, // AST to use
"hello", // function entry-point
None, // 'this' pointer, if any
[ String::from("abc").into(), 123_i64.into() ])?; // arguments
let result = engine.call_fn_dynamic(
&mut scope, // scope to use
ast.into(), // get 'Module' from 'AST'
"hello", // function entry-point
None, // 'this' pointer, if any
[ String::from("abc").into(), 123_i64.into() ] // arguments
)?;
```
@@ -82,8 +83,13 @@ let ast = engine.compile("fn action(x) { this += x; }")?;
let mut value: Dynamic = 1_i64.into();
let result = engine.call_fn_dynamic(&mut scope, &ast, "action", Some(&mut value), [ 41_i64.into() ])?;
// ^^^^^^^^^^^^^^^^ binding the 'this' pointer
let result = engine.call_fn_dynamic(
&mut scope,
ast.into(),
"action",
Some(&mut value), // binding the 'this' pointer
[ 41_i64.into() ]
)?;
assert_eq!(value.as_int().unwrap(), 42);
```