Reduce duplicated generics code.
This commit is contained in:
parent
09da9ddcbc
commit
7956f9d59c
@ -1338,6 +1338,7 @@ impl Engine {
|
|||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
#[inline(always)]
|
||||||
pub fn parse_json(
|
pub fn parse_json(
|
||||||
&self,
|
&self,
|
||||||
json: impl AsRef<str>,
|
json: impl AsRef<str>,
|
||||||
@ -1345,10 +1346,12 @@ impl Engine {
|
|||||||
) -> Result<Map, Box<EvalAltResult>> {
|
) -> Result<Map, Box<EvalAltResult>> {
|
||||||
use crate::token::Token;
|
use crate::token::Token;
|
||||||
|
|
||||||
let json = json.as_ref();
|
fn parse_json_inner(
|
||||||
|
engine: &Engine,
|
||||||
|
json: &str,
|
||||||
|
has_null: bool,
|
||||||
|
) -> Result<Map, Box<EvalAltResult>> {
|
||||||
let mut scope = Default::default();
|
let mut scope = Default::default();
|
||||||
|
|
||||||
// Trims the JSON string and add a '#' in front
|
|
||||||
let json_text = json.trim_start();
|
let json_text = json.trim_start();
|
||||||
let scripts = if json_text.starts_with(Token::MapStart.literal_syntax()) {
|
let scripts = if json_text.starts_with(Token::MapStart.literal_syntax()) {
|
||||||
[json_text, ""]
|
[json_text, ""]
|
||||||
@ -1362,8 +1365,7 @@ impl Engine {
|
|||||||
.into_err(Position::new(1, (json.len() - json_text.len() + 1) as u16))
|
.into_err(Position::new(1, (json.len() - json_text.len() + 1) as u16))
|
||||||
.into());
|
.into());
|
||||||
};
|
};
|
||||||
|
let (stream, tokenizer_control) = engine.lex_raw(
|
||||||
let (stream, tokenizer_control) = self.lex_raw(
|
|
||||||
&scripts,
|
&scripts,
|
||||||
Some(if has_null {
|
Some(if has_null {
|
||||||
|token| match token {
|
|token| match token {
|
||||||
@ -1375,22 +1377,20 @@ impl Engine {
|
|||||||
|t| t
|
|t| t
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
let mut state = ParseState::new(engine, tokenizer_control);
|
||||||
let mut state = ParseState::new(self, tokenizer_control);
|
let ast = engine.parse_global_expr(
|
||||||
|
|
||||||
let ast = self.parse_global_expr(
|
|
||||||
&mut stream.peekable(),
|
&mut stream.peekable(),
|
||||||
&mut state,
|
&mut state,
|
||||||
&scope,
|
&scope,
|
||||||
OptimizationLevel::None,
|
OptimizationLevel::None,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Handle null - map to ()
|
|
||||||
if has_null {
|
if has_null {
|
||||||
scope.push_constant("null", ());
|
scope.push_constant("null", ());
|
||||||
}
|
}
|
||||||
|
engine.eval_ast_with_scope(&mut scope, &ast)
|
||||||
|
}
|
||||||
|
|
||||||
self.eval_ast_with_scope(&mut scope, &ast)
|
parse_json_inner(self, json.as_ref(), has_null)
|
||||||
}
|
}
|
||||||
/// Compile a string containing an expression into an [`AST`],
|
/// Compile a string containing an expression into an [`AST`],
|
||||||
/// which can be used later for evaluation.
|
/// which can be used later for evaluation.
|
||||||
|
@ -192,15 +192,19 @@ impl<'a> NativeCallContext<'a> {
|
|||||||
///
|
///
|
||||||
/// If `is_method` is [`true`], the first argument is assumed to be passed
|
/// If `is_method` is [`true`], the first argument is assumed to be passed
|
||||||
/// by reference and is not consumed.
|
/// by reference and is not consumed.
|
||||||
#[inline]
|
#[inline(always)]
|
||||||
pub fn call_fn_dynamic_raw(
|
pub fn call_fn_dynamic_raw(
|
||||||
&self,
|
&self,
|
||||||
fn_name: impl AsRef<str>,
|
fn_name: impl AsRef<str>,
|
||||||
is_method_call: bool,
|
is_method_call: bool,
|
||||||
args: &mut [&mut Dynamic],
|
args: &mut [&mut Dynamic],
|
||||||
) -> RhaiResult {
|
) -> RhaiResult {
|
||||||
let fn_name = fn_name.as_ref();
|
fn call_fn_dynamic_inner(
|
||||||
|
context: &NativeCallContext,
|
||||||
|
is_method_call: bool,
|
||||||
|
fn_name: &str,
|
||||||
|
args: &mut [&mut Dynamic],
|
||||||
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let hash = if is_method_call {
|
let hash = if is_method_call {
|
||||||
FnCallHashes::from_script_and_native(
|
FnCallHashes::from_script_and_native(
|
||||||
calc_fn_hash(fn_name, args.len() - 1),
|
calc_fn_hash(fn_name, args.len() - 1),
|
||||||
@ -209,12 +213,12 @@ impl<'a> NativeCallContext<'a> {
|
|||||||
} else {
|
} else {
|
||||||
FnCallHashes::from_script(calc_fn_hash(fn_name, args.len()))
|
FnCallHashes::from_script(calc_fn_hash(fn_name, args.len()))
|
||||||
};
|
};
|
||||||
|
context
|
||||||
self.engine()
|
.engine()
|
||||||
.exec_fn_call(
|
.exec_fn_call(
|
||||||
&mut self.mods.cloned().unwrap_or_default(),
|
&mut context.mods.cloned().unwrap_or_default(),
|
||||||
&mut Default::default(),
|
&mut Default::default(),
|
||||||
self.lib,
|
context.lib,
|
||||||
fn_name,
|
fn_name,
|
||||||
hash,
|
hash,
|
||||||
args,
|
args,
|
||||||
@ -226,6 +230,9 @@ impl<'a> NativeCallContext<'a> {
|
|||||||
)
|
)
|
||||||
.map(|(r, _)| r)
|
.map(|(r, _)| r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
call_fn_dynamic_inner(self, is_method_call, fn_name.as_ref(), args)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consume a [`Shared`] resource and return a mutable reference to the wrapped value.
|
/// Consume a [`Shared`] resource and return a mutable reference to the wrapped value.
|
||||||
|
Loading…
Reference in New Issue
Block a user