Skip to main content

ezno_checker/features/
objects.rs

1use source_map::SpanWithSource;
2
3use crate::{
4	context::LocalInformation,
5	types::{
6		calling::ThisValue,
7		properties::{PropertyKey, PropertyValue, Publicity},
8		TypeStore,
9	},
10	FunctionId, TypeId,
11};
12
13/// Helper for building objects easy
14// TODO slice indexes
15pub struct ObjectBuilder {
16	pub object: TypeId,
17}
18
19impl ObjectBuilder {
20	pub fn new(
21		prototype: Option<TypeId>,
22		types: &mut TypeStore,
23		position: SpanWithSource,
24		info: &mut LocalInformation,
25	) -> Self {
26		// TODO is_under_dyn bad
27		let is_under_dyn = true;
28		Self { object: info.new_object(prototype, types, position, is_under_dyn) }
29	}
30
31	pub fn append(
32		&mut self,
33		publicity: Publicity,
34		under: PropertyKey<'static>,
35		value: PropertyValue,
36		position: SpanWithSource,
37		info: &mut LocalInformation,
38	) {
39		info.register_property(self.object, publicity, under, value, position);
40	}
41
42	#[must_use]
43	pub fn build_object(self) -> TypeId {
44		self.object
45	}
46}
47
48/// These are objects (`typeof * = "object"`) but have special behavior
49#[derive(Clone, Debug, binary_serialize_derive::BinarySerializable)]
50pub enum SpecialObject {
51	/// Hold state of the runtime
52	Promise {
53		from: FunctionId,
54		position: TypeId,
55	},
56	/// Hold state of the runtime
57	Generator {
58		from: FunctionId,
59		position: TypeId,
60	},
61	/// Needs overrides for calling, getting etc
62	Proxy(Proxy),
63	/// Not a [Constant] as `typeof /hi/ === "object"` and it has state
64	RegularExpression(super::regexp::RegExp),
65	/// This cannot be a regular object because of is because of let mutations
66	Import(super::modules::Exported),
67	/// Yeah here. Also for classes
68	/// TODO not all functions have `ThisValue`
69	Function(FunctionId, ThisValue),
70	Null,
71}
72
73/// Properties of handler called (`over` passed as first argument)
74///
75/// Has traps for `getPrototypeOf()`, `setPrototypeOf()`, `isExtensible()`,
76/// `preventExtensions()`, `getOwnPropertyDescriptor()`, `defineProperty()`, `has()`,
77/// `get()`, `set()`, `deleteProperty()`, `ownKeys()` and function methods
78/// `apply()` and `construct()`
79#[derive(Copy, Clone, Debug, binary_serialize_derive::BinarySerializable)]
80pub struct Proxy {
81	pub over: TypeId,
82	pub handler: TypeId,
83}