Skip to content

Commit

Permalink
Implement 'd' flag for RegExp
Browse files Browse the repository at this point in the history
Signed-off-by: Seonghyun Kim <[email protected]>
  • Loading branch information
ksh8281 committed Jul 15, 2024
1 parent cbe91e4 commit 7af984c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
14 changes: 8 additions & 6 deletions src/api/EscargotPublic.h
Original file line number Diff line number Diff line change
Expand Up @@ -1721,12 +1721,14 @@ class ESCARGOT_EXPORT RegExpObjectRef : public ObjectRef {
public:
enum RegExpObjectOption {
None = 0 << 0,
Global = 1 << 0,
IgnoreCase = 1 << 1,
MultiLine = 1 << 2,
Sticky = 1 << 3,
Unicode = 1 << 4,
DotAll = 1 << 5,
HasIndices = 1 << 0,
Global = 1 << 1,
IgnoreCase = 1 << 2,
MultiLine = 1 << 3,
DotAll = 1 << 4,
Unicode = 1 << 5,
UnicodeSets = 1 << 6,
Sticky = 1 << 7,
};

struct ESCARGOT_EXPORT RegexMatchResult {
Expand Down
12 changes: 12 additions & 0 deletions src/builtins/BuiltinRegExp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,11 @@ static Value builtinRegExpMultiLineGetter(ExecutionState& state, Value thisValue
return builtinRegExpOptionGetterHelper(state, thisValue, RegExpObject::Option::MultiLine);
}

static Value builtinRegExpHasIndicesGetter(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
return builtinRegExpOptionGetterHelper(state, thisValue, RegExpObject::Option::HasIndices);
}

static Value builtinRegExpSourceGetter(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (!thisValue.isObject()) {
Expand Down Expand Up @@ -887,6 +892,13 @@ void GlobalObject::installRegExp(ExecutionState& state)
m_regexpPrototype->directDefineOwnProperty(state, ObjectPropertyName(state, strings->multiline), desc);
}

{
Value getter = new NativeFunctionObject(state, NativeFunctionInfo(strings->getHasIndices, builtinRegExpHasIndicesGetter, 0, NativeFunctionInfo::Strict));
JSGetterSetter gs(getter, Value());
ObjectPropertyDescriptor desc(gs, ObjectPropertyDescriptor::ConfigurablePresent);
m_regexpPrototype->directDefineOwnProperty(state, ObjectPropertyName(state, strings->hasIndices), desc);
}

{
Value getter = new NativeFunctionObject(state, NativeFunctionInfo(strings->getSource, builtinRegExpSourceGetter, 0, NativeFunctionInfo::Strict));
JSGetterSetter gs(getter, Value());
Expand Down
35 changes: 35 additions & 0 deletions src/runtime/RegExpObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ bool RegExpObject::defineOwnProperty(ExecutionState& state, const ObjectProperty
|| name->equals(state.context()->staticStrings().sticky.string())
|| name->equals(state.context()->staticStrings().dotAll.string())
|| name->equals(state.context()->staticStrings().source.string())
|| name->equals(state.context()->staticStrings().hasIndices.string())
|| name->equals(state.context()->staticStrings().flags.string())) {
m_hasOwnPropertyWhichHasDefinedFromRegExpPrototype = true;
}
Expand Down Expand Up @@ -475,6 +476,40 @@ ArrayObject* RegExpObject::createRegExpMatchedArray(ExecutionState& state, const
}
}

if (option() & HasIndices) {
ArrayObject* indices = new ArrayObject(state, len);
arr->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().indices), ObjectPropertyDescriptor(Value(indices), ObjectPropertyDescriptor::AllPresent));

size_t idx = 0;
for (unsigned i = 0; i < result.m_matchResults.size(); i++) {
for (unsigned j = 0; j < result.m_matchResults[i].size(); j++) {
if (result.m_matchResults[i][j].m_start == std::numeric_limits<unsigned>::max()) {
indices->defineOwnIndexedPropertyWithoutExpanding(state, idx++, Value());
} else {
ArrayObject* pair = new ArrayObject(state, 2);
pair->defineOwnIndexedPropertyWithoutExpanding(state, 0, Value(result.m_matchResults[i][j].m_start));
pair->defineOwnIndexedPropertyWithoutExpanding(state, 1, Value(result.m_matchResults[i][j].m_end));

indices->defineOwnIndexedPropertyWithoutExpanding(state, idx++, Value(pair));
}
}
}

if (m_yarrPattern->m_namedGroupToParenIndices.empty()) {
indices->defineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().groups), ObjectPropertyDescriptor(Value(), ObjectPropertyDescriptor::AllPresent));
} else {
Object* groups = new Object(state, Object::PrototypeIsNull);
for (auto it = m_yarrPattern->m_captureGroupNames.begin(); it != m_yarrPattern->m_captureGroupNames.end(); ++it) {
auto foundMapElement = m_yarrPattern->m_namedGroupToParenIndices.find(*it);
if (foundMapElement != m_yarrPattern->m_namedGroupToParenIndices.end()) {
groups->directDefineOwnProperty(state, ObjectPropertyName(state, it->impl()),
ObjectPropertyDescriptor(indices->getOwnProperty(state, ObjectPropertyName(state, foundMapElement->second[0])).value(state, this), ObjectPropertyDescriptor::AllPresent));
}
}
indices->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().groups), ObjectPropertyDescriptor(Value(groups), ObjectPropertyDescriptor::AllPresent));
}
}

if (m_yarrPattern->m_namedGroupToParenIndices.empty()) {
arr->defineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().groups), ObjectPropertyDescriptor(Value(), ObjectPropertyDescriptor::AllPresent));
} else {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/StaticStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void StaticStrings::initStaticStrings()
INIT_STATIC_STRING(getFlags, "get flags");
INIT_STATIC_STRING(getFormat, "get format");
INIT_STATIC_STRING(getGlobal, "get global");
INIT_STATIC_STRING(getHasIndices, "get hasIndices");
INIT_STATIC_STRING(getHourCycle, "get hourCycle");
INIT_STATIC_STRING(getHourCycles, "get hourCycles");
INIT_STATIC_STRING(getIgnoreCase, "get ignoreCase");
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/StaticStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ namespace Escargot {
F(implements) \
F(imul) \
F(includes) \
F(indices) \
F(index) \
F(indexOf) \
F(input) \
Expand Down Expand Up @@ -1002,6 +1003,7 @@ class StaticStrings {
AtomicString getFlags;
AtomicString getFormat;
AtomicString getGlobal;
AtomicString getHasIndices;
AtomicString getHourCycle;
AtomicString getHourCycles;
AtomicString getIgnoreCase;
Expand Down
21 changes: 0 additions & 21 deletions tools/test/test262/excludelist.orig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -580,19 +580,6 @@
<test id="built-ins/Object/prototype/toString/symbol-tag-set-builtin"><reason>TODO</reason></test>
<test id="built-ins/Object/prototype/toString/symbol-tag-string-builtin"><reason>TODO</reason></test>
<test id="built-ins/Object/seal/proxy-with-defineProperty-handler"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array-element"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array-matched"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array-non-unicode-match"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array-properties"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array-unicode-match"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array-unicode-property-names"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-array-unmatched"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-groups-object"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-groups-object-undefined"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-groups-object-unmatched"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-groups-properties"><reason>TODO</reason></test>
<test id="built-ins/RegExp/match-indices/indices-property"><reason>TODO</reason></test>
<test id="built-ins/RegExp/named-groups/duplicate-names-exec"><reason>TODO</reason></test>
<test id="built-ins/RegExp/named-groups/duplicate-names-match"><reason>TODO</reason></test>
<test id="built-ins/RegExp/named-groups/duplicate-names-match-indices"><reason>TODO</reason></test>
Expand Down Expand Up @@ -775,14 +762,6 @@
<test id="built-ins/RegExp/prototype/exec/duplicate-named-groups-properties"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/exec/duplicate-named-indices-groups-properties"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/exec/u-lastindex-adv"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/cross-realm"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/length"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/name"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/prop-desc"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/this-val-invalid-obj"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/this-val-non-obj"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/this-val-regexp"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/hasIndices/this-val-regexp-prototype"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/source/value-line-terminator"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/unicodeSets/breaking-change-from-u-to-v-01"><reason>TODO</reason></test>
<test id="built-ins/RegExp/prototype/unicodeSets/breaking-change-from-u-to-v-02"><reason>TODO</reason></test>
Expand Down

0 comments on commit 7af984c

Please sign in to comment.