Compare commits
No commits in common. "3360f1491340d4035898c9a2923fc0fc7b4a09ae" and "1811b91922f8e76b7f563622b14d2a8933c15ab2" have entirely different histories.
3360f14913
...
1811b91922
@ -109,4 +109,3 @@ SETMETATABLE(table, {
|
||||
|
||||
print(table + 5)
|
||||
print(table("hello", "there"))
|
||||
print("hello ", table)
|
||||
@ -25,14 +25,7 @@ impl value::RustFunction for Max {
|
||||
pub struct Print;
|
||||
impl value::RustFunction for Print {
|
||||
fn execute(&self, parameters: Vec<value::Value>) -> Result<Vec<value::Value>, RuntimeError> {
|
||||
println!(
|
||||
"{}",
|
||||
parameters
|
||||
.iter()
|
||||
.map(|v| format!("{}", v))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\t")
|
||||
);
|
||||
println!("{:?}", parameters);
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
|
||||
111
src/vm/mod.rs
111
src/vm/mod.rs
@ -657,7 +657,7 @@ impl ClosureRunner {
|
||||
vec![value.unwrap().clone(), index_value.clone()],
|
||||
) {
|
||||
Ok(value) => {
|
||||
StackValue::Value(value?.first().unwrap().clone())
|
||||
StackValue::Value(value.first().unwrap().clone())
|
||||
}
|
||||
Err(_) => StackValue::Value(Value::Nil),
|
||||
}
|
||||
@ -816,7 +816,7 @@ impl ClosureRunner {
|
||||
let mut metamethod_params = vec![value.clone()];
|
||||
metamethod_params.extend(params);
|
||||
let ret_values =
|
||||
self.call_metamethod(&metatable, "__call", metamethod_params)??;
|
||||
self.call_metamethod(&metatable, "__call", metamethod_params)?;
|
||||
|
||||
if *ret_len != 0 {
|
||||
for i in 0..=(*ret_len - 2) {
|
||||
@ -890,7 +890,7 @@ impl ClosureRunner {
|
||||
"__concat",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
|
||||
@ -903,7 +903,7 @@ impl ClosureRunner {
|
||||
"__add",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::Sub(res, lhs, rhs) => {
|
||||
@ -915,7 +915,7 @@ impl ClosureRunner {
|
||||
"__sub",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::Mult(res, lhs, rhs) => {
|
||||
@ -927,7 +927,7 @@ impl ClosureRunner {
|
||||
"__mult",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::Div(res, lhs, rhs) => {
|
||||
@ -939,7 +939,7 @@ impl ClosureRunner {
|
||||
"__div",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::IDiv(res, lhs, rhs) => {
|
||||
@ -951,7 +951,7 @@ impl ClosureRunner {
|
||||
"__idiv",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::Mod(res, lhs, rhs) => {
|
||||
@ -963,7 +963,7 @@ impl ClosureRunner {
|
||||
"__mod",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::Exp(res, lhs, rhs) => {
|
||||
@ -975,7 +975,7 @@ impl ClosureRunner {
|
||||
"__exp",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
|
||||
@ -988,7 +988,7 @@ impl ClosureRunner {
|
||||
"__band",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::BitOr(res, lhs, rhs) => {
|
||||
@ -1000,7 +1000,7 @@ impl ClosureRunner {
|
||||
"__bor",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::BitXOr(res, lhs, rhs) => {
|
||||
@ -1012,7 +1012,7 @@ impl ClosureRunner {
|
||||
"__bxor",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::BitSRight(res, lhs, rhs) => {
|
||||
@ -1024,7 +1024,7 @@ impl ClosureRunner {
|
||||
"__shr",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::BitSLeft(res, lhs, rhs) => {
|
||||
@ -1036,7 +1036,7 @@ impl ClosureRunner {
|
||||
"__shl",
|
||||
lhs,
|
||||
rhs,
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
|
||||
@ -1051,7 +1051,7 @@ impl ClosureRunner {
|
||||
metatable,
|
||||
"__eq",
|
||||
vec![lhs.clone(), rhs.clone()],
|
||||
)??
|
||||
)?
|
||||
.first()
|
||||
.unwrap()
|
||||
.is_truthy(),
|
||||
@ -1073,7 +1073,7 @@ impl ClosureRunner {
|
||||
"__lt",
|
||||
lhs.clone(),
|
||||
rhs.clone(),
|
||||
)??
|
||||
)?
|
||||
.is_truthy(),
|
||||
))),
|
||||
);
|
||||
@ -1088,7 +1088,7 @@ impl ClosureRunner {
|
||||
"__le",
|
||||
lhs.clone(),
|
||||
rhs.clone(),
|
||||
)??
|
||||
)?
|
||||
.is_truthy(),
|
||||
))),
|
||||
);
|
||||
@ -1115,7 +1115,7 @@ impl ClosureRunner {
|
||||
value.unm(),
|
||||
"__unm",
|
||||
value.clone(),
|
||||
)??),
|
||||
)?),
|
||||
);
|
||||
}
|
||||
Instruction::Len(res, reg) => {
|
||||
@ -1130,7 +1130,7 @@ impl ClosureRunner {
|
||||
Ok(result) => {
|
||||
self.set_stack(
|
||||
*res,
|
||||
StackValue::Value(result?.first().unwrap().clone()),
|
||||
StackValue::Value(result.first().unwrap().clone()),
|
||||
);
|
||||
}
|
||||
Err(_) => {
|
||||
@ -1216,20 +1216,20 @@ impl ClosureRunner {
|
||||
value: Result<Value, RuntimeError>,
|
||||
metamethod: &str,
|
||||
param: Value,
|
||||
) -> Result<Result<Value, RuntimeError>, RuntimeError> {
|
||||
) -> Result<Value, RuntimeError> {
|
||||
match value {
|
||||
Ok(value) => Ok(Ok(value)),
|
||||
Ok(value) => Ok(value),
|
||||
Err(_) => match ¶m {
|
||||
Value::Table { metatable, .. } => {
|
||||
if metatable
|
||||
.borrow()
|
||||
.contains_key(&IndexableValue::String(metamethod.to_owned()))
|
||||
{
|
||||
Ok(extract_ret_value(self.call_metamethod(
|
||||
metatable,
|
||||
metamethod,
|
||||
vec![param.clone()],
|
||||
)?))
|
||||
Ok(self
|
||||
.call_metamethod(metatable, metamethod, vec![param.clone()])?
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap())
|
||||
} else {
|
||||
Err(RuntimeError::InvalidUnop(
|
||||
metamethod.to_owned(),
|
||||
@ -1251,31 +1251,35 @@ impl ClosureRunner {
|
||||
metamethod: &str,
|
||||
lhs: Value,
|
||||
rhs: Value,
|
||||
) -> Result<Result<Value, RuntimeError>, RuntimeError> {
|
||||
) -> Result<Value, RuntimeError> {
|
||||
match value {
|
||||
Ok(value) => Ok(Ok(value)),
|
||||
Ok(value) => Ok(value),
|
||||
Err(_) => {
|
||||
if let Value::Table { metatable, .. } = &lhs {
|
||||
if metatable
|
||||
.borrow()
|
||||
.contains_key(&IndexableValue::String(metamethod.to_owned()))
|
||||
{
|
||||
Ok(extract_ret_value(self.call_metamethod(
|
||||
&metatable,
|
||||
metamethod,
|
||||
vec![lhs.clone(), rhs],
|
||||
)?))
|
||||
Ok(self
|
||||
.call_metamethod(&metatable, metamethod, vec![lhs.clone(), rhs])?
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap())
|
||||
} else {
|
||||
if let Value::Table { metatable, .. } = &rhs {
|
||||
if metatable
|
||||
.borrow()
|
||||
.contains_key(&IndexableValue::String(metamethod.to_owned()))
|
||||
{
|
||||
Ok(extract_ret_value(self.call_metamethod(
|
||||
&metatable,
|
||||
metamethod,
|
||||
vec![lhs, rhs.clone()],
|
||||
)?))
|
||||
Ok(self
|
||||
.call_metamethod(
|
||||
&metatable,
|
||||
metamethod,
|
||||
vec![lhs, rhs.clone()],
|
||||
)?
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap())
|
||||
} else {
|
||||
Err(RuntimeError::InvalidBinop(
|
||||
metamethod.to_owned(),
|
||||
@ -1296,11 +1300,11 @@ impl ClosureRunner {
|
||||
.borrow()
|
||||
.contains_key(&IndexableValue::String(metamethod.to_owned()))
|
||||
{
|
||||
Ok(extract_ret_value(self.call_metamethod(
|
||||
&metatable,
|
||||
metamethod,
|
||||
vec![lhs, rhs.clone()],
|
||||
)?))
|
||||
Ok(self
|
||||
.call_metamethod(&metatable, metamethod, vec![lhs, rhs.clone()])?
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap())
|
||||
} else {
|
||||
Err(RuntimeError::InvalidBinop(
|
||||
metamethod.to_owned(),
|
||||
@ -1324,28 +1328,25 @@ impl ClosureRunner {
|
||||
metatable: &Table,
|
||||
metamethod: &str,
|
||||
params: Vec<Value>,
|
||||
) -> Result<Result<Vec<Value>, RuntimeError>, RuntimeError> {
|
||||
) -> Result<Vec<Value>, RuntimeError> {
|
||||
if let Some(value) = metatable
|
||||
.borrow()
|
||||
.get(&IndexableValue::String(metamethod.to_owned()))
|
||||
{
|
||||
match value {
|
||||
Value::RustFunction(function) => {
|
||||
let result = function.borrow_mut().execute(params);
|
||||
let result = function.borrow_mut().execute(params)?;
|
||||
Ok(result)
|
||||
}
|
||||
Value::Function(closure) => {
|
||||
let mut runnable = closure.run(params);
|
||||
#[allow(unused_assignments)]
|
||||
let mut return_value = Ok(None);
|
||||
let mut return_value = None;
|
||||
while {
|
||||
return_value = runnable.next();
|
||||
match return_value {
|
||||
Ok(None) => true,
|
||||
_ => false,
|
||||
}
|
||||
return_value = runnable.next()?;
|
||||
return_value.is_none()
|
||||
} {}
|
||||
Ok(return_value.map(|v| v.unwrap()))
|
||||
Ok(return_value.unwrap())
|
||||
}
|
||||
_ => Err(RuntimeError::MetafunctionNotCallable(value.clone())),
|
||||
}
|
||||
@ -1370,7 +1371,3 @@ impl ClosureRunner {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_ret_value(values: Result<Vec<Value>, RuntimeError>) -> Result<Value, RuntimeError> {
|
||||
values.map(|v| v.into_iter().next().unwrap())
|
||||
}
|
||||
|
||||
@ -125,8 +125,7 @@ impl Debug for Constant {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Table = Rc<RefCell<TableMap>>;
|
||||
pub type TableMap = HashMap<IndexableValue, Value>;
|
||||
pub type Table = Rc<RefCell<HashMap<IndexableValue, Value>>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Value {
|
||||
@ -152,7 +151,7 @@ impl Value {
|
||||
}
|
||||
Value::Function(closure) => Ok(IndexableValue::Function(closure.prototype)),
|
||||
Value::Nil => Err(RuntimeError::InvalidTableIndex(self)),
|
||||
Value::Table { contents, .. } => Ok(IndexableValue::Table(contents.as_ptr())),
|
||||
Value::Table { .. } => Err(RuntimeError::InvalidTableIndex(self)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -202,7 +201,7 @@ impl Display for Value {
|
||||
}
|
||||
Value::Function(closure) => write!(f, "<function({})>", closure.prototype),
|
||||
Value::Nil => write!(f, "nil"),
|
||||
Value::Table { contents, .. } => write!(f, "<table#{}>", contents.as_ptr() as u64),
|
||||
Value::Table { .. } => write!(f, "<table>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -622,7 +621,6 @@ pub enum IndexableValue {
|
||||
Bool(LuaBool),
|
||||
RustFunction(String),
|
||||
Function(u32),
|
||||
Table(*mut TableMap),
|
||||
}
|
||||
|
||||
impl From<&str> for IndexableValue {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user