Skip to content

Commit af03bf0

Browse files
committed
fix: toString() on generator
1 parent 96d18d4 commit af03bf0

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

src/org/mozilla/javascript/Decompiler.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,21 @@ 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)
72+
addToken(Token.MUL);
7173
append((char) functionType);
7274
}
7375
return savedOffset;
7476
}
7577

78+
int markFunctionStart(int functionType) {
79+
return markFunctionStart(functionType, false);
80+
}
81+
7682
int markFunctionEnd(int functionStart) {
7783
int offset = getCurrentOffset();
7884
append((char) FUNCTION_END);
@@ -340,7 +346,11 @@ public static String decompile(String source, int flags, UintMap properties) {
340346

341347
case Token.FUNCTION:
342348
++i; // skip function type
343-
result.append("function ");
349+
if (source.charAt(i) == Token.MUL) {
350+
result.append("function* ");
351+
++i;
352+
} else
353+
result.append("function ");
344354
break;
345355

346356
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
private ScriptableObject scope;
16+
17+
@Before
18+
public void setUp() {
19+
cx = Context.enter();
20+
cx.setLanguageVersion(Context.VERSION_ES6);
21+
scope = cx.initStandardObjects();
22+
}
23+
24+
@After
25+
public void tearDown() {
26+
Context.exit();
27+
}
28+
29+
30+
@Test
31+
public void generatorsTest() {
32+
String code =
33+
" function* f() {\n" +
34+
" yield 1;\n" +
35+
" }; f.toString();";
36+
test("\n" +
37+
"function* f() {\n" +
38+
" yield 1;\n" +
39+
"}\n", code);
40+
}
41+
42+
private static void test(Object expected, String js) {
43+
Utils.runWithAllOptimizationLevels(
44+
cx -> {
45+
cx.setLanguageVersion(Context.VERSION_ES6);
46+
ScriptableObject scope = new TopLevel();
47+
cx.initStandardObjects(scope);
48+
49+
Object result = cx.evaluateString(scope, js, "test", 1, null);
50+
assertEquals(expected, result);
51+
52+
return null;
53+
});
54+
}
55+
}

0 commit comments

Comments
 (0)