changeset 33:cb058fac14fa

Added an enumerate() function to the TCB global.
author Atul Varma <varmaa@toolness.com>
date Mon, 22 Jun 2009 14:39:04 -0700
parents bda50240b295
children afca92a2e72e
files spidermonkey-playground.cpp
diffstat 1 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/spidermonkey-playground.cpp	Mon Jun 22 13:32:03 2009 -0700
+++ b/spidermonkey-playground.cpp	Mon Jun 22 14:39:04 2009 -0700
@@ -136,6 +136,26 @@
   return JS_TRUE;
 }
 
+// This native JS function is a wrapper for JS_Enumerate().
+
+static JSBool enumerate(JSContext *cx, JSObject *obj, uintN argc,
+                        jsval *argv, jsval *rval)
+{
+  JSObject *target;
+
+  if (!JS_ConvertArguments(cx, argc, argv, "o", &target))
+    return JS_FALSE;
+
+  JSIdArray *ids = JS_Enumerate(cx, target);
+
+  if (ids == NULL)
+    return JS_FALSE;
+
+  JSObject *array = JS_NewArrayObject(cx, ids->length, ids->vector);
+  *rval = OBJECT_TO_JSVAL(array);
+  return JS_TRUE;
+}
+
 // This native JS function looks up the property of an object, bypassing
 // security checks and getters/setters.
 
@@ -347,6 +367,7 @@
   JS_FS("require",        require,        2, 0, 0),
   JS_FS("lookupProperty", lookupProperty, 2, 0, 0),
   JS_FS("functionInfo",   functionInfo,   1, 0, 0),
+  JS_FS("enumerate",      enumerate,      1, 0, 0),
   JS_FS_END
 };