1use crate::host::{with_host, JsObj};
13use fusevm::Value;
14use indexmap::IndexMap;
15
16pub const METHODS: &[&str] = &[
17 "parse",
18 "stringify",
19 "escape",
20 "unescape",
21 "encode",
22 "decode",
23 "unescapeBuffer",
24];
25
26pub fn call(method: &str, args: &[Value]) -> Option<Result<Value, String>> {
27 let arg = |i: usize| args.get(i).cloned().unwrap_or(Value::Undef);
28 Some(match method {
29 "parse" | "decode" => parse(&arg(0), &arg(1), &arg(2), &arg(3)),
30 "stringify" | "encode" => stringify(&arg(0), &arg(1), &arg(2), &arg(3)),
31 "escape" => escape_value(&arg(0)).map(|s| with_host(|h| h.new_str(s))),
32 "unescape" => {
33 let decode_spaces = with_host(|h| h.truthy(&arg(1)));
34 unescape(&arg(0), decode_spaces).map(|s| with_host(|h| h.new_str(s)))
35 }
36 "unescapeBuffer" => {
37 let s = js_string(&arg(0));
38 let decode_spaces = with_host(|h| h.truthy(&arg(1)));
39 Ok(super::buffer::from_bytes(&unescape_buffer(
40 &s,
41 decode_spaces,
42 )))
43 }
44 _ => return None,
45 })
46}
47
48fn str_of(v: &Value) -> String {
49 with_host(|h| h.str_of(v))
50}
51
52fn new_str(s: String) -> Value {
53 with_host(|h| h.new_str(s))
54}
55
56fn js_string(v: &Value) -> String {
58 match crate::host::to_string_value(v) {
59 Ok(s) => str_of(&s),
60 Err(_) => str_of(v),
61 }
62}
63
64fn utf16(s: &str) -> Vec<u16> {
65 s.encode_utf16().collect()
66}
67
68fn from_utf16(u: &[u16]) -> String {
69 String::from_utf16_lossy(u)
70}
71
72fn option_fn(options: &Value, name: &str) -> Result<Option<Value>, String> {
74 if !with_host(|h| h.truthy(options)) {
75 return Ok(None);
76 }
77 let f = crate::builtins::get_property(options, name)?;
78 Ok(with_host(|h| crate::host::is_callable(h, &f)).then_some(f))
79}
80
81fn escape_value(v: &Value) -> Result<String, String> {
87 let s = crate::host::to_string_value(v)?;
88 Ok(escape(&str_of(&s)))
89}
90
91fn escape(s: &str) -> String {
92 const UNRESERVED: &[u8] =
93 b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~*'()";
94 let mut out = String::with_capacity(s.len());
95 for &b in s.as_bytes() {
96 if UNRESERVED.contains(&b) {
97 out.push(b as char);
98 } else {
99 out.push_str(&format!("%{b:02X}"));
100 }
101 }
102 out
103}
104
105fn unescape(v: &Value, decode_spaces: bool) -> Result<String, String> {
108 match crate::builtins::call_builtin_function("decodeURIComponent", vec![v.clone()]) {
109 Ok(d) => Ok(str_of(&d)),
110 Err(e) if e.starts_with("URIError") => {
111 with_host(|h| h.exc = None);
112 let bytes = unescape_buffer(&js_string(v), decode_spaces);
113 Ok(String::from_utf8_lossy(&bytes).into_owned())
114 }
115 Err(e) => Err(e),
116 }
117}
118
119fn unescape_buffer(s: &str, decode_spaces: bool) -> Vec<u8> {
124 let u = utf16(s);
125 let hex = |c: u16| char::from_u32(c as u32).and_then(|c| c.to_digit(16));
126 let mut out = Vec::with_capacity(u.len());
127 let mut i = 0;
128 while i < u.len() {
129 let c = u[i];
130 if c == u16::from(b'+') && decode_spaces {
131 out.push(b' ');
132 i += 1;
133 continue;
134 }
135 if c == u16::from(b'%') && i + 2 < u.len() {
136 if let (Some(hi), Some(lo)) = (hex(u[i + 1]), hex(u[i + 2])) {
137 out.push((hi * 16 + lo) as u8);
138 i += 3;
139 continue;
140 }
141 }
142 out.push(c as u8);
143 i += 1;
144 }
145 out
146}
147
148fn add_key_val(
153 obj: &mut IndexMap<String, Value>,
154 key: &[u16],
155 value: &[u16],
156 encoded: (bool, bool),
157 decode: &Option<Value>,
158) -> Result<(), String> {
159 let decode_part = |part: &[u16], enc: bool| -> Result<Value, String> {
160 let s = new_str(from_utf16(part));
161 if part.is_empty() || !enc {
162 return Ok(s);
163 }
164 let decoded = match decode {
165 Some(f) => crate::host::invoke(f, vec![s.clone()], None),
166 None => unescape(&s, false).map(new_str),
167 };
168 decoded.or_else(|_| {
171 with_host(|h| h.exc = None);
172 unescape(&s, true).map(new_str)
173 })
174 };
175 let k = decode_part(key, encoded.0)?;
176 let k = str_of(&crate::host::to_string_value(&k)?);
177 let v = decode_part(value, encoded.1)?;
178 match obj.get(&k).cloned() {
179 None => {
180 obj.insert(k, v);
181 }
182 Some(cur) => {
183 let pushed = with_host(|h| match h.get_mut(&cur) {
184 Some(JsObj::Array(items)) => {
185 items.push(v.clone());
186 true
187 }
188 _ => false,
189 });
190 if !pushed {
191 let arr = with_host(|h| h.new_array(vec![cur, v]));
192 obj.insert(k, arr);
193 }
194 }
195 }
196 Ok(())
197}
198
199fn parse(qs: &Value, sep: &Value, eq: &Value, options: &Value) -> Result<Value, String> {
204 let mut obj: IndexMap<String, Value> = IndexMap::new();
205 if let Some(qs) = with_host(|h| h.as_str(qs)).filter(|s| !s.is_empty()) {
206 parse_into(&mut obj, &utf16(&qs), sep, eq, options)?;
207 }
208 Ok(with_host(|h| {
211 let o = h.new_object(obj);
212 let null = h.null();
213 h.set_proto(&o, null);
214 o
215 }))
216}
217
218fn parse_into(
219 obj: &mut IndexMap<String, Value>,
220 qs: &[u16],
221 sep: &Value,
222 eq: &Value,
223 options: &Value,
224) -> Result<(), String> {
225 let codes = |v: &Value, default: &str| {
226 if with_host(|h| h.truthy(v)) {
227 utf16(&js_string(v))
228 } else {
229 utf16(default)
230 }
231 };
232 let (sep_codes, eq_codes) = (codes(sep, "&"), codes(eq, "="));
233 let (sep_len, eq_len) = (sep_codes.len(), eq_codes.len());
234 let mut pairs = 1000.0;
235 if with_host(|h| h.truthy(options)) {
236 let max = crate::builtins::get_property(options, "maxKeys")?;
237 if with_host(|h| h.type_of(&max)) == "number" {
238 let n = with_host(|h| h.to_number(&max));
239 pairs = if n > 0.0 { n } else { -1.0 };
240 }
241 }
242 let decode = option_fn(options, "decodeURIComponent")?;
243 let custom = decode.is_some();
244 let plus: Vec<u16> = utf16(if custom { "%20" } else { " " });
245 let is_hex = |c: u16| (c as u32) < 128 && (c as u8 as char).is_ascii_hexdigit();
246 let (mut last_pos, mut sep_idx, mut eq_idx) = (0usize, 0usize, 0usize);
247 let (mut key, mut value): (Vec<u16>, Vec<u16>) = (Vec::new(), Vec::new());
248 let (mut key_enc, mut val_enc) = (custom, custom);
249 let mut encode_check = 0;
250 let mut i = 0;
251 while i < qs.len() {
252 let code = qs[i];
253 if Some(&code) == sep_codes.get(sep_idx) {
254 sep_idx += 1;
255 if sep_idx == sep_len {
256 let end = i + 1 - sep_idx;
257 if eq_idx < eq_len {
258 if last_pos < end {
259 key.extend_from_slice(&qs[last_pos..end]);
260 } else if key.is_empty() {
261 pairs -= 1.0;
262 if pairs == 0.0 {
263 return Ok(());
264 }
265 last_pos = i + 1;
266 sep_idx = 0;
267 eq_idx = 0;
268 i += 1;
269 continue;
270 }
271 } else if last_pos < end {
272 value.extend_from_slice(&qs[last_pos..end]);
273 }
274 add_key_val(obj, &key, &value, (key_enc, val_enc), &decode)?;
275 pairs -= 1.0;
276 if pairs == 0.0 {
277 return Ok(());
278 }
279 key_enc = custom;
280 val_enc = custom;
281 key.clear();
282 value.clear();
283 encode_check = 0;
284 last_pos = i + 1;
285 sep_idx = 0;
286 eq_idx = 0;
287 }
288 } else {
289 sep_idx = 0;
290 if eq_idx < eq_len {
291 if Some(&code) == eq_codes.get(eq_idx) {
292 eq_idx += 1;
293 if eq_idx == eq_len {
294 let end = i + 1 - eq_idx;
295 if last_pos < end {
296 key.extend_from_slice(&qs[last_pos..end]);
297 }
298 encode_check = 0;
299 last_pos = i + 1;
300 }
301 i += 1;
302 continue;
303 }
304 eq_idx = 0;
305 if !key_enc {
306 if code == u16::from(b'%') {
307 encode_check = 1;
308 i += 1;
309 continue;
310 } else if encode_check > 0 {
311 if is_hex(code) {
312 encode_check += 1;
313 if encode_check == 3 {
314 key_enc = true;
315 }
316 i += 1;
317 continue;
318 }
319 encode_check = 0;
320 }
321 }
322 if code == u16::from(b'+') {
323 if last_pos < i {
324 key.extend_from_slice(&qs[last_pos..i]);
325 }
326 key.extend_from_slice(&plus);
327 last_pos = i + 1;
328 }
329 i += 1;
330 continue;
331 }
332 if code == u16::from(b'+') {
333 if last_pos < i {
334 value.extend_from_slice(&qs[last_pos..i]);
335 }
336 value.extend_from_slice(&plus);
337 last_pos = i + 1;
338 } else if !val_enc {
339 if code == u16::from(b'%') {
340 encode_check = 1;
341 } else if encode_check > 0 {
342 if is_hex(code) {
343 encode_check += 1;
344 if encode_check == 3 {
345 val_enc = true;
346 }
347 } else {
348 encode_check = 0;
349 }
350 }
351 }
352 }
353 i += 1;
354 }
355 if last_pos < qs.len() {
356 if eq_idx < eq_len {
357 key.extend_from_slice(&qs[last_pos..]);
358 } else if sep_idx < sep_len {
359 value.extend_from_slice(&qs[last_pos..]);
360 }
361 } else if eq_idx == 0 && key.is_empty() {
362 return Ok(());
363 }
364 add_key_val(obj, &key, &value, (key_enc, val_enc), &decode)
365}
366
367fn primitive_text(v: &Value) -> String {
373 match with_host(|h| h.type_of(v)) {
374 "string" | "bigint" | "boolean" => str_of(v),
375 "number" if with_host(|h| h.to_number(v)).is_finite() => str_of(v),
376 _ => String::new(),
377 }
378}
379
380fn convert(v: &Value, encode: &Option<Value>) -> Result<String, String> {
384 let text = primitive_text(v);
385 match encode {
386 Some(f) => {
387 let r = crate::host::invoke(f, vec![new_str(text)], None)?;
388 Ok(str_of(&crate::host::to_string_value(&r)?))
389 }
390 None => {
391 let ty = with_host(|h| h.type_of(v));
392 Ok(match ty {
393 "string" => escape(&text),
394 "number" if with_host(|h| h.to_number(v)).abs() >= 1e21 => escape(&text),
395 _ => text,
396 })
397 }
398 }
399}
400
401fn stringify(obj: &Value, sep: &Value, eq: &Value, options: &Value) -> Result<Value, String> {
405 let part = |v: &Value, default: &str| -> Result<String, String> {
406 if with_host(|h| h.truthy(v)) {
407 Ok(str_of(&crate::host::to_string_value(v)?))
408 } else {
409 Ok(default.to_string())
410 }
411 };
412 let (sep, eq) = (part(sep, "&")?, part(eq, "=")?);
413 let encode = option_fn(options, "encodeURIComponent")?;
414 if with_host(|h| h.type_of(obj)) != "object" || with_host(|h| h.is_null(obj)) {
415 return Ok(new_str(String::new()));
416 }
417 let keys = crate::builtins::call_builtin_function("Object.keys", vec![obj.clone()])?;
418 let keys = with_host(|h| match h.get(&keys) {
419 Some(JsObj::Array(items)) => items.clone(),
420 _ => Vec::new(),
421 });
422 let mut fields = String::new();
423 for k in keys {
424 let v = crate::builtins::get_property(obj, &str_of(&k))?;
425 let ks = convert(&k, &encode)? + &eq;
426 let elems = with_host(|h| match h.get(&v) {
427 Some(JsObj::Array(items)) => Some(items.clone()),
428 _ => None,
429 });
430 match elems {
431 Some(list) if list.is_empty() => continue,
432 Some(list) => {
433 if !fields.is_empty() {
434 fields += &sep;
435 }
436 for (j, e) in list.iter().enumerate() {
437 if j > 0 {
438 fields += &sep;
439 }
440 fields += &ks;
441 fields += &convert(e, &encode)?;
442 }
443 }
444 None => {
445 if !fields.is_empty() {
446 fields += &sep;
447 }
448 fields += &ks;
449 fields += &convert(&v, &encode)?;
450 }
451 }
452 }
453 Ok(new_str(fields))
454}