toString() on generator functions prints with function*

This CL adds a %FunctionIsGenerator runtime function, and uses it in the
function toString() implementation.

R=mstarzinger@chromium.org
BUG=v8:2355
TEST=mjsunit/harmony/generators-runtime

Review URL: https://codereview.chromium.org/14912002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14544 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
wingo@igalia.com 2013-05-03 13:01:28 +00:00
parent 45a5535c85
commit 19e5f6cbf0
4 changed files with 13 additions and 2 deletions

View File

@ -2197,6 +2197,14 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionMarkNameShouldPrintAsAnonymous) {
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsGenerator) {
NoHandleAllocation ha(isolate);
ASSERT(args.length() == 1);
CONVERT_ARG_CHECKED(JSFunction, f, 0);
return isolate->heap()->ToBoolean(f->shared()->is_generator());
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) {
NoHandleAllocation ha(isolate);
ASSERT(args.length() == 1);

View File

@ -231,6 +231,7 @@ namespace internal {
F(FunctionSetName, 2, 1) \
F(FunctionNameShouldPrintAsAnonymous, 1, 1) \
F(FunctionMarkNameShouldPrintAsAnonymous, 1, 1) \
F(FunctionIsGenerator, 1, 1) \
F(FunctionBindArguments, 4, 1) \
F(BoundFunctionGetBindings, 1, 1) \
F(FunctionRemovePrototype, 1, 1) \

View File

@ -1665,7 +1665,6 @@ function FunctionSourceString(func) {
func = %GetCallTrap(func);
}
// TODO(wingo): Print source using function* for generators.
if (!IS_FUNCTION(func)) {
throw new $TypeError('Function.prototype.toString is not generic');
}
@ -1684,7 +1683,8 @@ function FunctionSourceString(func) {
var name = %FunctionNameShouldPrintAsAnonymous(func)
? 'anonymous'
: %FunctionGetName(func);
return 'function ' + name + source;
var head = %FunctionIsGenerator(func) ? 'function* ' : 'function ';
return head + name + source;
}

View File

@ -105,6 +105,8 @@ function TestGeneratorFunction() {
assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
assertTrue(g instanceof Function);
assertEquals("function* g() { yield 1; }", g.toString());
// Not all functions are generators.
assertTrue(f instanceof Function); // Sanity check.
assertTrue(!(f instanceof GeneratorFunction));