From 22eb55a91572cc040358e2eae89aadde4fd3fe3a Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Mon, 13 Mar 2023 13:50:27 +1100 Subject: [PATCH] concat (string) --- valuescript_vm/src/string_methods.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/valuescript_vm/src/string_methods.rs b/valuescript_vm/src/string_methods.rs index bbdbdcd..ec545cb 100644 --- a/valuescript_vm/src/string_methods.rs +++ b/valuescript_vm/src/string_methods.rs @@ -47,6 +47,7 @@ pub fn get_string_method(method: &str) -> Val { // "charCodeAt" => Val::Static(&CHAR_CODE_AT), // "codePointAt" => Val::Static(&CODE_POINT_AT), + "concat" => Val::Static(&CONCAT), _ => Val::Undefined, } } @@ -96,6 +97,23 @@ static CODE_POINT_AT: NativeFunction = NativeFunction { }, }; +static CONCAT: NativeFunction = NativeFunction { + fn_: |this: &mut Val, params: Vec| -> Val { + match this { + Val::String(string_data) => { + let mut result = string_data.as_str().to_string(); + + for param in params { + result.push_str(param.val_to_string().as_str()); + } + + Val::String(Rc::new(result)) + } + _ => std::panic!("Not implemented: exceptions/string indirection"), + } + }, +}; + fn unicode_at(bytes: &[u8], index: usize) -> Option { match code_point_at(bytes, index) { Some(code_point) => Some(