add settypeid

This commit is contained in:
Daniella / Tove 2024-10-13 23:56:57 +02:00
parent eb1335ade4
commit fccce8b705
Signed by: TudbuT
GPG key ID: B3CF345217F202D3

View file

@ -3,7 +3,6 @@ use std::{
env::{args, vars}, env::{args, vars},
fs, fs,
io::{stdin, stdout, Write}, io::{stdin, stdout, Write},
mem,
ops::{Add, Div, Mul, Rem, Sub}, ops::{Add, Div, Mul, Rem, Sub},
process::{self, Stdio}, process::{self, Stdio},
sync::Arc, sync::Arc,
@ -96,12 +95,27 @@ pub fn settype(stack: &mut Stack) -> OError {
let o = stack.pop(); let o = stack.pop();
let kind = runtime(|rt| rt.get_type_by_name(&s)) let kind = runtime(|rt| rt.get_type_by_name(&s))
.ok_or_else(|| stack.error(ErrorKind::TypeNotFound(s)))?; .ok_or_else(|| stack.error(ErrorKind::TypeNotFound(s)))?;
set_type_internal(&o, kind);
stack.push(o);
Ok(())
}
pub fn settypeid(stack: &mut Stack) -> OError {
let Value::Int(i) = stack.pop().lock_ro().native.clone() else {
return stack.err(ErrorKind::InvalidCall("settype".to_owned()));
};
let o = stack.pop();
let kind = runtime(|rt| rt.get_type_by_id(i as u32))
.ok_or_else(|| stack.error(ErrorKind::TypeNotFound(format!(";{i}"))))?;
set_type_internal(&o, kind);
stack.push(o);
Ok(())
}
fn set_type_internal(o: &Arc<Mut<Object>>, kind: Arc<Mut<Type>>) {
let mut obj = o.lock(); let mut obj = o.lock();
kind.lock_ro().write_into(&mut obj); kind.lock_ro().write_into(&mut obj);
obj.kind = kind; obj.kind = kind;
mem::drop(obj);
stack.push(o);
Ok(())
} }
pub fn gettype(stack: &mut Stack) -> OError { pub fn gettype(stack: &mut Stack) -> OError {
@ -1010,12 +1024,12 @@ pub fn properties(stack: &mut Stack) -> OError {
let o = stack.pop(); let o = stack.pop();
let o = o.lock_ro(); let o = o.lock_ro();
let additional: Vec<AMObject> = vec![ let additional: Vec<AMObject> = vec![
Value::Array(vec![";".to_owned().spl(), o.native.clone().spl()]).spl(),
Value::Array(vec![ Value::Array(vec![
":".to_owned().spl(), ":".to_owned().spl(),
o.kind.lock_ro().get_name().spl(), o.kind.lock_ro().get_name().spl(),
]) ])
.spl(), .spl(),
Value::Array(vec![";".to_owned().spl(), o.native.clone().spl()]).spl(),
]; ];
stack.push( stack.push(
Value::Array( Value::Array(
@ -1072,7 +1086,7 @@ pub fn from_properties(stack: &mut Stack) -> OError {
pub fn register(r: &mut Stack, o: Arc<Frame>) { pub fn register(r: &mut Stack, o: Arc<Frame>) {
type Fn = fn(&mut Stack) -> OError; type Fn = fn(&mut Stack) -> OError;
let fns: [(&str, Fn, u32); 64] = [ let fns: [(&str, Fn, u32); 65] = [
("pop", pop, 0), ("pop", pop, 0),
("dup", dup, 2), ("dup", dup, 2),
("dup2", dup2, 3), ("dup2", dup2, 3),
@ -1083,6 +1097,7 @@ pub fn register(r: &mut Stack, o: Arc<Frame>) {
("gettype", gettype, 1), ("gettype", gettype, 1),
("gettypeid", gettypeid, 1), ("gettypeid", gettypeid, 1),
("settype", settype, 1), ("settype", settype, 1),
("settypeid", settypeid, 1),
("banew", barray_new, 1), ("banew", barray_new, 1),
("anew", array_new, 1), ("anew", array_new, 1),
("array-len", array_len, 1), ("array-len", array_len, 1),