1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
use std::{cell::RefCell, mem::take, rc::Rc};
use crate::{
chunk::Chunk,
code::OpCode,
compiler::Compiler,
error::{ErrorTuple, ErrorTypes, SiltError},
function::{CallFrame, Closure, FunctionObject, NativeObject, UpValue},
table::Table,
token::Operator,
value::Value,
};
/** Convert Integer to Float, lossy for now */
macro_rules! int2f {
($left:ident) => {
$left as f64
};
}
macro_rules! intr2f {
($left:ident) => {
*$left as f64
};
}
macro_rules! devout {
($($arg:tt)*) => {
#[cfg(feature = "dev-out")]
println!($($arg)*);
}
}
macro_rules! str_op_str{
($left:ident $op:tt $right:ident $enu:ident )=>{
(||{
if let Ok(n1) = $left.parse::<i64>() {
if let Ok(n2) = $right.parse::<i64>() {
return Ok(Value::Integer(n1 $op n2));
}
if let Ok(n2) = $right.parse::<f64>() {
return Ok(Value::Number(int2f!(n1) $op n2));
}
}
if let Ok(n1) = $left.parse::<f64>() {
if let Ok(n2) = $right.parse::<f64>() {
return Ok(Value::Number(n1 $op n2));
}
}
return Err(SiltError::ExpOpValueWithValue(
ErrorTypes::String,
Operator::$enu,
ErrorTypes::String,
));
})()
}
}
macro_rules! str_op_int{
($left:ident $op:tt $right:ident $enu:ident)=>{
(||{
if let Ok(n1) = $left.parse::<i64>() {
return Ok(Value::Integer(n1 $op $right));
}
if let Ok(n1) = $left.parse::<f64>() {
return Ok(Value::Number(n1 $op int2f!($right)));
}
return Err(SiltError::ExpOpValueWithValue(
ErrorTypes::String,
Operator::$enu,
ErrorTypes::Integer,
));
})()
}
}
macro_rules! int_op_str{
($left:ident $op:tt $right:ident $enu:ident)=>{
(||{
if let Ok(n1) = $right.parse::<i64>() {
return Ok(Value::Integer($left $op n1));
}
if let Ok(n1) = $right.parse::<f64>() {
return Ok(Value::Number((int2f!($left) $op n1)));
}
return Err(SiltError::ExpOpValueWithValue(
ErrorTypes::Integer,
Operator::$enu,
ErrorTypes::String,
));
})()
}
}
macro_rules! op_error {
($left:ident $op:ident $right:ident ) => {{
return Err(SiltError::ExpOpValueWithValue($left, Operator::$op, $right));
}};
}
macro_rules! str_op_num{
($left:ident $op:tt $right:ident $enu:ident)=>{
if let Ok(n1) = $left.parse::<f64>() {
Value::Number(n1 $op $right)
}else {
return Err(SiltError::ExpOpValueWithValue(
ErrorTypes::String,
Operator::$enu,
ErrorTypes::String,
))
}
}
}
macro_rules! num_op_str{
($left:ident $op:tt $right:ident $enu:ident)=>{
if let Ok(n1) = $right.parse::<f64>() {
Value::Number($left $op n1)
}else{
return Err(SiltError::ExpOpValueWithValue(
ErrorTypes::Number,
Operator::$enu,
ErrorTypes::String,
))
}
}
}
macro_rules! binary_op {
($src:ident, $op:tt, $opp:tt) => {
{
// TODO test speed of this vs 1 pop and a mutate
let r = $src.pop();
let l = $src.pop();
$src.push(match (l, r) {
(Value::Number(left), Value::Number(right)) => (Value::Number(left $op right)),
(Value::Integer(left), Value::Integer(right)) => (Value::Integer(left $op right)),
(Value::Number(left), Value::Integer(right)) => (Value::Number(left $op right as f64)),
(Value::Integer(left), Value::Number(right)) =>(Value::Number(left as f64 $op right)),
(Value::String(left), Value::String(right)) => str_op_str!(left $op right $opp)?,
(Value::String(left), Value::Integer(right)) => str_op_int!(left $op right $opp)?,
(Value::Integer(left), Value::String(right)) => int_op_str!(left $op right $opp)?,
(Value::String(left), Value::Number(right)) => str_op_num!(left $op right $opp),
(Value::Number(left), Value::String(right)) => num_op_str!(left $op right $opp),
// TODO userdata
// (Value::UserData(left), Value::UserData(right)) => {
// if let Some(f) = left.get_meta_method($opp) {
// f(left, right)?
// } else {
// op_error!(left.to_error(), $opp, right.to_error())
// }
// }
(ll,rr) => return Err(SiltError::ExpOpValueWithValue(ll.to_error(), Operator::$opp, rr.to_error()))
});
}
};
}
pub struct Lua {
body: Rc<FunctionObject>,
compiler: Compiler,
// frames: Vec<CallFrame>,
// dummy_frame: CallFrame,
/** Instruction to be run at start of loop */
// ip: *const OpCode, // TODO usize vs *const OpCode, will rust optimize the same?
// stack: Vec<Value>, // TODO fixed size array vs Vec, how much less overhead is there?
stack: [Value; 256],
stack_top: *mut Value,
stack_count: usize,
/** Next empty location */
// stack_top: *mut Value,
globals: hashbrown::HashMap<String, Value>, // TODO store strings as identifer usize and use that as key
// original CI code uses linked list, most recent closed upvalue is the first and links to previous closed values down the chain
// allegedly performance of a linked list is heavier then an array and shifting values but is that true here or the opposite?
// resizing a sequential array is faster then non sequential heap items, BUT since we'll USUALLY resolve the upvalue on the top of the list we're derefencing once to get our Upvalue vs an index lookup which is slightly slower.
// TODO TLDR: benchmark this
open_upvalues: Vec<Rc<RefCell<UpValue>>>,
// references: Vec<Reference>,
// TODO should we store all strings in their own table/array for better equality checks? is this cheaper?
// obj
// TODO GC gray_stack
// gray_stack: Vec<Value>,
// TODO temporary solution to a hash id
table_counter: usize,
}
impl<'a> Lua {
/** Create a new lua compiler and runtime */
pub fn new() -> Self {
// TODO try the hard way
// force array to be 256 Values
// let stack = unsafe {
// std::alloc::alloc(std::alloc::Layout::array::<Value>(256).unwrap()) as *mut [Value; 256]
// };
// let stack: [Value; 256] = [const { Value::Nil }; 256];
let mut stack = [(); 256].map(|_| Value::default());
let stack_top = stack.as_mut_ptr() as *mut Value;
// let stack = vec![];
// let stack_top = stack.as_ptr() as *mut Value;
Self {
compiler: Compiler::new(),
body: Rc::new(FunctionObject::new(None, false)),
// dummy_frame: CallFrame::new(Rc::new(FunctionObject::new(None, false))),
// frames: vec![],
// ip: 0 as *const OpCode,
// stack, //: unsafe { *stack },
stack_count: 0,
stack,
stack_top,
globals: hashbrown::HashMap::new(),
open_upvalues: vec![],
table_counter: 1,
}
}
fn push(&mut self, value: Value) {
devout!("push: {}", value);
unsafe { self.stack_top.write(value) };
self.stack_top = unsafe { self.stack_top.add(1) };
self.stack_count += 1;
}
fn reserve(&mut self) -> *mut Value {
self.stack_count += 1;
let old = self.stack_top;
self.stack_top = unsafe { self.stack_top.add(1) };
old
}
/** pop N number of values from stack */
fn popn_drop(&mut self, n: u8) {
unsafe { self.stack_top = self.stack_top.sub(n as usize) };
self.stack_count -= n as usize;
}
fn print_upvalues(&self) {
self.open_upvalues.iter().for_each(|up| {
up.borrow().print();
});
}
fn close_n_upvalues(&mut self, n: u8) {
#[cfg(feature = "dev-out")]
self.print_upvalues();
// remove n from end of list
if n > 1 {
self.open_upvalues
.drain(self.open_upvalues.len() - n as usize..)
.rev()
.for_each(|up| {
let mut upvalue = up.borrow_mut();
upvalue.close_around(unsafe { self.stack_top.replace(Value::Nil) });
});
unsafe { self.stack_top = self.stack_top.sub(n as usize) };
} else {
let upvalue = self.open_upvalues.pop().unwrap();
upvalue.borrow_mut().close_around(self.pop());
}
}
fn close_upvalues_by_return(&mut self, last: *mut Value) {
#[cfg(feature = "dev-out")]
self.print_upvalues();
for upvalue in self.open_upvalues.iter() {
let mut up = upvalue.borrow_mut();
if up.get_location() < last {
break;
}
up.close();
}
}
/** pop and return top of stack */
fn pop(&mut self) -> Value {
self.stack_count -= 1;
unsafe { self.stack_top = self.stack_top.sub(1) };
let v = unsafe { self.stack_top.replace(Value::Nil) };
// TODO is there a way to read without segfaulting?
// We'd have to list the value to be forgotten, but is this even faster?
// let v = unsafe { self.stack_top.read() };
devout!("pop: {}", v);
v
}
// TODO can we make this faster with slices? can we slice a pointer? 🤔
fn popn(&mut self, n: u8) -> Vec<Value> {
// println!("popn: {}", n);
let mut values = vec![];
for _ in 0..n {
values.push(self.pop());
}
values.reverse();
values
}
fn safe_pop(&mut self) -> Value {
// let v3 = take(&mut self.stack[3]);
// println!("we took {}", v3);
// let v0 = take(&mut self.stack[self.stack_count - 1]);
// println!("we took {}", v0);
// let ve = v0.clone();
// std::mem::forget(v3);
// drop(v0);
// println!("we took {}", ve);
// self.print_raw_stack();
// core::ptr::read()
let v0 = take(&mut self.stack[self.stack_count - 1]);
// for i in self.stack.iter_mut().enumerate() {
// *i = Value::Nil;
// }
v0
}
/** Dangerous! */
fn read_top(&self) -> Value {
// match self.peek(0) {
// Some(v) => v.clone(),
// None => Value::Nil,
// }
unsafe { self.stack_top.sub(1).read() }
}
/** Safer but clones! */
fn duplicate(&self) -> Value {
unsafe { (*self.stack_top.sub(1)).clone() }
}
/** Look and get immutable reference to top of stack */
fn peek(&self) -> &Value {
// self.stack.last()
unsafe { &*self.stack_top.sub(1) }
}
/** Look and get mutable reference to top of stack */
fn peek_mut(&mut self) -> &mut Value {
unsafe { &mut *self.stack_top.sub(1) }
}
/** Look down N amount of stack and return immutable reference */
fn peekn(&self, n: u8) -> &Value {
// unsafe { *self.stack_top.sub(n as usize) }
// &self.stack[self.stack.len() - n as usize]
unsafe { &*self.stack_top.sub((n as usize) + 1) }
}
pub fn evaluate(&mut self, source: &str) -> FunctionObject {
self.compiler.compile(source.to_owned())
}
pub fn run(&mut self, source: &str) -> Result<Value, Vec<ErrorTuple>> {
let object = self.compiler.compile(source.to_owned());
if object.chunk.is_valid() {
match self.execute(object.into()) {
Ok(v) => Ok(v),
Err(e) => Err(vec![ErrorTuple {
code: e,
location: (0, 0),
}]),
}
} else {
Err(self.compiler.pop_errors())
}
}
pub fn execute(&mut self, object: Rc<FunctionObject>) -> Result<Value, SiltError> {
// TODO param is a reference of &'a
// self.ip = object.chunk.code.as_ptr();
// frame.ip = object.chunk.code.as_ptr();
// frame.slots = self.stack ???
// let rstack = self.stack.as_ptr();
#[cfg(feature = "dev-out")]
object.chunk.print_chunk(None);
self.stack_top = self.stack.as_mut_ptr() as *mut Value;
self.body = object.clone();
let closure = Rc::new(Closure::new(object.clone(), vec![]));
let mut frame = CallFrame::new(closure, 0);
frame.ip = object.chunk.code.as_ptr();
frame.local_stack = self.stack_top;
// frame.stack.resize(256, Value::Nil); // TODO
self.push(Value::Function(object)); // TODO this needs to store the function object itself somehow, RC?
let frames = vec![frame];
// self.body = object;
let res = self.process(frames);
res
}
fn process(&mut self, mut frames: Vec<CallFrame>) -> Result<Value, SiltError> {
let mut last = Value::Nil; // TODO temporary for testing
// let stack_pointer = self.stack.as_mut_ptr();
// let mut dummy_frame = CallFrame::new(Rc::new(FunctionObject::new(None, false)), 0);
let mut frame = frames.last_mut().unwrap();
let mut frame_count = 1;
loop {
let instruction = frame.current_instruction();
// devout!("ip: {:p} | {}", self.ip, instruction);
devout!(" | {}", instruction);
// TODO how much faster would it be to order these ops in order of usage, does match hash? probably.
match instruction {
OpCode::RETURN => {
frame_count -= 1;
if frame_count <= 0 {
if self.stack_count <= 1 {
return Ok(Value::Nil);
}
return Ok(self.safe_pop());
}
let res = self.pop();
self.stack_top = frame.local_stack;
self.close_upvalues_by_return(self.stack_top);
devout!("stack top {}", unsafe { &*self.stack_top });
self.stack_count = frame.stack_snapshot;
frames.pop();
frame = frames.last_mut().unwrap();
devout!("next instruction {}", frame.current_instruction());
#[cfg(feature = "dev-out")]
self.print_stack();
self.push(res);
// println!("<< {}", self.pop());
// match self.pop() {
// Some(v) => return Ok(v),
// None => return Ok(last),
// }
}
OpCode::CONSTANT { constant } => {
let value = Self::get_chunk(&frame).get_constant(*constant);
devout!("CONSTANT value {}", value);
self.push(value.clone());
// match value {
// Value::Number(f) => self.push(*f),
// Value::Integer(i) => self.push(*i as f64),
// _ => {}
// }
}
OpCode::DEFINE_GLOBAL { constant } => {
let value = self.body.chunk.get_constant(*constant);
if let Value::String(s) = value {
// DEV inline pop due to self lifetime nonsense
self.stack_count -= 1;
unsafe { self.stack_top = self.stack_top.sub(1) };
let v = unsafe { self.stack_top.read() };
// let v = self.pop();
self.globals.insert(s.to_string(), v);
} else {
return Err(SiltError::VmCorruptConstant);
}
}
// TODO does this need to exist?
OpCode::SET_GLOBAL { constant } => {
let value = self.body.chunk.get_constant(*constant);
if let Value::String(s) = value {
let v = self.duplicate();
// TODO we could take, expr statements send pop, this is a hack of sorts, ideally the compiler only sends a pop for nonassigment
// alternatively we can peek the value, that might be better to prevent side effects
// do we want expressions to evaluate to a value? probably? is this is ideal for implicit returns?
// if let Some(_) = self.globals.get(&**s) {
// self.globals.insert(s.to_string(), v);
// } else {
// self.globals.insert(s.to_string(), v);
// }
self.globals.insert(s.to_string(), v);
} else {
return Err(SiltError::VmCorruptConstant);
}
}
OpCode::GET_GLOBAL { constant } => {
let value = Self::get_chunk(&frame).get_constant(*constant);
if let Value::String(s) = value {
if let Some(v) = self.globals.get(&**s) {
self.push(v.clone());
} else {
self.push(Value::Nil);
}
} else {
return Err(SiltError::VmCorruptConstant);
}
}
OpCode::SET_LOCAL { index } => {
let value = self.duplicate();
// frame.stack[*index as usize] = value;
frame.set_val(*index, value)
}
OpCode::GET_LOCAL { index } => {
self.push(frame.get_val(*index).clone());
// self.push(frame.stack[*index as usize].clone());
// TODO ew cloning, is our cloning optimized yet?
// TODO also we should convert from stack to register based so we can use the index as a reference instead
}
OpCode::DEFINE_LOCAL { constant } => todo!(),
OpCode::ADD => binary_op!(self, +, Add),
OpCode::SUB => binary_op!(self,-, Sub),
OpCode::MULTIPLY => binary_op!(self,*, Multiply),
OpCode::DIVIDE => {
let right = self.pop();
let left = self.pop();
match (left, right) {
(Value::Number(left), Value::Number(right)) => {
self.push(Value::Number(left / right))
}
(Value::Integer(left), Value::Integer(right)) => {
self.push(Value::Number(left as f64 / right as f64))
}
(Value::Number(left), Value::Integer(right)) => {
self.push(Value::Number(left / right as f64))
}
(Value::Integer(left), Value::Number(right)) => {
self.push(Value::Number(left as f64 / right))
}
(l, r) => {
return Err(SiltError::ExpOpValueWithValue(
l.to_error(),
Operator::Divide,
r.to_error(),
))
}
}
}
OpCode::NEGATE => {
match self.peek() {
Value::Number(n) => {
let f = -n;
self.pop();
self.push(Value::Number(f))
}
Value::Integer(i) => {
let f = -i;
self.pop();
self.push(Value::Integer(f))
}
// None => Err(SiltError::EarlyEndOfFile)?,
c => Err(SiltError::ExpInvalidNegation(c.to_error()))?,
}
// TODO test this vs below: unsafe { *self.stack_top = -*self.stack_top };
}
OpCode::NIL => self.push(Value::Nil),
OpCode::TRUE => self.push(Value::Bool(true)),
OpCode::FALSE => self.push(Value::Bool(false)),
OpCode::NOT => {
let value = self.pop();
self.push(Value::Bool(!Self::is_truthy(&value)));
}
OpCode::EQUAL => {
let r = self.pop();
let l = self.pop();
self.push(Value::Bool(Self::is_equal(&l, &r)));
}
OpCode::NOT_EQUAL => {
let r = self.pop();
let l = self.pop();
self.push(Value::Bool(!Self::is_equal(&l, &r)));
}
OpCode::LESS => {
let r = self.pop();
let l = self.pop();
self.push(Value::Bool(Self::is_less(&l, &r)?));
}
OpCode::LESS_EQUAL => {
let r = self.pop();
let l = self.pop();
self.push(Value::Bool(!Self::is_greater(&l, &r)?));
}
OpCode::GREATER => {
let r = self.pop();
let l = self.pop();
self.push(Value::Bool(Self::is_greater(&l, &r)?));
}
OpCode::GREATER_EQUAL => {
let r = self.pop();
let l = self.pop();
self.push(Value::Bool(!Self::is_less(&l, &r)?));
}
OpCode::CONCAT => {
let r = self.pop();
let l = self.pop();
match (l, r) {
(Value::String(left), Value::String(right)) => {
self.push(Value::String(Box::new(*left + &right)))
}
(Value::String(left), v2) => {
self.push(Value::String(Box::new(*left + &v2.to_string())))
}
(v1, Value::String(right)) => {
self.push(Value::String(Box::new(v1.to_string() + &right)))
}
(v1, v2) => {
self.push(Value::String(Box::new(v1.to_string() + &v2.to_string())))
}
}
}
OpCode::LITERAL { dest, literal } => {}
OpCode::POP => {
last = self.pop();
}
OpCode::POPS(n) => self.popn_drop(*n), //TODO here's that 255 local limit again
OpCode::CLOSE_UPVALUES(n) => {
self.close_n_upvalues(*n);
}
OpCode::GOTO_IF_FALSE(offset) => {
let value = self.peek();
// println!("GOTO_IF_FALSE: {}", value);
if !Self::is_truthy(value) {
frame.forward(*offset);
}
}
OpCode::GOTO_IF_TRUE(offset) => {
let value = self.peek();
if Self::is_truthy(value) {
frame.forward(*offset);
}
}
OpCode::FORWARD(offset) => {
frame.forward(*offset);
}
OpCode::REWIND(offset) => {
frame.rewind(*offset);
}
OpCode::CLOSURE { constant } => {
let value = Self::get_chunk(&frame).get_constant(*constant);
devout!("CLOSURE: {}", value);
if let Value::Function(f) = value {
// f.upvalue_count
let mut closure =
Closure::new(f.clone(), Vec::with_capacity(f.upvalue_count as usize));
// let reserved_value = self.reserve();
if f.upvalue_count >= 0 {
let next_instruction = frame.get_next_n_codes(f.upvalue_count as usize);
for i in 0..f.upvalue_count {
if let OpCode::REGISTER_UPVALUE { index, neighboring } =
next_instruction[i as usize]
{
closure.upvalues.push(if neighboring {
// insert at i
self.capture_upvalue(index, frame)
// closure.upvalues.insert(
// i as usize,
// frame.function.upvalues[index as usize].clone(),
// );
// *slots.add(index) ?
} else {
frame.function.upvalues[index as usize].clone()
});
} else {
println!(
"next instruction is not CLOSE_UPVALUE {}",
next_instruction[i as usize]
);
unreachable!()
}
}
self.push(Value::Closure(Rc::new(closure)));
} else {
// devout!("closure is of type {}", closure.function.function.name);
return Err(SiltError::VmRuntimeError);
}
frame.shift(f.upvalue_count as usize);
}
}
OpCode::GET_UPVALUE { index } => {
let value = frame.function.upvalues[*index as usize]
.borrow()
.copy_value();
#[cfg(feature = "dev-out")]
{
frame.print_local_stack();
frame.function.upvalues.iter().for_each(|f| {
f.borrow().print();
});
}
devout!("GET_UPVALUE: {}", value);
self.push(value);
}
OpCode::SET_UPVALUE { index } => {
let value = self.pop();
let ff = &frame.function.upvalues;
ff[*index as usize].borrow_mut().close_around(value);
// unsafe { *upvalue.value = value };
}
OpCode::CALL(param_count) => {
let value = self.peekn(*param_count);
devout!("CALL: {}", value);
match value {
Value::Closure(c) => {
// TODO this logic is identical to function, but to make this a function causes some lifetime issues. A macro would work but we're already a little macro heavy aren't we?
let frame_top =
unsafe { self.stack_top.sub((*param_count as usize) + 1) };
let new_frame = CallFrame::new(
c.clone(),
self.stack_count - (*param_count as usize) - 1,
);
frames.push(new_frame);
frame = frames.last_mut().unwrap();
frame.local_stack = frame_top;
devout!("top of frame stack {}", unsafe { &*frame.local_stack });
frame_count += 1;
}
Value::Function(func) => {
// let frame_top =
// unsafe { self.stack_top.sub((*param_count as usize) + 1) };
// let new_frame = CallFrame::new(
// func.clone(),
// self.stack_count - (*param_count as usize) - 1,
// );
// frames.push(new_frame);
// frame = frames.last_mut().unwrap();
// frame.local_stack = frame_top;
// devout!("top of frame stack {}", unsafe { &*frame.local_stack });
// frame_count += 1;
// devout!("current stack count {}", frame.stack_snapshot);
// frame.ip = f.chunk.code.as_ptr();
// // frame.stack.resize(256, Value::Nil); // TODO
// self.push(Value::Function(f.clone())); // TODO this needs to store the function object itself somehow, RC?
}
Value::NativeFunction(_) => {
// get args including the function value at index 0. We do it here so don't have mutability issues with native fn
let mut args = self.popn(*param_count + 1);
if let Value::NativeFunction(f) = args.remove(0) {
let res = ((*f).function)(self, args);
// self.popn_drop(*param_count);
self.push(res);
} else {
unreachable!();
}
}
_ => {
return Err(SiltError::NotCallable(format!("Value: {}", value)));
}
}
}
OpCode::PRINT => {
println!("<<<<<< {} >>>>>>>", self.pop());
}
OpCode::META(_) => todo!(),
OpCode::REGISTER_UPVALUE {
index: _,
neighboring: _,
} => unreachable!(),
OpCode::LENGTH => {
let value = self.pop();
match value {
Value::String(s) => self.push(Value::Integer(s.len() as i64)),
Value::Table(t) => self.push(Value::Integer(t.borrow().len() as i64)),
_ => Err(SiltError::ExpInvalidLength(value.to_error()))?,
}
}
OpCode::NEW_TABLE => {
self.push(self.new_table());
self.table_counter += 1;
}
OpCode::TABLE_INSERT { offset } => {
self.insert_immediate_table(*offset)?;
}
OpCode::TABLE_BUILD(n) => {
self.build_table(*n)?;
}
OpCode::TABLE_SET { depth } => {
let value = self.pop();
self.operate_table(*depth, Some(value))?;
}
OpCode::TABLE_SET_BY_CONSTANT { constant } => {
let value = self.pop();
let key = Self::get_chunk(&frame).get_constant(*constant);
let table = self.peek_mut();
if let Value::Table(t) = table {
// TODO can we pre-hash this to avoid a clone?
t.borrow_mut().insert(key.clone(), value);
} else {
return Err(SiltError::VmNonTableOperations(table.to_error()));
}
}
OpCode::TABLE_GET { depth } => {
self.operate_table(*depth, None)?;
}
OpCode::TABLE_GET_FROM { index } => {
// let key = self.pop();
// let table = frame.get_val_mut(*index);
// if let Value::Table(t) = table {
// let v = t.borrow().get_value(&key);
// self.push(v);
// } else {
// return Err(SiltError::VmNonTableOperations(table.to_error()));
// }
todo!("TABLE_GET_FROM")
}
OpCode::TABLE_GET_BY_CONSTANT { constant } => {
let key = Self::get_chunk(&frame).get_constant(*constant);
let table = self.peek_mut();
if let Value::Table(t) = table {
let v = t.borrow().get_value(&key);
self.push(v);
} else {
return Err(SiltError::VmNonTableOperations(table.to_error()));
}
}
}
frame.iterate();
//stack
#[cfg(feature = "dev-out")]
{
self.print_stack();
println!("---");
}
}
}
// TODO is having a default empty chunk cheaper?
/** We're operating on the assumtpion a chunk is always present when using this */
fn get_chunk(frame: &CallFrame) -> &Chunk {
&frame.function.function.chunk
}
// pub fn reset_stack(&mut self) {
// // TODO we probably dont even need to clear the stack, just reset the stack_top
// // self.stack.clear();
// // set to 0 index of stack
// self.stack_top = unsafe { self.stack.as_mut_ptr() };
// }
fn is_truthy(v: &Value) -> bool {
match v {
Value::Bool(b) => *b,
Value::Nil => false,
_ => true,
}
}
fn is_equal(l: &Value, r: &Value) -> bool {
match (l, r) {
(Value::Number(left), Value::Number(right)) => left == right,
(Value::Integer(left), Value::Integer(right)) => left == right,
(Value::Number(left), Value::Integer(right)) => *left == *right as f64,
(Value::Integer(left), Value::Number(right)) => *left as f64 == *right,
(Value::String(left), Value::String(right)) => left == right,
(Value::Bool(left), Value::Bool(right)) => left == right,
(Value::Nil, Value::Nil) => true,
(Value::Infinity(left), Value::Infinity(right)) => left == right,
(_, _) => false,
}
}
fn is_less(l: &Value, r: &Value) -> Result<bool, SiltError> {
Ok(match (l, r) {
(Value::Number(left), Value::Number(right)) => left < right,
(Value::Integer(left), Value::Integer(right)) => left < right,
(Value::Number(left), Value::Integer(right)) => *left < *right as f64,
(Value::Integer(left), Value::Number(right)) => (*left as f64) < (*right),
(Value::Infinity(left), Value::Infinity(right)) => left != right && *left,
(_, _) => Err(SiltError::ExpOpValueWithValue(
l.to_error(),
Operator::Less,
r.to_error(),
))?,
})
}
fn is_greater(l: &Value, r: &Value) -> Result<bool, SiltError> {
Ok(match (l, r) {
(Value::Number(left), Value::Number(right)) => left > right,
(Value::Integer(left), Value::Integer(right)) => {
// println!(" is {} > {}", left, right);
left > right
}
(Value::Number(left), Value::Integer(right)) => *left > *right as f64,
(Value::Integer(left), Value::Number(right)) => (*left as f64) > (*right),
(Value::Infinity(left), Value::Infinity(right)) => left != right && !*left,
(_, _) => Err(SiltError::ExpOpValueWithValue(
l.to_error(),
Operator::Greater,
r.to_error(),
))?,
})
}
// /** unsafe as hell, we're relying on compiler*/
// fn read_string(&mut self, constant: u8) -> String {
// let value = self.get_chunk().get_constant(constant);
// if let Value::String(s) = value {
// return s.to_string();
// } else {
// unreachable!("Only strings can be identifiers")
// }
// }
fn call(&self, function: &Rc<Closure>, param_count: u8) -> CallFrame {
let frame_top = unsafe { self.stack_top.sub((param_count as usize) + 1) };
let new_frame = CallFrame::new(
function.clone(),
self.stack_count - (param_count as usize) - 1,
);
new_frame
}
fn capture_upvalue(&mut self, index: u8, frame: &CallFrame) -> Rc<RefCell<UpValue>> {
//, stack: *mut Value,
//stack
// self.print_stack();
#[cfg(feature = "dev-out")]
frame.print_local_stack();
let value = unsafe { frame.local_stack.add(index as usize) };
devout!("2capture_upvalue at index {} : {}", index, unsafe {
&*value
});
let mut ind = 0;
for (i, up) in self.open_upvalues.iter().enumerate() {
let upvalue = up.borrow();
if upvalue.location == value {
return up.clone();
}
ind = i;
if upvalue.location > value {
break;
}
}
let u = Rc::new(RefCell::new(UpValue::new(index, value)));
self.open_upvalues.insert(ind, u.clone());
#[cfg(feature = "dev-out")]
self.open_upvalues.iter().for_each(|up| {
up.borrow().print();
});
u
// self
// .open_upvalues
// .iter()
// // DEV originally we loop through until the pointer is not greater then the stack pointer
// .find(|upvalue| upvalue.index == index)
// {
// Some(u) => u.clone(),
// None => {
// // let v = unsafe { stack.sub(index as usize) };
// let u = Rc::new(UpValue::new(index));
// self.open_upvalues.push(u.clone());
// u
// }
// }
// let mut prev = stack;
// for _ in 0..index {
// prev = unsafe { prev.sub(1) };
// }
// unsafe { prev.read() }
}
fn close_upvalue(&mut self, value: Value) {
devout!("close_upvalue: {}", value);
// for up in
// self.open_upvalues
// .iter()
// .find(|up| {
// let mut upvalue = up.borrow_mut();
// if upvalue.index >= self.stack_count as u8 {
// false
// } else {
// true
// }
// })
// .unwrap()
// .borrow_mut()
// .close(value);
// TODO
// self.open_upvalues.retain(|up| {
// let mut upvalue = up.borrow_mut();
// if upvalue.index >= self.stack_count as u8 {
// upvalue.close(value);
// false
// } else {
// true
// }
// });
}
fn new_table(&self) -> Value {
let t = Table::new(self.table_counter);
Value::Table(Rc::new(RefCell::new(t)))
}
fn build_table(&mut self, n: u8) -> Result<(), SiltError> {
let offset = n as usize + 1;
let table_point = unsafe { self.stack_top.sub(offset) };
let table = unsafe { &*table_point };
if let Value::Table(t) = table {
let mut b = t.borrow_mut();
// push in reverse
for i in (0..n).rev() {
let value = unsafe { self.stack_top.sub(i as usize + 1).replace(Value::Nil) };
b.push(value);
}
self.stack_count -= offset - 1;
self.stack_top = unsafe { table_point.add(1) };
Ok(())
} else {
Err(SiltError::ChunkCorrupt) // shouldn't happen unless our compiler really screwed up
}
}
/** Used at table creation to simplify direct index insertion */
fn insert_immediate_table(&mut self, offset: u8) -> Result<(), SiltError> {
let table = unsafe { &*self.stack_top.sub(offset as usize + 3) }; // -3 because -1 for top of stack, -1 for key, -1 for value, and then offset from there
if let Value::Table(t) = table {
let value = self.pop();
let key = self.pop();
t.borrow_mut().insert(key, value);
Ok(())
} else {
Err(SiltError::ChunkCorrupt) // shouldn't happen unless our compiler really screwed up
}
}
fn operate_table(&mut self, index: u8, set: Option<Value>) -> Result<(), SiltError> {
// let value = unsafe { self.stack_top.read() };
// let value = unsafe { self.stack_top.replace(Value::Nil) };
let u = index as usize + 1;
let table_point = unsafe { self.stack_top.sub(u) };
let table = unsafe { &*table_point };
if let Value::Table(t) = table {
let mut current = t;
for i in 1..=index {
let key = unsafe { self.stack_top.sub(i as usize).replace(Value::Nil) };
devout!("get from table with key: {}", key);
if i == index {
let offset = index as usize;
self.stack_count -= offset;
unsafe { self.stack_top = self.stack_top.sub(offset) };
// assert!(self.stack_top == table_point);
match set {
Some(value) => {
current.borrow_mut().insert(key, value);
unsafe { table_point.replace(Value::Nil) };
}
None => {
let out = current.borrow().get_value(&key);
unsafe { table_point.replace(out) };
}
}
return Ok(());
} else {
let check = unsafe { current.try_borrow_unguarded() }.unwrap().get(&key);
match check {
Some(Value::Table(t)) => {
current = t;
}
Some(v) => {
return Err(SiltError::VmNonTableOperations(v.to_error()));
}
None => {
return Err(SiltError::VmNonTableOperations(ErrorTypes::Nil));
}
}
}
}
Err(SiltError::VmRuntimeError)
} else {
Err(SiltError::VmNonTableOperations(table.to_error()))
}
// self.stack_count -= 1;
// unsafe { self.stack_top = self.stack_top.sub(1) };
// let v = unsafe { self.stack_top.replace(Value::Nil) };
// // TODO is there a way to read without segfaulting?
// // We'd have to list the value to be forggoten, but is this even faster?
// // let v = unsafe { self.stack_top.read() };
// devout!("pop: {}", v);
// v
// // let value = self.stack[self.stack_count - (index as usize) - 1].clone();
// if let Value::Table(t) = value {
// t
// } else {
// unreachable!("Only tables can be indexed")
// }
}
/** Register a native function on the global table */
pub fn register_native_function(
&mut self,
name: &str,
function: fn(&mut Self, Vec<Value>) -> Value,
) {
let fn_obj = Rc::new(NativeObject::new(name.to_string(), function));
self.globals
.insert(name.to_string(), Value::NativeFunction(fn_obj));
}
/** Load standard library functions */
pub fn load_standard_library(&mut self) {
self.register_native_function("clock", crate::standard::clock);
self.register_native_function("print", crate::standard::print);
}
fn print_raw_stack(&self) {
println!("=== Stack ({}) ===", self.stack_count);
// 0 to stack_top
print!("[");
for i in self.stack.iter() {
print!("{} ", i);
}
print!("]");
println!("---");
}
pub fn print_stack(&self) {
println!("=== Stack ({}) ===", self.stack_count);
print!("[");
let mut c = 0;
for i in self.stack.iter() {
c += 1;
if c > self.stack_count {
break;
}
let s = format!("{:?}", i);
if s == "nil" {
print!("_");
} else {
print!("{} ", i);
}
}
print!("]");
println!("---");
}
}