Skip to content

Commit

Permalink
Small cleanups in builtin prop mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rdaum committed Oct 22, 2024
1 parent eff134b commit c6cac1e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 55 deletions.
2 changes: 1 addition & 1 deletion crates/daemon/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ impl RpcServer {
connection,
ObjectRef::Id(SYSTEM_OBJECT),
Symbol::mk("do_login_command"),
args.iter().map(|s| v_str(&s)).collect(),
args.iter().map(|s| v_str(s)).collect(),
args.join(" "),
SYSTEM_OBJECT,
session,
Expand Down
86 changes: 41 additions & 45 deletions crates/db/src/db_worldstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use moor_values::model::{PropAttrs, PropFlag};
use moor_values::model::{PropDef, PropDefs};
use moor_values::model::{VerbDef, VerbDefs};
use moor_values::util::BitEnum;
use moor_values::Objid;
use moor_values::Variant;
use moor_values::NOTHING;
use moor_values::{v_int, v_objid, Var};
use moor_values::{v_bool, Objid};
use moor_values::{v_list, Symbol};
use moor_values::{v_objid, Var};

use crate::worldstate_transaction::WorldStateTransaction;

Expand Down Expand Up @@ -83,6 +83,22 @@ impl DbTxWorldState {
self.tx.update_verb(obj, verbdef.uuid(), verb_attrs)?;
Ok(())
}

/// Check the permissions for the application of an application of inheritance to a parent.
/// This is a helper function for `create_object` and `change_parent`.
/// It checks that the parent is writable and fertile, and that the parent is either the
/// NOTHING object or that the parent is usable by the object's owner.
fn check_parent(&self, perms: Objid, parent: Objid) -> Result<(), WorldStateError> {
if parent != NOTHING {
let (parentflags, parentowner) = (self.flags_of(parent)?, self.owner_of(parent)?);
self.perms(perms)?.check_object_allows(
parentowner,
parentflags,
BitEnum::new_with(ObjFlag::Write) | ObjFlag::Fertile,
)?;
}
Ok(())
}
}

impl WorldState for DbTxWorldState {
Expand Down Expand Up @@ -148,14 +164,7 @@ impl WorldState for DbTxWorldState {
owner: Objid,
flags: BitEnum<ObjFlag>,
) -> Result<Objid, WorldStateError> {
if parent != NOTHING {
let (flags, parent_owner) = (self.flags_of(parent)?, self.owner_of(parent)?);
self.perms(perms)?.check_object_allows(
parent_owner,
flags,
BitEnum::new_with(ObjFlag::Read) | ObjFlag::Fertile,
)?;
}
self.check_parent(perms, parent)?;

// TODO: ownership_quota support
// If the intended owner of the new object has a property named `ownership_quota' and the value of that property is an integer, then `create()' treats that value
Expand Down Expand Up @@ -227,7 +236,7 @@ impl WorldState for DbTxWorldState {
return Err(WorldStateError::ObjectNotFound(ObjectRef::Id(obj)));
}

// Special properties like namnne, location, and contents get treated specially.
// Special properties like name, location, and contents get treated specially.
if pname == *NAME_SYM {
return self.names_of(perms, obj).map(|(name, _)| Var::from(name));
} else if pname == *LOCATION_SYM {
Expand All @@ -239,39 +248,34 @@ impl WorldState for DbTxWorldState {
return self.owner_of(obj).map(Var::from);
} else if pname == *PROGRAMMER_SYM {
let flags = self.flags_of(obj)?;
return if flags.contains(ObjFlag::Programmer) {
Ok(v_int(1))
} else {
Ok(v_int(0))
};
return Ok(flags
.contains(ObjFlag::Programmer)
.then(|| v_bool(true))
.unwrap_or(v_bool(false)));
} else if pname == *WIZARD_SYM {
let flags = self.flags_of(obj)?;
return if flags.contains(ObjFlag::Wizard) {
Ok(v_int(1))
} else {
Ok(v_int(0))
};
return Ok(flags
.contains(ObjFlag::Wizard)
.then(|| v_bool(true))
.unwrap_or(v_bool(false)));
} else if pname == *R_SYM {
let flags = self.flags_of(obj)?;
return if flags.contains(ObjFlag::Read) {
Ok(v_int(1))
} else {
Ok(v_int(0))
};
return Ok(flags
.contains(ObjFlag::Read)
.then(|| v_bool(true))
.unwrap_or(v_bool(false)));
} else if pname == *W_SYM {
let flags = self.flags_of(obj)?;
return if flags.contains(ObjFlag::Write) {
Ok(v_int(1))
} else {
Ok(v_int(0))
};
return Ok(flags
.contains(ObjFlag::Write)
.then(|| v_bool(true))
.unwrap_or(v_bool(false)));
} else if pname == *F_SYM {
let flags = self.flags_of(obj)?;
return if flags.contains(ObjFlag::Fertile) {
Ok(v_int(1))
} else {
Ok(v_int(0))
};
return Ok(flags
.contains(ObjFlag::Fertile)
.then(|| v_bool(true))
.unwrap_or(v_bool(false)));
}

let (_, value, propperms, _) = self.tx.resolve_property(obj, pname)?;
Expand Down Expand Up @@ -712,15 +716,7 @@ impl WorldState for DbTxWorldState {

let (objflags, owner) = (self.flags_of(obj)?, self.owner_of(obj)?);

if new_parent != NOTHING {
let (parentflags, parentowner) =
(self.flags_of(new_parent)?, self.owner_of(new_parent)?);
self.perms(perms)?.check_object_allows(
parentowner,
parentflags,
BitEnum::new_with(ObjFlag::Write) | ObjFlag::Fertile,
)?;
}
self.check_parent(perms, new_parent)?;
self.perms(perms)?
.check_object_allows(owner, objflags, ObjFlag::Write.into())?;

Expand Down
2 changes: 1 addition & 1 deletion crates/kernel/src/builtins/bf_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn bf_ctime(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
let tz_str = get_timezone().unwrap();
let tz: Tz = tz_str.parse().unwrap();
let offset = tz.offset_from_local_date(&date_time.date_naive()).unwrap();
let abbreviation = offset.abbreviation().unwrap_or_else(|| "??");
let abbreviation = offset.abbreviation().unwrap_or("??");
let datetime_str = format!(
"{} {}",
date_time.format("%a %b %d %H:%M:%S %Y"),
Expand Down
6 changes: 3 additions & 3 deletions crates/kernel/src/tasks/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ impl Scheduler {
};
vloc
} else {
let vloc = match vloc {

match vloc {
ObjectRef::Id(id) => id,
_ => panic!("Unexpected object reference in vloc"),
};
vloc
}
};

let task_start = Arc::new(TaskStart::StartVerb {
Expand Down
2 changes: 1 addition & 1 deletion crates/moot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<R: MootRunner> MootState<R> {
) -> eyre::Result<()> {
match command_kind {
CommandKind::Eval => {
runner.eval(player, &format!("{command} \"moot-line:{line_no}\";"))
runner.eval(player, format!("{command} \"moot-line:{line_no}\";"))
}
CommandKind::Command => runner.command(player, command),
}?;
Expand Down
2 changes: 1 addition & 1 deletion crates/values/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub trait ValSet<V: AsByteBuffer>: FromIterator<V> {
}

pub fn uuid_fb(&uuid: &Uuid) -> values_flatbuffers::moor::values::Uuid {
values_flatbuffers::moor::values::Uuid::new(&uuid.as_bytes())
values_flatbuffers::moor::values::Uuid::new(uuid.as_bytes())
}

#[derive(Debug, Error, Clone, Decode, Encode, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/values/src/model/verbdef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Named for VerbDef {
self.get_flatbuffer()
.names()
.map(|names| names.iter().collect())
.unwrap_or_else(Vec::new)
.unwrap_or_default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/values/src/var/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ mod tests {

#[test]
fn test_float_pack_unpack() {
let f = Var::mk_float(42.0.into());
let f = Var::mk_float(42.0);

match f.variant() {
Variant::Float(f) => assert_eq!(f, 42.0),
Expand Down
2 changes: 1 addition & 1 deletion crates/web-host/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn var_as_json(v: &Var) -> serde_json::Value {
pub fn json_as_var(j: &serde_json::Value) -> Option<Var> {
match j {
serde_json::Value::Null => Some(v_none()),
serde_json::Value::String(s) => Some(v_str(&s)),
serde_json::Value::String(s) => Some(v_str(s)),
serde_json::Value::Number(n) => Some(if n.is_i64() {
v_int(n.as_i64().unwrap())
} else {
Expand Down

0 comments on commit c6cac1e

Please sign in to comment.