Skip to content

Commit

Permalink
Move variant construction around a bit
Browse files Browse the repository at this point in the history
  * Var now holds the raw Bytes and constructs the Variant on demand
  * The hope being this makes it faster/easier to move in and out of
    storage
  * The inside of the Variant now holds an Arc< for non-Scalar values

In the future I might make it so Var can contain either a
"materialized" Variant, or the Bytes.
  • Loading branch information
rdaum committed Aug 30, 2024
1 parent 0185ff4 commit 2214a62
Show file tree
Hide file tree
Showing 28 changed files with 300 additions and 305 deletions.
22 changes: 11 additions & 11 deletions crates/compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl CodegenState {
// Create an anonymous jump label at the current position and return its unique ID.
fn make_jump_label(&mut self, name: Option<Name>) -> Label {
let id = Label(self.jumps.len() as u16);
let position = (self.ops.len()).into();
let position = self.ops.len().into();
self.jumps.push(JumpLabel { id, name, position });
id
}
Expand Down Expand Up @@ -316,15 +316,15 @@ impl CodegenState {
self.emit(Op::ImmNone);
}
Variant::Obj(oid) => {
self.emit(Op::ImmObjid(*oid));
self.emit(Op::ImmObjid(oid));
}
Variant::Int(i) => match i32::try_from(*i) {
Variant::Int(i) => match i32::try_from(i) {
Ok(n) => self.emit(Op::ImmInt(n)),
Err(_) => self.emit(Op::ImmBigInt(*i)),
Err(_) => self.emit(Op::ImmBigInt(i)),
},
Variant::Float(f) => self.emit(Op::ImmFloat(*f)),
Variant::Float(f) => self.emit(Op::ImmFloat(f)),
Variant::Err(e) => {
self.emit(Op::ImmErr(*e));
self.emit(Op::ImmErr(e));
}
_ => {
let literal = self.add_literal(v);
Expand Down Expand Up @@ -444,7 +444,7 @@ impl CodegenState {
self.pop_stack(1);
self.generate_expr(consequence.as_ref())?;
let end_label = self.make_jump_label(None);
self.emit(Op::Jump { label: end_label });
self.emit(Jump { label: end_label });
self.pop_stack(1);
self.commit_jump_label(else_label);
self.generate_expr(alternative.as_ref())?;
Expand Down Expand Up @@ -533,7 +533,7 @@ impl CodegenState {
self.emit(Op::EndScope {
num_bindings: arm.environment_width as u16,
});
self.emit(Op::Jump { label: end_label });
self.emit(Jump { label: end_label });

// This is where we jump to if the condition is false; either the end of the
// if statement, or the start of the next ('else or elseif') arm.
Expand Down Expand Up @@ -587,7 +587,7 @@ impl CodegenState {
for stmt in body {
self.generate_stmt(stmt)?;
}
self.emit(Op::Jump { label: loop_top });
self.emit(Jump { label: loop_top });
// This opcode should never get hit.
self.emit(Op::EndScope {
num_bindings: *environment_width as u16,
Expand Down Expand Up @@ -666,7 +666,7 @@ impl CodegenState {
for s in body {
self.generate_stmt(s)?;
}
self.emit(Op::Jump {
self.emit(Jump {
label: loop_start_label,
});
self.emit(Op::EndScope {
Expand Down Expand Up @@ -730,7 +730,7 @@ impl CodegenState {
self.generate_stmt(stmt)?;
}
if i + 1 < excepts.len() {
self.emit(Op::Jump { label: end_label });
self.emit(Jump { label: end_label });
}
}
self.emit(Op::EndScope {
Expand Down
2 changes: 1 addition & 1 deletion crates/console-host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ fn console_loop(
return;
}
Ok(ConnectionEvent::RequestInput(requested_input_id)) => {
(*output_input_request_id.lock().unwrap()) =
*output_input_request_id.lock().unwrap() =
Some(Uuid::from_u128(requested_input_id));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/daemon/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ impl RpcServer {
// with its new player objid and login result.
// If it's not an objid, that's considered an auth failure.
match v.variant() {
Variant::Obj(o) => *o,
Variant::Obj(o) => o,
_ => {
return Ok(LoginResult(None));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/db-wiredtiger/src/bindings/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Connection {
}

/// Open a session.
pub fn open_session(self: Arc<Self>, config: SessionConfig) -> Result<session::Session, u8> {
pub fn open_session(self: Arc<Self>, config: SessionConfig) -> Result<Session, u8> {
let mut session: *mut bindings::wiredtiger::WT_SESSION =
null::<bindings::wiredtiger::WT_SESSION>() as _;
let config_str = config.as_config_string();
Expand Down
8 changes: 4 additions & 4 deletions crates/db/src/db_worldstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,15 @@ impl WorldState for DbTxWorldState {
let Variant::Obj(owner) = value.variant() else {
return Err(WorldStateError::PropertyTypeMismatch);
};
self.tx.set_object_owner(obj, *owner)?;
self.tx.set_object_owner(obj, owner)?;
return Ok(());
}

if pname == *R_SYM {
let Variant::Int(v) = value.variant() else {
return Err(WorldStateError::PropertyTypeMismatch);
};
if *v == 1 {
if v == 1 {
flags.set(ObjFlag::Read);
} else {
flags.clear(ObjFlag::Read);
Expand All @@ -384,7 +384,7 @@ impl WorldState for DbTxWorldState {
let Variant::Int(v) = value.variant() else {
return Err(WorldStateError::PropertyTypeMismatch);
};
if *v == 1 {
if v == 1 {
flags.set(ObjFlag::Write);
} else {
flags.clear(ObjFlag::Write);
Expand All @@ -397,7 +397,7 @@ impl WorldState for DbTxWorldState {
let Variant::Int(v) = value.variant() else {
return Err(WorldStateError::PropertyTypeMismatch);
};
if *v == 1 {
if v == 1 {
flags.set(ObjFlag::Fertile);
} else {
flags.clear(ObjFlag::Fertile);
Expand Down
4 changes: 2 additions & 2 deletions crates/kernel/src/builtins/bf_list_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ fn do_re_match(bf_args: &mut BfCallState<'_>, reverse: bool) -> Result<BfRet, Bf
let Variant::Int(case_matters) = bf_args.args[2].variant() else {
return Err(BfErr::Code(E_TYPE));
};
*case_matters == 1
case_matters == 1
} else {
false
};
Expand Down Expand Up @@ -404,7 +404,7 @@ fn bf_substitute(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
let (Variant::Int(start), Variant::Int(end)) = (start.variant(), end.variant()) else {
return Err(BfErr::Code(E_INVARG));
};
mysubs.push((*start as isize, *end as isize));
mysubs.push((start as isize, end as isize));
}

match substitute(&template.as_string(), &mysubs, &source.as_string()) {
Expand Down
22 changes: 11 additions & 11 deletions crates/kernel/src/builtins/bf_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn bf_min(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
return Err(BfErr::Code(E_ARGS));
}

match (bf_args.args[0].variant(), bf_args.args[1].variant()) {
match (&bf_args.args[0].variant(), &bf_args.args[1].variant()) {
(Variant::Int(a), Variant::Int(b)) => Ok(Ret(v_int(*a.min(b)))),
(Variant::Float(a), Variant::Float(b)) => {
let m = R64::from(*a).min(R64::from(*b));
Expand All @@ -58,7 +58,7 @@ fn bf_max(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
return Err(BfErr::Code(E_ARGS));
}

match (bf_args.args[0].variant(), bf_args.args[1].variant()) {
match (&bf_args.args[0].variant(), &bf_args.args[1].variant()) {
(Variant::Int(a), Variant::Int(b)) => Ok(Ret(v_int(*a.max(b)))),
(Variant::Float(a), Variant::Float(b)) => {
let m = R64::from(*a).max(R64::from(*b));
Expand All @@ -75,7 +75,7 @@ fn bf_random(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
}

let mut rng = rand::thread_rng();
match bf_args.args.first().map(|var| var.variant()) {
match &bf_args.args.first().map(|var| var.variant()) {
Some(Variant::Int(i)) if *i > 0 => Ok(Ret(v_int(rng.gen_range(1..=*i)))),
Some(Variant::Int(_)) => Err(BfErr::Code(E_INVARG)),
None => Ok(Ret(v_int(rng.gen_range(1..=2147483647)))),
Expand All @@ -94,12 +94,12 @@ fn bf_floatstr(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
_ => return Err(BfErr::Code(E_TYPE)),
};

let precision = match bf_args.args[1].variant() {
let precision = match &bf_args.args[1].variant() {
Variant::Int(i) if *i > 0 => *i as usize,
_ => return Err(BfErr::Code(E_TYPE)),
};

let scientific = match bf_args.args[2].variant() {
let scientific = match &bf_args.args[2].variant() {
Variant::Int(b) => *b == 1,
_ => return Err(BfErr::Code(E_TYPE)),
};
Expand Down Expand Up @@ -165,7 +165,7 @@ fn bf_sqrt(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
_ => return Err(BfErr::Code(E_TYPE)),
};

if *x < 0.0 {
if x < 0.0 {
return Err(BfErr::Code(E_ARGS));
}

Expand All @@ -183,7 +183,7 @@ fn bf_asin(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
_ => return Err(BfErr::Code(E_TYPE)),
};

if !(-1.0..=1.0).contains(x) {
if !(-1.0..=1.0).contains(&x) {
return Err(BfErr::Code(E_ARGS));
}

Expand All @@ -201,7 +201,7 @@ fn bf_acos(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
_ => return Err(BfErr::Code(E_TYPE)),
};

if !(-1.0..=1.0).contains(x) {
if !(-1.0..=1.0).contains(&x) {
return Err(BfErr::Code(E_ARGS));
}

Expand All @@ -220,7 +220,7 @@ fn bf_atan(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
};

let x = match bf_args.args[1].variant() {
Variant::Float(f) => *f,
Variant::Float(f) => f,
_ => return Err(BfErr::Code(E_TYPE)),
};

Expand Down Expand Up @@ -294,7 +294,7 @@ fn bf_log(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
_ => return Err(BfErr::Code(E_TYPE)),
};

if *x <= 0.0 {
if x <= 0.0 {
return Err(BfErr::Code(E_ARGS));
}

Expand All @@ -312,7 +312,7 @@ fn bf_log10(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
_ => return Err(BfErr::Code(E_TYPE)),
};

if *x <= 0.0 {
if x <= 0.0 {
return Err(BfErr::Code(E_ARGS));
}

Expand Down
33 changes: 15 additions & 18 deletions crates/kernel/src/builtins/bf_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use moor_values::model::WorldStateError;
use moor_values::model::{ObjFlag, ValSet};
use moor_values::util::BitEnum;
use moor_values::Error::{E_ARGS, E_INVARG, E_NACC, E_PERM, E_TYPE};
use moor_values::Variant;
use moor_values::{v_bool, v_int, v_none, v_objid, v_str};
use moor_values::{v_list, Sequence, Symbol};
use moor_values::{v_list_iter, NOTHING};
use moor_values::Variant;

use crate::bf_declare;
use crate::builtins::BfRet::{Ret, VmInstr};
Expand All @@ -51,10 +51,7 @@ fn bf_valid(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
let Variant::Obj(obj) = bf_args.args[0].variant() else {
return Err(BfErr::Code(E_TYPE));
};
let is_valid = bf_args
.world_state
.valid(*obj)
.map_err(world_state_bf_err)?;
let is_valid = bf_args.world_state.valid(obj).map_err(world_state_bf_err)?;
Ok(Ret(v_bool(is_valid)))
}
bf_declare!(valid, bf_valid);
Expand All @@ -71,7 +68,7 @@ fn bf_parent(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
}
let parent = bf_args
.world_state
.parent_of(bf_args.task_perms_who(), *obj)
.parent_of(bf_args.task_perms_who(), obj)
.map_err(world_state_bf_err)?;
Ok(Ret(v_objid(parent)))
}
Expand All @@ -89,7 +86,7 @@ fn bf_chparent(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
};
bf_args
.world_state
.change_parent(bf_args.task_perms_who(), *obj, *new_parent)
.change_parent(bf_args.task_perms_who(), obj, new_parent)
.map_err(world_state_bf_err)?;
Ok(Ret(v_none()))
}
Expand All @@ -104,7 +101,7 @@ fn bf_children(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
};
let children = bf_args
.world_state
.children_of(bf_args.task_perms_who(), *obj)
.children_of(bf_args.task_perms_who(), obj)
.map_err(world_state_bf_err)?;

let children = children.iter().map(v_objid).collect::<Vec<_>>();
Expand Down Expand Up @@ -326,7 +323,7 @@ fn bf_recycle(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
// look again anyways.
let Ok(exitfunc) = bf_args.world_state.find_method_verb_on(
bf_args.task_perms_who(),
*head_obj,
head_obj,
*EXITFUNC_SYM,
) else {
// If there's no :exitfunc, we can just move on to the next object.
Expand All @@ -344,8 +341,8 @@ fn bf_recycle(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
resolved_verb: exitfunc,
call: VerbCall {
verb_name: *EXITFUNC_SYM,
location: *head_obj,
this: *head_obj,
location: head_obj,
this: head_obj,
player: bf_args.exec_state.top().player,
args: vec![v_objid(obj)],
argstr: "".to_string(),
Expand Down Expand Up @@ -613,7 +610,7 @@ fn bf_verbs(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
};
let verbs = bf_args
.world_state
.verbs(bf_args.task_perms_who(), *obj)
.verbs(bf_args.task_perms_who(), obj)
.map_err(world_state_bf_err)?;
let verbs: Vec<_> = verbs
.iter()
Expand All @@ -636,7 +633,7 @@ fn bf_properties(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
};
let props = bf_args
.world_state
.properties(bf_args.task_perms_who(), *obj)
.properties(bf_args.task_perms_who(), obj)
.map_err(world_state_bf_err)?;
let props: Vec<_> = props.iter().map(|p| v_str(p.name())).collect();
Ok(Ret(v_list(&props)))
Expand All @@ -654,7 +651,7 @@ fn bf_set_player_flag(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
return Err(BfErr::Code(E_INVARG));
};

let f = *f == 1;
let f = f == 1;

// User must be a wizard.
bf_args
Expand All @@ -666,7 +663,7 @@ fn bf_set_player_flag(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
// Get and set object flags
let mut flags = bf_args
.world_state
.flags_of(*obj)
.flags_of(obj)
.map_err(world_state_bf_err)?;

if f {
Expand All @@ -677,12 +674,12 @@ fn bf_set_player_flag(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {

bf_args
.world_state
.set_flags_of(bf_args.task_perms_who(), *obj, flags)
.set_flags_of(bf_args.task_perms_who(), obj, flags)
.map_err(world_state_bf_err)?;

// If the object was player, update the VM's copy of the perms.
if *obj == bf_args.task_perms().map_err(world_state_bf_err)?.who {
bf_args.exec_state.set_task_perms(*obj);
if obj == bf_args.task_perms().map_err(world_state_bf_err)?.who {
bf_args.exec_state.set_task_perms(obj);
}

Ok(Ret(v_none()))
Expand Down
Loading

0 comments on commit 2214a62

Please sign in to comment.