nanvm_lib/vm/any/instanceof_.rs
1use crate::vm::{Any, IVm, ToAny, Unpacked};
2
3/// The constructors `instanceof` may name: the EDAG's `constructorId`, a
4/// closed list — `Array` alone today, where `Map` and `Set` are one variant
5/// each once the VM has such values. A name, not a value: no global is a
6/// value in the EDAG, so the operator's right side is carried as the word
7/// it is, and a variant here rather than an `Any<A>`.
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum Constructor {
10 Array,
11}
12
13impl<A: IVm> Any<A> {
14 /// `instanceof`, against a built-in constructor. Not a `core::ops`
15 /// trait — Rust has no operator to spell it with — so this is a plain
16 /// method, named as `typeof_` is: a trailing underscore keeps the
17 /// reserved-word spelling recognizable. Never throws: the right side is
18 /// a constructor by construction, so JavaScript's `TypeError` for a
19 /// non-callable right operand has no case here; stays a `Result` to
20 /// match every other operator's shape.
21 /// <https://tc39.es/ecma262/#sec-instanceofoperator>
22 pub fn instanceof_(&self, constructor: Constructor) -> Result<Any<A>, Any<A>> {
23 Ok(match constructor {
24 Constructor::Array => self.with_unpacked(|v| matches!(v, Unpacked::Array(_))),
25 }
26 .to_any())
27 }
28}