-
-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy pathmod.rs
More file actions
37 lines (33 loc) · 1.21 KB
/
mod.rs
File metadata and controls
37 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use super::RegisterOperand;
use crate::{Context, JsResult, JsString, vm::opcode::Operation};
use thin_vec::ThinVec;
/// `ConcatToString` implements the Opcode Operation for `Opcode::ConcatToString`
///
/// Operation:
/// - Concat multiple stack objects into a string.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ConcatToString;
impl ConcatToString {
#[inline(always)]
pub(super) fn operation(
(string, values): (RegisterOperand, ThinVec<RegisterOperand>),
context: &mut Context,
) -> JsResult<()> {
let mut strings = Vec::with_capacity(values.len());
for value in values {
let val = context.vm.get_register(value.into()).clone();
strings.push(val.to_string(context)?);
}
let s = JsString::concat_array(&strings.iter().map(JsString::as_str).collect::<Vec<_>>())
.map_err(|_| {
crate::error::JsNativeError::range().with_message("Invalid string length")
})?;
context.vm.set_register(string.into(), s.into());
Ok(())
}
}
impl Operation for ConcatToString {
const NAME: &'static str = "ConcatToString";
const INSTRUCTION: &'static str = "INST - ConcatToString";
const COST: u8 = 6;
}