1use super::*;
5
6impl Interp {
7 pub(crate) fn eval_unary(
8 &mut self,
9 op: &UnaryOp,
10 expr: &Expression,
11 scope: &Rc<RefCell<Scope>>,
12 this: &Value,
13 ) -> EvalResult {
14 if let UnaryOp::TypeOf = op {
16 if let Expression::Identifier(name) = expr {
17 if scope.borrow().has_with {
18 if let Some((wobj, had_u)) = self.resolve_with_scope(scope, name)? {
19 let v = self.with_get_binding_value(&wobj, name, had_u)?;
20 return Ok(Value::str(v.type_of()));
21 }
22 }
23 let on_window = || match scope_get(scope, "globalThis") {
27 Some(Value::Object(w)) => w.borrow().props.contains_key(name) || w.borrow().has_accessor(name),
28 _ => false,
29 };
30 if scope_get(scope, name).is_none() && !on_window() {
31 return Ok(Value::str("undefined"));
32 }
33 }
34 }
35 if let UnaryOp::Delete = op {
38 return self.eval_delete(expr, scope, this);
39 }
40 let v = self.eval(expr, scope, this)?;
41 self.unary_on_value(op, v)
42 }
43
44 pub(crate) fn unary_on_value(&mut self, op: &UnaryOp, v: Value) -> EvalResult {
46 let v = if matches!(op, UnaryOp::Neg | UnaryOp::Pos | UnaryOp::BitNot) { self.prim_num(v)? } else { v };
48 Ok(match op {
49 UnaryOp::Neg => match &v {
50 Value::BigInt(b) => Value::bigint(b.neg()),
51 _ => Value::Number(-v.to_number()),
52 },
53 UnaryOp::Pos => {
54 if matches!(v, Value::BigInt(_)) {
56 return Err(self.type_error("Cannot convert a BigInt to a number"));
57 }
58 Value::Number(v.to_number())
59 }
60 UnaryOp::Not => Value::Bool(!v.truthy()),
61 UnaryOp::BitNot => match &v {
62 Value::BigInt(b) => Value::bigint(b.bitnot()),
63 _ => Value::Number(!(to_i32(v.to_number())) as f64),
64 },
65 UnaryOp::TypeOf => Value::str(v.type_of()),
66 UnaryOp::Void => Value::Undefined,
67 UnaryOp::Delete => Value::Bool(true),
69 })
70 }
71
72 pub(crate) fn eval_delete(
77 &mut self,
78 expr: &Expression,
79 scope: &Rc<RefCell<Scope>>,
80 this: &Value,
81 ) -> EvalResult {
82 match expr {
83 Expression::Member {
84 object, property, ..
85 } => {
86 let obj = self.eval(object, scope, this)?;
87 let r = self.delete_property(&obj, property)?;
88 self.delete_checked(r, property)
89 }
90 Expression::Index { object, index, .. } => {
91 let obj = self.eval(object, scope, this)?;
92 let key = self.eval_key_for(&obj, index, scope, this)?;
93 let k = to_property_key(&key);
94 let r = self.delete_property(&obj, &k)?;
95 self.delete_checked(r, &k)
96 }
97 Expression::Identifier(name) => {
98 if scope.borrow().has_with {
99 if let Some((wobj, _)) = self.resolve_with_scope(scope, name)? {
100 let r = self.delete_property(&Value::Object(wobj), name)?;
101 return self.delete_checked(r, name);
102 }
103 }
104 Ok(Value::Bool(self.delete_identifier(name, scope)))
105 }
106 _ => Ok(Value::Bool(true)),
107 }
108 }
109
110 #[inline]
112 pub(crate) fn delete_checked(&self, r: Value, key: &str) -> EvalResult {
113 if self.strict && matches!(r, Value::Bool(false)) {
114 return Err(self.type_error(alloc::format!("Cannot delete property '{}'", key)));
115 }
116 Ok(r)
117 }
118
119 pub(crate) fn delete_property(&mut self, obj: &Value, property: &str) -> EvalResult {
124 if let Value::Object(o) = obj {
125 if matches!(o.borrow().kind, ObjKind::Proxy(_)) {
126 return Ok(Value::Bool(super::super::builtins::pm_delete(self, o, property)?));
127 }
128 let host_ref = o.borrow().kind.host_ref();
131 if let Some(h) = host_ref {
132 if let Some(r) = (self.host.delete)(self, h, property) {
133 return Ok(Value::Bool(r));
134 }
135 }
136 if matches!(o.borrow().kind, ObjKind::Bytes(_))
138 && crate::os_lib::ecma::value::is_canonical_numeric_key(property)
139 {
140 let valid = crate::os_lib::ecma::value::ta_valid_index(&o.borrow(), property).is_some();
141 return Ok(Value::Bool(!valid));
142 }
143 if property == "lastIndex" && matches!(o.borrow().kind, ObjKind::RegExpObj(_)) && o.borrow().get_attr(property).is_none() {
144 return Ok(Value::Bool(false));
145 }
146 if let Some(attr) = o.borrow().get_attr(property) {
147 if !attr.configurable {
148 return Ok(Value::Bool(false));
149 }
150 }
151 if (o.borrow().is_frozen() || o.borrow().is_sealed())
154 && super::super::builtins::has_own_property(o, property)
155 {
156 return Ok(Value::Bool(false));
157 }
158 let is_array = matches!(&o.borrow().kind, ObjKind::Array(_));
159 if is_array {
160 if let Some(idx) = crate::os_lib::ecma::value::parse_index(property) {
164 let mut b = o.borrow_mut();
165 b.remove_accessor(property);
166 b.remove_attr(property);
167 let len = match &mut b.kind {
168 ObjKind::Array(items) if idx < items.len() => {
169 items[idx] = Value::Undefined;
170 Some(items.len())
171 }
172 _ => None,
173 };
174 if let Some(len) = len {
175 let mut mask = b.holes().map(|h| h.to_vec()).unwrap_or_default();
176 mask.resize(len, false);
177 if let Some(m) = mask.get_mut(idx) {
178 *m = true;
179 }
180 b.set_holes(Some(alloc::boxed::Box::new(mask)));
181 }
182 } else {
183 let mut b = o.borrow_mut();
185 b.props.shift_remove(property);
186 b.remove_accessor(property);
187 b.remove_attr(property);
188 }
189 } else {
190 let mut b = o.borrow_mut();
191 if let Some(attr) = b.get_attr(property) {
192 if !attr.configurable {
193 return Ok(Value::Bool(false));
194 }
195 }
196 b.props.shift_remove(property);
197 b.remove_accessor(property);
198 b.remove_attr(property);
199 b.delete_virtual_fn_prop(property);
200 drop(b);
201 if self.is_global_obj(o) && !self.global_lexicals.borrow().contains(property) {
202 self.global.borrow_mut().vars.shift_remove(property);
203 if let Some(i) = self.implicit_globals.iter().position(|n| &**n == property) {
204 self.implicit_globals.swap_remove(i);
205 }
206 }
207 }
208 }
209 Ok(Value::Bool(true))
210 }
211
212 pub(crate) fn eval_update(
213 &mut self,
214 op: &str,
215 prefix: bool,
216 target: &Expression,
217 scope: &Rc<RefCell<Scope>>,
218 this: &Value,
219 ) -> EvalResult {
220 if let Expression::Member { object, property, .. } = target {
221 if property.starts_with('#') && !matches!(**object, Expression::Super) {
222 let o = self.eval(object, scope, this)?;
223 let old_val = self.private_get(&o, property, scope)?;
224 let (old, new) = self.numeric_step(&old_val, if op == "++" { 1.0 } else { -1.0 })?;
225 self.private_set(&o, property, new.clone(), scope)?;
226 return Ok(if prefix { new } else { old });
227 }
228 }
229 let lref: Option<(Value, String)> = match target {
231 Expression::Member { object, property, .. } if !matches!(**object, Expression::Super) => {
232 Some((self.eval(object, scope, this)?, property.to_string()))
233 }
234 Expression::Index { object, index, .. } if !matches!(**object, Expression::Super) => {
235 let o = self.eval(object, scope, this)?;
236 let k = self.eval_key_for(&o, index, scope, this)?;
237 if matches!(k, Value::Number(_)) {
238 if let Some(old) = self.get_index_fast(&o, &k).filter(|v| !matches!(v, Value::BigInt(_))) {
239 let old = old.to_number();
240 let new = if op == "++" { old + 1.0 } else { old - 1.0 };
241 if let Some(v) = self.set_index_fast(&o, &k, Value::Number(new)) {
242 self.put_value(&o, &to_property_key(&k), v)?;
243 }
244 return Ok(Value::Number(if prefix { new } else { old }));
245 }
246 }
247 Some((o, to_property_key(&k).into_owned()))
248 }
249 Expression::Identifier(name) if scope.borrow().has_with => {
250 if let Some((wobj, _)) = self.resolve_with_scope(scope, name)? {
251 Some((Value::Object(wobj), name.to_string()))
252 } else {
253 None
254 }
255 }
256 _ => None,
257 };
258 let old = match &lref {
259 Some((Value::Object(wobj), k)) if matches!(target, Expression::Identifier(_)) => {
260 self.with_get_binding_value(wobj, k, true)?
261 }
262 Some((o, k)) => self.get_property(o, k)?,
263 None => self.eval(target, scope, this)?,
264 };
265 let (old, new) = self.numeric_step(&old, if op == "++" { 1.0 } else { -1.0 })?;
266 match &lref {
267 Some((Value::Object(wobj), k)) if matches!(target, Expression::Identifier(_)) => {
268 self.with_set_mutable_binding(wobj, k, new.clone())?;
269 }
270 Some((o, k)) => {
271 self.put_value(o, k, new.clone())?;
272 }
273 None => self.assign_to(target, new.clone(), scope, this)?,
274 }
275 Ok(if prefix { new } else { old })
276 }
277
278 pub(crate) fn eval_assign(
279 &mut self,
280 op: &str,
281 target: &Expression,
282 value: &Expression,
283 scope: &Rc<RefCell<Scope>>,
284 this: &Value,
285 ) -> EvalResult {
286 if let Expression::Member { object, property, .. } = target {
299 if property.starts_with('#') && !matches!(**object, Expression::Super) {
300 let o = self.eval(object, scope, this)?;
301 if matches!(op, "&&=" | "||=" | "??=") {
302 let cur = self.private_get(&o, property, scope)?;
303 let assign = match op {
304 "&&=" => cur.truthy(),
305 "||=" => !cur.truthy(),
306 _ => matches!(cur, Value::Undefined | Value::Null),
307 };
308 return if assign {
309 let v = self.eval(value, scope, this)?;
310 self.private_set(&o, property, v.clone(), scope)?;
311 Ok(v)
312 } else {
313 Ok(cur)
314 };
315 }
316 let cur = if op == "=" {
317 None
318 } else {
319 Some(self.private_get(&o, property, scope)?)
320 };
321 let rhs = self.eval(value, scope, this)?;
322 let final_val = if let Some(cur) = cur {
323 let bop = match op {
324 "+=" => BinaryOp::Add,
325 "-=" => BinaryOp::Sub,
326 "*=" => BinaryOp::Mul,
327 "/=" => BinaryOp::Div,
328 "%=" => BinaryOp::Mod,
329 "**=" => BinaryOp::Pow,
330 "&=" => BinaryOp::BitAnd,
331 "|=" => BinaryOp::BitOr,
332 "^=" => BinaryOp::BitXor,
333 "<<=" => BinaryOp::Shl,
334 ">>=" => BinaryOp::Shr,
335 ">>>=" => BinaryOp::UShr,
336 _ => BinaryOp::Add,
337 };
338 self.eval_binary(&bop, cur, rhs)?
339 } else {
340 rhs
341 };
342 self.private_set(&o, property, final_val.clone(), scope)?;
343 return Ok(final_val);
344 }
345 }
346 let mut late_key: Option<Value> = None;
347 let mut lref: Option<(Value, alloc::borrow::Cow<'_, str>)> = match target {
348 Expression::Member { object, property, .. } if !matches!(**object, Expression::Super) => {
349 Some((self.eval(object, scope, this)?, alloc::borrow::Cow::Borrowed(&**property)))
350 }
351 Expression::Index { object, index, .. } if !matches!(**object, Expression::Super) => {
352 let o = self.eval(object, scope, this)?;
353 let k = self.eval(index, scope, this)?;
354 let k = if op == "=" { k } else { self.prim_key_for(&o, k)? };
356 if matches!(k, Value::Number(_)) && matches!(op, "=" | "+=" | "-=" | "|=" | "&=" | "^=" | "<<=" | ">>=" | ">>>=") {
358 let cur = if op == "=" {
359 None
360 } else {
361 Some(match self.get_index_fast(&o, &k) {
362 Some(v) => v,
363 None => self.get_property(&o, &to_property_key(&k))?,
364 })
365 };
366 let rhs = self.eval(value, scope, this)?;
367 if matches!(o, Value::Undefined | Value::Null) {
368 return Err(self.set_on_nullish_error(&o, &to_property_key(&k)));
369 }
370 let final_val = match cur {
371 None => rhs,
372 Some(cur) => {
373 let bop = match op {
374 "+=" => BinaryOp::Add,
375 "-=" => BinaryOp::Sub,
376 "&=" => BinaryOp::BitAnd,
377 "|=" => BinaryOp::BitOr,
378 "^=" => BinaryOp::BitXor,
379 "<<=" => BinaryOp::Shl,
380 ">>=" => BinaryOp::Shr,
381 _ => BinaryOp::UShr,
382 };
383 self.eval_binary(&bop, cur, rhs)?
384 }
385 };
386 if let Some(v) = self.set_index_fast(&o, &k, final_val.clone()) {
387 self.put_value(&o, &to_property_key(&k), v)?;
388 }
389 return Ok(final_val);
390 }
391 if op == "=" && matches!(k, Value::Object(_)) {
392 late_key = Some(k.clone());
393 }
394 Some((o, alloc::borrow::Cow::Owned(to_property_key(&k).into_owned())))
395 }
396 Expression::Identifier(name) if scope.borrow().has_with => {
397 if let Some((wobj, _)) = self.resolve_with_scope(scope, name)? {
398 Some((Value::Object(wobj), alloc::borrow::Cow::Borrowed(&**name)))
399 } else {
400 None
401 }
402 }
403 _ => None,
404 };
405 match op {
406 "&&=" | "||=" | "??=" => {
407 let cur = match &lref {
408 Some((Value::Object(wobj), k)) if matches!(target, Expression::Identifier(_)) => {
409 self.with_get_binding_value(wobj, k.as_ref(), true)?
410 }
411 Some((o, k)) => self.get_property(o, k.as_ref())?,
412 None => self.eval(target, scope, this)?,
413 };
414 let assign = match op {
415 "&&=" => cur.truthy(),
416 "||=" => !cur.truthy(),
417 _ => matches!(cur, Value::Undefined | Value::Null),
418 };
419 return if assign {
420 let v = self.eval(value, scope, this)?;
421 match &lref {
422 Some((Value::Object(wobj), k)) if matches!(target, Expression::Identifier(_)) => {
423 self.with_set_mutable_binding(wobj, k.as_ref(), v.clone())?;
424 }
425 Some((o, k)) => {
426 self.put_value(o, k.as_ref(), v.clone())?;
427 }
428 None => match target {
429 Expression::Identifier(name) => {
430 self.assign_ident_no_with(name, v.clone(), scope)?;
431 }
432 _ => self.assign_to(target, v.clone(), scope, this)?,
433 },
434 }
435 Ok(v)
436 } else {
437 Ok(cur)
438 };
439 }
440 _ => {}
441 }
442 let cur = if op == "=" {
444 None
445 } else {
446 Some(match &lref {
447 Some((Value::Object(wobj), k)) if matches!(target, Expression::Identifier(_)) => {
448 self.with_get_binding_value(wobj, k.as_ref(), true)?
449 }
450 Some((o, k)) => self.get_property(o, k.as_ref())?,
451 None => self.eval(target, scope, this)?,
452 })
453 };
454 let rhs = self.eval(value, scope, this)?;
455 if op == "=" {
456 match target {
457 Expression::Identifier(n) => {
459 if matches!(
460 value,
461 Expression::Function { name: None, .. } | Expression::Class { name: None, .. }
462 ) {
463 super::infer_function_name(&rhs, n);
464 }
465 }
466 Expression::Member { property, .. } => {
469 if let Value::Object(o) = &rhs {
470 let anon = matches!(&o.borrow().kind, ObjKind::Function(fd) if fd.name().is_empty());
471 if anon {
472 o.borrow_mut().props.insert("_diag_name".into(), Value::str(&**property));
473 }
474 }
475 }
476 _ => {}
477 }
478 }
479 let final_val = if let Some(cur) = cur {
480 let bop = match op {
481 "+=" => BinaryOp::Add,
482 "-=" => BinaryOp::Sub,
483 "*=" => BinaryOp::Mul,
484 "/=" => BinaryOp::Div,
485 "%=" => BinaryOp::Mod,
486 "**=" => BinaryOp::Pow,
487 "&=" => BinaryOp::BitAnd,
488 "|=" => BinaryOp::BitOr,
489 "^=" => BinaryOp::BitXor,
490 "<<=" => BinaryOp::Shl,
491 ">>=" => BinaryOp::Shr,
492 ">>>=" => BinaryOp::UShr,
493 _ => BinaryOp::Add,
494 };
495 self.eval_binary(&bop, cur, rhs)?
496 } else {
497 rhs
498 };
499 if let (Some(kv), Some((o, k))) = (late_key, lref.as_mut()) {
500 if !matches!(o, Value::Undefined | Value::Null) {
501 let p = self.prim_key(kv)?;
502 *k = alloc::borrow::Cow::Owned(to_property_key(&p).into_owned());
503 }
504 }
505 match &lref {
506 Some((o, k)) if matches!(o, Value::Undefined | Value::Null) => {
508 let err = self.set_on_nullish_error(o, k.as_ref());
509 if let (Value::Object(eo), Expression::Member { object, .. } | Expression::Index { object, .. }) = (&err, target) {
511 let desc = super::properties::describe_expr(object);
512 let stack = eo.borrow().error_stack().unwrap_or("").to_string();
513 let mut lines = stack.splitn(2, '\n');
514 let first = lines.next().unwrap_or("");
515 let mut ns = alloc::format!("{first}\n (null/undefined だった式: {desc})");
516 if let Some(rest) = lines.next() {
517 ns.push('\n');
518 ns.push_str(rest);
519 }
520 eo.borrow_mut().set_error_stack(ns);
521 }
522 return Err(err);
523 }
524 Some((o, _)) if matches!(target, Expression::Member { .. }) => {
525 if let Expression::Member { property, .. } = target {
526 self.put_value_rc(o, property, final_val.clone())?;
527 }
528 }
529 Some((Value::Object(wobj), k)) if matches!(target, Expression::Identifier(_)) => {
530 self.with_set_mutable_binding(wobj, k.as_ref(), final_val.clone())?;
531 }
532 Some((o, k)) => {
533 self.put_value(o, k.as_ref(), final_val.clone())?;
534 }
535 None => match target {
536 Expression::Identifier(name) => {
537 self.assign_ident_no_with(name, final_val.clone(), scope)?;
538 }
539 _ => self.assign_to(target, final_val.clone(), scope, this)?,
540 },
541 }
542 Ok(final_val)
543 }
544
545 fn delete_identifier(&mut self, name: &Rc<str>, scope: &Rc<RefCell<Scope>>) -> bool {
548 let mut cur = scope.clone();
549 loop {
550 let next = cur.borrow().parent.clone();
551 match next {
552 Some(p) => {
553 if cur.borrow().vars.contains_key(name) {
554 return false;
555 }
556 cur = p;
557 }
558 None => break,
559 }
560 }
561 if cur.borrow().vars.contains_key(name) {
562 let Some(i) = self.implicit_globals.iter().position(|n| n == name) else { return false };
563 self.implicit_globals.swap_remove(i);
564 cur.borrow_mut().vars.shift_remove(name);
565 if let Some(w) = self.global_object() {
566 w.borrow_mut().props.shift_remove(name);
567 w.borrow_mut().remove_attr(name);
568 }
569 return true;
570 }
571 if let Some(w) = self.global_object() {
572 if w.borrow().get_attr(name).is_some_and(|a| !a.configurable) {
573 return false;
574 }
575 w.borrow_mut().props.shift_remove(name);
576 w.borrow_mut().remove_attr(name);
577 }
578 true
579 }
580
581 fn assign_ident_no_with(
583 &mut self,
584 name: &Rc<str>,
585 val: Value,
586 scope: &Rc<RefCell<Scope>>,
587 ) -> Result<(), Value> {
588 if !scope_assign(scope, name, val.clone()) {
589 if self.strict {
592 let on_global = match self.global_object() {
593 Some(w) => self.has_property_chain(&w, name) || !matches!(self.object_get_prop(&w, name), Value::Undefined),
594 None => false,
595 };
596 if !on_global {
597 return Err(self.reference_error(alloc::format!("{} is not defined", name)));
598 }
599 }
600 self.implicit_globals.push(name.clone());
602 let mut s = scope.clone();
603 loop {
604 let p = s.borrow().parent.clone();
605 match p {
606 Some(pp) => s = pp,
607 None => break,
608 }
609 }
610 scope_declare(&s, name, val);
611 }
612 Ok(())
613 }
614
615 pub(crate) fn assign_to(
617 &mut self,
618 target: &Expression,
619 val: Value,
620 scope: &Rc<RefCell<Scope>>,
621 this: &Value,
622 ) -> Result<(), Value> {
623 match target {
624 Expression::Identifier(name) => {
625 if scope.borrow().has_with {
626 if let Some((wobj, _)) = self.resolve_with_scope(scope, name)? {
627 return self.with_set_mutable_binding(&wobj, name, val);
628 }
629 }
630 self.assign_ident_no_with(name, val, scope)
631 }
632 Expression::Member { object, property, .. } if matches!(**object, Expression::Super) => {
634 self.super_set(scope, property, val, this)
635 }
636 Expression::Index { object, index, .. } if matches!(**object, Expression::Super) => {
637 let key = self.eval_key(index, scope, this)?;
638 self.super_set(scope, &to_property_key(&key), val, this)
639 }
640 Expression::Member {
641 object, property, ..
642 } => {
643 let obj = self.eval(object, scope, this)?;
644 if property.starts_with('#') {
645 return self.private_set(&obj, property, val, scope);
646 }
647 if matches!(obj, Value::Undefined | Value::Null) {
648 return Err(self.set_on_nullish_error(&obj, property));
649 }
650 self.put_value_rc(&obj, property, val)?;
651 Ok(())
652 }
653 Expression::Index { object, index, .. } => {
654 let obj = self.eval(object, scope, this)?;
655 let key = self.eval_key_for(&obj, index, scope, this)?;
656 if matches!(obj, Value::Undefined | Value::Null) {
657 return Err(self.set_on_nullish_error(&obj, &to_property_key(&key)));
658 }
659 if let Some(val) = self.set_index_fast(&obj, &key, val) {
660 self.put_value(&obj, &to_property_key(&key), val)?;
661 }
662 Ok(())
663 }
664 Expression::Array(items) => self.assign_array_destructure(items, val, scope, this),
668 Expression::Object(props) => self.assign_object_destructure(props, val, scope, this),
669 _ => Ok(()),
670 }
671 }
672
673 pub(crate) fn set_on_nullish_error(&self, obj: &Value, key: &str) -> Value {
675 self.type_error(alloc::format!(
676 "Cannot set properties of {} (setting '{}')",
677 obj.to_js_string(),
678 key
679 ))
680 }
681
682 fn assign_array_destructure(
684 &mut self,
685 items: &[Expression],
686 val: Value,
687 scope: &Rc<RefCell<Scope>>,
688 this: &Value,
689 ) -> Result<(), Value> {
690 if matches!(val, Value::Object(_)) {
691 if let Some((iterator, next_fn)) = self.open_lazy_iterator(&val)? {
693 return self.assign_array_destructure_lazy(items, &iterator, &next_fn, scope, this);
694 }
695 }
696 let values: Vec<Value> = match &val {
697 Value::Undefined | Value::Null | Value::Number(_) | Value::Bool(_) | Value::BigInt(_) => {
698 return Err(self.type_error(alloc::format!("{} is not iterable", val.to_js_string())));
699 }
700 Value::Object(o) if matches!(o.borrow().kind, ObjKind::Array(_)) => match &o.borrow().kind {
701 ObjKind::Array(v) => v.clone(),
702 _ => Vec::new(),
703 },
704 _ => {
705 let need = if items.iter().any(|e| matches!(e, Expression::Spread(_))) { None } else { Some(items.len()) };
706 self.destructure_values(&val, need)?
707 }
708 };
709 for (i, el) in items.iter().enumerate() {
710 match el {
711 Expression::Elision => {}
712 Expression::Spread(inner) => {
713 let rest = values.get(i..).map(<[Value]>::to_vec).unwrap_or_default();
714 self.assign_to(inner, Value::Object(Obj::array(rest)), scope, this)?;
715 break;
716 }
717 Expression::Assign { op, target, value: def } if *op == "=" => {
718 let mut v = values.get(i).cloned().unwrap_or(Value::Undefined);
719 if matches!(v, Value::Undefined) {
720 v = self.eval(def, scope, this)?;
721 if let Expression::Identifier(n) = &**target {
722 if matches!(&**def, Expression::Function { name: None, .. } | Expression::Class { name: None, .. }) {
723 super::infer_function_name(&v, n);
724 }
725 }
726 }
727 self.assign_to(target, v, scope, this)?;
728 }
729 other => {
730 let v = values.get(i).cloned().unwrap_or(Value::Undefined);
731 self.assign_to(other, v, scope, this)?;
732 }
733 }
734 }
735 Ok(())
736 }
737
738 fn eval_target_ref(
741 &mut self,
742 target: &Expression,
743 scope: &Rc<RefCell<Scope>>,
744 this: &Value,
745 ) -> Result<Option<(Value, Value)>, Value> {
746 match target {
747 Expression::Member { object, property, .. }
748 if !matches!(**object, Expression::Super) && !property.starts_with('#') =>
749 {
750 let obj = self.eval(object, scope, this)?;
751 Ok(Some((obj, Value::str(&**property))))
752 }
753 Expression::Index { object, index, .. } if !matches!(**object, Expression::Super) => {
754 let obj = self.eval(object, scope, this)?;
755 let key = self.eval_key_for(&obj, index, scope, this)?;
756 Ok(Some((obj, key)))
757 }
758 _ => Ok(None),
759 }
760 }
761
762 fn assign_prepared(
764 &mut self,
765 target: &Expression,
766 pre: Option<(Value, Value)>,
767 val: Value,
768 scope: &Rc<RefCell<Scope>>,
769 this: &Value,
770 ) -> Result<(), Value> {
771 let Some((obj, key)) = pre else {
772 return self.assign_to(target, val, scope, this);
773 };
774 if matches!(obj, Value::Undefined | Value::Null) {
775 return Err(self.set_on_nullish_error(&obj, &to_property_key(&key)));
776 }
777 if matches!(target, Expression::Index { .. }) {
778 if let Some(val) = self.set_index_fast(&obj, &key, val) {
779 self.put_value(&obj, &to_property_key(&key), val)?;
780 }
781 } else {
782 self.put_value(&obj, &to_property_key(&key), val)?;
783 }
784 Ok(())
785 }
786
787 fn assign_array_destructure_lazy(
788 &mut self,
789 items: &[Expression],
790 iterator: &Value,
791 next_fn: &Value,
792 scope: &Rc<RefCell<Scope>>,
793 this: &Value,
794 ) -> Result<(), Value> {
795 let mut done = false;
796 let res = (|| -> Result<(), Value> {
797 for el in items {
798 if let Expression::Spread(inner) = el {
799 let pre = self.eval_target_ref(inner, scope, this)?;
801 let mut rest = Vec::new();
802 while !done {
803 match self.iter_step_once(next_fn, iterator) {
804 Ok(Some(v)) => rest.push(v),
805 Ok(None) => done = true,
806 Err(e) => {
807 done = true;
808 return Err(e);
809 }
810 }
811 }
812 self.assign_prepared(inner, pre, Value::Object(Obj::array(rest)), scope, this)?;
813 break;
814 }
815 let pre = match el {
816 Expression::Elision => None,
817 Expression::Assign { op, target, .. } if *op == "=" => self.eval_target_ref(target, scope, this)?,
818 other => self.eval_target_ref(other, scope, this)?,
819 };
820 let mut v = Value::Undefined;
821 if !done {
822 match self.iter_step_once(next_fn, iterator) {
823 Ok(Some(x)) => v = x,
824 Ok(None) => done = true,
825 Err(e) => {
826 done = true;
827 return Err(e);
828 }
829 }
830 }
831 match el {
832 Expression::Elision => {}
833 Expression::Assign { op, target, value: def } if *op == "=" => {
834 if matches!(v, Value::Undefined) {
835 v = self.eval(def, scope, this)?;
836 if let Expression::Identifier(n) = &**target {
837 if matches!(&**def, Expression::Function { name: None, .. } | Expression::Class { name: None, .. }) {
838 super::infer_function_name(&v, n);
839 }
840 }
841 }
842 self.assign_prepared(target, pre, v, scope, this)?;
843 }
844 other => self.assign_prepared(other, pre, v, scope, this)?,
845 }
846 }
847 Ok(())
848 })();
849 match res {
850 Ok(()) => {
851 if !done {
852 self.iterator_close(iterator)?;
853 }
854 Ok(())
855 }
856 Err(e) if done => Err(e),
857 Err(e) => {
858 if !(self.aborted || self.is_suspending() || self.is_gen_returning()) {
859 let _ = self.iterator_close(iterator);
860 }
861 Err(e)
862 }
863 }
864 }
865
866 fn assign_object_destructure(
868 &mut self,
869 props: &[crate::os_lib::ecma::ast::Property],
870 val: Value,
871 scope: &Rc<RefCell<Scope>>,
872 this: &Value,
873 ) -> Result<(), Value> {
874 if matches!(val, Value::Undefined | Value::Null) {
875 return Err(self.type_error(alloc::format!(
876 "Cannot destructure '{}' as it is {}.",
877 val.to_js_string(),
878 val.to_js_string()
879 )));
880 }
881 let mut consumed: Vec<String> = Vec::new();
882 for p in props {
883 if &*p.key == crate::os_lib::ecma::ast::OBJECT_SPREAD_KEY {
884 let rest = Obj::plain();
886 super::super::builtins::copy_data_properties(self, &rest, &val, &consumed)?;
887 self.assign_to(&p.value, Value::Object(rest), scope, this)?;
888 continue;
889 }
890 let key: String = match &p.computed {
891 Some(ce) => {
892 let kv = self.eval_key(ce, scope, this)?;
893 to_property_key(&kv).into_owned()
894 }
895 None => String::from(&*p.key),
896 };
897 let (target, def) = match &p.value {
898 Expression::Assign { op, target, value } if *op == "=" => (&**target, Some(&**value)),
899 other => (other, None),
900 };
901 let mut v = self.get_property(&val, &key)?;
902 if matches!(v, Value::Undefined) {
903 if let Some(d) = def {
904 v = self.eval(d, scope, this)?;
905 if let Expression::Identifier(n) = target {
906 if matches!(d, Expression::Function { name: None, .. } | Expression::Class { name: None, .. }) {
907 super::infer_function_name(&v, n);
908 }
909 }
910 }
911 }
912 consumed.push(key);
913 self.assign_to(target, v, scope, this)?;
914 }
915 Ok(())
916 }
917
918 #[inline(always)]
920 pub(crate) fn eval_key(&mut self, e: &Expression, scope: &Rc<RefCell<Scope>>, this: &Value) -> EvalResult {
921 let v = self.eval(e, scope, this)?;
922 self.prim_key(v)
923 }
924
925 #[inline(always)]
927 pub(crate) fn prim_key_for(&mut self, base: &Value, k: Value) -> EvalResult {
928 if matches!(base, Value::Undefined | Value::Null) {
929 return Ok(k);
930 }
931 self.prim_key(k)
932 }
933
934 #[inline(always)]
937 pub(crate) fn eval_key_for(&mut self, base: &Value, e: &Expression, scope: &Rc<RefCell<Scope>>, this: &Value) -> EvalResult {
938 let v = self.eval(e, scope, this)?;
939 if matches!(base, Value::Undefined | Value::Null) {
940 return Ok(v);
941 }
942 self.prim_key(v)
943 }
944
945 #[inline(always)]
947 pub(crate) fn prim_num(&mut self, v: Value) -> EvalResult {
948 if let Value::Object(_) = v {
949 self.to_primitive(v, "number")
950 } else {
951 Ok(v)
952 }
953 }
954
955 #[inline(always)]
957 pub(crate) fn prim_key(&mut self, v: Value) -> EvalResult {
958 if let Value::Object(_) = v {
959 self.to_primitive(v, "string")
960 } else {
961 Ok(v)
962 }
963 }
964
965 pub(crate) fn numeric_step(&mut self, v: &Value, delta: f64) -> Result<(Value, Value), Value> {
968 let prim = if let Value::Object(_) = v { self.to_primitive(v.clone(), "number")? } else { v.clone() };
969 if let Value::BigInt(b) = &prim {
970 let one = super::super::bigint::BigInt::from_i64(1);
971 let nb = if delta > 0.0 { b.add(&one) } else { b.sub(&one) };
972 return Ok((prim.clone(), Value::bigint(nb)));
973 }
974 let n = self.num_of(&prim)?;
975 Ok((Value::Number(n), Value::Number(n + delta)))
976 }
977
978 #[inline(always)]
980 pub(crate) fn num_of(&mut self, v: &Value) -> Result<f64, Value> {
981 let prim = if let Value::Object(_) = v {
982 self.to_primitive(v.clone(), "number")?
983 } else {
984 v.clone()
985 };
986 if matches!(prim, Value::BigInt(_)) {
987 return Err(self.type_error("Cannot convert a BigInt value to a number"));
988 }
989 if matches!(prim, Value::Str(_)) && prim.type_of() == "symbol" {
991 return Err(self.type_error("Cannot convert a Symbol value to a number"));
992 }
993 Ok(prim.to_number())
994 }
995
996 #[allow(clippy::wrong_self_convention)] pub(crate) fn to_primitive(&mut self, v: Value, hint: &str) -> EvalResult {
1004 let Value::Object(o) = &v else { return Ok(v) };
1005 let wrapper_prim = o.borrow().primitive().cloned();
1006 if !matches!(
1007 o.borrow().kind,
1008 ObjKind::Plain | ObjKind::Function(_) | ObjKind::HostRef(_) | ObjKind::Array(_)
1009 ) {
1010 return Ok(v);
1011 }
1012 let mut has = [false; 3];
1014 let names = ["Symbol(Symbol.toPrimitive)", "toString", "valueOf"];
1015 let mut cur = Some(o.clone());
1016 let mut guard = 0;
1017 while let Some(c) = cur {
1018 let host_ref = c.borrow().kind.host_ref();
1019 if let Some(h) = host_ref {
1020 for (i, n) in names.iter().enumerate() {
1021 if !matches!(
1022 (self.host.own)(self, h, n),
1023 crate::os_lib::ecma::host::HostOwn::Generic
1024 | crate::os_lib::ecma::host::HostOwn::Absent
1025 ) {
1026 has[i] = true;
1027 }
1028 }
1029 if !has[1] && (self.host.resolves_on_read)(h) && (self.host.get)(self, &c, h, "toString").is_some() {
1030 has[1] = true;
1031 }
1032 }
1033 let next = {
1034 let b = c.borrow();
1035 for (i, n) in names.iter().enumerate() {
1036 if b.props.contains_key(n) || b.has_accessor(n) {
1037 has[i] = true;
1038 }
1039 }
1040 let p = b.proto.clone();
1041 if p.is_none() && !b.is_null_proto() {
1042 if matches!(b.kind, ObjKind::Array(_)) {
1043 super::super::builtins::global_ctor_proto(self, "Array")
1045 } else {
1046 self.host_proto_of(&b.kind).flatten()
1047 }
1048 } else {
1049 p
1050 }
1051 };
1052 guard += 1;
1053 if guard > 64 {
1054 break;
1055 }
1056 cur = next;
1057 }
1058 let is_callable = |f: &Value| matches!(f, Value::Object(x) if x.borrow().is_callable());
1059 if has[0] {
1060 let f = self.get_property(&v, names[0])?;
1061 if !matches!(f, Value::Undefined | Value::Null) {
1062 if !is_callable(&f) {
1063 return Err(self.type_error("Symbol.toPrimitive is not a function"));
1064 }
1065 let r = self.call_value(&f, v.clone(), &[Value::str(hint)])?;
1066 if !matches!(r, Value::Object(_)) {
1067 return Ok(r);
1068 }
1069 return Err(self.type_error("Cannot convert object to primitive value"));
1070 }
1071 }
1072 if matches!(&o.borrow().kind, ObjKind::Array(_)) && !has[1] && !has[2] {
1073 return super::super::builtins::arr_join(self, v.clone(), &[]);
1074 }
1075 if !has[1] && !has[2] {
1076 return Ok(wrapper_prim.unwrap_or(v));
1077 }
1078 let order: [&str; 2] = if hint == "string" { ["toString", "valueOf"] } else { ["valueOf", "toString"] };
1079 let mut returned_object = false;
1080 for name in order {
1081 let f = self.get_property(&v, name)?;
1082 if is_callable(&f) {
1083 let r = self.call_value(&f, v.clone(), &[])?;
1084 if !matches!(r, Value::Object(_)) {
1085 return Ok(r);
1086 }
1087 returned_object = true;
1088 }
1089 }
1090 if returned_object {
1091 return Err(self.type_error("Cannot convert object to primitive value"));
1092 }
1093 Ok(wrapper_prim.unwrap_or(v))
1094 }
1095
1096 #[allow(clippy::wrong_self_convention)] pub(crate) fn to_string_value(&mut self, v: Value) -> Result<String, Value> {
1099 Ok(self.to_primitive(v, "string")?.to_js_string())
1100 }
1101
1102 pub fn eval_binary(&mut self, op: &BinaryOp, l: Value, r: Value) -> EvalResult {
1103 let (l, r) = if matches!(l, Value::Object(_)) || matches!(r, Value::Object(_)) {
1107 match op {
1108 BinaryOp::Add => (self.to_primitive(l, "default")?, self.to_primitive(r, "default")?),
1109 BinaryOp::Eq | BinaryOp::NotEq
1112 if matches!(l, Value::Object(_)) != matches!(r, Value::Object(_))
1113 && !matches!(l, Value::Undefined | Value::Null)
1114 && !matches!(r, Value::Undefined | Value::Null) =>
1115 {
1116 (self.to_primitive(l, "default")?, self.to_primitive(r, "default")?)
1117 }
1118 BinaryOp::In => (self.prim_key(l)?, r),
1119 BinaryOp::Eq
1120 | BinaryOp::NotEq
1121 | BinaryOp::StrictEq
1122 | BinaryOp::StrictNotEq
1123 | BinaryOp::InstanceOf => (l, r),
1124 _ => (self.to_primitive(l, "number")?, self.to_primitive(r, "number")?),
1125 }
1126 } else {
1127 (l, r)
1128 };
1129 if matches!(l, Value::BigInt(_)) || matches!(r, Value::BigInt(_)) {
1131 return self.eval_binary_bigint(op, l, r);
1132 }
1133 Ok(match op {
1134 BinaryOp::Add => {
1135 let l_str = matches!(l, Value::Str(_) | Value::Object(_));
1137 let r_str = matches!(r, Value::Str(_) | Value::Object(_));
1138 if l_str || r_str {
1139 let a: alloc::borrow::Cow<'_, str> = match &l {
1142 Value::Str(s) => alloc::borrow::Cow::Borrowed(s.as_str()),
1143 _ => alloc::borrow::Cow::Owned(l.to_js_string()),
1144 };
1145 let b: alloc::borrow::Cow<'_, str> = match &r {
1146 Value::Str(s) => alloc::borrow::Cow::Borrowed(s.as_str()),
1147 _ => alloc::borrow::Cow::Owned(r.to_js_string()),
1148 };
1149 let mut out = String::with_capacity(a.len() + b.len());
1150 out.push_str(&a);
1151 out.push_str(&b);
1152 Value::Str(Rc::new(out))
1153 } else {
1154 Value::Number(l.to_number() + r.to_number())
1155 }
1156 }
1157 BinaryOp::Sub => Value::Number(l.to_number() - r.to_number()),
1158 BinaryOp::Mul => Value::Number(l.to_number() * r.to_number()),
1159 BinaryOp::Div => Value::Number(l.to_number() / r.to_number()),
1160 BinaryOp::Mod => {
1161 let a = l.to_number();
1162 let b = r.to_number();
1163 Value::Number(if b == 0.0 { f64::NAN } else { a % b })
1164 }
1165 BinaryOp::Pow => Value::Number(powf(l.to_number(), r.to_number())),
1166 BinaryOp::Eq => Value::Bool(loose_eq(&l, &r)),
1167 BinaryOp::NotEq => Value::Bool(!loose_eq(&l, &r)),
1168 BinaryOp::StrictEq => Value::Bool(l.strict_eq(&r)),
1169 BinaryOp::StrictNotEq => Value::Bool(!l.strict_eq(&r)),
1170 BinaryOp::Lt => cmp(&l, &r, |o| o == core::cmp::Ordering::Less),
1171 BinaryOp::Gt => cmp(&l, &r, |o| o == core::cmp::Ordering::Greater),
1172 BinaryOp::LtEq => cmp(&l, &r, |o| o != core::cmp::Ordering::Greater),
1173 BinaryOp::GtEq => cmp(&l, &r, |o| o != core::cmp::Ordering::Less),
1174 BinaryOp::BitAnd => {
1175 Value::Number((to_i32(l.to_number()) & to_i32(r.to_number())) as f64)
1176 }
1177 BinaryOp::BitOr => {
1178 Value::Number((to_i32(l.to_number()) | to_i32(r.to_number())) as f64)
1179 }
1180 BinaryOp::BitXor => {
1181 Value::Number((to_i32(l.to_number()) ^ to_i32(r.to_number())) as f64)
1182 }
1183 BinaryOp::Shl => Value::Number(
1184 (to_i32(l.to_number()).wrapping_shl(to_u32(r.to_number()) & 31)) as f64,
1185 ),
1186 BinaryOp::Shr => Value::Number(
1187 (to_i32(l.to_number()).wrapping_shr(to_u32(r.to_number()) & 31)) as f64,
1188 ),
1189 BinaryOp::UShr => Value::Number(
1190 ((to_u32(l.to_number())).wrapping_shr(to_u32(r.to_number()) & 31)) as f64,
1191 ),
1192 BinaryOp::In => {
1193 let key = l.to_js_string();
1194 match &r {
1195 Value::Object(o) => {
1196 if super::super::builtins::is_proxy_or_has_proxy_proto(o) {
1197 return Ok(Value::Bool(super::super::builtins::pm_has_property(self, o, &key)?));
1198 }
1199 if self.has_property_chain(o, &key) {
1200 return Ok(Value::Bool(true));
1201 }
1202 if let Some(h) = o.borrow().kind.host_ref() {
1206 if !matches!(
1207 (self.host.own)(self, h, &key),
1208 crate::os_lib::ecma::host::HostOwn::Generic
1209 | crate::os_lib::ecma::host::HostOwn::Absent
1210 ) {
1211 return Ok(Value::Bool(true));
1212 }
1213 if (self.host.resolves_on_read)(h) {
1214 let v = self.get_property(&r, &key)?;
1215 return Ok(Value::Bool(!matches!(v, Value::Undefined)));
1216 }
1217 }
1218 Value::Bool(false)
1219 }
1220 _ => {
1222 return Err(self.type_error(alloc::format!(
1223 "Cannot use 'in' operator to search for '{}' in {}",
1224 key,
1225 r.to_js_string()
1226 )));
1227 }
1228 }
1229 }
1230 BinaryOp::InstanceOf => self.instance_of(&l, &r)?,
1231 })
1232 }
1233
1234 pub(crate) fn instance_of(&mut self, l: &Value, r: &Value) -> EvalResult {
1237 let ctor = match r {
1238 Value::Object(o) if o.borrow().is_callable() => o.clone(),
1239 _ => return Err(self.type_error("Right-hand side of 'instanceof' is not callable")),
1240 };
1241 let has_instance = ctor.borrow().props.get("Symbol(Symbol.hasInstance)").cloned();
1246 if let Some(f) = has_instance {
1247 if matches!(&f, Value::Object(fo) if fo.borrow().is_callable()) {
1248 let result = self.call_value(&f, Value::Object(ctor), core::slice::from_ref(l))?;
1249 return Ok(Value::Bool(result.truthy()));
1250 }
1251 }
1252 let proto = match Obj::fn_prototype(&ctor) {
1253 Some(Value::Object(p)) => p,
1254 _ => return Ok(Value::Bool(false)),
1255 };
1256 let mut cur = match l {
1257 Value::Object(o) if matches!(o.borrow().kind, ObjKind::Proxy(_)) => {
1258 match super::super::builtins::pm_get_prototype_of(self, o)? {
1259 Value::Object(n) => Some(n),
1260 _ => None,
1261 }
1262 }
1263 Value::Object(o) => {
1264 let host_proto = if o.borrow().proto.is_none() { self.host_proto_of(&o.borrow().kind) } else { None };
1265 match host_proto {
1266 Some(p) => p,
1267 None => super::super::builtins::implicit_proto(self, o),
1268 }
1269 }
1270 _ => None,
1271 };
1272 let mut guard = 0;
1273 while let Some(p) = cur {
1274 if Rc::ptr_eq(&p, &proto) {
1275 return Ok(Value::Bool(true));
1276 }
1277 if matches!(p.borrow().kind, ObjKind::Proxy(_)) {
1279 cur = match super::super::builtins::pm_get_prototype_of(self, &p)? {
1280 Value::Object(n) => Some(n),
1281 _ => None,
1282 };
1283 } else {
1284 cur = super::super::builtins::implicit_proto(self, &p);
1286 }
1287 guard += 1;
1288 if guard > 1000 {
1289 break;
1290 }
1291 }
1292 Ok(Value::Bool(false))
1293 }
1294
1295 pub(crate) fn eval_binary_bigint(&mut self, op: &BinaryOp, l: Value, r: Value) -> EvalResult {
1298 use super::super::bigint::BigInt;
1299 if matches!(op, BinaryOp::Add) && (matches!(l, Value::Str(_)) || matches!(r, Value::Str(_)))
1301 {
1302 return Ok(Value::str(format!(
1303 "{}{}",
1304 l.to_js_string(),
1305 r.to_js_string()
1306 )));
1307 }
1308
1309 match op {
1311 BinaryOp::StrictEq => return Ok(Value::Bool(l.strict_eq(&r))),
1312 BinaryOp::StrictNotEq => return Ok(Value::Bool(!l.strict_eq(&r))),
1313 BinaryOp::Eq => return Ok(Value::Bool(self.bigint_loose_eq(&l, &r))),
1314 BinaryOp::NotEq => return Ok(Value::Bool(!self.bigint_loose_eq(&l, &r))),
1315 BinaryOp::Lt | BinaryOp::Gt | BinaryOp::LtEq | BinaryOp::GtEq => {
1316 return Ok(self.bigint_compare(op, &l, &r));
1317 }
1318 BinaryOp::In | BinaryOp::InstanceOf => {
1319 return Ok(Value::Bool(false));
1320 }
1321 _ => {}
1322 }
1323
1324 let (a, b) = match (&l, &r) {
1326 (Value::BigInt(a), Value::BigInt(b)) => (a.clone(), b.clone()),
1327 _ => {
1328 return Err(
1329 self.type_error("Cannot mix BigInt and other types, use explicit conversions")
1330 );
1331 }
1332 };
1333 let result: BigInt = match op {
1334 BinaryOp::Add => a.add(&b),
1335 BinaryOp::Sub => a.sub(&b),
1336 BinaryOp::Mul => a.mul(&b),
1337 BinaryOp::Div => match a.div(&b) {
1338 Some(v) => v,
1339 None => return Err(self.range_error("Division by zero")),
1340 },
1341 BinaryOp::Mod => match a.rem(&b) {
1342 Some(v) => v,
1343 None => return Err(self.range_error("Division by zero")),
1344 },
1345 BinaryOp::Pow => match a.pow(&b) {
1346 Some(v) => v,
1347 None => return Err(self.range_error("Exponent must be non-negative")),
1348 },
1349 BinaryOp::BitAnd => a.bitand(&b),
1350 BinaryOp::BitOr => a.bitor(&b),
1351 BinaryOp::BitXor => a.bitxor(&b),
1352 BinaryOp::Shl => {
1353 match shift_amount(&b) {
1355 Some(n) => a.shl(n),
1356 None => return Err(self.range_error("BigInt shift amount out of range")),
1357 }
1358 }
1359 BinaryOp::Shr => match shift_amount(&b) {
1360 Some(n) => a.shr(n),
1361 None => return Err(self.range_error("BigInt shift amount out of range")),
1362 },
1363 BinaryOp::UShr => {
1364 return Err(self.type_error("BigInts have no unsigned right shift, use >> instead"));
1366 }
1367 _ => return Err(self.type_error("Unsupported BigInt operation")),
1368 };
1369 Ok(Value::bigint(result))
1370 }
1371
1372 pub(crate) fn bigint_loose_eq(&self, l: &Value, r: &Value) -> bool {
1374 use core::cmp::Ordering;
1375 match (l, r) {
1376 (Value::BigInt(a), Value::BigInt(b)) => a.cmp(b) == Ordering::Equal,
1377 (Value::BigInt(a), Value::Number(n)) | (Value::Number(n), Value::BigInt(a)) => {
1378 n.is_finite() && a.to_f64() == *n
1379 }
1380 (Value::BigInt(a), Value::Str(s)) | (Value::Str(s), Value::BigInt(a)) => {
1381 match super::super::bigint::BigInt::parse_str(s) {
1382 Some(bi) => a.cmp(&bi) == Ordering::Equal,
1383 None => false,
1384 }
1385 }
1386 (Value::BigInt(a), Value::Bool(bb)) | (Value::Bool(bb), Value::BigInt(a)) => {
1387 a.to_f64() == if *bb { 1.0 } else { 0.0 }
1388 }
1389 _ => false,
1390 }
1391 }
1392
1393 pub(crate) fn bigint_compare(&self, op: &BinaryOp, l: &Value, r: &Value) -> Value {
1395 use core::cmp::Ordering;
1396 let ord: Option<Ordering> = match (l, r) {
1397 (Value::BigInt(a), Value::BigInt(b)) => Some(a.cmp(b)),
1398 (Value::BigInt(a), _) => {
1399 let x = a.to_f64();
1400 let y = r.to_number();
1401 cmp_f64(x, y)
1402 }
1403 (_, Value::BigInt(b)) => {
1404 let x = l.to_number();
1405 let y = b.to_f64();
1406 cmp_f64(x, y)
1407 }
1408 _ => None,
1409 };
1410 let ord = match ord {
1411 Some(o) => o,
1412 None => return Value::Bool(false),
1413 };
1414 let res = match op {
1415 BinaryOp::Lt => ord == Ordering::Less,
1416 BinaryOp::Gt => ord == Ordering::Greater,
1417 BinaryOp::LtEq => ord != Ordering::Greater,
1418 BinaryOp::GtEq => ord != Ordering::Less,
1419 _ => false,
1420 };
1421 Value::Bool(res)
1422 }
1423
1424}