shithub: libmujs

Download patch

ref: 9103b4f4de07ea9d2470b6b21da0c0a8b8d5a897
parent: f66986d3d866e321e379dc2e7c176373b9411ef0
author: Tor Andersson <tor@ccxvii.net>
date: Sat Jan 25 12:46:43 EST 2014

Add 'own' argument to iterator.

If set, it will only create an iterator for own properties.
If unset, it will iterate over all properties in the prototype
chain.

--- a/jsproperty.c
+++ b/jsproperty.c
@@ -214,10 +214,16 @@
 	return iter;
 }
 
-js_Object *jsV_newiterator(js_State *J, js_Object *obj)
+js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own)
 {
 	js_Object *iobj = jsV_newobject(J, JS_CITERATOR, NULL);
-	iobj->u.iter = itflatten(J, obj);
+	if (own) {
+		iobj->u.iter = NULL;
+		if (obj->properties != &sentinel)
+			iobj->u.iter = itwalk(J, iobj->u.iter, obj->properties, NULL);
+	} else {
+		iobj->u.iter = itflatten(J, obj);
+	}
 	return iobj;
 }
 
@@ -245,7 +251,7 @@
 	const char *s;
 	unsigned int k;
 	if (newlen < obj->u.a.length) {
-		js_Object *it = jsV_newiterator(J, obj);
+		js_Object *it = jsV_newiterator(J, obj, 1);
 		while ((s = jsV_nextiterator(J, it))) {
 			k = jsV_numbertouint32(jsV_stringtonumber(J, s));
 			if (k >= newlen && !strcmp(s, jsV_numbertostring(J, k)))
--- a/jsrun.c
+++ b/jsrun.c
@@ -833,7 +833,7 @@
 
 		case OP_ITERATOR:
 			if (!js_isundefined(J, -1) && !js_isnull(J, -1)) {
-				obj = jsV_newiterator(J, js_toobject(J, -1));
+				obj = jsV_newiterator(J, js_toobject(J, -1), 0);
 				js_pop(J, 1);
 				js_pushobject(J, obj);
 			}
--- a/jsvalue.h
+++ b/jsvalue.h
@@ -117,7 +117,7 @@
 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_newiterator(js_State *J, js_Object *obj);
+js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own);
 const char *jsV_nextiterator(js_State *J, js_Object *iobj);
 
 void jsV_resizearray(js_State *J, js_Object *obj, unsigned int newlen);