Skip to content

Commit 600819f

Browse files
authored
feat(ecmascript): String Objects (#112)
* feat(ecmascript): String Objects * fix clippy * fix typo * fix missing file * stiilll fix wrong file * feat(ecmascript): RegExp Objects * chore: Move RegExpHeapData into ecmascript * chore: Rename vestigal heap/object.rs into object_entry.rs and remove unused functions
1 parent 94cfad5 commit 600819f

File tree

28 files changed

+1338
-410
lines changed

28 files changed

+1338
-410
lines changed

nova_vm/src/builtin_strings

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
[object String]
1010
[object Undefined]
1111
[Symbol.hasInstance]
12+
[Symbol.iterator]
13+
[Symbol.match]
14+
[Symbol.matchAll]
15+
[Symbol.replace]
16+
[Symbol.search]
17+
[Symbol.split]
1218
[Symbol.toPrimitive]
1319
abs
1420
acos
@@ -42,7 +48,11 @@ caller
4248
cause
4349
cbrt
4450
ceil
51+
charAt
52+
charCodeAt
4553
clz32
54+
codePointAt
55+
concat
4656
constructor
4757
copyWithin
4858
cos
@@ -55,11 +65,14 @@ defineProperties
5565
defineProperty
5666
description
5767
done
68+
dotAll
5869
E
70+
endsWith
5971
entries
6072
EPSILON
6173
Error
6274
EvalError
75+
exec
6376
exp
6477
expm1
6578
false
@@ -69,20 +82,33 @@ find
6982
findIndex
7083
findLast
7184
findLastIndex
85+
flags
7286
flat
7387
flatMap
7488
Float32Array
7589
Float64Array
7690
floor
7791
for
7892
freeze
93+
fromCharCode
94+
fromCodePoint
7995
fromEntries
8096
fround
8197
function
8298
Function
8399
get [Symbol.species]
84100
get byteLength
85101
get description
102+
get dotAll
103+
get flags
104+
get global
105+
get hasIndices
106+
get ignoreCase
107+
get multiline
108+
get source
109+
get stricky
110+
get unicode
111+
get unicodeSets
86112
getDate
87113
getDay
88114
getFullYear
@@ -106,14 +132,18 @@ getUTCMilliseconds
106132
getUTCMinutes
107133
getUTCMonth
108134
getUTCSeconds
135+
global
109136
globalThis
110137
groupBy
138+
hasIndices
111139
hasInstance
112140
hasOwn
113141
hasOwnProperty
114142
hypot
143+
ignoreCase
115144
imul
116145
includes
146+
indexOf
117147
Infinity
118148
Int16Array
119149
Int32Array
@@ -128,12 +158,15 @@ isNaN
128158
isPrototypeOf
129159
isSafeInteger
130160
isSealed
161+
isWellFormed
131162
iterator
132163
keyFor
133164
keys
165+
lastIndexOf
134166
length
135167
LN10
136168
LN2
169+
localeCompare
137170
log
138171
log10
139172
LOG10E
@@ -152,16 +185,20 @@ message
152185
min
153186
MIN_SAFE_INTEGER
154187
MIN_VALUE
188+
multiline
155189
name
156190
NaN
157191
NEGATIVE_INFINITY
158192
next
193+
normalize
159194
now
160195
null
161196
number
162197
Number
163198
object
164199
Object
200+
padEnd
201+
padStart
165202
parse
166203
parseFloat
167204
parseInt
@@ -175,9 +212,13 @@ prototype
175212
Proxy
176213
random
177214
RangeError
215+
raw
178216
ReferenceError
179217
RegExp
218+
RegExp String Iterator
219+
repeat
180220
replace
221+
replaceAll
181222
round
182223
seal
183224
search
@@ -202,26 +243,35 @@ SharedArrayBuffer
202243
sign
203244
sin
204245
sinh
246+
slice
247+
source
205248
species
206249
split
207250
sqrt
208251
SQRT1_2
209252
SQRT2
253+
startsWith
254+
stricky
210255
string
211256
String
257+
String Iterator
212258
symbol
213259
Symbol
214260
SyntaxError
215261
tan
216262
tanh
263+
test
217264
toDateString
218265
toExponential
219266
toFixed
220267
toISOString
221268
toJSON
222269
toLocaleDateString
270+
toLocaleLowerCase
223271
toLocaleString
224272
toLocaleTimeString
273+
toLocaleUpperCase
274+
toLowerCase
225275
toPrecision
226276
toPrimitive
227277
toReversed
@@ -230,7 +280,12 @@ toSpliced
230280
toString
231281
toStringTag
232282
toTimeString
283+
toUpperCase
233284
toUTCString
285+
toWellFormed
286+
trim
287+
trimEnd
288+
trimStart
234289
true
235290
trunc
236291
TypeError
@@ -239,6 +294,8 @@ Uint32Array
239294
Uint8Array
240295
Uint8ClampedArray
241296
undefined
297+
unicode
298+
unicodeSets
242299
unscopables
243300
URIError
244301
utc

nova_vm/src/ecmascript/builders/builtin_function_builder.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::property_builder::{self, PropertyBuilder};
1919
pub struct NoPrototype;
2020

2121
#[derive(Clone, Copy)]
22-
pub struct CreatorPrototype(Object);
22+
pub struct CreatorPrototype(Option<Object>);
2323

2424
#[derive(Default, Clone, Copy)]
2525
pub struct NoLength;
@@ -162,7 +162,30 @@ impl<'agent, L, N, B, Pr> BuiltinFunctionBuilder<'agent, NoPrototype, L, N, B, P
162162
this: self.this,
163163
object_index,
164164
realm: self.realm,
165-
prototype: CreatorPrototype(prototype),
165+
prototype: CreatorPrototype(Some(prototype)),
166+
length: self.length,
167+
name: self.name,
168+
behaviour: self.behaviour,
169+
properties: self.properties,
170+
}
171+
}
172+
173+
#[must_use]
174+
pub fn with_null_prototype(
175+
self,
176+
) -> BuiltinFunctionBuilder<'agent, CreatorPrototype, L, N, B, Pr> {
177+
let object_index = if self.object_index.is_none() {
178+
self.agent.heap.objects.push(None);
179+
Some(ObjectIndex::last(&self.agent.heap.objects))
180+
} else {
181+
self.object_index
182+
};
183+
BuiltinFunctionBuilder {
184+
agent: self.agent,
185+
this: self.this,
186+
object_index,
187+
realm: self.realm,
188+
prototype: CreatorPrototype(None),
166189
length: self.length,
167190
name: self.name,
168191
behaviour: self.behaviour,
@@ -518,7 +541,7 @@ impl<'agent>
518541
assert!(slot.is_none());
519542
*slot = Some(ObjectHeapData {
520543
extensible: true,
521-
prototype: Some(prototype.0),
544+
prototype: prototype.0,
522545
keys,
523546
values,
524547
});

nova_vm/src/ecmascript/builtins.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub mod error;
1414
pub(crate) mod fundamental_objects;
1515
pub(crate) mod numbers_and_dates;
1616
pub mod ordinary;
17+
pub(crate) mod regexp;
18+
pub(crate) mod text_processing;
1719

1820
pub(crate) use arguments::*;
1921
pub(crate) use array::abstract_operations::*;

nova_vm/src/ecmascript/builtins/fundamental_objects/function_objects/function_prototype.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ impl FunctionPrototype {
204204
Some(this_object_index),
205205
)
206206
.with_property_capacity(8)
207+
.with_null_prototype()
207208
// 10.2.4 AddRestrictedFunctionProperties ( F, realm )
208209
.with_property(|builder| {
209210
builder

nova_vm/src/ecmascript/builtins/numbers_and_dates/date_objects/date_prototype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl DatePrototype {
577577
let date_constructor = intrinsics.date();
578578

579579
OrdinaryObjectBuilder::new_intrinsic_object(agent, realm, this)
580-
.with_property_capacity(7)
580+
.with_property_capacity(45)
581581
.with_constructor_property(date_constructor)
582582
.with_builtin_function_property::<DatePrototypeGetDate>()
583583
.with_builtin_function_property::<DatePrototypeGetDay>()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use crate::heap::indexes::ObjectIndex;
2+
3+
#[derive(Debug, Clone, Copy)]
4+
pub struct RegExpHeapData {
5+
pub(crate) object_index: ObjectIndex,
6+
// _regex: RegExp,
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub(crate) mod regexp_objects;
2+
pub(crate) mod string_objects;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub(crate) mod regexp_constructor;
2+
pub(crate) mod regexp_prototype;
3+
pub(crate) mod regexp_string_iterator_prototype;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::ecmascript::builders::builtin_function_builder::BuiltinFunctionBuilder;
2+
use crate::ecmascript::builtins::ArgumentsList;
3+
use crate::ecmascript::builtins::Behaviour;
4+
use crate::ecmascript::builtins::Builtin;
5+
use crate::ecmascript::execution::Agent;
6+
use crate::ecmascript::execution::JsResult;
7+
use crate::ecmascript::execution::RealmIdentifier;
8+
use crate::ecmascript::types::IntoFunction;
9+
use crate::ecmascript::types::IntoObject;
10+
use crate::ecmascript::types::Object;
11+
use crate::ecmascript::types::String;
12+
use crate::ecmascript::types::Value;
13+
use crate::ecmascript::types::BUILTIN_STRING_MEMORY;
14+
use crate::heap::WellKnownSymbolIndexes;
15+
16+
pub struct RegExpConstructor;
17+
18+
impl Builtin for RegExpConstructor {
19+
const BEHAVIOUR: Behaviour = Behaviour::Constructor(Self::behaviour);
20+
const LENGTH: u8 = 1;
21+
const NAME: String = BUILTIN_STRING_MEMORY.RegExp;
22+
}
23+
24+
struct RegExpSpecies;
25+
impl Builtin for RegExpSpecies {
26+
const BEHAVIOUR: Behaviour = Behaviour::Regular(RegExpConstructor::species);
27+
const LENGTH: u8 = 0;
28+
const NAME: String = BUILTIN_STRING_MEMORY.get__Symbol_species_;
29+
}
30+
impl RegExpConstructor {
31+
fn behaviour(
32+
_agent: &mut Agent,
33+
_this_value: Value,
34+
_arguments: ArgumentsList,
35+
_new_target: Option<Object>,
36+
) -> JsResult<Value> {
37+
todo!();
38+
}
39+
40+
fn species(
41+
_agent: &mut Agent,
42+
_this_value: Value,
43+
_arguments: ArgumentsList,
44+
) -> JsResult<Value> {
45+
todo!();
46+
}
47+
48+
pub(crate) fn create_intrinsic(agent: &mut Agent, realm: RealmIdentifier) {
49+
let intrinsics = agent.get_realm(realm).intrinsics();
50+
let regexp_prototype = intrinsics.reg_exp_prototype();
51+
let this = intrinsics.reg_exp();
52+
let this_object_index = intrinsics.reg_exp_base_object();
53+
54+
BuiltinFunctionBuilder::new_intrinsic_constructor::<RegExpConstructor>(
55+
agent,
56+
realm,
57+
this,
58+
Some(this_object_index),
59+
)
60+
.with_property_capacity(2)
61+
.with_prototype_property(regexp_prototype.into_object())
62+
.with_property(|builder| {
63+
builder
64+
.with_key(WellKnownSymbolIndexes::Species.into())
65+
.with_getter(|agent| {
66+
BuiltinFunctionBuilder::new::<RegExpSpecies>(agent, realm)
67+
.build()
68+
.into_function()
69+
})
70+
.with_enumerable(false)
71+
.build()
72+
})
73+
.build();
74+
}
75+
}

0 commit comments

Comments
 (0)