Implement ES6+ String methods, Iterator, Map, Set Object (#43)

Signed-off-by: seonghyun kim <sh8281.kim@samsung.com>
This commit is contained in:
김승현/Tizen Platform Lab(SR)/Engineer/삼성전자 2018-01-15 19:58:01 +09:00 committed by 최영일/Tizen Platform Lab(SR)/Principal Engineer/삼성전자
commit 28f6a51c31
34 changed files with 1999 additions and 23 deletions

View file

@ -106,4 +106,82 @@ ObjectGetResult StringObject::getIndexedProperty(ExecutionState& state, const Va
}
return get(state, ObjectPropertyName(state, property));
}
IteratorObject* StringObject::iterator(ExecutionState& state)
{
return new StringIteratorObject(state, m_primitiveValue);
}
StringIteratorObject::StringIteratorObject(ExecutionState& state, String* s)
: IteratorObject(state)
, m_string(s)
, m_iteratorNextIndex(0)
{
setPrototype(state, state.context()->globalObject()->stringIteratorPrototype());
}
void* StringIteratorObject::operator new(size_t size)
{
static bool typeInited = false;
static GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(StringIteratorObject)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(StringIteratorObject, m_structure));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(StringIteratorObject, m_prototype));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(StringIteratorObject, m_values));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(StringIteratorObject, m_string));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(StringIteratorObject));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
std::pair<Value, bool> StringIteratorObject::advance(ExecutionState& state)
{
// Let s be the value of the [[IteratedString]] internal slot of O.
String* s = m_string;
// If a is undefined, return CreateIterResultObject(undefined, true).
if (s == nullptr) {
return std::make_pair(Value(), true);
}
// Let position be the value of the [[StringIteratorNextIndex]] internal slot of O.
size_t position = m_iteratorNextIndex;
// Let len be the number of elements in s.
auto len = s->length();
// If position ≥ len, then
if (position >= len) {
// Set the value of the [[IteratedString]] internal slot of O to undefined.
m_string = nullptr;
// Return CreateIterResultObject(undefined, true).
return std::make_pair(Value(), true);
}
// Let first be the code unit value at index position in s.
auto first = s->charAt(position);
// If first < 0xD800 or first > 0xDBFF or position+1 = len, let resultString be the string consisting of the single code unit first.
String* resultString;
if (first < 0xD800 || first > 0xDBFF || (position + 1 == len)) {
resultString = String::fromInt32(first);
} else {
// Let second be the code unit value at index position+1 in the String S.
auto second = s->charAt(position + 1);
// If second < 0xDC00 or second > 0xDFFF, let resultString be the string consisting of the single code unit first.
if (second < 0xDC00 || second > 0xDFFF) {
resultString = String::fromInt32(first);
} else {
// Else, let resultString be the string consisting of the code unit first followed by the code unit second.
char16_t s[2] = { first, second };
resultString = new UTF16String(s, 2);
}
}
// Let resultSize be the number of code units in resultString.
auto resultSize = resultString->length();
// Set the value of the [[StringIteratorNextIndex]] internal slot of O to position + resultSize.
m_iteratorNextIndex = position + resultSize;
// Return CreateIterResultObject(resultString, false).
return std::make_pair(Value(resultString), false);
}
}