Skip to content

Commit

Permalink
🦄 refactor: Rename stack frame to lexical scope
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Oct 31, 2024
1 parent 99e40bf commit a46a25d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstAccessibility;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;

import java.util.List;
import java.util.Objects;

public final class Ts2JavaAstClassFunction implements ITs2JavaAstTranspile<Swc4jAstFunction> {
Expand Down Expand Up @@ -63,9 +63,9 @@ public DynamicType.Builder<?> transpile(
ast.getParams().stream()
.map(Ts2JavaAstParam::getLocalVariable)
.forEach(functionContext::addLocalVariable);
final List<TypeDescription> parameters = functionContext.getParameters();
final TypeList parameters = functionContext.getParameters();
final int initialOffset = functionContext.getMaxOffset();
functionContext.pushStackFrame();
functionContext.pushLexicalScope();
ast.getBody().ifPresent(blockStmt -> new Ts2JavaAstBlockStmt().manipulate(functionContext, blockStmt));
final StackManipulation[] stackManipulations =
functionContext.getStackManipulations().toArray(new StackManipulation[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.caoccao.javet.utils.SimpleList;
import com.caoccao.javet.utils.SimpleMap;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.implementation.bytecode.StackManipulation;

import java.util.ArrayList;
Expand All @@ -30,8 +31,8 @@

public final class JavaFunctionContext {
private final boolean _static;
private final List<JavaLexicalScope> lexicalScopes;
private final TypeDescription returnType;
private final List<JavaStackFrame> stackFrames;
private final List<StackManipulation> stackManipulations;
private int maxOffset;
private int nextOffset;
Expand All @@ -40,14 +41,14 @@ public JavaFunctionContext(boolean _static, TypeDescription returnType) {
this._static = _static;
nextOffset = _static ? 0 : 1;
maxOffset = nextOffset;
this.stackFrames = SimpleList.of(new JavaStackFrame(0));
this.lexicalScopes = SimpleList.of(new JavaLexicalScope(0));
this.returnType = Objects.requireNonNull(returnType);
this.stackManipulations = new ArrayList<>();
}

public void addLocalVariable(JavaLocalVariable localVariable) {
JavaStackFrame stackFrame = stackFrames.get(stackFrames.size() - 1);
stackFrame.putLocalVariable(localVariable);
JavaLexicalScope lexicalScope = lexicalScopes.get(lexicalScopes.size() - 1);
lexicalScope.putLocalVariable(localVariable);
localVariable.setOffset(nextOffset);
nextOffset += localVariable.getType().getStackSize().getSize();
if (nextOffset > maxOffset) {
Expand All @@ -60,9 +61,9 @@ public void addStackManipulation(StackManipulation stackManipulation) {
}

public JavaLocalVariable getLocalVariable(String name) {
for (int stackFrameIndex = stackFrames.size() - 1; stackFrameIndex >= 0; stackFrameIndex--) {
JavaStackFrame stackFrame = stackFrames.get(stackFrameIndex);
JavaLocalVariable localVariable = stackFrame.getLocalVariable(name);
for (int lexicalScopeIndex = lexicalScopes.size() - 1; lexicalScopeIndex >= 0; lexicalScopeIndex--) {
JavaLexicalScope lexicalScope = lexicalScopes.get(lexicalScopeIndex);
JavaLocalVariable localVariable = lexicalScope.getLocalVariable(name);
if (localVariable != null) {
return localVariable;
}
Expand All @@ -80,10 +81,11 @@ public int getNextOffset() {
return nextOffset;
}

public List<TypeDescription> getParameters() {
return stackFrames.get(0).getLocalVariables().stream()
.map(JavaLocalVariable::getType)
.collect(Collectors.toList());
public TypeList getParameters() {
return new TypeList.Explicit(
lexicalScopes.get(0).getLocalVariables().stream()
.map(JavaLocalVariable::getType)
.collect(Collectors.toList()));
}

public TypeDescription getReturnType() {
Expand All @@ -98,14 +100,14 @@ public boolean isStatic() {
return _static;
}

public void popStackFrame() {
JavaStackFrame stackFrame = stackFrames.remove(stackFrames.size() - 1);
nextOffset -= stackFrame.getLocalVariables().stream()
public void popLexicalScope() {
JavaLexicalScope lexicalScope = lexicalScopes.remove(lexicalScopes.size() - 1);
nextOffset -= lexicalScope.getLocalVariables().stream()
.mapToInt(v -> v.getType().getStackSize().getSize())
.sum();
}

public void pushStackFrame() {
stackFrames.add(new JavaStackFrame(stackFrames.size()));
public void pushLexicalScope() {
lexicalScopes.add(new JavaLexicalScope(lexicalScopes.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.List;
import java.util.Map;

public final class JavaStackFrame {
public final class JavaLexicalScope {
private final int index;
private final Map<String, JavaLocalVariable> localVariableMap;

public JavaStackFrame(int index) {
public JavaLexicalScope(int index) {
this.index = index;
localVariableMap = new LinkedHashMap<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected void init() {

@Test
public void testAssignAndCast() throws Exception {
assertEquals(3L, assignAndCast(1, 2L));
assertEquals(3.0D, assignAndCast(1, 2L), 0.001D);
Method method = clazz.getMethod("assignAndCast", int.class, long.class);
assertNotNull(method);
assertEquals(double.class, method.getReturnType());
Expand Down

0 comments on commit a46a25d

Please sign in to comment.