Skip to content

Commit

Permalink
Handle null/undefined in OP_NEXTITER rather than creating empty itera…
Browse files Browse the repository at this point in the history
…tor.

Only create an iterator for coercible types in OP_ITERATOR, and then
detect the lack of a real iterator in OP_NEXTITER.
Thus we don't need to allocate and push an empty iterator object for
these cases.
  • Loading branch information
ccxvii committed Jan 7, 2019
1 parent 7be32a0 commit bd9920c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
8 changes: 0 additions & 8 deletions jsproperty.c
Expand Up @@ -252,14 +252,6 @@ static js_Iterator *itflatten(js_State *J, js_Object *obj)
return iter;
}

js_Object *jsV_emptyiterator(js_State *J)
{
js_Object *io = jsV_newobject(J, JS_CITERATOR, NULL);
io->u.iter.target = NULL;
io->u.iter.head = NULL;
return io;
}

js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own)
{
char buf[32];
Expand Down
24 changes: 14 additions & 10 deletions jsrun.c
Expand Up @@ -1469,20 +1469,24 @@ static void jsR_run(js_State *J, js_Function *F)
break;

case OP_ITERATOR:
if (js_isundefined(J, -1) || js_isnull(J, -1))
obj = jsV_emptyiterator(J);
else
if (js_iscoercible(J, -1)) {
obj = jsV_newiterator(J, js_toobject(J, -1), 0);
js_pop(J, 1);
js_pushobject(J, obj);
js_pop(J, 1);
js_pushobject(J, obj);
}
break;

case OP_NEXTITER:
obj = js_toobject(J, -1);
str = jsV_nextiterator(J, obj);
if (str) {
js_pushliteral(J, str);
js_pushboolean(J, 1);
if (js_isobject(J, -1)) {
obj = js_toobject(J, -1);
str = jsV_nextiterator(J, obj);
if (str) {
js_pushliteral(J, str);
js_pushboolean(J, 1);
} else {
js_pop(J, 1);
js_pushboolean(J, 0);
}
} else {
js_pop(J, 1);
js_pushboolean(J, 0);
Expand Down
1 change: 0 additions & 1 deletion jsvalue.h
Expand Up @@ -173,7 +173,6 @@ js_Property *jsV_setproperty(js_State *J, js_Object *obj, const char *name);
js_Property *jsV_nextproperty(js_State *J, js_Object *obj, const char *name);
void jsV_delproperty(js_State *J, js_Object *obj, const char *name);

js_Object *jsV_emptyiterator(js_State *J);
js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own);
const char *jsV_nextiterator(js_State *J, js_Object *iter);

Expand Down

0 comments on commit bd9920c

Please sign in to comment.