Commit e76e44f0 authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

[Intl] Add "position" to the return of next()

Add position to the return of %SegmentIterator%.next()
which newly added to the spec in
https://github.com/tc39/proposal-intl-segmenter/pull/42

Bug: v8:8305
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I8de7102acb670a6c529ab3e35601c78a8dc7703c
Reviewed-on: https://chromium-review.googlesource.com/c/1278636Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Reviewed-by: 's avatarDaniel Ehrenberg <littledan@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56963}
parent 5d90d70a
......@@ -143,33 +143,6 @@ Handle<Object> JSSegmentIterator::Position(
return isolate->factory()->NewNumberFromInt(icu_break_iterator->current());
}
namespace {
Handle<JSReceiver> CreateResultForNext(Isolate* isolate, Handle<String> segment,
Handle<Object> break_type) {
Factory* factory = isolate->factory();
// Step 10-13 of %SegmentIteratorPrototype%.next( )
// ecma402 #sec-segment-iterator-prototype-next
// 10. Let result be ! ObjectCreate(%ObjectPrototype%).
Handle<JSObject> result = factory->NewJSObject(isolate->object_function());
// 11. Perform ! CreateDataProperty(result "segment", segment).
CHECK(JSReceiver::CreateDataProperty(
isolate, result, factory->segment_string(), segment, kDontThrow)
.FromJust());
// 12. Perform ! CreateDataProperty(result, "breakType", breakType).
CHECK(JSReceiver::CreateDataProperty(isolate, result,
factory->breakType_string(), break_type,
kDontThrow)
.FromJust());
// 13. Return CreateIterResultObject(result, false).
return factory->NewJSIteratorResult(result, false);
}
} // namespace
// ecma402 #sec-segment-iterator-prototype-next
MaybeHandle<JSReceiver> JSSegmentIterator::Next(
Isolate* isolate, Handle<JSSegmentIterator> segment_iterator) {
......@@ -185,6 +158,8 @@ MaybeHandle<JSReceiver> JSSegmentIterator::Next(
return factory->NewJSIteratorResult(isolate->factory()->undefined_value(),
true);
}
// 6. Let newPosition be iterator.[[SegmentIteratorPosition]].
Handle<Object> new_position = factory->NewNumberFromInt(position);
// 8. Let segment be the substring of string from previousPosition to
// newPosition, inclusive of previousPosition and exclusive of newPosition.
......@@ -192,8 +167,32 @@ MaybeHandle<JSReceiver> JSSegmentIterator::Next(
ASSIGN_RETURN_ON_EXCEPTION(
isolate, segment, segment_iterator->GetSegment(isolate, prev, position),
JSReceiver);
// 9. Let breakType be iterator.[[SegmentIteratorBreakType]].
Handle<Object> break_type = segment_iterator->BreakType();
return CreateResultForNext(isolate, segment, break_type);
// 10. Let result be ! ObjectCreate(%ObjectPrototype%).
Handle<JSObject> result = factory->NewJSObject(isolate->object_function());
// 11. Perform ! CreateDataProperty(result "segment", segment).
CHECK(JSReceiver::CreateDataProperty(
isolate, result, factory->segment_string(), segment, kDontThrow)
.FromJust());
// 12. Perform ! CreateDataProperty(result, "breakType", breakType).
CHECK(JSReceiver::CreateDataProperty(isolate, result,
factory->breakType_string(), break_type,
kDontThrow)
.FromJust());
// 13. Perform ! CreateDataProperty(result, "position", newPosition).
CHECK(JSReceiver::CreateDataProperty(isolate, result,
factory->position_string(), new_position,
kDontThrow)
.FromJust());
// 14. Return CreateIterResultObject(result, false).
return factory->NewJSIteratorResult(result, false);
}
// ecma402 #sec-segment-iterator-prototype-following
......
......@@ -25,12 +25,16 @@ for (const text of [
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertEquals(undefined, v.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
......@@ -73,3 +73,19 @@ for (let rec of otherReceivers) {
assertThrows(() => prototype.following.call(rec), TypeError);
assertThrows(() => prototype.preceding.call(rec), TypeError);
}
// Check the property of the return object of next()
let nextReturn = segmentIterator.next();
function checkProperty(obj, property) {
let desc = Object.getOwnPropertyDescriptor(obj, property);
assertTrue(desc.writable);
assertTrue(desc.enumerable);
assertTrue(desc.configurable);
}
checkProperty(nextReturn, 'done');
checkProperty(nextReturn, 'value');
checkProperty(nextReturn.value, 'segment');
checkProperty(nextReturn.value, 'breakType');
checkProperty(nextReturn.value, 'position');
......@@ -25,12 +25,16 @@ for (const text of [
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertTrue(["soft", "hard"].includes(iter.breakType), iter.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
}
assertEquals(text, segments.join(''));
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
......@@ -25,12 +25,16 @@ for (const text of [
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertTrue(["sep", "term"].includes(iter.breakType), iter.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
......@@ -25,12 +25,16 @@ for (const text of [
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertTrue(["word", "none"].includes(iter.breakType), iter.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment