1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Embedding hook: let a host program expose native functions to a script.
//!
//! TextWrite itself stays dependency-free, but it is often embedded in a larger
//! program (an editor, a game, a web page) that wants to expose its own
//! functions to scripts. Implement [`Host`] and install it with
//! [`Interpreter::set_host`](crate::Interpreter::set_host); host functions are
//! then callable exactly like builtins and take precedence over them.
//!
//! ```no_run
//! use std::cell::RefCell;
//! use std::rc::Rc;
//! use textwrite::{Host, Interpreter, Span, Value};
//!
//! struct Greeter;
//! impl Host for Greeter {
//! fn functions(&self) -> &'static [&'static str] {
//! &["greet"]
//! }
//! fn call(&mut self, name: &str, args: Vec<Value>, span: Span) -> textwrite::Result<Value> {
//! let _ = span;
//! match name {
//! "greet" => Ok(Value::str(format!("hello, {}", args[0].to_display()))),
//! _ => unreachable!(),
//! }
//! }
//! }
//!
//! let mut tw = Interpreter::new();
//! tw.set_host(Rc::new(RefCell::new(Greeter)));
//! let v = tw.eval_expression("greet(\"world\")").unwrap();
//! assert_eq!(v.to_display(), "hello, world");
//! ```
use RefCell;
use Rc;
use crate;
use crateValue;
/// A provider of native functions available to scripts.
/// A shared reference to a host.
pub type HostRef = ;