Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Function.prototype.toString as per the latest specification #1520

Merged
merged 9 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/ArrowFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.mozilla.javascript;

import java.util.EnumSet;

/** The class for Arrow Function Definitions EcmaScript 6 Rev 14, March 8, 2013 Draft spec , 13.2 */
public class ArrowFunction extends BaseFunction {

Expand Down Expand Up @@ -41,7 +43,8 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar

@Override
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
throw ScriptRuntime.typeErrorById("msg.not.ctor", decompile(0, 0));
throw ScriptRuntime.typeErrorById(
"msg.not.ctor", decompile(0, EnumSet.noneOf(DecompilerFlag.class)));
}

@Override
Expand All @@ -66,7 +69,7 @@ public int getArity() {
}

@Override
String decompile(int indent, int flags) {
String decompile(int indent, EnumSet<DecompilerFlag> flags) {
if (targetFunction instanceof BaseFunction) {
return ((BaseFunction) targetFunction).decompile(indent, flags);
}
Expand Down
12 changes: 7 additions & 5 deletions rhino/src/main/java/org/mozilla/javascript/BaseFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.mozilla.javascript;

import java.util.EnumSet;

/**
* The base class for Function objects. That is one of two purposes. It is also the prototype for
* every "function" defined except those that are used as GeneratorFunctions via the ES6 "function
Expand Down Expand Up @@ -319,18 +321,18 @@ public Object execIdCall(
{
BaseFunction realf = realFunction(thisObj, f);
int indent = ScriptRuntime.toInt32(args, 0);
return realf.decompile(indent, 0);
return realf.decompile(indent, EnumSet.noneOf(DecompilerFlag.class));
}

case Id_toSource:
{
BaseFunction realf = realFunction(thisObj, f);
int indent = 0;
int flags = Decompiler.TO_SOURCE_FLAG;
EnumSet<DecompilerFlag> flags = EnumSet.of(DecompilerFlag.TO_SOURCE);
if (args.length != 0) {
indent = ScriptRuntime.toInt32(args[0]);
if (indent >= 0) {
flags = 0;
flags = EnumSet.noneOf(DecompilerFlag.class);
} else {
indent = 0;
}
Expand Down Expand Up @@ -453,9 +455,9 @@ public Scriptable createObject(Context cx, Scriptable scope) {
* @param indent How much to indent the decompiled result.
* @param flags Flags specifying format of decompilation output.
*/
String decompile(int indent, int flags) {
String decompile(int indent, EnumSet<DecompilerFlag> flags) {
StringBuilder sb = new StringBuilder();
boolean justbody = (0 != (flags & Decompiler.ONLY_BODY_FLAG));
boolean justbody = flags.contains(DecompilerFlag.ONLY_BODY);
if (!justbody) {
sb.append("function ");
sb.append(getFunctionName());
Expand Down
8 changes: 4 additions & 4 deletions rhino/src/main/java/org/mozilla/javascript/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CodeGenerator extends Icode {
public InterpreterData compile(
CompilerEnvirons compilerEnv,
ScriptNode tree,
String encodedSource,
String rawSource,
boolean returnFunction) {
this.compilerEnv = compilerEnv;

Expand All @@ -84,7 +84,7 @@ public InterpreterData compile(
new InterpreterData(
compilerEnv.getLanguageVersion(),
scriptOrFn.getSourceName(),
encodedSource,
rawSource,
scriptOrFn.isInStrictMode());
itsData.topLevel = true;

Expand Down Expand Up @@ -190,8 +190,8 @@ private void generateICodeFromTree(Node tree) {
itsData.argCount = scriptOrFn.getParamCount();
itsData.argsHasRest = scriptOrFn.hasRestParameter();

itsData.encodedSourceStart = scriptOrFn.getEncodedSourceStart();
itsData.encodedSourceEnd = scriptOrFn.getEncodedSourceEnd();
itsData.rawSourceStart = scriptOrFn.getRawSourceStart();
itsData.rawSourceEnd = scriptOrFn.getRawSourceEnd();

if (literalIds.size() != 0) {
itsData.literalIds = literalIds.toArray();
Expand Down
18 changes: 13 additions & 5 deletions rhino/src/main/java/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
Expand Down Expand Up @@ -1474,7 +1475,7 @@ final Function compileFunction(
*/
public final String decompileScript(Script script, int indent) {
NativeFunction scriptImpl = (NativeFunction) script;
return scriptImpl.decompile(indent, 0);
return scriptImpl.decompile(indent, EnumSet.noneOf(DecompilerFlag.class));
}

/**
Expand All @@ -1489,7 +1490,8 @@ public final String decompileScript(Script script, int indent) {
* @return a string representing the function source
*/
public final String decompileFunction(Function fun, int indent) {
if (fun instanceof BaseFunction) return ((BaseFunction) fun).decompile(indent, 0);
if (fun instanceof BaseFunction)
return ((BaseFunction) fun).decompile(indent, EnumSet.noneOf(DecompilerFlag.class));

return "function " + fun.getClassName() + "() {\n\t[native code]\n}\n";
}
Expand All @@ -1509,7 +1511,7 @@ public final String decompileFunction(Function fun, int indent) {
public final String decompileFunctionBody(Function fun, int indent) {
if (fun instanceof BaseFunction) {
BaseFunction bf = (BaseFunction) fun;
return bf.decompile(indent, Decompiler.ONLY_BODY_FLAG);
return bf.decompile(indent, EnumSet.of(DecompilerFlag.ONLY_BODY));
}
// ALERT: not sure what the right response here is.
return "[native code]\n";
Expand Down Expand Up @@ -2452,7 +2454,7 @@ protected Object compileImpl(
compiler = createCompiler();
}

bytecode = compiler.compile(compilerEnv, tree, tree.getEncodedSource(), returnFunction);
bytecode = compiler.compile(compilerEnv, tree, sourceString, returnFunction);
} catch (ClassFileFormatException e) {
// we hit some class file limit, fall back to interpreter or report

Expand All @@ -2468,7 +2470,7 @@ protected Object compileImpl(
returnFunction);

compiler = createInterpreter();
bytecode = compiler.compile(compilerEnv, tree, tree.getEncodedSource(), returnFunction);
bytecode = compiler.compile(compilerEnv, tree, sourceString, returnFunction);
}

if (debugger != null) {
Expand Down Expand Up @@ -2522,6 +2524,12 @@ private ScriptNode parse(

IRFactory irf = new IRFactory(compilerEnv, sourceString, compilationErrorReporter);
ScriptNode tree = irf.transformTree(ast);

if (compilerEnv.isGeneratingSource()) {
tree.setRawSource(sourceString);
tree.setRawSourceBounds(0, sourceString.length());
}

return tree;
}

Expand Down
Loading
Loading