Merge branch 'master' into plugins_dev
This commit is contained in:
20
src/api.rs
20
src/api.rs
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::any::{Dynamic, Variant};
|
||||
use crate::engine::{Engine, Imports, State};
|
||||
use crate::error::ParseError;
|
||||
use crate::error::{ParseError, ParseErrorType};
|
||||
use crate::fn_native::{IteratorFn, SendSync};
|
||||
use crate::module::{FuncReturn, Module};
|
||||
use crate::optimize::OptimizationLevel;
|
||||
@@ -895,6 +895,8 @@ impl Engine {
|
||||
|
||||
/// Parse a JSON string into a map.
|
||||
///
|
||||
/// The JSON string must be an object hash. It cannot be a simple JavaScript primitive.
|
||||
///
|
||||
/// Set `has_null` to `true` in order to map `null` values to `()`.
|
||||
/// Setting it to `false` will cause a _variable not found_ error during parsing.
|
||||
///
|
||||
@@ -934,12 +936,20 @@ impl Engine {
|
||||
let mut scope = Scope::new();
|
||||
|
||||
// Trims the JSON string and add a '#' in front
|
||||
let json = json.trim();
|
||||
let scripts = if json.starts_with(Token::MapStart.syntax().as_ref()) {
|
||||
[json, ""]
|
||||
let json_text = json.trim_start();
|
||||
let scripts = if json_text.starts_with(Token::MapStart.syntax().as_ref()) {
|
||||
[json_text, ""]
|
||||
} else if json_text.starts_with(Token::LeftBrace.syntax().as_ref()) {
|
||||
["#", json_text]
|
||||
} else {
|
||||
["#", json]
|
||||
return Err(ParseErrorType::MissingToken(
|
||||
Token::LeftBrace.syntax().to_string(),
|
||||
"to start a JSON object hash".to_string(),
|
||||
)
|
||||
.into_err(Position::new(1, (json.len() - json_text.len() + 1) as u16))
|
||||
.into());
|
||||
};
|
||||
|
||||
let stream = lex(
|
||||
&scripts,
|
||||
if has_null {
|
||||
|
@@ -525,13 +525,13 @@ impl Engine {
|
||||
|
||||
let result = if _is_method {
|
||||
// Method call of script function - map first argument to `this`
|
||||
let (first, rest) = args.split_at_mut(1);
|
||||
let (first, rest) = args.split_first_mut().unwrap();
|
||||
self.call_script_fn(
|
||||
scope,
|
||||
mods,
|
||||
state,
|
||||
lib,
|
||||
&mut Some(first[0]),
|
||||
&mut Some(*first),
|
||||
fn_name,
|
||||
func,
|
||||
rest,
|
||||
@@ -978,6 +978,7 @@ impl Engine {
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let modules = modules.as_ref().unwrap();
|
||||
let mut arg_values: StaticVec<_>;
|
||||
let mut first_arg_value = None;
|
||||
let mut args: StaticVec<_>;
|
||||
|
||||
if args_expr.is_empty() {
|
||||
@@ -992,17 +993,28 @@ impl Engine {
|
||||
Expr::Variable(x) if x.1.is_none() => {
|
||||
arg_values = args_expr
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|expr| self.eval_expr(scope, mods, state, lib, this_ptr, expr, level))
|
||||
.enumerate()
|
||||
.map(|(i, expr)| {
|
||||
// Skip the first argument
|
||||
if i == 0 {
|
||||
Ok(Default::default())
|
||||
} else {
|
||||
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)
|
||||
}
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
// Get target reference to first argument
|
||||
let (target, _, _, pos) =
|
||||
search_scope_only(scope, state, this_ptr, args_expr.get(0).unwrap())?;
|
||||
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| err.new_position(pos))?;
|
||||
|
||||
args = once(target).chain(arg_values.iter_mut()).collect();
|
||||
let (first, rest) = arg_values.split_first_mut().unwrap();
|
||||
first_arg_value = Some(first);
|
||||
|
||||
args = once(target).chain(rest.iter_mut()).collect();
|
||||
}
|
||||
// func(..., ...) or func(mod::x, ...)
|
||||
_ => {
|
||||
@@ -1041,6 +1053,13 @@ impl Engine {
|
||||
match func {
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Some(f) if f.is_script() => {
|
||||
// Clone first argument
|
||||
if let Some(first) = first_arg_value {
|
||||
let first_val = args[0].clone();
|
||||
args[0] = first;
|
||||
*args[0] = first_val;
|
||||
}
|
||||
|
||||
let args = args.as_mut();
|
||||
let func = f.get_fn_def();
|
||||
|
||||
@@ -1060,7 +1079,19 @@ impl Engine {
|
||||
self.call_script_fn(scope, mods, state, lib, &mut None, name, func, args, level)
|
||||
}
|
||||
Some(f) if f.is_plugin_fn() => f.get_plugin_fn().call(args.as_mut(), Position::none()),
|
||||
Some(f) => f.get_native_fn()(self, lib, args.as_mut()),
|
||||
Some(f) if f.is_native() => {
|
||||
if !f.is_method() {
|
||||
// Clone first argument
|
||||
if let Some(first) = first_arg_value {
|
||||
let first_val = args[0].clone();
|
||||
args[0] = first;
|
||||
*args[0] = first_val;
|
||||
}
|
||||
}
|
||||
|
||||
f.get_native_fn()(self, lib, args.as_mut())
|
||||
}
|
||||
Some(_) => unreachable!(),
|
||||
None if def_val.is_some() => Ok(def_val.unwrap().into()),
|
||||
None => EvalAltResult::ErrorFunctionNotFound(
|
||||
format!(
|
||||
|
Reference in New Issue
Block a user