| 218 | } |
| 219 | |
| 220 | fn concat<'js>(ctx: Ctx<'js>, list: Array<'js>, max_length: Opt<usize>) -> Result<Value<'js>> { |
| 221 | let mut bytes = Vec::new(); |
| 222 | let mut total_length = 0; |
| 223 | let mut length; |
| 224 | for value in list.iter::<Object>() { |
| 225 | let typed_array = TypedArray::<u8>::from_object(value?)?; |
| 226 | let bytes_ref: &[u8] = typed_array.as_ref(); |
| 227 | |
| 228 | length = bytes_ref.len(); |
| 229 | |
| 230 | if length == 0 { |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | if let Some(max_length) = max_length.0 { |
| 235 | total_length += length; |
| 236 | if total_length > max_length { |
| 237 | let diff = max_length - (total_length - length); |
| 238 | bytes.extend_from_slice(&bytes_ref[0..diff]); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | bytes.extend_from_slice(bytes_ref); |
| 243 | } |
| 244 | |
| 245 | Buffer(bytes).into_js(&ctx) |
| 246 | } |
| 247 | |
| 248 | fn from<'js>( |
| 249 | ctx: Ctx<'js>, |