Skip to content

Commit

Permalink
Fix exceptions try-catch and networking functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mliezun committed Oct 31, 2023
1 parent b990b63 commit ce4d9f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
42 changes: 29 additions & 13 deletions rewrite_in_rust/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,15 @@ impl StmtVisitor<Chunk> for Compiler {

let try_body_chunk = stmt.try_body.accept(self);
chunk.append(&mut try_body_chunk.instructions.clone());
chunk.push(
Instruction {
opcode: OpCode::DeregisterTryCatch,
a: 0,
b: 0,
c: 0,
},
None,
);

// Jmp to end, needs to be patched
chunk.push(
Expand Down Expand Up @@ -530,29 +539,32 @@ impl StmtVisitor<Chunk> for Compiler {
},
Some(stmt.name.clone()),
);
chunk.push(
Instruction {
opcode: OpCode::DeregisterTryCatch,
a: 0,
b: 0,
c: 0,
},
None,
);
let catch_body_chunk = stmt.catch_body.accept(self);
self.leave_block();
chunk.append(&mut catch_body_chunk.instructions.clone());

let jmp_offset = chunk.instructions.len();
let jmp_offset = chunk.instructions.len() - catch_offset + 1;
for inst in &mut chunk.instructions {
if inst.inst.opcode == OpCode::Jmp && inst.inst.b == 0 && inst.inst.c == 0 {
if inst.inst.opcode == OpCode::Jmp
&& inst.inst.a == 0
&& inst.inst.b == 0
&& inst.inst.c == 0
{
inst.inst.b = (jmp_offset >> 8) as u8;
inst.inst.c = jmp_offset as u8;
break;
}
}

chunk.push(
Instruction {
opcode: OpCode::DeregisterTryCatch,
a: 0,
b: 0,
c: 0,
},
None,
);

return chunk;
}

Expand Down Expand Up @@ -1088,7 +1100,11 @@ impl StmtVisitor<Chunk> for Compiler {
chunk.append(&mut else_body.clone());
let chunk_size = chunk.instructions.len();
for (offset, inst) in chunk.instructions.iter_mut().enumerate() {
if inst.inst.opcode == OpCode::Jmp && inst.inst.b == 0 && inst.inst.c == 0 {
if inst.inst.opcode == OpCode::Jmp
&& inst.inst.a == 0
&& inst.inst.b == 0
&& inst.inst.c == 0
{
// Jump to the next instruction after this chunk
let jmp_offset = chunk_size - offset;
inst.inst.b = (jmp_offset >> 8) as u8;
Expand Down
29 changes: 20 additions & 9 deletions rewrite_in_rust/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl Net {
let native_value = match &values[0] {
Value::Native(n) => n,
_ => {
return Err(ERR_EXPECTED_STRING);
return Err(ERR_EXPECTED_OBJECT);
}
};
let baggage = native_value.baggage.as_ref().unwrap();
Expand All @@ -569,18 +569,22 @@ impl Net {
let native_value = match &values[0] {
Value::Native(n) => n,
_ => {
return Err(ERR_EXPECTED_STRING);
return Err(ERR_EXPECTED_OBJECT);
}
};
let baggage = native_value.baggage.as_ref().unwrap();
let mut buf = String::new();
let mut buf: Vec<u8> = vec![0; 4096];
let size;
match baggage.borrow_mut().deref_mut() {
NativeBaggage::TcpSocket(socket) => {
socket.read_to_string(&mut buf).expect("Read connection")
size = socket.read(&mut buf).expect("Read from connection");
}
_ => return Err(ERR_EXPECTED_OBJECT),
};
return Ok(Value::String(StringValue { s: buf }));
let read_from_connection = &buf[0..size];
return Ok(Value::String(StringValue {
s: String::from_utf8(read_from_connection.to_vec()).expect("Read as string"),
}));
}

fn conn_write(values: Vec<Value>) -> Result<Value, RuntimeErr> {
Expand Down Expand Up @@ -618,7 +622,7 @@ impl Net {
let native_value = match &values[0] {
Value::Native(n) => n,
_ => {
return Err(ERR_EXPECTED_STRING);
return Err(ERR_EXPECTED_OBJECT);
}
};
let baggage = native_value.baggage.as_ref().unwrap();
Expand All @@ -641,7 +645,7 @@ impl Net {
let native_value = match &values[0] {
Value::Native(n) => n,
_ => {
return Err(ERR_EXPECTED_STRING);
return Err(ERR_EXPECTED_OBJECT);
}
};
let baggage = native_value.baggage.as_ref().unwrap();
Expand All @@ -661,7 +665,7 @@ impl Net {
let native_value = match &values[0] {
Value::Native(n) => n,
_ => {
return Err(ERR_EXPECTED_STRING);
return Err(ERR_EXPECTED_OBJECT);
}
};
let baggage = native_value.baggage.as_ref().unwrap();
Expand Down Expand Up @@ -751,7 +755,14 @@ impl Net {
});
}
};
let mut address = string_value.s.to_socket_addrs();
let bind_to_address = if string_value.s.starts_with(":") {
let mut ip = "0.0.0.0".to_string();
ip.push_str(string_value.s.as_str());
ip
} else {
string_value.s.clone()
};
let mut address = bind_to_address.to_socket_addrs();
let address = match &mut address {
Ok(a) => a.next().unwrap(),
Err(_) => {
Expand Down
4 changes: 3 additions & 1 deletion rewrite_in_rust/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ macro_rules! throw_exception {
}
let stack = &$self.stack[catch_exc.stack_ix];
$this = stack.this.clone();
$sp = stack.sp;
$sp = catch_exc.sp;
$pc = catch_exc.catch_block_pc;
catch_exc.exception = Some($error);

Expand Down Expand Up @@ -119,6 +119,7 @@ impl Record {
#[derive(Debug)]
pub struct CatchException {
stack_ix: usize,
sp: usize,
catch_block_pc: usize,
exception: Option<RuntimeErr>,
}
Expand Down Expand Up @@ -1488,6 +1489,7 @@ impl VM {
self.catch_exceptions.push(CatchException {
catch_block_pc: pc + inst.bx() as usize,
stack_ix: self.stack.len() - 1,
sp: sp,
exception: None,
});
pc += 1;
Expand Down

0 comments on commit ce4d9f5

Please sign in to comment.