-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
672 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/com/caoccao/javet/buddy/ts2java/Ts2JavaX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 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; | ||
|
||
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoDynamicType; | ||
import com.caoccao.javet.buddy.ts2java.ast.stmt.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; | ||
import com.caoccao.javet.swc4j.enums.Swc4jMediaType; | ||
import com.caoccao.javet.swc4j.enums.Swc4jParseMode; | ||
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException; | ||
import com.caoccao.javet.swc4j.options.Swc4jParseOptions; | ||
import com.caoccao.javet.swc4j.outputs.Swc4jParseOutput; | ||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.dynamic.DynamicType; | ||
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class Ts2JavaX { | ||
protected final static Swc4j swc4j = new Swc4j(); | ||
protected final static Swc4jParseOptions swc4jParseOptions = new Swc4jParseOptions() | ||
.setMediaType(Swc4jMediaType.TypeScript) | ||
.setParseMode(Swc4jParseMode.Module) | ||
.setCaptureAst(true); | ||
protected final String packageName; | ||
protected final String tsCode; | ||
protected List<Class<?>> classes; | ||
|
||
public Ts2JavaX(String packageName, String tsCode) { | ||
classes = new ArrayList<>(); | ||
this.packageName = packageName; | ||
this.tsCode = Objects.requireNonNull(tsCode); | ||
} | ||
|
||
public List<Class<?>> getClasses() { | ||
return classes; | ||
} | ||
|
||
public String getPackageName() { | ||
return packageName; | ||
} | ||
|
||
public String getTsCode() { | ||
return tsCode; | ||
} | ||
|
||
public void transpile() throws Swc4jCoreException { | ||
classes.clear(); | ||
Swc4jParseOutput output = swc4j.parse(getTsCode(), swc4jParseOptions); | ||
Swc4jAstModule module = (Swc4jAstModule) output.getProgram(); | ||
if (module == null) { | ||
throw new Ts2JavaException("The TypeScript code must be a module, not a script."); | ||
} | ||
if (module.getBody().isEmpty()) { | ||
throw new Ts2JavaException("The TypeScript code must contain at least one statement."); | ||
} | ||
List<Swc4jAstClassDecl> classDecls = module.getBody().stream() | ||
.filter(ast -> ast instanceof Swc4jAstClassDecl) | ||
.map(ast -> (Swc4jAstClassDecl) ast) | ||
.collect(Collectors.toList()); | ||
if (classDecls.isEmpty()) { | ||
throw new Ts2JavaException("There must be at least one class declaration in the TypeScript code."); | ||
} | ||
for (Swc4jAstClassDecl classDecl : classDecls) { | ||
DynamicType.Builder<?> builder = new ByteBuddy() | ||
.subclass(Object.class, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR); | ||
Ts2JavaMemoDynamicType memo = new Ts2JavaMemoDynamicType(builder); | ||
Ts2JavaAstClassDecl ast = new Ts2JavaAstClassDecl(null, classDecl, memo, getPackageName()); | ||
ast.compile(); | ||
builder = memo.getBuilder(); | ||
try (DynamicType.Unloaded<?> unloadedType = builder.make()) { | ||
classes.add(unloadedType.load(getClass().getClassLoader()).getLoaded()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/com/caoccao/javet/buddy/ts2java/ast/expr/lit/Ts2JavaAstNumber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 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.expr.lit; | ||
|
||
import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAst; | ||
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst; | ||
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstLit; | ||
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstPropName; | ||
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstTsLit; | ||
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction; | ||
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException; | ||
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstNumber; | ||
import com.caoccao.javet.utils.SimpleFreeMarkerFormat; | ||
import com.caoccao.javet.utils.SimpleMap; | ||
import com.caoccao.javet.utils.StringUtils; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.Implementation; | ||
import net.bytebuddy.implementation.bytecode.StackManipulation; | ||
import net.bytebuddy.implementation.bytecode.constant.DoubleConstant; | ||
import net.bytebuddy.implementation.bytecode.constant.FloatConstant; | ||
import net.bytebuddy.implementation.bytecode.constant.IntegerConstant; | ||
import net.bytebuddy.implementation.bytecode.constant.LongConstant; | ||
import net.bytebuddy.jar.asm.MethodVisitor; | ||
|
||
public class Ts2JavaAstNumber | ||
extends BaseTs2JavaAst<Swc4jAstNumber, Ts2JavaMemoFunction> | ||
implements ITs2JavaAstLit<Swc4jAstNumber, Ts2JavaMemoFunction>, | ||
ITs2JavaAstPropName<Swc4jAstNumber, Ts2JavaMemoFunction>, | ||
ITs2JavaAstTsLit<Swc4jAstNumber, Ts2JavaMemoFunction> { | ||
public Ts2JavaAstNumber( | ||
ITs2JavaAst<?, ?> parent, | ||
Swc4jAstNumber ast, | ||
TypeDescription type, | ||
Ts2JavaMemoFunction memo) { | ||
super(parent, ast, memo); | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) { | ||
visitLineNumber(methodVisitor); | ||
StackManipulation stackManipulation; | ||
final boolean isNegative = ast.getMinusCount() % 2 == 1; | ||
if (type.represents(int.class) || type.represents(short.class) || type.represents(byte.class)) { | ||
stackManipulation = IntegerConstant.forValue(isNegative ? -ast.asInt() : ast.asInt()); | ||
} else if (type.represents(long.class)) { | ||
stackManipulation = LongConstant.forValue(isNegative ? -ast.asLong() : ast.asLong()); | ||
} else if (type.represents(float.class)) { | ||
stackManipulation = FloatConstant.forValue(isNegative ? -ast.asFloat() : ast.asFloat()); | ||
} else if (type.represents(double.class)) { | ||
stackManipulation = DoubleConstant.forValue(isNegative ? -ast.asDouble() : ast.asDouble()); | ||
} else { | ||
throw new Ts2JavaAstException( | ||
ast, | ||
SimpleFreeMarkerFormat.format("Number type ${type} is not supported.", | ||
SimpleMap.of("type", type.getName()))); | ||
} | ||
return stackManipulation.apply(methodVisitor, context); | ||
} | ||
|
||
@Override | ||
public void compile() { | ||
if (type == null) { | ||
type = ast.getRaw() | ||
.map(raw -> { | ||
if (StringUtils.isNotEmpty(raw)) { | ||
if (raw.contains(".")) { | ||
return TypeDescription.ForLoadedType.of(double.class); | ||
} else { | ||
long value = Long.parseLong(raw); | ||
if (value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE) { | ||
return TypeDescription.ForLoadedType.of(int.class); | ||
} else { | ||
return TypeDescription.ForLoadedType.of(long.class); | ||
} | ||
} | ||
} else { | ||
return null; | ||
} | ||
}) | ||
.orElse(TypeDescription.ForLoadedType.of(int.class)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/caoccao/javet/buddy/ts2java/ast/interfaces/ITs2JavaAstJsxAttrValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 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.interfaces; | ||
|
||
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemo; | ||
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstJsxAttrValue; | ||
|
||
public interface ITs2JavaAstJsxAttrValue<AST extends ISwc4jAstJsxAttrValue, Memo extends Ts2JavaMemo> | ||
extends ITs2JavaAst<AST, Memo> { | ||
} |
Oops, something went wrong.