-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCompiledCode.java
More file actions
57 lines (50 loc) · 2.32 KB
/
CompiledCode.java
File metadata and controls
57 lines (50 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.perlonjava.codegen;
import org.perlonjava.runtime.RuntimeCode;
import java.lang.invoke.MethodHandle;
/**
* Compiled bytecode that extends RuntimeCode.
*
* This class represents Perl code that has been compiled to JVM bytecode.
* It wraps the generated Class<?> and provides the same RuntimeCode interface
* as InterpretedCode, enabling seamless switching between compiler and interpreter.
*
* DESIGN: Following the InterpretedCode pattern:
* - InterpretedCode stores bytecode[] and overrides apply() to call BytecodeInterpreter
* - CompiledCode stores Class<?> and uses parent apply() to call MethodHandle
*
* This allows the EmitterMethodCreator.createRuntimeCode() factory to return either
* CompiledCode or InterpretedCode based on whether compilation succeeded or fell
* back to the interpreter.
*/
public class CompiledCode extends RuntimeCode {
// The generated JVM class (useful for debugging and EmitSubroutine bytecode generation)
public final Class<?> generatedClass;
// The compiler context used to create this code (may be useful for debugging)
public final EmitterContext compileContext;
/**
* Constructor for CompiledCode.
*
* @param methodHandle The MethodHandle for the apply() method
* @param codeObject The instance of the generated class (with closure variables)
* @param prototype The subroutine prototype (e.g., "$" for one scalar parameter)
* @param generatedClass The compiled JVM class
* @param compileContext The compiler context (optional, for debugging)
*/
public CompiledCode(MethodHandle methodHandle, Object codeObject,
String prototype, Class<?> generatedClass,
EmitterContext compileContext) {
super(methodHandle, codeObject, prototype);
this.generatedClass = generatedClass;
this.compileContext = compileContext;
}
// No need to override apply() - parent RuntimeCode implementation works perfectly
// The MethodHandle dispatches to compiled JVM bytecode automatically
@Override
public String toString() {
return "CompiledCode{" +
"class=" + (generatedClass != null ? generatedClass.getName() : "null") +
", prototype='" + prototype + '\'' +
", defined=" + defined() +
'}';
}
}