-
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.
🦄 refactor: Add JavaByteCodeMethodVariableAccess
- Loading branch information
Showing
8 changed files
with
108 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { type int } from "./type.aliases"; | ||
import { type int, type long } from "./type.aliases"; | ||
|
||
class Test { | ||
public add_II_I(a: int, b: int): int { | ||
return a + b; | ||
} | ||
// public add_IL_L(a: int, b: long): long { | ||
// return a + b; | ||
// } | ||
} | ||
|
||
console.log(new Test().add_II_I(1, 2)); | ||
// console.log(new Test().add_IL_L(1, 2)); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export type int = number; | ||
export type long = number; |
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
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
58 changes: 58 additions & 0 deletions
58
src/main/java/com/caoccao/javet/buddy/ts2java/compiler/JavaByteCodeMethodVariableAccess.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,58 @@ | ||
/* | ||
* 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.compiler; | ||
|
||
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException; | ||
import com.caoccao.javet.utils.SimpleFreeMarkerFormat; | ||
import com.caoccao.javet.utils.SimpleMap; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess; | ||
|
||
public final class JavaByteCodeMethodVariableAccess { | ||
private JavaByteCodeMethodVariableAccess() { | ||
} | ||
|
||
private static JavaStackObject getStackObject(JavaFunctionContext functionContext, String name) { | ||
final int size = functionContext.getStackFrames().size(); | ||
JavaStackObject stackObject; | ||
int stackFrameIndex; | ||
for (stackFrameIndex = size - 1; stackFrameIndex >= 0; stackFrameIndex--) { | ||
JavaStackFrame stackFrame = functionContext.getStackFrames().get(stackFrameIndex); | ||
stackObject = stackFrame.getObjectMap().get(name); | ||
if (stackObject != null) { | ||
return stackObject; | ||
} | ||
} | ||
throw new Ts2JavaException( | ||
SimpleFreeMarkerFormat.format("The variable ${name} is not defined", | ||
SimpleMap.of("name", name))); | ||
} | ||
|
||
public static void load(JavaFunctionContext functionContext, String name) { | ||
JavaStackObject stackObject = getStackObject(functionContext, name); | ||
MethodVariableAccess methodVariableAccess = | ||
MethodVariableAccess.of(TypeDescription.ForLoadedType.of(stackObject.getType())); | ||
functionContext.getStackManipulations().add(methodVariableAccess.loadFrom(stackObject.getIndex())); | ||
} | ||
|
||
public static void store(JavaFunctionContext functionContext, String name) { | ||
JavaStackObject stackObject = getStackObject(functionContext, name); | ||
MethodVariableAccess methodVariableAccess = | ||
MethodVariableAccess.of(TypeDescription.ForLoadedType.of(stackObject.getType())); | ||
functionContext.getStackManipulations().add(methodVariableAccess.storeAt(stackObject.getIndex())); | ||
} | ||
} |
67 changes: 0 additions & 67 deletions
67
src/main/java/com/caoccao/javet/buddy/ts2java/compiler/JavaByteCodeOpLoad.java
This file was deleted.
Oops, something went wrong.
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