mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
p20
This commit is contained in:
59
valuescript_vm/src/builtins/bigint_builtin.rs
Normal file
59
valuescript_vm/src/builtins/bigint_builtin.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
|
||||
use crate::native_function::ThisWrapper;
|
||||
use crate::{
|
||||
vs_class::VsClass,
|
||||
vs_value::{LoadFunctionResult, Val},
|
||||
};
|
||||
|
||||
use super::builtin_object::BuiltinObject;
|
||||
use super::error_builtin::ToError;
|
||||
use super::range_error_builtin::ToRangeError;
|
||||
use super::type_error_builtin::ToTypeError;
|
||||
|
||||
pub struct BigIntBuiltin {}
|
||||
|
||||
impl BuiltinObject for BigIntBuiltin {
|
||||
fn bo_name() -> &'static str {
|
||||
"BigInt"
|
||||
}
|
||||
|
||||
fn bo_sub(_key: &str) -> Val {
|
||||
Val::Undefined
|
||||
}
|
||||
|
||||
fn bo_load_function() -> LoadFunctionResult {
|
||||
LoadFunctionResult::NativeFunction(|_: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
match params.get(0) {
|
||||
Some(Val::Number(value)) => {
|
||||
if *value != f64::floor(*value) {
|
||||
Err(
|
||||
format!(
|
||||
"{} can't be converted to BigInt because it isn't an integer",
|
||||
value
|
||||
)
|
||||
.to_range_error(),
|
||||
)
|
||||
} else {
|
||||
Ok(Val::BigInt(BigInt::from(*value as u64)))
|
||||
}
|
||||
}
|
||||
Some(Val::Undefined) | None => Err("Can't convert undefined to BigInt".to_type_error()),
|
||||
_ => Err("TODO: Other BigInt conversions".to_error()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn bo_as_class_data() -> Option<Rc<VsClass>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for BigIntBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function BigInt() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
mod array_builtin;
|
||||
mod bigint_builtin;
|
||||
mod boolean_builtin;
|
||||
mod builtin_object;
|
||||
mod debug_builtin;
|
||||
@@ -18,10 +19,11 @@ use crate::{
|
||||
};
|
||||
|
||||
use self::{
|
||||
array_builtin::ArrayBuiltin, boolean_builtin::BooleanBuiltin, debug_builtin::DebugBuiltin,
|
||||
error_builtin::ErrorBuiltin, math_builtin::MathBuiltin, number_builtin::NumberBuiltin,
|
||||
range_error_builtin::RangeErrorBuiltin, string_builtin::StringBuiltin,
|
||||
symbol_builtin::SymbolBuiltin, type_error_builtin::TypeErrorBuiltin,
|
||||
array_builtin::ArrayBuiltin, bigint_builtin::BigIntBuiltin, boolean_builtin::BooleanBuiltin,
|
||||
debug_builtin::DebugBuiltin, error_builtin::ErrorBuiltin, math_builtin::MathBuiltin,
|
||||
number_builtin::NumberBuiltin, range_error_builtin::RangeErrorBuiltin,
|
||||
string_builtin::StringBuiltin, symbol_builtin::SymbolBuiltin,
|
||||
type_error_builtin::TypeErrorBuiltin,
|
||||
};
|
||||
|
||||
pub static BUILTIN_VALS: [fn() -> Val; BUILTIN_COUNT] = [
|
||||
@@ -40,4 +42,5 @@ pub static BUILTIN_VALS: [fn() -> Val; BUILTIN_COUNT] = [
|
||||
|| RangeErrorBuiltin {}.to_val(),
|
||||
|| SymbolBuiltin {}.to_val(),
|
||||
|| VsSymbol::ITERATOR.to_val(),
|
||||
|| BigIntBuiltin {}.to_val(),
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user