diff --git a/inputs/passing/stringMethods/repeat.ts b/inputs/passing/stringMethods/repeat.ts new file mode 100644 index 0000000..7e38c76 --- /dev/null +++ b/inputs/passing/stringMethods/repeat.ts @@ -0,0 +1,5 @@ +// test_output! "foofoofoo" + +export default function () { + return "foo".repeat(3); +} diff --git a/valuescript_vm/src/string_builtin.rs b/valuescript_vm/src/string_builtin.rs index c586ca8..6b9931f 100644 --- a/valuescript_vm/src/string_builtin.rs +++ b/valuescript_vm/src/string_builtin.rs @@ -62,6 +62,7 @@ impl ValTrait for StringBuiltin { match key.val_to_string().as_str() { "fromCodePoint" => Val::Static(&FROM_CODE_POINT), // "fromCharCode" => Val::Static(&FROM_CHAR_CODE), + // "raw" => Val::Static(&RAW), // TODO _ => Val::Undefined, } } diff --git a/valuescript_vm/src/string_methods.rs b/valuescript_vm/src/string_methods.rs index 9fb0523..8cd7855 100644 --- a/valuescript_vm/src/string_methods.rs +++ b/valuescript_vm/src/string_methods.rs @@ -56,6 +56,7 @@ pub fn get_string_method(method: &str) -> Val { "normalize" => Val::Static(&NORMALIZE), // (TODO) "padEnd" => Val::Static(&PAD_END), "padStart" => Val::Static(&PAD_START), + "repeat" => Val::Static(&REPEAT), _ => Val::Undefined, } } @@ -405,6 +406,31 @@ static PAD_START: NativeFunction = NativeFunction { }, }; +static REPEAT: NativeFunction = NativeFunction { + fn_: |this: &mut Val, params: Vec| -> Val { + match this { + Val::String(string_data) => { + let count = match params.get(0) { + Some(p) => match p.to_index() { + Some(i) => i, + None => return Val::String(string_data.clone()), + }, + _ => return Val::String(string_data.clone()), + }; + + let mut result = String::new(); + + for _ in 0..count { + result.push_str(string_data); + } + + Val::String(Rc::new(result)) + } + _ => panic!("TODO: exceptions/string indirection"), + } + }, +}; + fn index_of(string_bytes: &[u8], search_bytes: &[u8], start_pos: usize) -> Option { let search_length = search_bytes.len();