Skip to content

Commit

Permalink
fix: toString() on generator
Browse files Browse the repository at this point in the history
  • Loading branch information
0xe authored and rbri committed Apr 27, 2024
1 parent 7c710f1 commit 709874b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/org/mozilla/javascript/Decompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,20 @@ int getCurrentOffset() {
return sourceTop;
}

int markFunctionStart(int functionType) {
int markFunctionStart(int functionType, boolean isGenerator) {
int savedOffset = getCurrentOffset();
if (functionType != FunctionNode.ARROW_FUNCTION) {
addToken(Token.FUNCTION);
if (isGenerator) addToken(Token.MUL);
append((char) functionType);
}
return savedOffset;
}

int markFunctionStart(int functionType) {
return markFunctionStart(functionType, false);
}

int markFunctionEnd(int functionStart) {
int offset = getCurrentOffset();
append((char) FUNCTION_END);
Expand Down Expand Up @@ -340,7 +345,10 @@ public static String decompile(String source, int flags, UintMap properties) {

case Token.FUNCTION:
++i; // skip function type
result.append("function ");
if (source.charAt(i) == Token.MUL) {
result.append("function* ");
++i;
} else result.append("function ");
break;

case FUNCTION_END:
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/IRFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ private Node transformForLoop(ForLoop loop) {

private Node transformFunction(FunctionNode fn) {
int functionType = fn.getFunctionType();
int start = decompiler.markFunctionStart(functionType);
int start = decompiler.markFunctionStart(functionType, fn.isES6Generator());
Node mexpr = decompileFunctionHeader(fn);
int index = parser.currentScriptOrFn.addFunction(fn);

Expand Down
4 changes: 4 additions & 0 deletions testsrc/jstests/harmony/generators-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@ r = g.next();
assertEquals(undefined, r.value);
assertTrue(r.done);

// toString returns the correct value

assertEquals("\nfunction* gen() {\n for (var i = 0; i < 3; i++) {\n yield i;\n }\n}\n", gen.toString());

"success";
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.mozilla.javascript.tests.es5;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.mozilla.javascript.tests.Utils;

public class GeneratorToStringTest {
private Context cx;

@Before
public void setUp() {
cx = Context.enter();
cx.setLanguageVersion(Context.VERSION_ES6);
}

@After
public void tearDown() {
Context.exit();
}

@Test
public void generatorsTest() {
String code = " function* f() {\n" + " yield 1;\n" + " }; f.toString();";
test("\n" + "function* f() {\n" + " yield 1;\n" + "}\n", code);
}

private static void test(Object expected, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = new TopLevel();
cx.initStandardObjects(scope);

Object result = cx.evaluateString(scope, js, "test", 1, null);
assertEquals(expected, result);

return null;
});
}
}

0 comments on commit 709874b

Please sign in to comment.