Commit 19e5f6cb authored by wingo@igalia.com's avatar wingo@igalia.com

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
parent 45a5535c
...@@ -2197,6 +2197,14 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionMarkNameShouldPrintAsAnonymous) { ...@@ -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) { RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) {
NoHandleAllocation ha(isolate); NoHandleAllocation ha(isolate);
ASSERT(args.length() == 1); ASSERT(args.length() == 1);
......
...@@ -231,6 +231,7 @@ namespace internal { ...@@ -231,6 +231,7 @@ namespace internal {
F(FunctionSetName, 2, 1) \ F(FunctionSetName, 2, 1) \
F(FunctionNameShouldPrintAsAnonymous, 1, 1) \ F(FunctionNameShouldPrintAsAnonymous, 1, 1) \
F(FunctionMarkNameShouldPrintAsAnonymous, 1, 1) \ F(FunctionMarkNameShouldPrintAsAnonymous, 1, 1) \
F(FunctionIsGenerator, 1, 1) \
F(FunctionBindArguments, 4, 1) \ F(FunctionBindArguments, 4, 1) \
F(BoundFunctionGetBindings, 1, 1) \ F(BoundFunctionGetBindings, 1, 1) \
F(FunctionRemovePrototype, 1, 1) \ F(FunctionRemovePrototype, 1, 1) \
......
...@@ -1665,7 +1665,6 @@ function FunctionSourceString(func) { ...@@ -1665,7 +1665,6 @@ function FunctionSourceString(func) {
func = %GetCallTrap(func); func = %GetCallTrap(func);
} }
// TODO(wingo): Print source using function* for generators.
if (!IS_FUNCTION(func)) { if (!IS_FUNCTION(func)) {
throw new $TypeError('Function.prototype.toString is not generic'); throw new $TypeError('Function.prototype.toString is not generic');
} }
...@@ -1684,7 +1683,8 @@ function FunctionSourceString(func) { ...@@ -1684,7 +1683,8 @@ function FunctionSourceString(func) {
var name = %FunctionNameShouldPrintAsAnonymous(func) var name = %FunctionNameShouldPrintAsAnonymous(func)
? 'anonymous' ? 'anonymous'
: %FunctionGetName(func); : %FunctionGetName(func);
return 'function ' + name + source; var head = %FunctionIsGenerator(func) ? 'function* ' : 'function ';
return head + name + source;
} }
......
...@@ -105,6 +105,8 @@ function TestGeneratorFunction() { ...@@ -105,6 +105,8 @@ function TestGeneratorFunction() {
assertSame(Function, Object.getPrototypeOf(GeneratorFunction)); assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
assertTrue(g instanceof Function); assertTrue(g instanceof Function);
assertEquals("function* g() { yield 1; }", g.toString());
// Not all functions are generators. // Not all functions are generators.
assertTrue(f instanceof Function); // Sanity check. assertTrue(f instanceof Function); // Sanity check.
assertTrue(!(f instanceof GeneratorFunction)); assertTrue(!(f instanceof GeneratorFunction));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment