-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEvalStringHandler.java
More file actions
326 lines (285 loc) · 14.8 KB
/
EvalStringHandler.java
File metadata and controls
326 lines (285 loc) · 14.8 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package org.perlonjava.backend.bytecode;
import org.perlonjava.frontend.astnode.Node;
import org.perlonjava.backend.jvm.EmitterContext;
import org.perlonjava.backend.jvm.JavaClassInfo;
import org.perlonjava.frontend.lexer.Lexer;
import org.perlonjava.frontend.lexer.LexerToken;
import org.perlonjava.frontend.parser.Parser;
import org.perlonjava.runtime.runtimetypes.*;
import org.perlonjava.runtime.operators.WarnDie;
import org.perlonjava.frontend.semantic.ScopedSymbolTable;
import org.perlonjava.app.cli.CompilerOptions;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
/**
* Handler for eval STRING operations in the interpreter.
*
* Implements dynamic code evaluation with proper variable capture and error handling:
* - Parses Perl string to AST
* - Compiles AST to interpreter bytecode
* - Captures variables from outer scope
* - Executes with eval block semantics (catch errors, set $@)
*/
public class EvalStringHandler {
private static final boolean EVAL_TRACE =
System.getenv("JPERL_EVAL_TRACE") != null;
private static void evalTrace(String msg) {
if (EVAL_TRACE) {
System.err.println("[eval-trace] " + msg);
}
}
/**
* Evaluate a Perl string dynamically.
*
* This implements eval STRING semantics:
* - Parse and compile the string
* - Execute in the current scope context
* - Capture variables referenced from outer scope
* - Return result or undef on error
* - Set $@ on error
*
* @param perlCode The Perl code string to evaluate
* @param currentCode The current InterpretedCode (for context)
* @param registers Current register array (for variable access)
* @param sourceName Source name for error messages
* @param sourceLine Source line for error messages
* @param callContext The calling context (VOID/SCALAR/LIST) for wantarray inside eval
* @return RuntimeScalar result of evaluation (undef on error)
*/
public static RuntimeScalar evalString(String perlCode,
InterpretedCode currentCode,
RuntimeBase[] registers,
String sourceName,
int sourceLine,
int callContext) {
return evalStringList(perlCode, currentCode, registers, sourceName, sourceLine, callContext).scalar();
}
public static RuntimeList evalStringList(String perlCode,
InterpretedCode currentCode,
RuntimeBase[] registers,
String sourceName,
int sourceLine,
int callContext) {
try {
evalTrace("EvalStringHandler enter ctx=" + callContext + " srcName=" + sourceName +
" srcLine=" + sourceLine + " codeLen=" + (perlCode != null ? perlCode.length() : -1));
// Step 1: Clear $@ at start of eval
GlobalVariable.getGlobalVariable("main::@").set("");
// Step 2: Parse the string to AST
Lexer lexer = new Lexer(perlCode);
List<LexerToken> tokens = lexer.tokenize();
// Create minimal EmitterContext for parsing
// IMPORTANT: Inherit strict/feature/warning flags from parent scope
// This matches Perl's eval STRING semantics where eval inherits lexical pragmas
CompilerOptions opts = new CompilerOptions();
opts.fileName = sourceName + " (eval)";
ScopedSymbolTable symbolTable = new ScopedSymbolTable();
// Inherit lexical pragma flags from parent if available
if (currentCode != null) {
// Replace default values with parent's flags
symbolTable.strictOptionsStack.pop();
symbolTable.strictOptionsStack.push(currentCode.strictOptions);
symbolTable.featureFlagsStack.pop();
symbolTable.featureFlagsStack.push(currentCode.featureFlags);
symbolTable.warningFlagsStack.pop();
symbolTable.warningFlagsStack.push((java.util.BitSet) currentCode.warningFlags.clone());
}
// Use the runtime package at the eval call site.
// InterpreterState.currentPackage tracks runtime package changes from SET_PACKAGE/
// PUSH_PACKAGE opcodes, so it correctly reflects the package active when eval is called.
// This matches Perl's behaviour: eval("__PACKAGE__") returns the package at call site.
String compilePackage = InterpreterState.currentPackage.get().toString();
symbolTable.setCurrentPackage(compilePackage, false);
evalTrace("EvalStringHandler compilePackage=" + compilePackage + " fileName=" + opts.fileName);
ErrorMessageUtil errorUtil = new ErrorMessageUtil(sourceName, tokens);
EmitterContext ctx = new EmitterContext(
new JavaClassInfo(),
symbolTable,
null, // mv
null, // cw
callContext,
false, // isBoxed
errorUtil,
opts,
null // unitcheckBlocks
);
Parser parser = new Parser(ctx, tokens);
Node ast = parser.parse();
// Step 3: Build captured variables and adjusted registry for eval context
// Collect all parent scope variables (except reserved registers 0-2)
RuntimeBase[] capturedVars = new RuntimeBase[0];
Map<String, Integer> adjustedRegistry = null;
if (currentCode != null && currentCode.variableRegistry != null && registers != null) {
// Sort parent variables by register index for consistent ordering
List<Map.Entry<String, Integer>> sortedVars = new ArrayList<>(
currentCode.variableRegistry.entrySet()
);
sortedVars.sort(Map.Entry.comparingByValue());
// Build capturedVars array and adjusted registry
// Captured variables will be placed at registers 3+ in eval'd code
List<RuntimeBase> capturedList = new ArrayList<>();
adjustedRegistry = new HashMap<>();
// Always include reserved registers in adjusted registry
adjustedRegistry.put("this", 0);
adjustedRegistry.put("@_", 1);
adjustedRegistry.put("wantarray", 2);
int captureIndex = 0;
for (Map.Entry<String, Integer> entry : sortedVars) {
String varName = entry.getKey();
int parentRegIndex = entry.getValue();
// Skip reserved registers (they're handled separately in interpreter)
if (parentRegIndex < 3) {
continue;
}
if (parentRegIndex < registers.length) {
RuntimeBase value = registers[parentRegIndex];
// Skip non-Perl values (like Iterator objects from for loops)
// Only capture actual Perl variables: Scalar, Array, Hash, Code
if (value == null) {
// Null is fine - capture it
} else if (value instanceof RuntimeScalar) {
// Check if the scalar contains an Iterator (used by for loops)
RuntimeScalar scalar = (RuntimeScalar) value;
if (scalar.value instanceof java.util.Iterator) {
// Skip - this is a for loop iterator, not a user variable
continue;
}
} else if (!(value instanceof RuntimeArray ||
value instanceof RuntimeHash ||
value instanceof RuntimeCode)) {
// Skip this register - it contains an internal object
continue;
}
capturedList.add(value);
// Map to new register index starting at 3
adjustedRegistry.put(varName, 3 + captureIndex);
captureIndex++;
}
}
capturedVars = capturedList.toArray(new RuntimeBase[0]);
}
// Step 4: Compile AST to interpreter bytecode with adjusted variable registry.
// The compile-time package is already propagated via ctx.symbolTable.
BytecodeCompiler compiler = new BytecodeCompiler(
sourceName + " (eval)",
sourceLine,
errorUtil,
adjustedRegistry // Pass adjusted registry for variable capture
);
InterpretedCode evalCode = compiler.compile(ast, ctx); // Pass ctx for context propagation
evalTrace("EvalStringHandler compiled bytecodeLen=" + (evalCode != null ? evalCode.bytecode.length : -1) +
" src=" + (evalCode != null ? evalCode.sourceName : "null"));
if (RuntimeCode.DISASSEMBLE) {
System.out.println(evalCode.disassemble());
}
// Step 4.5: Store source lines in debugger symbol table if $^P flags are set
int debugFlags = GlobalVariable.getGlobalVariable(GlobalContext.encodeSpecialVar("P")).getInt();
if (debugFlags != 0) {
String evalFilename = RuntimeCode.getNextEvalFilename();
RuntimeCode.storeSourceLines(perlCode, evalFilename, ast, tokens);
}
// Step 5: Attach captured variables to eval'd code
if (capturedVars.length > 0) {
evalCode = evalCode.withCapturedVars(capturedVars);
} else if (currentCode != null && currentCode.capturedVars != null) {
// Fallback: share captured variables from parent scope (nested evals)
evalCode = evalCode.withCapturedVars(currentCode.capturedVars);
}
// Step 6: Execute the compiled code
RuntimeArray args = new RuntimeArray(); // Empty @_
RuntimeList result = evalCode.apply(args, callContext);
evalTrace("EvalStringHandler exec ok ctx=" + callContext +
" resultScalar=" + (result != null ? result.scalar().toString() : "null") +
" resultBool=" + (result != null && result.scalar() != null ? result.scalar().getBoolean() : false) +
" $@=" + GlobalVariable.getGlobalVariable("main::@").toString());
return result;
} catch (Exception e) {
evalTrace("EvalStringHandler exec exception ctx=" + callContext + " ex=" + e.getClass().getSimpleName() + " msg=" + e.getMessage());
WarnDie.catchEval(e);
return new RuntimeList(new RuntimeScalar());
}
}
/**
* Evaluate a Perl string with explicit variable capture.
*
* This version allows passing specific captured variables for the eval context.
*
* @param perlCode The Perl code string to evaluate
* @param capturedVars Variables to capture from outer scope
* @param sourceName Source name for error messages
* @param sourceLine Source line for error messages
* @return RuntimeScalar result of evaluation (undef on error)
*/
public static RuntimeScalar evalString(String perlCode,
RuntimeBase[] capturedVars,
String sourceName,
int sourceLine) {
try {
// Clear $@ at start
GlobalVariable.getGlobalVariable("main::@").set("");
// Parse the string
Lexer lexer = new Lexer(perlCode);
List<LexerToken> tokens = lexer.tokenize();
CompilerOptions opts = new CompilerOptions();
opts.fileName = sourceName + " (eval)";
ScopedSymbolTable symbolTable = new ScopedSymbolTable();
ErrorMessageUtil errorUtil = new ErrorMessageUtil(sourceName, tokens);
EmitterContext ctx = new EmitterContext(
new JavaClassInfo(),
symbolTable,
null, null,
RuntimeContextType.SCALAR,
false,
errorUtil,
opts,
null
);
Parser parser = new Parser(ctx, tokens);
Node ast = parser.parse();
// Compile to bytecode.
// IMPORTANT: Do NOT call compiler.setCompilePackage() here — same reason as the
// first evalString overload above: it corrupts die/warn location baking.
BytecodeCompiler compiler = new BytecodeCompiler(
sourceName + " (eval)",
sourceLine
);
InterpretedCode evalCode = compiler.compile(ast, ctx); // Pass ctx for context propagation
if (RuntimeCode.DISASSEMBLE) {
System.out.println(evalCode.disassemble());
}
// Store source lines in debugger symbol table if $^P flags are set
int debugFlags = GlobalVariable.getGlobalVariable(GlobalContext.encodeSpecialVar("P")).getInt();
if (debugFlags != 0) {
String evalFilename = RuntimeCode.getNextEvalFilename();
RuntimeCode.storeSourceLines(perlCode, evalFilename, ast, tokens);
}
// Attach captured variables
evalCode = evalCode.withCapturedVars(capturedVars);
// Execute
RuntimeArray args = new RuntimeArray();
RuntimeList result = evalCode.apply(args, RuntimeContextType.SCALAR);
return result.scalar();
} catch (Exception e) {
WarnDie.catchEval(e);
return RuntimeScalarCache.scalarUndef;
}
}
/**
* Detect which variables from outer scope are referenced in eval string.
*
* This is used for proper variable capture (similar to closure analysis).
* TODO: Implement proper lexical variable detection from AST
*
* @param ast The parsed AST
* @return Map of variable names to their types
*/
private static Map<String, String> detectCapturedVariables(Node ast) {
// TODO: Use VariableCollectorVisitor or similar to detect:
// - Lexical variables referenced from outer scope
// - Package variables accessed
// For now, return empty map
return new HashMap<>();
}
}