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: SharedHeapShared heap for memory allocation tracking.
lex_env: JsLexEnvironmentTypeCurrent lexical environment (for let/const and block scoping).
var_env: JsLexEnvironmentTypeCurrent variable environment (for var declarations).
ctx_stack: ExecutionContextStackExecution context stack for tracking function calls.
strict: boolWhether we’re in strict mode.
lex_env_version: u64Lexical environment version for cache invalidation.
super_global: SharedSuperGlobalSuper-global scope for lazy resolution of built-in and plugin objects.
Implementations§
Source§impl EvalContext
impl EvalContext
Sourcepub fn new() -> Self
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_builtinsto 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 yetSourcepub fn with_heap_config(config: HeapConfig) -> Self
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);Sourcepub fn current_lex_env_version(&self) -> u64
pub fn current_lex_env_version(&self) -> u64
Current lexical environment version.
Sourcepub fn add_resolver(&mut self, resolver: Box<dyn PluginResolver>)
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));Sourcepub fn install_core_builtins(&mut self, registry: BuiltInRegistry)
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 availableSourcepub fn deallocate(&self, bytes: usize)
pub fn deallocate(&self, bytes: usize)
Track a heap deallocation.
Sourcepub fn heap_usage(&self) -> usize
pub fn heap_usage(&self) -> usize
Get the current heap usage in bytes.
Sourcepub fn new_tracked_object(&self) -> Result<SimpleObject, JErrorType>
pub fn new_tracked_object(&self) -> Result<SimpleObject, JErrorType>
Create a tracked SimpleObject that participates in heap accounting.
Sourcepub fn get_binding(&mut self, name: &str) -> Result<JsValue, JErrorType>
pub fn get_binding(&mut self, name: &str) -> Result<JsValue, JErrorType>
Look up a binding in the environment chain.
Sourcepub fn get_binding_with_env(
&mut self,
name: &str,
) -> Result<(JsValue, JsLexEnvironmentType), JErrorType>
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.
Sourcepub fn get_binding_in_env(
&mut self,
env: &JsLexEnvironmentType,
name: &str,
) -> Result<JsValue, JErrorType>
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).
Sourcepub fn resolve_binding(&mut self, name: &String) -> Result<JsValue, JErrorType>
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.
Sourcepub fn set_binding(
&mut self,
name: &str,
value: JsValue,
) -> Result<(), JErrorType>
pub fn set_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>
Set a binding in the environment chain.
Sourcepub fn set_binding_with_env(
&mut self,
name: &str,
value: JsValue,
) -> Result<JsLexEnvironmentType, JErrorType>
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.
Sourcepub fn set_binding_in_env_cached(
&mut self,
env: &JsLexEnvironmentType,
name: &str,
value: JsValue,
) -> Result<(), JErrorType>
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).
Sourcepub fn create_binding(
&mut self,
name: &str,
is_const: bool,
) -> Result<(), JErrorType>
pub fn create_binding( &mut self, name: &str, is_const: bool, ) -> Result<(), JErrorType>
Create a new binding in the current lexical environment.
Sourcepub fn create_var_binding(&mut self, name: &str) -> Result<(), JErrorType>
pub fn create_var_binding(&mut self, name: &str) -> Result<(), JErrorType>
Create a var binding in the variable environment.
Sourcepub fn initialize_binding(
&mut self,
name: &str,
value: JsValue,
) -> Result<(), JErrorType>
pub fn initialize_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>
Initialize a binding with a value.
Sourcepub fn initialize_var_binding(
&mut self,
name: &str,
value: JsValue,
) -> Result<(), JErrorType>
pub fn initialize_var_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>
Initialize a var binding with a value.
Sourcepub fn has_var_binding(&self, name: &str) -> bool
pub fn has_var_binding(&self, name: &str) -> bool
Check if a var binding exists.
Sourcepub fn set_var_binding(
&mut self,
name: &str,
value: JsValue,
) -> Result<(), JErrorType>
pub fn set_var_binding( &mut self, name: &str, value: JsValue, ) -> Result<(), JErrorType>
Set a var binding’s value (for re-declaration).
Sourcepub fn push_block_scope(&mut self)
pub fn push_block_scope(&mut self)
Push a new block scope (for let/const).
Sourcepub fn pop_block_scope(&mut self)
pub fn pop_block_scope(&mut self)
Pop a block scope.
Sourcepub fn has_binding(&self, name: &str) -> bool
pub fn has_binding(&self, name: &str) -> bool
Check if a binding exists in the current environment chain or in the super-global scope.