Function.isGenerator (bug 648355, r=brendan)
authorDave Herman <dherman@mozilla.com>
Fri, 08 Apr 2011 13:31:14 -0700
changeset 67935 90fa43dcb844d2f2051a3cdfe9ad00d3268d0ff9
parent 67934 6c8becdd1574ed5b05167ec2fa8dfccfd610e208
child 67936 dbecfa4351015d66092cec1185a28c66d9201199
push id19455
push usercleary@mozilla.com
push dateTue, 12 Apr 2011 03:42:33 +0000
treeherdermozilla-central@d208845094bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbrendan
bugs648355
milestone2.2a1pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Function.isGenerator (bug 648355, r=brendan)
js/src/jsfun.cpp
js/src/tests/js1_8_5/extensions/is-generator.js
js/src/tests/js1_8_5/extensions/jstests.list
--- a/js/src/jsfun.cpp
+++ b/js/src/jsfun.cpp
@@ -2331,16 +2331,40 @@ CallOrConstructBoundFunction(JSContext *
         return false;
 
     *vp = args.rval();
     return true;
 }
 
 }
 
+#if JS_HAS_GENERATORS
+static JSBool
+fun_isGenerator(JSContext *cx, uintN argc, Value *vp)
+{
+    JSObject *funobj;
+    if (!IsFunctionObject(vp[1], &funobj)) {
+        JS_SET_RVAL(cx, vp, BooleanValue(false));
+        return true;
+    }
+
+    JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
+
+    bool result = false;
+    if (fun->isInterpreted()) {
+        JSScript *script = fun->u.i.script;
+        JS_ASSERT(script->length != 0);
+        result = script->code[0] == JSOP_GENERATOR;
+    }
+
+    JS_SET_RVAL(cx, vp, BooleanValue(result));
+    return true;
+}
+#endif
+
 /* ES5 15.3.4.5. */
 static JSBool
 fun_bind(JSContext *cx, uintN argc, Value *vp)
 {
     /* Step 1. */
     Value &thisv = vp[1];
 
     /* Step 2. */
@@ -2400,16 +2424,17 @@ fun_bind(JSContext *cx, uintN argc, Valu
 static JSFunctionSpec function_methods[] = {
 #if JS_HAS_TOSOURCE
     JS_FN(js_toSource_str,   fun_toSource,   0,0),
 #endif
     JS_FN(js_toString_str,   fun_toString,   0,0),
     JS_FN(js_apply_str,      js_fun_apply,   2,0),
     JS_FN(js_call_str,       js_fun_call,    1,0),
     JS_FN("bind",            fun_bind,       1,0),
+    JS_FN("isGenerator",     fun_isGenerator,0,0),
     JS_FS_END
 };
 
 /*
  * Report "malformed formal parameter" iff no illegal char or similar scanner
  * error was already reported.
  */
 static bool
new file mode 100644
--- /dev/null
+++ b/js/src/tests/js1_8_5/extensions/is-generator.js
@@ -0,0 +1,23 @@
+/*
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/licenses/publicdomain/
+ */
+
+/*
+ * Bug 648355: Function.prototype.isGenerator
+ */
+
+reportCompare(true, "isGenerator" in Function.prototype, "Function.prototype.isGenerator present");
+
+reportCompare(false, (function(){}).isGenerator(), "lambda is not a generator fn");
+reportCompare(false, Function.prototype.toString.isGenerator(), "native is not a generator fn");
+reportCompare(false, (function(){with(obj){}}).isGenerator(), "heavyweight is not a generator fn");
+reportCompare(false, (function(){obj}).isGenerator(), "upvar function is not a generator fn");
+
+reportCompare(true, (function(){yield}).isGenerator(), "simple generator fn");
+reportCompare(true, (function(){with(obj){yield}}).isGenerator(), "heavyweight generator fn");
+reportCompare(true, (function(){yield; obj}).isGenerator(), "upvar generator fn");
+
+reportCompare(false, Function.prototype.isGenerator.call(42), "number is not a generator fn");
+reportCompare(false, Function.prototype.isGenerator.call({}), "vanilla object is not a generator fn");
+reportCompare(false, Function.prototype.isGenerator.call(new Date()), "date object is not a generator fn");
--- a/js/src/tests/js1_8_5/extensions/jstests.list
+++ b/js/src/tests/js1_8_5/extensions/jstests.list
@@ -34,8 +34,9 @@ script regress-627984-3.js
 script regress-627984-4.js
 script regress-627984-5.js
 script regress-627984-6.js
 script regress-627984-7.js
 script regress-630377.js
 script regress-631723.js
 skip-if(!xulRuntime.shell) script regress-636818.js
 script regress-636697.js
+script is-generator.js