Implement validated wordcode execution with shared code objects and frames
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m27s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m27s
This commit is contained in:
+21
-67
@@ -150,89 +150,42 @@ impl ObjectPayload for EndorCellObject {
|
||||
}
|
||||
}
|
||||
|
||||
/// Code data only: bytecode is opaque until an instruction format/executor
|
||||
/// is defined. Constants may own managed objects through the enclosing function.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorCode {
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
parameters: Vec<String>,
|
||||
free_variables: Vec<String>,
|
||||
}
|
||||
|
||||
impl EndorCode {
|
||||
pub fn new(
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
parameters: Vec<String>,
|
||||
free_variables: Vec<String>,
|
||||
) -> Result<Self, VmError> {
|
||||
for names in [¶meters, &free_variables] {
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for name in names {
|
||||
if name.is_empty() || !seen.insert(name) {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"empty or duplicate code variable name",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
bytecode,
|
||||
constants,
|
||||
parameters,
|
||||
free_variables,
|
||||
})
|
||||
}
|
||||
pub fn bytecode(&self) -> &[u8] {
|
||||
&self.bytecode
|
||||
}
|
||||
pub fn constants(&self) -> &[Value] {
|
||||
&self.constants
|
||||
}
|
||||
pub fn parameters(&self) -> &[String] {
|
||||
&self.parameters
|
||||
}
|
||||
pub fn free_variables(&self) -> &[String] {
|
||||
&self.free_variables
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorFunctionObject {
|
||||
name: String,
|
||||
code: EndorCode,
|
||||
code: ObjectRef,
|
||||
builtins: Option<ObjectRef>,
|
||||
globals: ObjectRef,
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
}
|
||||
|
||||
impl EndorFunctionObject {
|
||||
pub fn new(
|
||||
name: impl Into<String>,
|
||||
code: EndorCode,
|
||||
pub(crate) fn new(
|
||||
name: String,
|
||||
code: ObjectRef,
|
||||
globals: ObjectRef,
|
||||
builtins: Option<ObjectRef>,
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
) -> Result<Self, VmError> {
|
||||
if defaults.len() > code.parameters.len() || closure.len() != code.free_variables.len() {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"function defaults or closure do not match code",
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
name: name.into(),
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
code,
|
||||
globals,
|
||||
builtins,
|
||||
defaults,
|
||||
closure,
|
||||
})
|
||||
}
|
||||
}
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
pub fn code(&self) -> &EndorCode {
|
||||
&self.code
|
||||
pub fn code(&self) -> ObjectRef {
|
||||
self.code
|
||||
}
|
||||
pub fn builtins(&self) -> Option<ObjectRef> {
|
||||
self.builtins
|
||||
}
|
||||
pub fn globals(&self) -> ObjectRef {
|
||||
self.globals
|
||||
@@ -256,20 +209,21 @@ impl ObjectPayload for EndorFunctionObject {
|
||||
true
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
visit(self.code);
|
||||
visit(self.globals);
|
||||
if let Some(builtins) = self.builtins {
|
||||
visit(builtins);
|
||||
}
|
||||
for reference in &self.closure {
|
||||
visit(*reference);
|
||||
}
|
||||
for value in self.defaults.iter().chain(&self.code.constants) {
|
||||
for value in &self.defaults {
|
||||
trace_value(value, visit);
|
||||
}
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!("<function {}>", self.name))
|
||||
}
|
||||
fn call(&self, _arguments: &[Value]) -> Result<Value, VmError> {
|
||||
Err(VmError::UnsupportedOperation("language function execution"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
||||
Reference in New Issue
Block a user