Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZZank committed Jun 23, 2024
1 parent 4fdf833 commit 734c0bb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package dev.latvian.mods.rhino;

import dev.latvian.mods.rhino.util.Deletable;
import dev.latvian.mods.rhino.util.wrap.TypeWrapperFactory;
import dev.latvian.mods.rhino.util.wrap.TypeWrappers;
import lombok.val;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -470,8 +470,8 @@ static Object coerceTypeImpl(@Nullable TypeWrappers typeWrappers, Class<?> type,
return value;
}

Object unwrappedValue = Wrapper.unwrapped(value);
TypeWrapperFactory<?> typeWrapper = typeWrappers == null ? null : typeWrappers.getWrapperFactory(type, unwrappedValue);
val unwrappedValue = Wrapper.unwrapped(value);
val typeWrapper = typeWrappers == null ? null : typeWrappers.getWrapperFactory(type, unwrappedValue);

if (typeWrapper != null) {
return typeWrapper.wrap(unwrappedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.latvian.mods.rhino.ObjToIntMap;
import dev.latvian.mods.rhino.Token;
import dev.latvian.mods.rhino.ast.Jump;
import lombok.val;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -39,9 +40,9 @@ class Block {
}

static void runFlowAnalyzes(OptFunctionNode fn, Node[] statementNodes) {
int paramCount = fn.fnode.getParamCount();
int varCount = fn.fnode.getParamAndVarCount();
int[] varTypes = new int[varCount];
val paramCount = fn.fnode.getParamCount();
val varCount = fn.fnode.getParamAndVarCount();
val varTypes = new int[varCount];
// If the variable is a parameter, it could have any type.
for (int i = 0; i != paramCount; ++i) {
varTypes[i] = Optimizer.AnyType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import dev.latvian.mods.rhino.ObjToIntMap;
import dev.latvian.mods.rhino.Parser;
import dev.latvian.mods.rhino.ScriptRuntime;
import dev.latvian.mods.rhino.ast.AstRoot;
import dev.latvian.mods.rhino.ast.FunctionNode;
import dev.latvian.mods.rhino.ast.ScriptNode;
import lombok.val;

/**
* Generates class files from script sources.
Expand Down Expand Up @@ -124,66 +123,57 @@ protected String makeAuxiliaryClassName(String mainClassName,
* array. The initial element of the array always holds
* mainClassName and array[1] holds its byte code.
*/
public Object[] compileToClassFiles(String source,
String sourceLocation,
int lineno,
String mainClassName) {
Parser p = new Parser(compilerEnv);
AstRoot ast = p.parse(source, sourceLocation, lineno);
IRFactory irf = new IRFactory(compilerEnv);
ScriptNode tree = irf.transformTree(ast);

// release reference to original parse tree & parser
irf = null;
ast = null;
p = null;
public Object[] compileToClassFiles(String source, String sourceLocation, int lineno, String mainClassName) {
ScriptNode tree;
{
val p = new Parser(compilerEnv);
val ast = p.parse(source, sourceLocation, lineno);
val irf = new IRFactory(compilerEnv);
tree = irf.transformTree(ast);
//release these references early
}

Class<?> superClass = getTargetExtends();
Class<?>[] interfaces = getTargetImplements();
val interfaces = getTargetImplements();
String scriptClassName;
boolean isPrimary = (interfaces == null && superClass == null);
val isPrimary = (interfaces == null && superClass == null);
if (isPrimary) {
scriptClassName = mainClassName;
} else {
scriptClassName = makeAuxiliaryClassName(mainClassName, "1");
}

Codegen codegen = new Codegen();
val codegen = new Codegen();
codegen.setMainMethodClass(mainMethodClassName);
byte[] scriptClassBytes = codegen.compileToClassFile(compilerEnv, scriptClassName,
tree, tree.getEncodedSource(),
false
);
val scriptClassBytes =
codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getEncodedSource(), false);

if (isPrimary) {
return new Object[]{scriptClassName, scriptClassBytes};
}
int functionCount = tree.getFunctionCount();
ObjToIntMap functionNames = new ObjToIntMap(functionCount);
val functionCount = tree.getFunctionCount();
val functionNames = new ObjToIntMap(functionCount);
for (int i = 0; i != functionCount; ++i) {
FunctionNode ofn = tree.getFunctionNode(i);
String name = ofn.getName();
val ofn = tree.getFunctionNode(i);
val name = ofn.getName();
if (name != null && name.length() != 0) {
functionNames.put(name, ofn.getParamCount());
}
}
if (superClass == null) {
superClass = ScriptRuntime.ObjectClass;
}
byte[] mainClassBytes
= JavaAdapter.createAdapterCode(
functionNames, mainClassName,
superClass, interfaces, scriptClassName
);

return new Object[]{mainClassName, mainClassBytes,
scriptClassName, scriptClassBytes};
val mainClassBytes =
JavaAdapter.createAdapterCode(functionNames, mainClassName, superClass, interfaces, scriptClassName);

return new Object[]{
mainClassName, mainClassBytes, scriptClassName, scriptClassBytes
};
}

private String mainMethodClassName;
private final CompilerEnvirons compilerEnv;
private Class<?> targetExtends;
private Class<?>[] targetImplements;

}

34 changes: 15 additions & 19 deletions common/src/main/java/dev/latvian/mods/rhino/optimizer/Codegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.latvian.mods.rhino.classfile.ByteCode;
import dev.latvian.mods.rhino.classfile.ClassFileWriter;
import dev.latvian.mods.rhino.util.JavaPortingHelper;
import lombok.val;

import java.lang.reflect.Constructor;
import java.util.HashMap;
Expand Down Expand Up @@ -54,10 +55,7 @@ public void setEvalScriptFlag(Script script) {
}

@Override
public Object compile(CompilerEnvirons compilerEnv,
ScriptNode tree,
String encodedSource,
boolean returnFunction) {
public Object compile(CompilerEnvirons compilerEnv, ScriptNode tree, String encodedSource, boolean returnFunction) {
int serial;
synchronized (globalLock) {
serial = ++globalSerialClassCounter;
Expand All @@ -71,7 +69,7 @@ public Object compile(CompilerEnvirons compilerEnv,
}
}

String mainClassName = "dev.latvian.mods.rhino.gen." + baseName + "_" + serial;
val mainClassName = "dev.latvian.mods.rhino.gen." + baseName + "_" + serial;

byte[] mainClassBytes = compileToClassFile(compilerEnv, mainClassName,
tree, encodedSource,
Expand Down Expand Up @@ -230,11 +228,11 @@ private static void collectScriptNodes_r(ScriptNode n, ObjArray x) {
}

private byte[] generateCode() {
boolean hasScript = (scriptOrFnNodes[0].getType() == Token.SCRIPT);
boolean hasFunctions = (scriptOrFnNodes.length > 1 || !hasScript);
boolean isStrictMode = scriptOrFnNodes[0].isInStrictMode();
val hasScript = (scriptOrFnNodes[0].getType() == Token.SCRIPT);
val hasFunctions = (scriptOrFnNodes.length > 1 || !hasScript);
val isStrictMode = scriptOrFnNodes[0].isInStrictMode();

ClassFileWriter cfw = new ClassFileWriter(mainClassName,
val cfw = new ClassFileWriter(mainClassName,
SUPER_CLASS_NAME,
null
);
Expand All @@ -258,9 +256,9 @@ private byte[] generateCode() {

int count = scriptOrFnNodes.length;
for (int i = 0; i != count; ++i) {
ScriptNode n = scriptOrFnNodes[i];
val n = scriptOrFnNodes[i];

BodyCodegen bodygen = new BodyCodegen();
val bodygen = new BodyCodegen();
bodygen.cfw = cfw;
bodygen.codegen = this;
bodygen.compilerEnv = compilerEnv;
Expand All @@ -270,7 +268,7 @@ private byte[] generateCode() {
bodygen.generateBodyCode();

if (n.getType() == Token.FUNCTION) {
OptFunctionNode ofn = OptFunctionNode.get(n);
val ofn = OptFunctionNode.get(n);
generateFunctionInit(cfw, ofn);
if (ofn.isTargetOfDirectCall()) {
emitDirectConstructor(cfw, ofn);
Expand All @@ -285,8 +283,7 @@ private byte[] generateCode() {
return cfw.toByteArray();
}

private void emitDirectConstructor(ClassFileWriter cfw,
OptFunctionNode ofn) {
private void emitDirectConstructor(ClassFileWriter cfw, OptFunctionNode ofn) {
/*
we generate ..
Scriptable directConstruct(<directCallArgs>) {
Expand All @@ -303,8 +300,8 @@ Scriptable directConstruct(<directCallArgs>) {
(short) (ClassFileWriter.ACC_STATIC | ClassFileWriter.ACC_PRIVATE)
);

int argCount = ofn.fnode.getParamCount();
int firstLocal = (4 + argCount * 3) + 1;
val argCount = ofn.fnode.getParamCount();
val firstLocal = (4 + argCount * 3) + 1;

cfw.addALoad(0); // this
cfw.addALoad(1); // cx
Expand Down Expand Up @@ -348,8 +345,7 @@ Scriptable directConstruct(<directCallArgs>) {
}

static boolean isGenerator(ScriptNode node) {
return (node.getType() == Token.FUNCTION) &&
((FunctionNode) node).isGenerator();
return (node.getType() == Token.FUNCTION) && ((FunctionNode) node).isGenerator();
}

// How dispatch to generators works:
Expand All @@ -366,7 +362,7 @@ static boolean isGenerator(ScriptNode node) {
// appended by "_gen".
private void generateResumeGenerator(ClassFileWriter cfw) {
boolean hasGenerators = false;
for (ScriptNode scriptOrFnNode : scriptOrFnNodes) {
for (val scriptOrFnNode : scriptOrFnNodes) {
if (isGenerator(scriptOrFnNode)) {
hasGenerators = true;
}
Expand Down

0 comments on commit 734c0bb

Please sign in to comment.