Skip to content

Commit

Permalink
SimpleValue refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Oct 24, 2023
1 parent 8b8f233 commit d84b16a
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 305 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn run_proj_tty<C: CustomTypes<StdSystem<C>>>(project_name: &str, server: String
}
RawKeyCode::Backspace => if in_input_mode() && input_value.pop().is_some() { update_flag.set(true) }
RawKeyCode::Enter => if let Some((_, res_key)) = input_queries.borrow_mut().pop_front() {
res_key.complete(Ok(C::Intermediate::from_json(Json::String(mem::take(&mut input_value)))));
res_key.complete(Ok(SimpleValue::String(mem::take(&mut input_value)).into()));
update_flag.set(true);
}
RawKeyCode::Up => if !in_input_mode() { input_sequence.push(Input::KeyDown { key: KeyCode::Up }) }
Expand Down
29 changes: 9 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use std::io::{BufRead, Write, BufReader, BufWriter};

use netsblox_vm::cli::{run, Mode};
use netsblox_vm::template::SyscallMenu;
use netsblox_vm::runtime::{GetType, Value, Type, ErrorCause, EntityKind, Request, RequestStatus, Config, CustomTypes, IntermediateType, Key};
use netsblox_vm::runtime::{GetType, Value, Type, ErrorCause, EntityKind, Request, RequestStatus, Config, CustomTypes, Key, SimpleValue};
use netsblox_vm::std_system::StdSystem;
use netsblox_vm::gc::Mutation;
use netsblox_vm::json::{Json, json};
use clap::Parser;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -49,20 +48,12 @@ impl From<EntityKind<'_, '_, C, StdSystem<C>>> for EntityState {
}

enum Intermediate {
Json(Json),
Image(Vec<u8>),
Audio(Vec<u8>),
Simple(SimpleValue),
Native(NativeValue),
}
impl IntermediateType for Intermediate {
fn from_json(json: Json) -> Self {
Self::Json(json)
}
fn from_image(img: Vec<u8>) -> Self {
Self::Image(img)
}
fn from_audio(audio: Vec<u8>) -> Self {
Self::Audio(audio)
impl From<SimpleValue> for Intermediate {
fn from(value: SimpleValue) -> Self {
Intermediate::Simple(value)
}
}

Expand All @@ -75,9 +66,7 @@ impl CustomTypes<StdSystem<C>> for C {

fn from_intermediate<'gc>(mc: &Mutation<'gc>, value: Self::Intermediate) -> Result<Value<'gc, C, StdSystem<C>>, ErrorCause<C, StdSystem<C>>> {
Ok(match value {
Intermediate::Json(x) => Value::from_json(mc, x)?,
Intermediate::Image(x) => Value::Image(Rc::new(x)),
Intermediate::Audio(x) => Value::Audio(Rc::new(x)),
Intermediate::Simple(x) => Value::from_simple(mc, x),
Intermediate::Native(x) => Value::Native(Rc::new(x)),
})
}
Expand Down Expand Up @@ -153,7 +142,7 @@ fn main() {
NativeValue::InputFile { handle } => *handle.borrow_mut() = None,
NativeValue::OutputFile { handle } => *handle.borrow_mut() = None,
}
key.complete(Ok(Intermediate::from_json(json!("OK"))));
key.complete(Ok(SimpleValue::String("OK".into()).into()));
RequestStatus::Handled
}
_ => {
Expand All @@ -177,7 +166,7 @@ fn main() {
return RequestStatus::Handled;
}

key.complete(Ok(Intermediate::from_json(json!(res))));
key.complete(Ok(SimpleValue::String(res).into()));
RequestStatus::Handled
}
None => {
Expand Down Expand Up @@ -206,7 +195,7 @@ fn main() {
NativeValue::OutputFile { handle } => match handle.borrow_mut().as_mut() {
Some(handle) => match writeln!(*handle, "{content}") {
Ok(_) => {
key.complete(Ok(Intermediate::Json(json!("OK"))));
key.complete(Ok(SimpleValue::String("OK".into()).into()));
RequestStatus::Handled
}
Err(e) => {
Expand Down
10 changes: 5 additions & 5 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Process<'gc, C, S> {
Some(Defer::MessageReply { key, aft_pos }) => match global_context.system.poll_reply(key) {
AsyncResult::Completed(x) => {
let value = match x {
Some(x) => Value::from_json(mc, x)?,
Some(x) => Value::from_simple(mc, SimpleValue::from_json(x)?),
None => empty_string().into(),
};
self.value_stack.push(value);
Expand Down Expand Up @@ -652,7 +652,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Process<'gc, C, S> {
}

Instruction::ListJson => {
let value = self.value_stack.pop().unwrap().to_json()?;
let value = self.value_stack.pop().unwrap().to_simple()?.into_json()?;
self.value_stack.push(Rc::new(value.to_string()).into());
self.pos = aft_pos;
}
Expand Down Expand Up @@ -1159,7 +1159,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Process<'gc, C, S> {
let values = {
let field_names = tokens.map(ToOwned::to_owned).collect::<Vec<_>>();
let field_count = field_names.len();
iter::zip(field_names.into_iter(), self.value_stack.drain(self.value_stack.len() - field_count..)).map(|(k, v)| v.to_json().map(|x| (k, x))).collect::<Result<_,_>>()?
iter::zip(field_names.into_iter(), self.value_stack.drain(self.value_stack.len() - field_count..)).map(|(k, v)| Ok((k, v.to_simple()?.into_json()?))).collect::<Result<_,ErrorCause<C, S>>>()?
};

match global_context.system.send_message(msg_type.into(), values, targets, expect_reply)? {
Expand All @@ -1168,7 +1168,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Process<'gc, C, S> {
}
}
Instruction::SendNetworkReply => {
let value = self.value_stack.pop().unwrap().to_json()?;
let value = self.value_stack.pop().unwrap().to_simple()?.into_json()?;
if let Some(key) = self.reply_key.take() {
global_context.system.send_reply(key, value)?;
}
Expand Down Expand Up @@ -1700,7 +1700,7 @@ mod ops {
UnaryOp::SplitJson => unary_op_impl(mc, system, x, &mut cache, &|mc, _, x| {
let value = x.as_string()?;
match parse_json::<Json>(&value) {
Ok(json) => Ok(Value::from_json(mc, json)?),
Ok(json) => Ok(Value::from_simple(mc, SimpleValue::from_json(json)?)),
Err(_) => Err(ErrorCause::NotJson { value: value.into_owned() }),
}
}),
Expand Down
4 changes: 2 additions & 2 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Project<'gc, C, S> {
let mut locals = SymbolTable::default();
for field in fields.iter() {
locals.define_or_redefine(field,
args.get(field).and_then(|x| Value::from_json(mc, x.clone()).ok())
args.get(field).and_then(|x| SimpleValue::from_json(x.clone()).ok().map(|x| Value::from_simple(mc, x)))
.unwrap_or_else(|| Number::new(0.0).unwrap().into()).into());
}

Expand Down Expand Up @@ -280,7 +280,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Project<'gc, C, S> {
let mut locals = SymbolTable::default();
for field in fields.iter() {
locals.define_or_redefine(field,
values.get(field).and_then(|x| Value::from_json(mc, x.clone()).ok())
values.get(field).and_then(|x| SimpleValue::from_json(x.clone()).ok().map(|x| Value::from_simple(mc, x)))
.unwrap_or_else(|| Number::new(0.0).unwrap().into()).into());
}

Expand Down
Loading

0 comments on commit d84b16a

Please sign in to comment.