diff --git a/valuescript_common/src/builtins.rs b/valuescript_common/src/builtins.rs index c886e9a..98579a0 100644 --- a/valuescript_common/src/builtins.rs +++ b/valuescript_common/src/builtins.rs @@ -31,6 +31,9 @@ pub enum BuiltinName { SymbolIterator, BigInt, + + #[allow(non_camel_case_types)] + console, } pub const BUILTIN_NAMES: [&str; BuiltinName::COUNT] = [ @@ -51,6 +54,7 @@ pub const BUILTIN_NAMES: [&str; BuiltinName::COUNT] = [ "Symbol", "SymbolIterator", "BigInt", + "console", ]; pub const BUILTIN_COUNT: usize = BuiltinName::COUNT; diff --git a/valuescript_vm/src/builtins/console_builtin.rs b/valuescript_vm/src/builtins/console_builtin.rs new file mode 100644 index 0000000..4155b83 --- /dev/null +++ b/valuescript_vm/src/builtins/console_builtin.rs @@ -0,0 +1,52 @@ +use std::fmt; +use std::rc::Rc; + +use crate::native_function::{native_fn, NativeFunction}; +use crate::vs_class::VsClass; +use crate::vs_value::{LoadFunctionResult, ToVal, Val}; + +use super::builtin_object::BuiltinObject; + +pub struct ConsoleBuiltin {} + +impl BuiltinObject for ConsoleBuiltin { + fn bo_name() -> &'static str { + "console" + } + + fn bo_sub(key: &str) -> Val { + match key { + "log" => LOG.to_val(), + + _ => Val::Undefined, + } + } + + fn bo_load_function() -> LoadFunctionResult { + LoadFunctionResult::NotAFunction + } + + fn bo_as_class_data() -> Option> { + None + } +} + +impl fmt::Display for ConsoleBuiltin { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "[object console]") + } +} + +static LOG: NativeFunction = native_fn(|_this, params| { + for (i, p) in params.iter().enumerate() { + if i > 0 { + print!(" "); + } + + print!("{}", p); + } + + println!(); + + Ok(Val::Undefined) +}); diff --git a/valuescript_vm/src/builtins/mod.rs b/valuescript_vm/src/builtins/mod.rs index 0d3626d..55b5cc5 100644 --- a/valuescript_vm/src/builtins/mod.rs +++ b/valuescript_vm/src/builtins/mod.rs @@ -2,6 +2,7 @@ mod array_builtin; mod bigint_builtin; mod boolean_builtin; mod builtin_object; +mod console_builtin; mod debug_builtin; pub mod error_builtin; pub mod internal_error_builtin; @@ -21,7 +22,7 @@ use crate::{ use self::{ array_builtin::ArrayBuiltin, bigint_builtin::BigIntBuiltin, boolean_builtin::BooleanBuiltin, - debug_builtin::DebugBuiltin, error_builtin::ErrorBuiltin, + console_builtin::ConsoleBuiltin, 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, @@ -46,4 +47,5 @@ pub static BUILTIN_VALS: [fn() -> Val; BUILTIN_COUNT] = [ || SymbolBuiltin {}.to_val(), || VsSymbol::ITERATOR.to_val(), || BigIntBuiltin {}.to_val(), + || ConsoleBuiltin {}.to_val(), ]; diff --git a/vstc/src/run_command.rs b/vstc/src/run_command.rs index 72ff96b..acfdddb 100644 --- a/vstc/src/run_command.rs +++ b/vstc/src/run_command.rs @@ -46,6 +46,7 @@ pub fn run_command(args: &Vec) { .collect(); match vm.run(bytecode, None, &val_args) { + Ok(Val::Undefined) => {}, Ok(result) => { println!("{}", result.pretty()); }