mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Add internal error and use it for todos
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
109
valuescript_vm/src/builtins/internal_error_builtin.rs
Normal file
109
valuescript_vm/src/builtins/internal_error_builtin.rs
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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,
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user