Skip to content

Commit

Permalink
feat: regexp feature
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Oct 18, 2024
1 parent fb83620 commit cffd6c1
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 49 deletions.
14 changes: 12 additions & 2 deletions nova_vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,26 @@ unicode-normalization = { workspace = true }
wtf8 = { workspace = true }

[features]
default = ["math", "json", "date", "array-buffer", "shared-array-buffer", "weak-refs", "atomics"]
default = [
"math",
"json",
"date",
"array-buffer",
"shared-array-buffer",
"weak-refs",
"atomics",
"regexp"
]
array-buffer = []
atomics = ["array-buffer", "shared-array-buffer"]
date = []
interleaved-gc = []
json = ["sonic-rs"]
math = []
shared-array-buffer = []
typescript = []
weak-refs = []
regexp = []
typescript = []

[build-dependencies]
small_string = { path = "../small_string" }
1 change: 1 addition & 0 deletions nova_vm/src/ecmascript/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(crate) mod primitive_objects;
pub(crate) mod promise;
pub(crate) mod proxy;
pub(crate) mod reflection;
#[cfg(feature = "regexp")]
pub(crate) mod regexp;
pub(crate) mod set;
#[cfg(feature = "shared-array-buffer")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl ObjectPrototype {
// TODO: Check for [[Call]] slot of EmbedderObject
Value::EmbedderObject(_) => todo!(),
// 13. Else if O has a [[RegExpMatcher]] internal slot, let builtinTag be "RegExp".
#[cfg(feature = "regexp")]
Value::RegExp(_) => Ok(BUILTIN_STRING_MEMORY._object_RegExp_.into_value()),
Value::PrimitiveObject(idx) => match &agent[idx].data {
PrimitiveObjectData::Boolean(_) => {
Expand Down
5 changes: 4 additions & 1 deletion nova_vm/src/ecmascript/builtins/ordinary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use crate::{

#[cfg(feature = "date")]
use super::date::data::DateHeapData;
#[cfg(feature = "regexp")]
use super::regexp::RegExpHeapData;
#[cfg(feature = "shared-array-buffer")]
use super::shared_array_buffer::data::SharedArrayBufferHeapData;
use super::{
Expand All @@ -40,7 +42,6 @@ use super::{
map::data::MapHeapData,
primitive_objects::PrimitiveObjectHeapData,
promise::data::PromiseHeapData,
regexp::RegExpHeapData,
set::data::SetHeapData,
ArrayHeapData,
};
Expand Down Expand Up @@ -947,6 +948,7 @@ pub(crate) fn ordinary_object_create_with_intrinsics(
.create(MapIteratorHeapData::default())
.into_object(),
ProtoIntrinsics::Promise => agent.heap.create(PromiseHeapData::default()).into_object(),
#[cfg(feature = "regexp")]
ProtoIntrinsics::RegExp => agent.heap.create(RegExpHeapData::default()).into_object(),
ProtoIntrinsics::Set => agent.heap.create(SetHeapData::default()).into_object(),
ProtoIntrinsics::SetIterator => agent
Expand Down Expand Up @@ -1097,6 +1099,7 @@ pub(crate) fn get_prototype_from_constructor(
ProtoIntrinsics::Promise => Some(intrinsics.promise().into_function()),
ProtoIntrinsics::RangeError => Some(intrinsics.range_error().into_function()),
ProtoIntrinsics::ReferenceError => Some(intrinsics.reference_error().into_function()),
#[cfg(feature = "regexp")]
ProtoIntrinsics::RegExp => Some(intrinsics.reg_exp().into_function()),
ProtoIntrinsics::Set => Some(intrinsics.set().into_function()),
ProtoIntrinsics::SetIterator => None,
Expand Down
2 changes: 1 addition & 1 deletion nova_vm/src/ecmascript/builtins/text_processing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#[cfg(feature = "regexp")]
pub(crate) mod regexp_objects;
pub(crate) mod string_objects;
27 changes: 15 additions & 12 deletions nova_vm/src/ecmascript/execution/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,17 +811,19 @@ pub(crate) fn set_default_global_bindings(
define_property_or_throw(agent, global, name, desc)?;

// 19.3.26 RegExp ( . . . )
let name = PropertyKey::from(BUILTIN_STRING_MEMORY.RegExp);
let value = agent.get_realm(realm_id).intrinsics().reg_exp();
let desc = PropertyDescriptor {
value: Some(value.into_value()),
writable: Some(true),
enumerable: Some(false),
configurable: Some(true),
..Default::default()
};
define_property_or_throw(agent, global, name, desc)?;

#[cfg(feature = "regexp")]
{
let name = PropertyKey::from(BUILTIN_STRING_MEMORY.RegExp);
let value = agent.get_realm(realm_id).intrinsics().reg_exp();
let desc = PropertyDescriptor {
value: Some(value.into_value()),
writable: Some(true),
enumerable: Some(false),
configurable: Some(true),
..Default::default()
};
define_property_or_throw(agent, global, name, desc)?;
}
// 19.3.27 Set ( . . . )
let name = PropertyKey::from(BUILTIN_STRING_MEMORY.Set);
let value = agent.get_realm(realm_id).intrinsics().set();
Expand Down Expand Up @@ -1127,7 +1129,7 @@ pub(crate) fn initialize_default_realm(agent: &mut Agent) {

#[cfg(test)]
mod test {

#[allow(unused_imports)]
use crate::heap::{
IntrinsicConstructorIndexes, IntrinsicFunctionIndexes, IntrinsicObjectIndexes,
LAST_INTRINSIC_CONSTRUCTOR_INDEX, LAST_INTRINSIC_FUNCTION_INDEX,
Expand Down Expand Up @@ -1175,6 +1177,7 @@ mod test {
}

#[test]
#[cfg(feature = "regexp")]
fn test_default_realm_sanity() {
use super::initialize_default_realm;
use crate::ecmascript::execution::{agent::Options, Agent, DefaultHostHooks};
Expand Down
33 changes: 23 additions & 10 deletions nova_vm/src/ecmascript/execution/realm/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ use crate::ecmascript::builtins::structured_data::shared_array_buffer_objects::{
shared_array_buffer_constructor::SharedArrayBufferConstructor,
shared_array_buffer_prototype::SharedArrayBufferPrototype,
};
#[cfg(feature = "regexp")]
use crate::ecmascript::builtins::text_processing::regexp_objects::{
regexp_constructor::RegExpConstructor, regexp_prototype::RegExpPrototype,
regexp_string_iterator_prototype::RegExpStringIteratorPrototype,
};
#[cfg(feature = "array-buffer")]
use crate::ecmascript::builtins::{
indexed_collections::typed_array_objects::{
Expand Down Expand Up @@ -98,16 +103,10 @@ use crate::{
},
primitive_objects::PrimitiveObject,
reflection::{proxy_constructor::ProxyConstructor, reflect_object::ReflectObject},
text_processing::{
regexp_objects::{
regexp_constructor::RegExpConstructor, regexp_prototype::RegExpPrototype,
regexp_string_iterator_prototype::RegExpStringIteratorPrototype,
},
string_objects::{
string_constructor::StringConstructor,
string_iterator_objects::StringIteratorPrototype,
string_prototype::StringPrototype,
},
text_processing::string_objects::{
string_constructor::StringConstructor,
string_iterator_objects::StringIteratorPrototype,
string_prototype::StringPrototype,
},
Array, BuiltinFunction,
},
Expand Down Expand Up @@ -204,6 +203,7 @@ pub enum ProtoIntrinsics {
Promise,
RangeError,
ReferenceError,
#[cfg(feature = "regexp")]
RegExp,
Set,
SetIterator,
Expand Down Expand Up @@ -289,8 +289,11 @@ impl Intrinsics {
StringPrototype::create_intrinsic(agent, realm);
StringConstructor::create_intrinsic(agent, realm);
StringIteratorPrototype::create_intrinsic(agent, realm);
#[cfg(feature = "regexp")]
RegExpPrototype::create_intrinsic(agent, realm);
#[cfg(feature = "regexp")]
RegExpConstructor::create_intrinsic(agent, realm);
#[cfg(feature = "regexp")]
RegExpStringIteratorPrototype::create_intrinsic(agent, realm);
ArrayPrototype::create_intrinsic(agent, realm);
ArrayConstructor::create_intrinsic(agent, realm);
Expand Down Expand Up @@ -411,6 +414,7 @@ impl Intrinsics {
ProtoIntrinsics::Map => self.map_prototype().into(),
ProtoIntrinsics::MapIterator => self.map_iterator_prototype().into(),
ProtoIntrinsics::Promise => self.promise_prototype().into(),
#[cfg(feature = "regexp")]
ProtoIntrinsics::RegExp => self.reg_exp_prototype().into(),
ProtoIntrinsics::Set => self.set_prototype().into(),
ProtoIntrinsics::SetIterator => self.set_iterator_prototype().into(),
Expand Down Expand Up @@ -1211,31 +1215,36 @@ impl Intrinsics {
}

/// %RegExp.prototype.exec%
#[cfg(feature = "regexp")]
pub(crate) fn reg_exp_prototype_exec(&self) -> BuiltinFunction {
IntrinsicFunctionIndexes::RegExpPrototypeExec
.get_builtin_function_index(self.builtin_function_index_base)
.into()
}

/// %RegExp.prototype%
#[cfg(feature = "regexp")]
pub(crate) fn reg_exp_prototype(&self) -> OrdinaryObject {
IntrinsicObjectIndexes::RegExpPrototype
.get_object_index(self.object_index_base)
.into()
}

/// %RegExp%
#[cfg(feature = "regexp")]
pub(crate) fn reg_exp(&self) -> BuiltinFunction {
IntrinsicConstructorIndexes::RegExp
.get_builtin_function_index(self.builtin_function_index_base)
.into()
}

#[cfg(feature = "regexp")]
pub(crate) fn reg_exp_base_object(&self) -> ObjectIndex {
IntrinsicConstructorIndexes::RegExp.get_object_index(self.object_index_base)
}

/// %RegExpStringIteratorPrototype%
#[cfg(feature = "regexp")]
pub(crate) fn reg_exp_string_iterator_prototype(&self) -> OrdinaryObject {
IntrinsicObjectIndexes::RegExpStringIteratorPrototype
.get_object_index(self.object_index_base)
Expand Down Expand Up @@ -1709,9 +1718,13 @@ impl HeapMarkAndSweep for Intrinsics {
self.reference_error_prototype().mark_values(queues);
self.reference_error().mark_values(queues);
self.reflect().mark_values(queues);
#[cfg(feature = "regexp")]
self.reg_exp_prototype_exec().mark_values(queues);
#[cfg(feature = "regexp")]
self.reg_exp_prototype().mark_values(queues);
#[cfg(feature = "regexp")]
self.reg_exp().mark_values(queues);
#[cfg(feature = "regexp")]
self.reg_exp_string_iterator_prototype().mark_values(queues);
self.set_prototype_values().mark_values(queues);
self.set_prototype().mark_values(queues);
Expand Down
Loading

0 comments on commit cffd6c1

Please sign in to comment.