Skip to content

Commit fe6f3d5

Browse files
committed
Implement the Iterator.prototype.join() proposal
https://github.com/tc39/proposal-iterator-join
1 parent fced162 commit fe6f3d5

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

quickjs.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45697,6 +45697,69 @@ static JSValue js_iterator_proto_toArray(JSContext *ctx, JSValueConst this_val,
4569745697
return JS_EXCEPTION;
4569845698
}
4569945699

45700+
static JSValue js_iterator_proto_join(JSContext *ctx, JSValueConst this_val,
45701+
int argc, JSValueConst *argv)
45702+
{
45703+
JSValue item, method, sep = JS_UNDEFINED;
45704+
StringBuffer b_s, *b = &b_s;
45705+
JSString *p = NULL;
45706+
bool first;
45707+
int c, done;
45708+
45709+
if (check_iterator(ctx, this_val) < 0)
45710+
return JS_EXCEPTION;
45711+
c = ','; /* default separator */
45712+
if (!JS_IsUndefined(argv[0])) {
45713+
sep = JS_ToString(ctx, argv[0]);
45714+
if (JS_IsException(sep)) {
45715+
JS_IteratorClose(ctx, this_val, /*is_exception_pending*/true);
45716+
return JS_EXCEPTION;
45717+
}
45718+
p = JS_VALUE_GET_STRING(sep);
45719+
if (p->len == 1 && !p->is_wide_char)
45720+
c = str8(p)[0];
45721+
else
45722+
c = -1;
45723+
}
45724+
method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
45725+
if (JS_IsException(method)) {
45726+
JS_FreeValue(ctx, sep);
45727+
return JS_EXCEPTION;
45728+
}
45729+
string_buffer_init(ctx, b, 0);
45730+
for (first = true; /*empty*/; first = false) {
45731+
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
45732+
if (JS_IsException(item))
45733+
goto fail;
45734+
if (done)
45735+
break;
45736+
if (!first) {
45737+
if (c >= 0)
45738+
string_buffer_putc8(b, c);
45739+
else
45740+
string_buffer_concat(b, p, 0, p->len);
45741+
}
45742+
/* nullish values contribute the empty string */
45743+
if (JS_IsNull(item) || JS_IsUndefined(item)) {
45744+
JS_FreeValue(ctx, item);
45745+
continue;
45746+
}
45747+
if (string_buffer_concat_value_free(b, item))
45748+
goto close;
45749+
}
45750+
JS_FreeValue(ctx, method);
45751+
JS_FreeValue(ctx, sep);
45752+
return string_buffer_end(b);
45753+
45754+
close:
45755+
JS_IteratorClose(ctx, this_val, /*is_exception_pending*/true);
45756+
fail:
45757+
string_buffer_free(b);
45758+
JS_FreeValue(ctx, method);
45759+
JS_FreeValue(ctx, sep);
45760+
return JS_EXCEPTION;
45761+
}
45762+
4570045763
static JSValue js_async_dispose_to_undef(JSContext *ctx, JSValueConst this_val,
4570145764
int argc, JSValueConst *argv,
4570245765
int magic, JSValueConst *func_data);
@@ -46041,6 +46104,7 @@ static const JSCFunctionListEntry js_iterator_proto_funcs[] = {
4604146104
JS_CFUNC_MAGIC_DEF("find", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_FIND),
4604246105
JS_CFUNC_MAGIC_DEF("forEach", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_FOR_EACH ),
4604346106
JS_CFUNC_MAGIC_DEF("some", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_SOME ),
46107+
JS_CFUNC_DEF("join", 1, js_iterator_proto_join ),
4604446108
JS_CFUNC_DEF("reduce", 1, js_iterator_proto_reduce ),
4604546109
JS_CFUNC_DEF("toArray", 0, js_iterator_proto_toArray ),
4604646110
JS_CFUNC_DEF("[Symbol.dispose]", 0, js_iterator_proto_dispose ),

test262.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Intl.NumberFormat-v3=skip
141141
Intl.RelativeTimeFormat=skip
142142
Intl.Segmenter=skip
143143
IsHTMLDDA
144-
Iterator.prototype.join=skip
144+
Iterator.prototype.join
145145
iterator-chunking=skip
146146
iterator-helpers
147147
iterator-includes=skip

0 commit comments

Comments
 (0)