Skip to main content

EvalContext

Struct EvalContext 

Source
pub struct EvalContext {
    pub global_this: Option<JsValue>,
    pub heap: SharedHeap,
    pub lex_env: JsLexEnvironmentType,
    pub var_env: JsLexEnvironmentType,
    pub ctx_stack: ExecutionContextStack,
    pub strict: bool,
    pub lex_env_version: u64,
    pub super_global: SharedSuperGlobal,
}
Expand description

Execution context for JavaScript evaluation.

Contains the lexical environment chain, heap, and super-global scope. This is the main context object passed through the interpreter and VMs.

§Examples

use just::parser::JsParser;
use just::runner::plugin::types::EvalContext;
use just::runner::plugin::registry::BuiltInRegistry;
use just::runner::eval::statement::execute_statement;

// Create context with built-ins
let mut ctx = EvalContext::new();
ctx.install_core_builtins(BuiltInRegistry::with_core());

// Parse and execute code
let code = "var x = Math.abs(-42);";
let ast = JsParser::parse_to_ast_from_str(code).unwrap();
for stmt in &ast.body {
    execute_statement(stmt, &mut ctx).unwrap();
}

// Access variables
let x = ctx.get_binding("x").unwrap();

Fields§

§global_this: Option<JsValue>

The global this value.

§heap: SharedHeap

Shared heap for memory allocation tracking.

§lex_env: JsLexEnvironmentType

Current lexical environment (for let/const and block scoping).

§var_env: JsLexEnvironmentType

Current variable environment (for var declarations).

§ctx_stack: ExecutionContextStack

Execution context stack for tracking function calls.

§strict: bool

Whether we’re in strict mode.

§lex_env_version: u64

Lexical environment version for cache invalidation.

§super_global: SharedSuperGlobal

Super-global scope for lazy resolution of built-in and plugin objects.

Implementations§

Source§

impl EvalContext

Source

pub fn new() -> Self

Create a new evaluation context with default heap configuration.

Creates a fresh context with:

  • Empty super-global scope (call install_core_builtins to add built-ins)
  • Default heap with no memory limits
  • Global lexical environment
§Examples
use just::runner::plugin::types::EvalContext;

let mut ctx = EvalContext::new();
// Context is ready to use, but has no built-ins yet
Source

pub fn with_heap_config(config: HeapConfig) -> Self

Create a new evaluation context with a specific heap configuration.

Use this to set memory limits or custom heap behavior.

§Examples
use just::runner::plugin::types::EvalContext;
use just::runner::ds::heap::HeapConfig;

let config = HeapConfig {
    max_bytes: Some(10 * 1024 * 1024), // 10MB limit
    gc_threshold: 1024 * 1024,
};
let mut ctx = EvalContext::with_heap_config(config);
Source

pub fn current_lex_env_version(&self) -> u64

Current lexical environment version.

Source

pub fn add_resolver(&mut self, resolver: Box<dyn PluginResolver>)

Install a plugin resolver into the super-global scope.

Resolvers are queried in registration order. The first resolver that claims a name wins.

§Examples
use just::runner::plugin::types::EvalContext;
use just::runner::plugin::resolver::PluginResolver;
use just::runner::ds::value::JsValue;
use just::runner::ds::error::JErrorType;

struct MyPlugin;

impl PluginResolver for MyPlugin {
    fn has_binding(&self, name: &str) -> bool {
        name == "MyObject"
    }
     
    fn resolve(&self, _name: &str, _ctx: &mut EvalContext) -> Result<JsValue, JErrorType> {
        Ok(JsValue::Undefined)
    }
     
    fn call_method(&self, _obj: &str, _method: &str, _ctx: &mut EvalContext,
                   _this: JsValue, _args: Vec<JsValue>) -> Option<Result<JsValue, JErrorType>> {
        None
    }
     
    fn name(&self) -> &str { "my_plugin" }
}

let mut ctx = EvalContext::new();
ctx.add_resolver(Box::new(MyPlugin));
Source

pub fn install_core_builtins(&mut self, registry: BuiltInRegistry)

Install the core built-in objects (Math, console, JSON, etc.).

This is a convenience method that wraps the BuiltInRegistry in a CorePluginResolver and adds it to the super-global scope.

§Examples
use just::runner::plugin::types::EvalContext;
use just::runner::plugin::registry::BuiltInRegistry;

let mut ctx = EvalContext::new();
ctx.install_core_builtins(BuiltInRegistry::with_core());

// Now Math, console, etc. are available
Source

pub fn allocate(&self, bytes: usize) -> Result<(), JErrorType>

Track a heap allocation.

Source

pub fn deallocate(&self, bytes: usize)

Track a heap deallocation.

Source

pub fn heap_usage(&self) -> usize

Get the current heap usage in bytes.

Source

pub fn new_tracked_object(&self) -> Result<SimpleObject, JErrorType>

Create a tracked SimpleObject that participates in heap accounting.

Source

pub fn get_binding(&mut self, name: &str) -> Result<JsValue, JErrorType>

Look up a binding in the environment chain.

Source

pub fn get_binding_with_env( &mut self, name: &str, ) -> Result<(JsValue, JsLexEnvironmentType), JErrorType>

Look up a binding and return the environment where it was found.

Source

pub fn get_binding_in_env( &mut self, env: &JsLexEnvironmentType, name: &str, ) -> Result<JsValue, JErrorType>

Get a binding from a specific environment (used by inline caches).

Source

pub fn resolve_binding(&mut self, name: &String) -> Result<JsValue, JErrorType>

Resolve a binding by walking up the environment chain, falling back to the super-global scope if not found.

Source

pub fn set_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>

Set a binding in the environment chain.

Source

pub fn set_binding_with_env( &mut self, name: &str, value: JsValue, ) -> Result<JsLexEnvironmentType, JErrorType>

Set a binding and return the environment where it was set.

Source

pub fn set_binding_in_env_cached( &mut self, env: &JsLexEnvironmentType, name: &str, value: JsValue, ) -> Result<(), JErrorType>

Set a binding in a specific environment (inline cache fast-path).

Source

pub fn create_binding( &mut self, name: &str, is_const: bool, ) -> Result<(), JErrorType>

Create a new binding in the current lexical environment.

Source

pub fn create_var_binding(&mut self, name: &str) -> Result<(), JErrorType>

Create a var binding in the variable environment.

Source

pub fn initialize_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>

Initialize a binding with a value.

Source

pub fn initialize_var_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>

Initialize a var binding with a value.

Source

pub fn has_var_binding(&self, name: &str) -> bool

Check if a var binding exists.

Source

pub fn set_var_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>

Set a var binding’s value (for re-declaration).

Source

pub fn push_block_scope(&mut self)

Push a new block scope (for let/const).

Source

pub fn pop_block_scope(&mut self)

Pop a block scope.

Source

pub fn has_binding(&self, name: &str) -> bool

Check if a binding exists in the current environment chain or in the super-global scope.

Trait Implementations§

Source§

impl Default for EvalContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.