Skip to content

Commit

Permalink
Always use Engine's strings interner.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Feb 19, 2024
1 parent 54d1484 commit 6fbd4fc
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 266 deletions.
19 changes: 3 additions & 16 deletions src/api/compile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Module that defines the public compilation API of [`Engine`].
use crate::func::native::locked_write;
use crate::parser::{ParseResult, ParseState};
use crate::{Engine, OptimizationLevel, Scope, AST};
#[cfg(feature = "no_std")]
Expand Down Expand Up @@ -219,19 +218,13 @@ impl Engine {
) -> ParseResult<AST> {
let (stream, tc) = self.lex(scripts.as_ref());

let guard = &mut self
.interned_strings
.as_ref()
.and_then(|interner| locked_write(interner));
let interned_strings = guard.as_deref_mut();

let input = &mut stream.peekable();
let lib = &mut <_>::default();
let state = &mut ParseState::new(scope, interned_strings, input, tc, lib);
let state = ParseState::new(scope, input, tc.clone(), lib);
let mut _ast = self.parse(state, optimization_level)?;
#[cfg(feature = "metadata")]
{
let global_comments = &state.tokenizer_control.borrow().global_comments;
let global_comments = &tc.borrow().global_comments;
_ast.doc = global_comments.into();
}
Ok(_ast)
Expand Down Expand Up @@ -299,15 +292,9 @@ impl Engine {
let scripts = [script];
let (stream, t) = self.lex(&scripts);

let guard = &mut self
.interned_strings
.as_ref()
.and_then(|interner| locked_write(interner));
let interned_strings = guard.as_deref_mut();

let input = &mut stream.peekable();
let lib = &mut <_>::default();
let state = &mut ParseState::new(Some(scope), interned_strings, input, t, lib);
let state = ParseState::new(Some(scope), input, t, lib);

self.parse_global_expr(state, |_| {}, self.optimization_level)
}
Expand Down
9 changes: 1 addition & 8 deletions src/api/eval.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Module that defines the public evaluation API of [`Engine`].
use crate::eval::{Caches, GlobalRuntimeState};
use crate::func::native::locked_write;
use crate::parser::ParseState;
use crate::types::dynamic::Variant;
use crate::{Dynamic, Engine, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR};
Expand Down Expand Up @@ -114,17 +113,11 @@ impl Engine {
) -> RhaiResultOf<T> {
let scripts = [script];
let ast = {
let guard = &mut self
.interned_strings
.as_ref()
.and_then(|interner| locked_write(interner));
let interned_strings = guard.as_deref_mut();

let (stream, tc) = self.lex(&scripts);

let input = &mut stream.peekable();
let lib = &mut <_>::default();
let state = &mut ParseState::new(Some(scope), interned_strings, input, tc, lib);
let state = ParseState::new(Some(scope), input, tc, lib);

// No need to optimize a lone expression
self.parse_global_expr(
Expand Down
16 changes: 4 additions & 12 deletions src/api/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Module that provide formatting services to the [`Engine`].
use crate::func::locked_write;
use crate::packages::iter_basic::{BitRange, CharsStream, StepRange};
use crate::parser::{ParseResult, ParseState};
use crate::{
Expand Down Expand Up @@ -265,26 +264,19 @@ impl Engine {
tc.borrow_mut().compressed = Some(String::new());
stream.state.last_token = Some(SmartString::new_const());

let guard = &mut self
.interned_strings
.as_ref()
.and_then(|interner| locked_write(interner));
let interned_strings = guard.as_deref_mut();

let input = &mut stream.peekable();
let lib = &mut <_>::default();
let mut state = ParseState::new(None, interned_strings, input, tc, lib);
let state = ParseState::new(None, input, tc.clone(), lib);

let mut _ast = self.parse(
&mut state,
state,
#[cfg(not(feature = "no_optimize"))]
crate::OptimizationLevel::None,
#[cfg(feature = "no_optimize")]
(),

Check failure on line 276 in src/api/formatting.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_time,no_function,no_float,no_position,no_inde...

mismatched types

Check failure on line 276 in src/api/formatting.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,sync,no_time,no_function,no_float,no_position,no...

mismatched types

Check failure on line 276 in src/api/formatting.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_optimize,serde,metadata,internals,debugging, ...

mismatched types
)?;

let tc = state.tokenizer_control.borrow();

Ok(tc.compressed.as_ref().unwrap().into())
let guard = tc.borrow();
Ok(guard.compressed.as_ref().unwrap().into())
}
}
9 changes: 1 addition & 8 deletions src/api/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Module that defines JSON manipulation functions for [`Engine`].
#![cfg(not(feature = "no_object"))]

use crate::func::native::locked_write;
use crate::parser::{ParseSettingFlags, ParseState};
use crate::tokenizer::Token;
use crate::types::dynamic::Union;
Expand Down Expand Up @@ -117,15 +116,9 @@ impl Engine {
);

let ast = {
let guard = &mut self
.interned_strings
.as_ref()
.and_then(|interner| locked_write(interner));
let interned_strings = guard.as_deref_mut();

let input = &mut stream.peekable();
let lib = &mut <_>::default();
let state = &mut ParseState::new(None, interned_strings, input, tokenizer_control, lib);
let state = ParseState::new(None, input, tokenizer_control, lib);

self.parse_global_expr(
state,
Expand Down
9 changes: 1 addition & 8 deletions src/api/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Module that defines the public evaluation API of [`Engine`].
use crate::eval::Caches;
use crate::func::native::locked_write;
use crate::parser::ParseState;
use crate::{Engine, RhaiResultOf, Scope, AST};
#[cfg(feature = "no_std")]
Expand Down Expand Up @@ -60,15 +59,9 @@ impl Engine {
let ast = {
let (stream, tc) = self.lex(&scripts);

let guard = &mut self
.interned_strings
.as_ref()
.and_then(|interner| locked_write(interner));
let interned_strings = guard.as_deref_mut();

let input = &mut stream.peekable();
let lib = &mut <_>::default();
let state = &mut ParseState::new(Some(scope), interned_strings, input, tc, lib);
let state = ParseState::new(Some(scope), input, tc, lib);

self.parse(state, self.optimization_level)?
};
Expand Down
4 changes: 2 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use stmt::{
SwitchCasesCollection,
};

/// _(internals)_ Placeholder for a script-defined function.
/// _(internals)_ Empty placeholder for a script-defined function.
/// Exported under the `internals` feature only.
#[cfg(feature = "no_function")]
pub type ScriptFuncDef = ();
pub struct ScriptFuncDef;
35 changes: 35 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,41 @@ impl Engine {
None => string.into(),
}
}
/// Get an interned property getter, creating one if it is not yet interned.
#[cfg(not(feature = "no_object"))]
#[inline]
#[must_use]
pub(crate) fn get_interned_getter(
&self,
text: impl AsRef<str> + Into<ImmutableString>,
) -> ImmutableString {
match self.interned_strings {
Some(ref interner) => locked_write(interner).unwrap().get_with_mapper(
b'g',
|s| make_getter(s.as_ref()).into(),
text,
),
None => make_getter(text.as_ref()).into(),
}
}

/// Get an interned property setter, creating one if it is not yet interned.
#[cfg(not(feature = "no_object"))]
#[inline]
#[must_use]
pub(crate) fn get_interned_setter(
&self,
text: impl AsRef<str> + Into<ImmutableString>,
) -> ImmutableString {
match self.interned_strings {
Some(ref interner) => locked_write(interner).unwrap().get_with_mapper(
b's',
|s| make_setter(s.as_ref()).into(),
text,
),
None => make_setter(text.as_ref()).into(),
}
}

/// Get an empty [`ImmutableString`] which refers to a shared instance.
#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ pub use module::resolvers as module_resolvers;
#[cfg(not(feature = "no_optimize"))]
pub use optimizer::OptimizationLevel;

/// Placeholder for the optimization level.
/// Empty placeholder for the optimization level.
#[cfg(feature = "no_optimize")]
type OptimizationLevel = ();
struct OptimizationLevel;

// Expose internal data structures.

Expand Down
Loading

0 comments on commit 6fbd4fc

Please sign in to comment.