Skip to content

Commit

Permalink
✨ feat: Add Ts2JavaAstException
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Oct 27, 2024
1 parent cd8fff2 commit 798d4af
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/caoccao/javet/buddy/ts2java/Ts2Java.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.caoccao.javet.buddy.ts2java;

import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaAstClassDecl;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException;
import com.caoccao.javet.swc4j.Swc4j;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstModule;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstClassDecl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

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

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.buddy.ts2java.compiler.JavaByteCodeOpLoad;
import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
Expand All @@ -40,10 +40,11 @@ public void manipulate(JavaFunctionContext functionContext, Swc4jAstBinExpr ast)
switch (expr.getType()) {
case Ident:
String name = expr.as(Swc4jAstIdent.class).getSym();
stackSize += JavaByteCodeOpLoad.generate(functionContext, name, methodVisitor);
stackSize += JavaByteCodeOpLoad.visit(functionContext, name, methodVisitor);
break;
default:
throw new Ts2JavaException(
throw new Ts2JavaAstException(
expr,
SimpleFreeMarkerFormat.format("BinExpr expr type ${exprType} is not supported",
SimpleMap.of("exprType", expr.getType().name())));
}
Expand All @@ -53,7 +54,8 @@ public void manipulate(JavaFunctionContext functionContext, Swc4jAstBinExpr ast)
methodVisitor.visitInsn(Opcodes.IADD);
break;
default:
throw new Ts2JavaException(
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("BinExpr op ${op} is not supported",
SimpleMap.of("op", ast.getOp().name())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstBlockStmt;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstReturnStmt;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
Expand All @@ -32,7 +32,8 @@ public void manipulate(JavaFunctionContext functionContext, Swc4jAstBlockStmt as
new Ts2JavaAstReturnStmt().manipulate(functionContext, stmt.as(Swc4jAstReturnStmt.class));
break;
default:
throw new Ts2JavaException(
throw new Ts2JavaAstException(
stmt,
SimpleFreeMarkerFormat.format("BlockStmt type ${type} is not supported",
SimpleMap.of("type", stmt.getType().name())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstClassMethod;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstAccessibility;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstMethodKind;
Expand All @@ -31,15 +31,19 @@ public DynamicType.Builder<?> transpile(
DynamicType.Builder<?> builder,
Swc4jAstClassMethod ast) {
if (ast.isStatic()) {
throw new Ts2JavaException("Static method is not implemented");
throw new Ts2JavaAstException(
ast,
"Static method is not implemented");
}
if (ast.getKind() != Swc4jAstMethodKind.Method) {
throw new Ts2JavaException(
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("ClassMethod kind ${kind} is not supported",
SimpleMap.of("kind", ast.getKind().name())));
}
if (!(ast.getKey() instanceof Swc4jAstIdentName)) {
throw new Ts2JavaException(
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("ClassMethod key type ${keyType} is not supported",
SimpleMap.of("keyType", ast.getKey().getClass().getSimpleName())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.buddy.ts2java.compiler.JavaStackObject;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstParam;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPat;
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstBindingIdent;
Expand All @@ -36,7 +36,8 @@ public static JavaStackObject getStackObject(int index, Swc4jAstParam ast) {
Class<?> type = Ts2JavaAstBindingIdent.getClass(pat.as(Swc4jAstBindingIdent.class));
return new JavaStackObject(index, ident, type);
default:
throw new Ts2JavaException(
throw new Ts2JavaAstException(
pat,
SimpleFreeMarkerFormat.format("Param pat type ${patType} is not supported",
SimpleMap.of("patType", pat.getType().name())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstReturnStmt;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
Expand All @@ -35,7 +35,8 @@ public void manipulate(JavaFunctionContext functionContext, Swc4jAstReturnStmt a
new Ts2JavaAstBinExpr().manipulate(functionContext, arg.as(Swc4jAstBinExpr.class));
break;
default:
throw new Ts2JavaException(
throw new Ts2JavaAstException(
arg,
SimpleFreeMarkerFormat.format("ReturnStmt arg type ${argType} is not supported",
SimpleMap.of("argType", arg.getType().name())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

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

import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsEntityName;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;

public final class Ts2JavaAstTsEntityName {
private Ts2JavaAstTsEntityName() {
Expand All @@ -28,7 +31,10 @@ public static String getName(ISwc4jAstTsEntityName ast) {
case Ident:
return ast.as(Swc4jAstIdent.class).getSym();
default:
return null;
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("TsEntityName type ${type} is not supported",
SimpleMap.of("type", ast.getType().name())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

import com.caoccao.javet.buddy.ts2java.Ts2JavaException;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
import net.bytebuddy.jar.asm.MethodVisitor;
Expand All @@ -42,7 +42,7 @@ public final class JavaByteCodeOpLoad {
private JavaByteCodeOpLoad() {
}

public static int generate(JavaFunctionContext functionContext, String name, MethodVisitor methodVisitor) {
public static int visit(JavaFunctionContext functionContext, String name, MethodVisitor methodVisitor) {
final int size = functionContext.getStackFrames().size();
JavaStackObject stackObject = null;
int stackFrameIndex = 0;
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.exceptions;

import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;

import java.util.Objects;

public class Ts2JavaAstException extends Ts2JavaException {
protected ISwc4jAst ast;

public Ts2JavaAstException(ISwc4jAst ast, String message) {
super(message);
this.ast = Objects.requireNonNull(ast);
}

public Ts2JavaAstException(ISwc4jAst ast, String message, Throwable cause) {
super(message, cause);
this.ast = Objects.requireNonNull(ast);
}

public ISwc4jAst getAst() {
return ast;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* 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.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

public class Ts2JavaException extends RuntimeException {
public Ts2JavaException(String message) {
Expand Down

0 comments on commit 798d4af

Please sign in to comment.