refactor: split VmContext
This commit is contained in:
+27
-16
@@ -64,15 +64,26 @@ pub enum ForceMode {
|
||||
}
|
||||
|
||||
pub trait VmContext {
|
||||
fn intern_string(&mut self, s: impl AsRef<str>) -> StringId;
|
||||
fn resolve_string(&self, id: StringId) -> &str;
|
||||
fn bytecode(&self) -> &[u8];
|
||||
fn get_const(&self, id: u32) -> StaticValue;
|
||||
|
||||
fn compile(&mut self, source: Source);
|
||||
fn split(&mut self) -> (&mut impl VmCode, &mut impl VmRuntimeCtx);
|
||||
}
|
||||
|
||||
pub(crate) trait VmContextExt: VmContext {
|
||||
pub trait VmRuntimeCtx {
|
||||
fn intern_string(&mut self, s: impl AsRef<str>) -> StringId;
|
||||
fn resolve_string(&self, id: StringId) -> &str;
|
||||
fn get_const(&self, id: u32) -> StaticValue;
|
||||
fn add_const(&mut self, val: StaticValue) -> u32;
|
||||
}
|
||||
|
||||
pub trait VmCode {
|
||||
fn bytecode(&self) -> &[u8];
|
||||
fn compile(
|
||||
&mut self,
|
||||
source: Source,
|
||||
ctx: &mut impl VmRuntimeCtx,
|
||||
) -> fix_error::Result<InstructionPtr>;
|
||||
}
|
||||
|
||||
pub(crate) trait VmRuntimeCtxExt: VmRuntimeCtx {
|
||||
fn get_string<'a, 'gc: 'a>(&'a self, val: StrictValue<'gc>) -> Option<&'a str>;
|
||||
fn get_string_id<'a, 'gc: 'a>(
|
||||
&'a mut self,
|
||||
@@ -81,7 +92,7 @@ pub(crate) trait VmContextExt: VmContext {
|
||||
fn convert_value(&self, val: Value) -> fix_common::Value;
|
||||
}
|
||||
|
||||
impl<T: VmContext> VmContextExt for T {
|
||||
impl<T: VmRuntimeCtx> VmRuntimeCtxExt for T {
|
||||
fn get_string<'a, 'gc: 'a>(&'a self, val: StrictValue<'gc>) -> Option<&'a str> {
|
||||
if let Some(sid) = val.as_inline::<StringId>() {
|
||||
Some(self.resolve_string(sid))
|
||||
@@ -213,7 +224,7 @@ pub(crate) enum AttrKeyData {
|
||||
Dynamic(OperandData),
|
||||
}
|
||||
|
||||
fn init_builtins<'gc>(mc: &Mutation<'gc>, ctx: &mut impl VmContext) -> Value<'gc> {
|
||||
fn init_builtins<'gc>(mc: &Mutation<'gc>, ctx: &mut impl VmRuntimeCtx) -> Value<'gc> {
|
||||
let mut entries = SmallVec::with_capacity(BUILTINS.len());
|
||||
|
||||
for (idx, &(name, arity)) in BUILTINS.iter().enumerate() {
|
||||
@@ -268,7 +279,7 @@ fn init_builtins<'gc>(mc: &Mutation<'gc>, ctx: &mut impl VmContext) -> Value<'gc
|
||||
}
|
||||
|
||||
impl<'gc> Vm<'gc> {
|
||||
fn new(force_mode: ForceMode, mc: &Mutation<'gc>, ctx: &mut impl VmContext) -> Self {
|
||||
fn new(force_mode: ForceMode, mc: &Mutation<'gc>, ctx: &mut impl VmRuntimeCtx) -> Self {
|
||||
let builtins = init_builtins(mc, ctx);
|
||||
Vm {
|
||||
stack: Vec::with_capacity(8192),
|
||||
@@ -448,15 +459,15 @@ impl Vm<'_> {
|
||||
ip: InstructionPtr,
|
||||
force_mode: ForceMode,
|
||||
) -> Result<fix_common::Value> {
|
||||
let mut arena: Arena<Rootable![Vm<'_>]> =
|
||||
Arena::new(|mc| Vm::new(force_mode, mc, ctx));
|
||||
let (code, runtime) = ctx.split();
|
||||
let mut arena: Arena<Rootable![Vm<'_>]> = Arena::new(|mc| Vm::new(force_mode, mc, runtime));
|
||||
|
||||
const COLLECTOR_GRANULARITY: f64 = 1024.0;
|
||||
|
||||
let mut pc = ip.0;
|
||||
let bytecode: Vec<u8> = ctx.bytecode().to_vec();
|
||||
loop {
|
||||
match arena.mutate_root(|mc, root| root.dispatch_batch(&bytecode, ctx, pc, mc)) {
|
||||
let bytecode = code.bytecode();
|
||||
match arena.mutate_root(|mc, root| root.dispatch_batch(bytecode, runtime, pc, mc)) {
|
||||
Action::Continue { pc: new_pc } => {
|
||||
pc = new_pc;
|
||||
if arena.metrics().allocation_debt() > COLLECTOR_GRANULARITY {
|
||||
@@ -477,7 +488,7 @@ impl<'gc> Vm<'gc> {
|
||||
const DEFAULT_FUEL_AMOUNT: u32 = 1024;
|
||||
|
||||
#[inline(always)]
|
||||
fn dispatch_batch<C: VmContext>(
|
||||
fn dispatch_batch<C: VmRuntimeCtx>(
|
||||
&mut self,
|
||||
bytecode: &[u8],
|
||||
ctx: &mut C,
|
||||
@@ -507,7 +518,7 @@ impl<'gc> Vm<'gc> {
|
||||
fn execute_batch(
|
||||
&mut self,
|
||||
bytecode: &[u8],
|
||||
ctx: &mut impl VmContext,
|
||||
ctx: &mut impl VmRuntimeCtx,
|
||||
pc: usize,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Action {
|
||||
|
||||
Reference in New Issue
Block a user