Struct starlark::eval::Evaluator

source ·
pub struct Evaluator<'v, 'a> {
    pub extra: Option<&'a dyn AnyLifetime<'a>>,
    /* private fields */
}
Expand description

Holds everything about an ongoing evaluation (local variables, globals, module resolution etc).

Fields§

§extra: Option<&'a dyn AnyLifetime<'a>>

Field that can be used for any purpose you want (can store types you define). Typically accessed via native functions you also define.

Implementations§

source§

impl<'v, 'a> Evaluator<'v, 'a>

source

pub fn eval_statements(&mut self, statements: AstModule) -> Result<Value<'v>>

Evaluate statements in the existing context. This function is designed for debugging, not production use.

There are lots of health warnings on this code. Might not work with frozen modules, unassigned variables, nested definitions etc. It would be a bad idea to rely on the results of continued execution after evaluating stuff randomly.

source§

impl<'v, 'a> Evaluator<'v, 'a>

source

pub fn local_variables(&self) -> SmallMap<String, Value<'v>>

Obtain the local variables currently in scope. When at top-level these will be Module variables, otherwise local definitions. The precise number of variables may change over time due to optimisation. The only legitimate use of this function is for debugging.

source§

impl<'v, 'a> Evaluator<'v, 'a>

source

pub fn new(module: &'v Module) -> Self

Crate a new Evaluator specifying the Module used for module variables.

If your program contains load() statements, you also need to call set_loader.

source

pub fn disable_gc(&mut self)

Disables garbage collection from now onwards. Cannot be re-enabled. Usually called because you have captured Value’s unsafely, either in global variables or the extra field.

source

pub fn verbose_gc(&mut self)

Enable GC logging.

source

pub fn enable_static_typechecking(&mut self, enable: bool)

Enable static typechecking. For example:

def foo() -> int: return "hello"

would fail when static typechecking is enabled even if foo is never called.

source

pub fn set_loader(&mut self, loader: &'a dyn FileLoader)

Set the FileLoader used to resolve load() statements. A list of all load statements can be obtained through AstModule::loads.

source

pub fn enable_profile(&mut self, mode: &ProfileMode) -> Result<()>

Enable profiling, allowing Evaluator::write_profile to be used. Profilers add overhead, and while some profilers can be used together, it’s better to run at most one profiler at a time.

source

pub fn write_profile<P: AsRef<Path>>(&mut self, filename: P) -> Result<()>

Write a profile to a file. Only valid if corresponding profiler was enabled.

source

pub fn gen_profile(&mut self) -> Result<ProfileData>

Generate profile for a given mode. Only valid if corresponding profiler was enabled.

source

pub fn coverage(&self) -> Result<HashSet<ResolvedFileSpan>>

Get code coverage.

Works if statement profile is enabled.

Note coverage is not precise, because

  • some optimizer transformations may create incorrect spans
  • some optimizer transformations may remove statements
source

pub fn enable_terminal_breakpoint_console(&mut self)

Enable interactive breakpoint(). When enabled, breakpoint() reads commands from stdin and write to stdout. When disabled (default), breakpoint() function results in error.

source

pub fn call_stack(&self) -> CallStack

Obtain the current call-stack, suitable for use in diagnostics.

source

pub fn call_stack_top_frame(&self) -> Option<Frame>

Obtain the top frame on the call-stack. May be None if the call happened via native functions.

source

pub fn call_stack_count(&self) -> usize

Current size (in frames) of the stack.

source

pub fn call_stack_top_location(&self) -> Option<FileSpan>

Obtain the top location on the call-stack. May be None if the call happened via native functions.

source

pub fn set_print_handler(&mut self, handler: &'a (dyn PrintHandler + 'a))

Set the handler invoked when print function is used.

source

pub fn heap(&self) -> &'v Heap

The active heap where Values are allocated.

source

pub fn module(&self) -> &'v Module

Module which was passed to the evaluator.

source

pub fn frozen_heap(&self) -> &'v FrozenHeap

The frozen heap. It’s possible to allocate FrozenValues here, but often not a great idea, as they will remain allocated as long as the results of this execution are required. Suitable for use with add_reference and OwnedFrozenValue::owned_frozen_value.

source

pub fn set_module_variable_at_some_point( &mut self, name: &str, value: Value<'v> ) -> Result<()>

Set a variable in the top-level module currently being processed. This may not be the module the function is being called in.

Any variables which are set will be available in the Module after evaluation returns. If those variables are also existing top-level variables, then the program from that point on will incorporate those values. If they aren’t existing top-level variables, they will be ignored. These details are subject to change. As such, use this API with a healthy dose of caution and in limited settings.

source

pub unsafe fn garbage_collect(&mut self)

Perform a garbage collection. After this operation all Values not reachable from the evaluator will be invalid, and using them will lead to a segfault. Do not call during Starlark evaluation.

source

pub fn set_max_callstack_size(&mut self, stack_size: usize) -> Result<()>

Sets max call stack size. Stack allocation will happen on entry point of evaluation if not allocated yet.

source§

impl<'v, 'a> Evaluator<'v, 'a>

source

pub fn eval_module( &mut self, ast: AstModule, globals: &Globals ) -> Result<Value<'v>>

Evaluate an AstModule with this Evaluator, modifying the in-scope Module as appropriate.

source

pub fn eval_function( &mut self, function: Value<'v>, positional: &[Value<'v>], named: &[(&str, Value<'v>)] ) -> Result<Value<'v>>

Evaluate a function stored in a Value, passing in positional and named arguments.

Trait Implementations§

source§

impl Drop for Evaluator<'_, '_>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'v, 'a> !RefUnwindSafe for Evaluator<'v, 'a>

§

impl<'v, 'a> !Send for Evaluator<'v, 'a>

§

impl<'v, 'a> !Sync for Evaluator<'v, 'a>

§

impl<'v, 'a> Unpin for Evaluator<'v, 'a>

§

impl<'v, 'a> !UnwindSafe for Evaluator<'v, 'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToAst for T

source§

fn ast(self, begin: usize, end: usize) -> Spanned<Self>

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.