Skip to content

Commit

Permalink
Merge pull request #312 from schungx/master
Browse files Browse the repository at this point in the history
Finalize 0.19.8.
  • Loading branch information
schungx committed Dec 22, 2020
2 parents b1e8f52 + 206400b commit 714b8de
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 65 deletions.
9 changes: 6 additions & 3 deletions RELEASES.md
Expand Up @@ -10,8 +10,9 @@ Each function defined in an `AST` can optionally attach _doc-comments_ (which, a
are comments prefixed by either `///` or `/**`). Doc-comments allow third-party tools to
automatically generate documentation for functions defined in a Rhai script.

A new API, `Engine::gen_fn_metadata_to_json`, paired with the new `metadata` feature,
exports the full list of functions metadata (including those in an `AST`) as a JSON document.
A new API, `Engine::gen_fn_metadata_to_json` and `Engine::gen_fn_metadata_with_ast_to_json`,
paired with the new `metadata` feature, exports the full list of functions metadata
(including those in an `AST`) as a JSON document.

Bug fixes
---------
Expand All @@ -36,7 +37,9 @@ New features

* `AST::iter_functions` now returns `ScriptFnMetadata` which includes, among others, _doc-comments_ for functions prefixed by `///` or `/**`.
* _Doc-comments_ can be enabled/disabled with the new `Engine::set_doc_comments` method.
* A new feature `metadata` is added that pulls in `serde_json` and enables `Engine::gen_fn_metadata_to_json` which exports the full list of functions metadata (including those inside an `AST`) in JSON format.
* A new feature `metadata` is added that pulls in `serde_json` and enables `Engine::gen_fn_metadata_to_json` and ``Engine::gen_fn_metadata_with_ast_to_json` which exports the full list of functions metadata (including those inside an `AST`) in JSON format.
* `Engine::on_debug` provides two additional parameters: `source: Option<&str>` and `pos: Position`.
* `NativeCallContext` and `EvalContext` both expose `source()` which returns the current source, if any.

Enhancements
------------
Expand Down
1 change: 1 addition & 0 deletions doc/src/engine/custom-syntax.md
Expand Up @@ -122,6 +122,7 @@ where:
| &bull; `scope()` | `&Scope` | reference to the current [`Scope`] |
| &bull; `scope_mut()` | `&mut Scope` | mutable reference to the current [`Scope`]; variables can be added to/removed from it |
| &bull; `engine()` | `&Engine` | reference to the current [`Engine`] |
| &bull; `source()` | `Option<&str>` | reference to the current source, if any |
| &bull; `imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
| &bull; `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
| &bull; `this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
Expand Down
1 change: 1 addition & 0 deletions doc/src/engine/var.md
Expand Up @@ -74,6 +74,7 @@ where:
| `context` | `&EvalContext` | reference to the current evaluation _context_ |
| &bull; `scope()` | `&Scope` | reference to the current [`Scope`] |
| &bull; `engine()` | `&Engine` | reference to the current [`Engine`] |
| &bull; `source()` | `Option<&str>` | reference to the current source, if any |
| &bull; `imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
| &bull; `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
| &bull; `this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
Expand Down
9 changes: 5 additions & 4 deletions doc/src/language/fn-ptr.md
Expand Up @@ -228,6 +228,7 @@ of the particular call to a registered Rust function. It is a type that exposes
| Field | Type | Description |
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
| `source()` | `Option<&str>` | reference to the current source, if any |
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |

Expand All @@ -254,11 +255,11 @@ let fn_ptr = engine.eval_ast::<FnPtr>(&ast)?;
// Get rid of the script, retaining only functions
ast.retain_functions(|_, _, _| true);

// Create function namespace from the 'AST'
let lib = [ast.as_ref()];

// Create native call context
let context = NativeCallContext::new(
&engine, // the 'Engine'
&[ast.as_ref()] // function namespace from the 'AST'
);
let context = NativeCallContext::new(&engine, &lib);

// 'f' captures: the engine, the AST, and the closure
let f = move |x: i64| fn_ptr.call_dynamic(context, None, [x.into()]);
Expand Down
1 change: 1 addition & 0 deletions doc/src/plugins/function.md
Expand Up @@ -89,6 +89,7 @@ specially by the plugins system.
| Field | Type | Description |
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
| `source()` | `Option<&str>` | reference to the current source, if any |
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |

Expand Down
1 change: 1 addition & 0 deletions doc/src/plugins/module.md
Expand Up @@ -400,6 +400,7 @@ specially by the plugins system.
| Field | Type | Description |
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
| `source()` | `Option<&str>` | reference to the current source, if any |
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |

Expand Down
1 change: 1 addition & 0 deletions doc/src/rust/register-raw.md
Expand Up @@ -70,6 +70,7 @@ where:
| `T` | `impl Clone` | return type of the function |
| `context` | `NativeCallContext` | the current _native call context_ |
| &bull; `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
| &bull; `source()` | `Option<&str>` | reference to the current source, if any |
| &bull; `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
| &bull; `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
| `args` | `&mut [&mut Dynamic]` | a slice containing `&mut` references to [`Dynamic`] values.<br/>The slice is guaranteed to contain enough arguments _of the correct types_. |
Expand Down
12 changes: 10 additions & 2 deletions src/engine.rs
Expand Up @@ -560,6 +560,11 @@ impl<'e, 'x, 'px, 'a, 's, 'm, 'pm, 't, 'pt> EvalContext<'e, 'x, 'px, 'a, 's, 'm,
pub fn engine(&self) -> &'e Engine {
self.engine
}
/// The current source.
#[inline(always)]
pub fn source<'z: 's>(&'z self) -> Option<&'s str> {
self.state.source.as_ref().map(|s| s.as_str())
}
/// The current [`Scope`].
#[inline(always)]
pub fn scope(&self) -> &Scope<'px> {
Expand Down Expand Up @@ -2002,9 +2007,12 @@ impl Engine {
// Overriding exact implementation
if func.is_plugin_fn() {
func.get_plugin_fn()
.call((self, &*mods, lib).into(), args)?;
.call((self, &state.source, &*mods, lib).into(), args)?;
} else {
func.get_native_fn()((self, &*mods, lib).into(), args)?;
func.get_native_fn()(
(self, &state.source, &*mods, lib).into(),
args,
)?;
}
}
// Built-in op-assignment function
Expand Down
33 changes: 19 additions & 14 deletions src/fn_call.rs
Expand Up @@ -178,7 +178,9 @@ impl Engine {
// Search for the native function
// First search registered functions (can override packages)
// Then search packages
// lib.get_fn(hash_fn, pub_only)
// Finally search modules

//lib.get_fn(hash_fn, pub_only)
let f = self
.global_namespace
.get_fn(hash_fn, pub_only)
Expand All @@ -198,9 +200,10 @@ impl Engine {

// Run external function
let result = if func.is_plugin_fn() {
func.get_plugin_fn().call((self, mods, lib).into(), args)
func.get_plugin_fn()
.call((self, &state.source, mods, lib).into(), args)
} else {
func.get_native_fn()((self, mods, lib).into(), args)
func.get_native_fn()((self, &state.source, mods, lib).into(), args)
};

// Restore the original reference
Expand All @@ -210,17 +213,16 @@ impl Engine {

// See if the function match print/debug (which requires special processing)
return Ok(match fn_name {
KEYWORD_PRINT => (
(self.print)(result.as_str().map_err(|typ| {
KEYWORD_PRINT => {
let text = result.as_str().map_err(|typ| {
EvalAltResult::ErrorMismatchOutputType(
self.map_type_name(type_name::<ImmutableString>()).into(),
typ.into(),
pos,
)
})?)
.into(),
false,
),
})?;
((self.print)(text).into(), false)
}
KEYWORD_DEBUG => {
let text = result.as_str().map_err(|typ| {
EvalAltResult::ErrorMismatchOutputType(
Expand Down Expand Up @@ -534,10 +536,13 @@ impl Engine {
// Get function
let (func, mut source) = lib
.iter()
.find_map(|&m| m.get_fn(hash_script, pub_only).map(|f| (f, m.clone_id())))
.find_map(|&m| {
m.get_fn(hash_script, pub_only)
.map(|f| (f, m.id_raw().clone()))
})
//.or_else(|| self.global_namespace.get_fn(hash_script, pub_only))
.or_else(|| self.packages.get_fn(hash_script).map(|f| (f, None)))
//.or_else(|| mods.iter().find_map(|(_, m)| m.get_qualified_fn(hash_script).map(|f| (f, m.clone_id()))))
//.or_else(|| mods.iter().find_map(|(_, m)| m.get_qualified_fn(hash_script).map(|f| (f, m.id_raw().clone()))))
.unwrap();

if func.is_script() {
Expand Down Expand Up @@ -1176,7 +1181,7 @@ impl Engine {
let new_scope = &mut Default::default();
let fn_def = f.get_fn_def().clone();

let mut source = module.clone_id();
let mut source = module.id_raw().clone();
mem::swap(&mut state.source, &mut source);

let result = self.call_script_fn(
Expand All @@ -1190,7 +1195,7 @@ impl Engine {
Some(f) if f.is_plugin_fn() => f
.get_plugin_fn()
.clone()
.call((self, &*mods, lib).into(), args.as_mut()),
.call((self, module.id_raw(), &*mods, lib).into(), args.as_mut()),
Some(f) if f.is_native() => {
if !f.is_method() {
// Clone first argument
Expand All @@ -1201,7 +1206,7 @@ impl Engine {
}
}

f.get_native_fn()((self, &*mods, lib).into(), args.as_mut())
f.get_native_fn()((self, module.id_raw(), &*mods, lib).into(), args.as_mut())
}
Some(_) => unreachable!(),
None if def_val.is_some() => Ok(def_val.unwrap().clone()),
Expand Down

0 comments on commit 714b8de

Please sign in to comment.