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

Refactor IRFactory #1188

Closed
wants to merge 8 commits into from
Closed
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, compilationErrorReporter);
ScriptNode tree = irf.transformTree(ast);

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

return tree;
}

Expand Down
17 changes: 17 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/DecompilerFlag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.javascript;

public enum DecompilerFlag {
/**
* Flag to indicate that the decompilation should omit the function header and trailing brace.
*/
ONLY_BODY,

/** Flag to indicate that the decompilation generates toSource result. */
TO_SOURCE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private static Object[] sortedSet(final Set<?> s) {

private static boolean equalInterpretedFunctions(
final InterpretedFunction f1, final InterpretedFunction f2) {
return Objects.equals(f1.getEncodedSource(), f2.getEncodedSource());
return Objects.equals(f1.getRawSource(), f2.getRawSource());
}

// Sort IDs deterministically
Expand Down
4 changes: 2 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public interface Evaluator {
*
* @param compilerEnv Compiler environment
* @param tree parse tree
* @param encodedSource encoding of the source code for decompilation
* @param rawSource the source code
* @param returnFunction if true, compiling a function
* @return an opaque object that can be passed to either createFunctionObject or
* createScriptObject, depending on the value of returnFunction
*/
public Object compile(
CompilerEnvirons compilerEnv,
ScriptNode tree,
String encodedSource,
String rawSource,
boolean returnFunction);

/**
Expand Down
Loading
Loading