feat: add managed types, source classes and scoped native calls
This commit is contained in:
+141
-47
@@ -4,6 +4,12 @@ use crate::{
|
||||
EndorStringObject, ExecutionLimits, ExecutionLocation, Instruction, ObjectRef, Opcode, Value,
|
||||
Vm, VmError,
|
||||
};
|
||||
use crate::{NativeContext, NativeResult};
|
||||
|
||||
enum PreparedCall {
|
||||
Value(Value),
|
||||
Frame(EndorFrame),
|
||||
}
|
||||
|
||||
fn invalid(offset: usize, reason: &'static str) -> VmError {
|
||||
VmError::InvalidBytecode { offset, reason }
|
||||
@@ -49,20 +55,17 @@ impl Vm {
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<Value, VmError> {
|
||||
self.last_traceback.clear();
|
||||
if !self
|
||||
.object(function)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.is::<EndorFunctionObject>()
|
||||
{
|
||||
return self.invoke_native(function, arguments);
|
||||
}
|
||||
let mut executed = 0;
|
||||
let frame = match self.prepare_call(function, arguments.to_vec(), &mut executed, limits)? {
|
||||
PreparedCall::Value(value) => return Ok(value),
|
||||
PreparedCall::Frame(frame) => frame,
|
||||
};
|
||||
if limits.max_frames == 0 {
|
||||
self.clear_frame(frame);
|
||||
return Err(VmError::ExecutionLimit("frames"));
|
||||
}
|
||||
let frame = self.prepare_frame(function, arguments)?;
|
||||
let mut frames = vec![frame];
|
||||
let result = self.eval_frames(&mut frames, limits);
|
||||
let result = self.eval_frames(&mut frames, limits, &mut executed);
|
||||
if result.is_err() {
|
||||
self.last_traceback = frames
|
||||
.iter()
|
||||
@@ -196,14 +199,68 @@ impl Vm {
|
||||
&mut self,
|
||||
function: ObjectRef,
|
||||
arguments: &[Value],
|
||||
) -> Result<Value, VmError> {
|
||||
) -> Result<NativeResult, VmError> {
|
||||
self.object(function)?;
|
||||
for argument in arguments {
|
||||
self.validate_value(argument)?;
|
||||
}
|
||||
if let Some(callable) = self.object(function)?.payload().native_callable() {
|
||||
return NativeContext::execute(&mut self.objects, function, arguments, callable);
|
||||
}
|
||||
let result = self.object(function)?.payload().call(arguments)?;
|
||||
self.retain_value(&result)?;
|
||||
Ok(result)
|
||||
Ok(NativeResult::Return(result))
|
||||
}
|
||||
|
||||
fn prepare_call(
|
||||
&mut self,
|
||||
mut function: ObjectRef,
|
||||
mut arguments: Vec<Value>,
|
||||
executed: &mut usize,
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<PreparedCall, VmError> {
|
||||
let mut owned = false;
|
||||
let result = (|| loop {
|
||||
if self
|
||||
.object(function)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.is::<EndorFunctionObject>()
|
||||
{
|
||||
return self
|
||||
.prepare_frame(function, &arguments)
|
||||
.map(PreparedCall::Frame);
|
||||
}
|
||||
if *executed >= limits.max_instructions {
|
||||
return Err(VmError::ExecutionLimit("instructions"));
|
||||
}
|
||||
*executed += 1;
|
||||
match self.invoke_native(function, &arguments)? {
|
||||
NativeResult::Return(value) => return Ok(PreparedCall::Value(value)),
|
||||
NativeResult::Invoke {
|
||||
function: next,
|
||||
arguments: values,
|
||||
} => {
|
||||
if owned {
|
||||
self.release(function)?;
|
||||
for value in arguments.drain(..) {
|
||||
self.release_value(value)?;
|
||||
}
|
||||
}
|
||||
function = next;
|
||||
arguments = values;
|
||||
owned = true;
|
||||
}
|
||||
}
|
||||
})();
|
||||
if owned {
|
||||
self.release(function)
|
||||
.expect("delegated call owns its function");
|
||||
for value in arguments {
|
||||
self.drop_root(value);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn stack_value(frame: &EndorFrame, index: usize) -> Result<Value, VmError> {
|
||||
@@ -220,16 +277,15 @@ impl Vm {
|
||||
Self::stack_value(frame, frame.stack.len() - 1)
|
||||
}
|
||||
|
||||
fn assignment_value(&self, previous: Option<Value>, incoming: Value) -> Result<Value, VmError> {
|
||||
if let (Some(Value::Object(old)), Value::Object(new)) = (&previous, &incoming) {
|
||||
if old == new
|
||||
|| self
|
||||
.object(*old)?
|
||||
.payload()
|
||||
.assign_from(self.object(*new)?.payload())?
|
||||
{
|
||||
return Ok(Value::Object(*old));
|
||||
}
|
||||
fn assignment_value(
|
||||
&mut self,
|
||||
previous: Option<Value>,
|
||||
incoming: Value,
|
||||
) -> Result<Value, VmError> {
|
||||
if let (Some(Value::Object(old)), Value::Object(new)) = (&previous, &incoming)
|
||||
&& (old == new || self.objects.assign_from(*old, *new)?)
|
||||
{
|
||||
return Ok(Value::Object(*old));
|
||||
}
|
||||
Ok(incoming)
|
||||
}
|
||||
@@ -356,17 +412,53 @@ impl Vm {
|
||||
self.new_function(name, code, frame.globals, defaults, closure)
|
||||
}
|
||||
|
||||
fn build_class(&mut self, frame: &EndorFrame) -> Result<ObjectRef, VmError> {
|
||||
let start = frame.stack.len() - 4;
|
||||
let Value::Text(name) = Self::stack_value(frame, start)? else {
|
||||
return Err(VmError::ObjectKindMismatch("class name"));
|
||||
};
|
||||
let Value::Object(base) = Self::stack_value(frame, start + 1)? else {
|
||||
return Err(VmError::ObjectKindMismatch("base Type"));
|
||||
};
|
||||
let Value::Object(fields) = Self::stack_value(frame, start + 2)? else {
|
||||
return Err(VmError::ObjectKindMismatch("field map"));
|
||||
};
|
||||
let Value::Object(methods) = Self::stack_value(frame, start + 3)? else {
|
||||
return Err(VmError::ObjectKindMismatch("method map"));
|
||||
};
|
||||
let fields = self
|
||||
.dict_items(fields)?
|
||||
.into_iter()
|
||||
.map(|(name, type_name)| match (name, type_name) {
|
||||
(Value::Text(name), Value::Text(type_name)) => Ok(crate::ClassField {
|
||||
name: name.clone(),
|
||||
type_name: type_name.clone(),
|
||||
}),
|
||||
_ => Err(VmError::ObjectKindMismatch("class field annotation")),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let methods = self
|
||||
.dict_items(methods)?
|
||||
.into_iter()
|
||||
.map(|(name, function)| match (name, function) {
|
||||
(Value::Text(name), Value::Object(function)) => Ok((name.clone(), *function)),
|
||||
_ => Err(VmError::ObjectKindMismatch("class method")),
|
||||
})
|
||||
.collect::<Result<std::collections::BTreeMap<_, _>, _>>()?;
|
||||
self.objects.new_class(name, base, fields, methods)
|
||||
}
|
||||
|
||||
fn eval_frames(
|
||||
&mut self,
|
||||
frames: &mut Vec<EndorFrame>,
|
||||
limits: ExecutionLimits,
|
||||
executed: &mut usize,
|
||||
) -> Result<Value, VmError> {
|
||||
let mut executed = 0;
|
||||
loop {
|
||||
if executed >= limits.max_instructions {
|
||||
if *executed >= limits.max_instructions {
|
||||
return Err(VmError::ExecutionLimit("instructions"));
|
||||
}
|
||||
executed += 1;
|
||||
*executed += 1;
|
||||
let frame = frames.last_mut().expect("an evaluation frame is active");
|
||||
let decoded = frame.executable.instruction_at(frame.instruction_offset)?;
|
||||
frame.current_offset = decoded.offset;
|
||||
@@ -485,27 +577,24 @@ impl Vm {
|
||||
for index in start + 2..frame.stack.len() {
|
||||
arguments.push(Self::stack_value(frame, index)?);
|
||||
}
|
||||
let language_function = self
|
||||
.object(function)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.is::<EndorFunctionObject>();
|
||||
if language_function {
|
||||
if frames.len() >= limits.max_frames {
|
||||
return Err(VmError::ExecutionLimit("frames"));
|
||||
match self.prepare_call(function, arguments, executed, limits)? {
|
||||
PreparedCall::Frame(mut callee) => {
|
||||
if frames.len() >= limits.max_frames {
|
||||
self.clear_frame(callee);
|
||||
return Err(VmError::ExecutionLimit("frames"));
|
||||
}
|
||||
callee.previous = Some(frames.len() - 1);
|
||||
self.discard(frames.last_mut().unwrap(), argument + 2);
|
||||
frames.push(callee);
|
||||
}
|
||||
PreparedCall::Value(value) => {
|
||||
self.discard(frames.last_mut().unwrap(), argument + 2);
|
||||
frames
|
||||
.last_mut()
|
||||
.unwrap()
|
||||
.stack
|
||||
.push(StackEntry::Value(value));
|
||||
}
|
||||
let mut callee = self.prepare_frame(function, &arguments)?;
|
||||
callee.previous = Some(frames.len() - 1);
|
||||
self.discard(frames.last_mut().unwrap(), argument + 2);
|
||||
frames.push(callee);
|
||||
} else {
|
||||
let value = self.invoke_native(function, &arguments)?;
|
||||
self.discard(frames.last_mut().unwrap(), argument + 2);
|
||||
frames
|
||||
.last_mut()
|
||||
.unwrap()
|
||||
.stack
|
||||
.push(StackEntry::Value(value));
|
||||
}
|
||||
}
|
||||
Opcode::ReturnValue => {
|
||||
@@ -554,11 +643,16 @@ impl Vm {
|
||||
let Value::Object(owner) = Self::top_value(frame)? else {
|
||||
return Err(VmError::ObjectKindMismatch("object"));
|
||||
};
|
||||
let value = self.get_attr(owner, &frame.executable.layout().names[argument])?;
|
||||
self.retain_value(&value)?;
|
||||
let value =
|
||||
self.bind_attr(owner, &frame.executable.layout().names[argument])?;
|
||||
self.discard(frame, 1);
|
||||
frame.stack.push(StackEntry::Value(value));
|
||||
}
|
||||
Opcode::BuildClass => {
|
||||
let class = self.build_class(frame)?;
|
||||
self.discard(frame, 4);
|
||||
frame.stack.push(StackEntry::Value(Value::Object(class)));
|
||||
}
|
||||
Opcode::StoreAttr => {
|
||||
let Value::Object(owner) = Self::top_value(frame)? else {
|
||||
return Err(VmError::ObjectKindMismatch("object"));
|
||||
|
||||
Reference in New Issue
Block a user