Skip to content

Commit

Permalink
Add internal error and use it for todos
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jun 28, 2023
1 parent aab67fd commit 14f7ee2
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 90 deletions.
2 changes: 2 additions & 0 deletions valuescript_common/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum BuiltinName {
Error,
TypeError,
RangeError,
InternalError,

Symbol,
SymbolIterator,
Expand All @@ -46,6 +47,7 @@ pub const BUILTIN_NAMES: [&str; BuiltinName::COUNT] = [
"Error",
"TypeError",
"RangeError",
"InternalError",
"Symbol",
"SymbolIterator",
"BigInt",
Expand Down
8 changes: 4 additions & 4 deletions valuescript_vm/src/bigint_methods.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use num_bigint::BigInt;

use crate::{
builtins::error_builtin::ToError,
builtins::internal_error_builtin::ToInternalError,
native_function::{native_fn, NativeFunction},
todo_fn::TODO,
vs_value::{ToVal, Val},
Expand All @@ -21,18 +21,18 @@ static TO_STRING: NativeFunction = native_fn(|this, params| {
Ok(match this.get() {
Val::BigInt(bigint) => match params.get(0) {
Some(_) => {
return Err("TODO: toString with radix".to_error());
return Err("TODO: toString with radix".to_internal_error());
}

None => bigint.clone().to_val().to_string().to_val(),
},
_ => return Err("TODO: bigint indirection".to_error()),
_ => return Err("TODO: bigint indirection".to_internal_error()),
})
});

static VALUE_OF: NativeFunction = native_fn(|this, _params| {
Ok(match this.get() {
Val::BigInt(bigint) => Val::BigInt(bigint.clone()),
_ => return Err("TODO: bigint indirection".to_error()),
_ => return Err("TODO: bigint indirection".to_internal_error()),
})
});
4 changes: 2 additions & 2 deletions valuescript_vm/src/builtins/bigint_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};

use super::builtin_object::BuiltinObject;
use super::error_builtin::ToError;
use super::internal_error_builtin::ToInternalError;
use super::range_error_builtin::ToRangeError;
use super::type_error_builtin::ToTypeError;

Expand Down Expand Up @@ -42,7 +42,7 @@ impl BuiltinObject for BigIntBuiltin {
}
}
Some(Val::Undefined) | None => Err("Can't convert undefined to BigInt".to_type_error()),
_ => Err("TODO: Other BigInt conversions".to_error()),
_ => Err("TODO: Other BigInt conversions".to_internal_error()),
}
})
}
Expand Down
109 changes: 109 additions & 0 deletions valuescript_vm/src/builtins/internal_error_builtin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::fmt;
use std::{collections::BTreeMap, rc::Rc};

use crate::native_function::{native_fn, ThisWrapper};
use crate::vs_value::ToVal;
use crate::ValTrait;
use crate::{
native_function::NativeFunction,
operations::op_submov,
vs_class::VsClass,
vs_object::VsObject,
vs_value::{LoadFunctionResult, Val},
};

use super::builtin_object::BuiltinObject;

pub struct InternalErrorBuiltin {}

impl BuiltinObject for InternalErrorBuiltin {
fn bo_name() -> &'static str {
"InternalError"
}

fn bo_sub(_key: &str) -> Val {
Val::Undefined
}

fn bo_load_function() -> LoadFunctionResult {
LoadFunctionResult::NativeFunction(|_: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
Ok(
match params.get(0) {
Some(param) => param.clone().to_val_string(),
None => "".to_val(),
}
.to_internal_error(),
)
})
}

fn bo_as_class_data() -> Option<Rc<VsClass>> {
Some(Rc::new(VsClass {
constructor: Val::Static(&SET_MESSAGE),
prototype: make_internal_error_prototype(),
static_: VsObject::default().to_val(),
}))
}
}

impl fmt::Display for InternalErrorBuiltin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "function InternalError() {{ [native code] }}")
}
}

// TODO: Static? (Rc -> Arc?)
fn make_internal_error_prototype() -> Val {
VsObject {
string_map: BTreeMap::from([
("name".to_string(), "InternalError".to_val()),
("toString".to_string(), INTERNAL_ERROR_TO_STRING.to_val()),
]),
symbol_map: Default::default(),
prototype: None,
}
.to_val()
}

static SET_MESSAGE: NativeFunction = native_fn(|mut this, params| {
let message = match params.get(0) {
Some(param) => param.to_string(),
None => "".to_string(),
};

op_submov(this.get_mut()?, &"message".to_val(), message.to_val())?;

Ok(Val::Undefined)
});

static INTERNAL_ERROR_TO_STRING: NativeFunction = native_fn(|this, _params| {
let message = this.get().sub(&"message".to_val())?;
Ok(format!("InternalError({})", message).to_val())
});

pub trait ToInternalError {
fn to_internal_error(self) -> Val;
}

impl ToInternalError for &str {
fn to_internal_error(self) -> Val {
self.to_string().to_internal_error()
}
}

impl ToInternalError for String {
fn to_internal_error(self) -> Val {
self.to_val().to_internal_error()
}
}

impl ToInternalError for Val {
fn to_internal_error(self) -> Val {
VsObject {
string_map: BTreeMap::from([("message".to_string(), self)]),
symbol_map: Default::default(),
prototype: Some(make_internal_error_prototype()),
}
.to_val()
}
}
5 changes: 4 additions & 1 deletion valuescript_vm/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod boolean_builtin;
mod builtin_object;
mod debug_builtin;
pub mod error_builtin;
pub mod internal_error_builtin;
mod math_builtin;
mod number_builtin;
pub mod range_error_builtin;
Expand All @@ -20,7 +21,8 @@ use crate::{

use self::{
array_builtin::ArrayBuiltin, bigint_builtin::BigIntBuiltin, boolean_builtin::BooleanBuiltin,
debug_builtin::DebugBuiltin, error_builtin::ErrorBuiltin, math_builtin::MathBuiltin,
debug_builtin::DebugBuiltin, error_builtin::ErrorBuiltin,
internal_error_builtin::InternalErrorBuiltin, math_builtin::MathBuiltin,
number_builtin::NumberBuiltin, range_error_builtin::RangeErrorBuiltin,
string_builtin::StringBuiltin, symbol_builtin::SymbolBuiltin,
type_error_builtin::TypeErrorBuiltin,
Expand All @@ -40,6 +42,7 @@ pub static BUILTIN_VALS: [fn() -> Val; BUILTIN_COUNT] = [
|| ErrorBuiltin {}.to_val(),
|| TypeErrorBuiltin {}.to_val(),
|| RangeErrorBuiltin {}.to_val(),
|| InternalErrorBuiltin {}.to_val(),
|| SymbolBuiltin {}.to_val(),
|| VsSymbol::ITERATOR.to_val(),
|| BigIntBuiltin {}.to_val(),
Expand Down
4 changes: 2 additions & 2 deletions valuescript_vm/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use num_bigint::BigInt;

use crate::{
builtins::{error_builtin::ToError, type_error_builtin::ToTypeError},
builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError},
iteration::{iteration_result::IterationResult, return_this::RETURN_THIS},
native_frame_function::NativeFrameFunction,
native_function::ThisWrapper,
Expand Down Expand Up @@ -132,7 +132,7 @@ impl StackFrameTrait for GeneratorFrame {
fn write_this(&mut self, const_: bool, this: Val) -> Result<(), Val> {
let mut dynamic = match this {
Val::Dynamic(dynamic) => dynamic,
_ => return Err("TODO: indirection".to_error()),
_ => return Err("TODO: indirection".to_internal_error()),
};

if const_ {
Expand Down
4 changes: 2 additions & 2 deletions valuescript_vm/src/iteration/array_entries_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt, rc::Rc};
use num_bigint::BigInt;

use crate::{
builtins::{error_builtin::ToError, type_error_builtin::ToTypeError},
builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError},
native_function::{native_fn, NativeFunction},
vs_array::VsArray,
vs_class::VsClass,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl fmt::Display for ArrayEntriesIterator {
static NEXT: NativeFunction = native_fn(|mut this, _| {
let dynamic = match this.get_mut()? {
Val::Dynamic(dynamic) => dynamic,
_ => return Err("TODO: indirection".to_error()),
_ => return Err("TODO: indirection".to_internal_error()),
};

let iter = dynamic_make_mut(dynamic)
Expand Down
4 changes: 2 additions & 2 deletions valuescript_vm/src/iteration/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt, rc::Rc};
use num_bigint::BigInt;

use crate::{
builtins::{error_builtin::ToError, type_error_builtin::ToTypeError},
builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError},
native_function::{native_fn, NativeFunction},
vs_array::VsArray,
vs_class::VsClass,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl fmt::Display for ArrayIterator {
static NEXT: NativeFunction = native_fn(|mut this, _| {
let dynamic = match this.get_mut()? {
Val::Dynamic(dynamic) => dynamic,
_ => return Err("TODO: indirection".to_error()),
_ => return Err("TODO: indirection".to_internal_error()),
};

let iter = dynamic_make_mut(dynamic)
Expand Down
4 changes: 2 additions & 2 deletions valuescript_vm/src/iteration/string_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt, rc::Rc};
use num_bigint::BigInt;

use crate::{
builtins::{error_builtin::ToError, type_error_builtin::ToTypeError},
builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError},
native_function::{native_fn, NativeFunction},
vs_array::VsArray,
vs_class::VsClass,
Expand Down Expand Up @@ -149,7 +149,7 @@ impl fmt::Display for StringIterator {
static NEXT: NativeFunction = native_fn(|mut this, _| {
let dynamic = match this.get_mut()? {
Val::Dynamic(dynamic) => dynamic,
_ => return Err("TODO: indirection".to_error()),
_ => return Err("TODO: indirection".to_internal_error()),
};

let iter = dynamic_make_mut(dynamic)
Expand Down
4 changes: 2 additions & 2 deletions valuescript_vm/src/native_frame_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Rc;

use num_bigint::BigInt;

use crate::builtins::error_builtin::ToError;
use crate::builtins::internal_error_builtin::ToInternalError;
use crate::builtins::type_error_builtin::ToTypeError;
use crate::stack_frame::StackFrame;
use crate::vs_array::VsArray;
Expand Down Expand Up @@ -60,7 +60,7 @@ impl ValTrait for NativeFrameFunction {
}

fn sub(&self, _key: &Val) -> Result<Val, Val> {
Err("TODO: Subscript native function".to_error())
Err("TODO: Subscript native function".to_internal_error())
}

fn submov(&mut self, _key: &Val, _value: Val) -> Result<(), Val> {
Expand Down
4 changes: 2 additions & 2 deletions valuescript_vm/src/native_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Rc;

use num_bigint::BigInt;

use crate::builtins::error_builtin::ToError;
use crate::builtins::internal_error_builtin::ToInternalError;
use crate::builtins::type_error_builtin::ToTypeError;
use crate::vs_array::VsArray;
use crate::vs_class::VsClass;
Expand Down Expand Up @@ -81,7 +81,7 @@ impl ValTrait for NativeFunction {
}

fn sub(&self, _key: &Val) -> Result<Val, Val> {
Err("TODO: Subscript native function".to_error())
Err("TODO: Subscript native function".to_internal_error())
}

fn submov(&mut self, _key: &Val, _value: Val) -> Result<(), Val> {
Expand Down
14 changes: 7 additions & 7 deletions valuescript_vm/src/number_methods.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::builtins::error_builtin::ToError;
use crate::builtins::internal_error_builtin::ToInternalError;
use crate::builtins::range_error_builtin::ToRangeError;
use crate::native_function::native_fn;
use crate::vs_value::ToVal;
Expand Down Expand Up @@ -67,32 +67,32 @@ static TO_EXPONENTIAL: NativeFunction = native_fn(|this, params| {
}
None => format_exponential(*number, None),
},
_ => return Err("number indirection".to_error()),
_ => return Err("number indirection".to_internal_error()),
})
});

static TODO_LOCALE: NativeFunction = native_fn(|this, _params| match this.get() {
Val::Number(_number) => return Err("TODO: locale".to_error()),
_ => return Err("number indirection".to_error()),
Val::Number(_number) => return Err("TODO: locale".to_internal_error()),
_ => return Err("number indirection".to_internal_error()),
});

static TO_STRING: NativeFunction = native_fn(|this, params| {
Ok(match this.get() {
Val::Number(number) => match params.get(0) {
Some(_) => {
return Err("TODO: toString with radix".to_error());
return Err("TODO: toString with radix".to_internal_error());
}

None => number.to_val().to_string().to_val(),
},
_ => return Err("number indirection".to_error()),
_ => return Err("number indirection".to_internal_error()),
})
});

static VALUE_OF: NativeFunction = native_fn(|this, _params| {
Ok(match this.get() {
Val::Number(number) => Val::Number(*number),
_ => return Err("number indirection".to_error()),
_ => return Err("number indirection".to_internal_error()),
})
});

Expand Down
Loading

0 comments on commit 14f7ee2

Please sign in to comment.