Extract copy/restore of first argument in method call.

This commit is contained in:
Stephen Chung
2020-05-29 18:15:58 +08:00
parent 9616452c00
commit e1242df5c8
5 changed files with 74 additions and 46 deletions

View File

@@ -47,7 +47,7 @@ It doesn't attempt to be a new language. For example:
* No classes. Well, Rust doesn't either. On the other hand...
* No traits... so it is also not Rust. Do your Rusty stuff in Rust.
* No structures - definte your types in Rust instead; Rhai can seamless work with _any Rust type_.
* No structures - define your types in Rust instead; Rhai can seamlessly work with _any Rust type_.
* No first-class functions - Code your functions in Rust instead, and register them with Rhai.
* No closures - do your closure magic in Rust instead; [turn a Rhai scripted function into a Rust closure](#calling-rhai-functions-from-rust).
* It is best to expose an API in Rhai for scripts to call. All your core functionalities should be in Rust.
@@ -2094,14 +2094,24 @@ Members and methods
-------------------
Properties and methods in a Rust custom type registered with the [`Engine`] can be called just like in Rust.
Unlike functions defined in script (for which all arguments are passed by _value_),
native Rust functions may mutate the object (or the first argument if called in normal function call style).
```rust
let a = new_ts(); // constructor function
a.field = 500; // property access
a.update(); // method call, 'a' can be changed
a.field = 500; // property setter
a.update(); // method call, 'a' can be modified
update(a); // this works, but 'a' is unchanged because only
// a COPY of 'a' is passed to 'update' by VALUE
update(a); // <- this de-sugars to 'a.update()' this if 'a' is a simple variable
// unlike scripted functions, 'a' can be modified and is not a copy
let array = [ a ];
update(array[0]); // <- 'array[0]' is an expression returning a calculated value,
// a transient (i.e. a copy) so this statement has no effect
// except waste a lot of time cloning
array[0].update(); // <- call this method-call style will update 'a'
```
Custom types, properties and methods can be disabled via the [`no_object`] feature.