Skip to content

Commit

Permalink
✨ feat: Change abstract to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Oct 26, 2024
1 parent 07302bc commit 5fd7751
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024-2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.swc4j.ast.Swc4jAst;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;

import java.util.List;

public interface ITs2JavaAstAppend<AST extends Swc4jAst> {
void append(List<ByteCodeAppender> appenders, AST ast);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
import com.caoccao.javet.swc4j.ast.Swc4jAst;
import net.bytebuddy.dynamic.DynamicType;

public abstract class BaseTs2JavaAst<AST extends Swc4jAst> {
public abstract DynamicType.Builder<?> transpile(DynamicType.Builder<?> builder, AST ast);
public interface ITs2JavaAstTranspile<AST extends Swc4jAst> {
DynamicType.Builder<?> transpile(DynamicType.Builder<?> builder, AST ast);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024-2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.jar.asm.MethodVisitor;
import net.bytebuddy.jar.asm.Opcodes;

import java.util.List;

public final class Ts2JavaAstBinExpr implements ITs2JavaAstAppend<Swc4jAstBinExpr> {
@Override
public void append(List<ByteCodeAppender> appenders, Swc4jAstBinExpr ast) {
// TODO
appenders.add((methodVisitor, implementationContext, instrumentedMethod) -> {
StackManipulation.Size size = new StackManipulation.Compound(
new StackManipulation.Simple((
MethodVisitor simpleMethodVisitor,
Implementation.Context simpleImplementationContext) -> {
simpleMethodVisitor.visitVarInsn(Opcodes.ILOAD, 1);
simpleMethodVisitor.visitVarInsn(Opcodes.ILOAD, 2);
simpleMethodVisitor.visitInsn(Opcodes.IADD);
return new StackManipulation.Size(2, 0);
}),
MethodReturn.INTEGER
).apply(methodVisitor, implementationContext);
return new ByteCodeAppender.Size(
size.getMaximalSize(),
instrumentedMethod.getStackSize());
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024-2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstBlockStmt;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstReturnStmt;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;

import java.util.List;

public final class Ts2JavaAstBlockStmt implements ITs2JavaAstAppend<Swc4jAstBlockStmt> {
@Override
public void append(List<ByteCodeAppender> appenders, Swc4jAstBlockStmt ast) {
ast.getStmts().forEach(stmt -> {
switch (stmt.getType()) {
case ReturnStmt:
new Ts2JavaAstReturnStmt().append(appenders, stmt.as(Swc4jAstReturnStmt.class));
break;
default:
throw new Ts2JavaException(stmt.getType().name() + " is not supported");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstClassMember;
import net.bytebuddy.dynamic.DynamicType;

public final class Ts2JavaAstClass extends BaseTs2JavaAst<Swc4jAstClass> {
public final class Ts2JavaAstClass implements ITs2JavaAstTranspile<Swc4jAstClass> {
@Override
public DynamicType.Builder<?> transpile(
DynamicType.Builder<?> builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.caoccao.javet.utils.StringUtils;
import net.bytebuddy.dynamic.DynamicType;

public final class Ts2JavaAstClassDecl extends BaseTs2JavaAst<Swc4jAstClassDecl> {
public final class Ts2JavaAstClassDecl implements ITs2JavaAstTranspile<Swc4jAstClassDecl> {
private final String packageName;

public Ts2JavaAstClassDecl(String packageName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstAccessibility;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public final class Ts2JavaAstClassFunction extends BaseTs2JavaAst<Swc4jAstFunction> {
public final class Ts2JavaAstClassFunction implements ITs2JavaAstTranspile<Swc4jAstFunction> {
private final Swc4jAstAccessibility accessibility;
private final String name;

Expand Down Expand Up @@ -64,10 +65,11 @@ public DynamicType.Builder<?> transpile(
final Class<?>[] parameters = stackFrame.getObjects().stream()
.map(JavaStackObject::getType)
.toArray(Class[]::new);
final List<ByteCodeAppender> appenders = new ArrayList<>();
ast.getBody().ifPresent(blockStmt -> new Ts2JavaAstBlockStmt().append(appenders, blockStmt));
builder = builder.defineMethod(name, returnType, visibility)
.withParameters(parameters)
// TODO
.intercept(MethodDelegation.to(this));
.intercept(new Implementation.Simple(appenders.toArray(new ByteCodeAppender[0])));
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdentName;
import net.bytebuddy.dynamic.DynamicType;

public final class Ts2JavaAstClassMethod extends BaseTs2JavaAst<Swc4jAstClassMethod> {
public final class Ts2JavaAstClassMethod implements ITs2JavaAstTranspile<Swc4jAstClassMethod> {
@Override
public DynamicType.Builder<?> transpile(
DynamicType.Builder<?> builder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024-2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstReturnStmt;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;

import java.util.List;

public final class Ts2JavaAstReturnStmt implements ITs2JavaAstAppend<Swc4jAstReturnStmt> {
@Override
public void append(List<ByteCodeAppender> appenders, Swc4jAstReturnStmt ast) {
ast.getArg().ifPresent(arg -> {
switch (arg.getType()) {
case BinExpr:
new Ts2JavaAstBinExpr().append(appenders, arg.as(Swc4jAstBinExpr.class));
break;
default:
throw new Ts2JavaException(arg.getType().name() + " is not supported");
}
});
}
}

0 comments on commit 5fd7751

Please sign in to comment.