Skip to content

Commit 709874b

Browse files
0xerbri
authored andcommitted
fix: toString() on generator
1 parent 7c710f1 commit 709874b

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/org/mozilla/javascript/Decompiler.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,20 @@ int getCurrentOffset() {
6464
return sourceTop;
6565
}
6666

67-
int markFunctionStart(int functionType) {
67+
int markFunctionStart(int functionType, boolean isGenerator) {
6868
int savedOffset = getCurrentOffset();
6969
if (functionType != FunctionNode.ARROW_FUNCTION) {
7070
addToken(Token.FUNCTION);
71+
if (isGenerator) addToken(Token.MUL);
7172
append((char) functionType);
7273
}
7374
return savedOffset;
7475
}
7576

77+
int markFunctionStart(int functionType) {
78+
return markFunctionStart(functionType, false);
79+
}
80+
7681
int markFunctionEnd(int functionStart) {
7782
int offset = getCurrentOffset();
7883
append((char) FUNCTION_END);
@@ -340,7 +345,10 @@ public static String decompile(String source, int flags, UintMap properties) {
340345

341346
case Token.FUNCTION:
342347
++i; // skip function type
343-
result.append("function ");
348+
if (source.charAt(i) == Token.MUL) {
349+
result.append("function* ");
350+
++i;
351+
} else result.append("function ");
344352
break;
345353

346354
case FUNCTION_END:

src/org/mozilla/javascript/IRFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ private Node transformForLoop(ForLoop loop) {
615615

616616
private Node transformFunction(FunctionNode fn) {
617617
int functionType = fn.getFunctionType();
618-
int start = decompiler.markFunctionStart(functionType);
618+
int start = decompiler.markFunctionStart(functionType, fn.isES6Generator());
619619
Node mexpr = decompileFunctionHeader(fn);
620620
int index = parser.currentScriptOrFn.addFunction(fn);
621621

testsrc/jstests/harmony/generators-basic.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,8 @@ r = g.next();
162162
assertEquals(undefined, r.value);
163163
assertTrue(r.done);
164164

165+
// toString returns the correct value
166+
167+
assertEquals("\nfunction* gen() {\n for (var i = 0; i < 3; i++) {\n yield i;\n }\n}\n", gen.toString());
168+
165169
"success";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.mozilla.javascript.tests.es5;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.mozilla.javascript.Context;
9+
import org.mozilla.javascript.ScriptableObject;
10+
import org.mozilla.javascript.TopLevel;
11+
import org.mozilla.javascript.tests.Utils;
12+
13+
public class GeneratorToStringTest {
14+
private Context cx;
15+
16+
@Before
17+
public void setUp() {
18+
cx = Context.enter();
19+
cx.setLanguageVersion(Context.VERSION_ES6);
20+
}
21+
22+
@After
23+
public void tearDown() {
24+
Context.exit();
25+
}
26+
27+
@Test
28+
public void generatorsTest() {
29+
String code = " function* f() {\n" + " yield 1;\n" + " }; f.toString();";
30+
test("\n" + "function* f() {\n" + " yield 1;\n" + "}\n", code);
31+
}
32+
33+
private static void test(Object expected, String js) {
34+
Utils.runWithAllOptimizationLevels(
35+
cx -> {
36+
cx.setLanguageVersion(Context.VERSION_ES6);
37+
ScriptableObject scope = new TopLevel();
38+
cx.initStandardObjects(scope);
39+
40+
Object result = cx.evaluateString(scope, js, "test", 1, null);
41+
assertEquals(expected, result);
42+
43+
return null;
44+
});
45+
}
46+
}

0 commit comments

Comments
 (0)