-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRuntimeStashEntry.java
More file actions
296 lines (259 loc) · 13.2 KB
/
RuntimeStashEntry.java
File metadata and controls
296 lines (259 loc) · 13.2 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
package org.perlonjava.runtime;
import org.perlonjava.mro.InheritanceResolver;
import static org.perlonjava.runtime.RuntimeScalarCache.scalarFalse;
import static org.perlonjava.runtime.RuntimeScalarCache.scalarTrue;
import static org.perlonjava.runtime.RuntimeScalarType.*;
/**
* Represents a stash entry in Perl.
*/
public class RuntimeStashEntry extends RuntimeGlob {
/**
* Constructor for RuntimeStashEntry.
* Initializes a new instance of the RuntimeStashEntry class with the specified glob name.
*
* @param globName The name of the typeglob.
*/
public RuntimeStashEntry(String globName, boolean isDefined) {
super(globName);
if (!isDefined) {
type = RuntimeScalarType.UNDEF;
}
// System.out.println("Stash Entry create: " + globName + " " + isDefined);
}
// Note on Stash Operations:
//
// In Perl, a typeglob is a structure that holds a symbol table entry and a key (or slot).
// An example of using a typeglob is:
// $constant::{_CAN_PCS} = \$const;
// This line effectively binds a constant at compile time, allowing it to be accessed without a sigil.
//
// When Perl encounters a bareword (such as _CAN_PCS), it resolves it by:
// 1. Checking if it matches a subroutine name.
// 2. Looking up the symbol table for a corresponding typeglob entry.
// 3. If a reference is found in the symbol table, Perl uses that value.
//
// Additionally, if you place an array reference in a symbol table slot where Perl expects a subroutine reference
// (e.g., using \@list where a \&sub would normally be), Perl automatically creates a subroutine that returns
// the array value when called. This behavior also applies to scalar values.
/**
* Sets the value of the typeglob based on the type of the provided RuntimeScalar.
* Supports setting CODE and GLOB types, with special handling for IO objects.
*
* @param value The RuntimeScalar value to set.
* @return The set RuntimeScalar value.
* @throws IllegalStateException if the typeglob assignment is not implemented for the given type.
*/
public RuntimeScalar set(RuntimeScalar value) {
type = RuntimeScalarType.GLOB;
if (value.type == REFERENCE) {
if (value.value instanceof RuntimeScalar) {
RuntimeScalar deref = value.scalarDeref();
if (deref.type == CODE) {
// `*foo = \&bar` creates a constant subroutine returning the code reference
RuntimeCode code = new RuntimeCode("", null);
code.constantValue = deref.getList();
GlobalVariable.getGlobalCodeRef(this.globName).set(
new RuntimeScalar(code));
InheritanceResolver.invalidateCache();
} else if (deref.type == HASHREFERENCE) {
// `*foo = \$hash_ref` creates a constant subroutine returning the hash reference
RuntimeCode code = new RuntimeCode("", null);
code.constantValue = deref.getList();
GlobalVariable.getGlobalCodeRef(this.globName).set(
new RuntimeScalar(code));
} else if (deref.type == ARRAYREFERENCE) {
// `*foo = \$array_ref` creates a constant subroutine returning the array reference
RuntimeCode code = new RuntimeCode("", null);
code.constantValue = deref.getList();
GlobalVariable.getGlobalCodeRef(this.globName).set(
new RuntimeScalar(code));
} else if (deref.type == HASHREFERENCE && deref.value instanceof RuntimeHash hash) {
// `*foo = \%bar` assigns to the HASH slot.
GlobalVariable.globalHashes.put(this.globName, hash);
} else if (deref.type == ARRAYREFERENCE && deref.value instanceof RuntimeArray arr) {
// `*foo = \@bar` assigns to the ARRAY slot.
GlobalVariable.globalArrays.put(this.globName, arr);
} else if (deref.type == GLOB) {
// `*foo = \*bar` creates a constant subroutine returning the glob
RuntimeCode code = new RuntimeCode("", null);
code.constantValue = new RuntimeList(deref);
GlobalVariable.getGlobalCodeRef(this.globName).set(
new RuntimeScalar(code));
} else {
// Default: scalar slot.
GlobalVariable.globalVariables.put(this.globName, deref);
// Also create a constant subroutine for bareword access
RuntimeCode code = new RuntimeCode("", null);
code.constantValue = deref.getList();
GlobalVariable.getGlobalCodeRef(this.globName).set(
new RuntimeScalar(code));
}
}
return value;
}
if (value.type == ARRAYREFERENCE) {
if (value.value instanceof RuntimeArray) {
RuntimeArray targetArray = value.arrayDeref();
// Make the target array slot point to the same RuntimeArray object (aliasing)
GlobalVariable.globalArrays.put(this.globName, targetArray);
// Also create a constant subroutine for bareword access
RuntimeCode code = new RuntimeCode("", null);
code.constantValue = targetArray.getList();
GlobalVariable.getGlobalCodeRef(this.globName).set(
new RuntimeScalar(code));
}
return value;
}
switch (value.type) {
case CODE:
GlobalVariable.getGlobalCodeRef(this.globName).set(value);
// Invalidate the method resolution cache
InheritanceResolver.invalidateCache();
return value;
case FORMAT:
// Handle format assignments to typeglobs
GlobalVariable.getGlobalFormatRef(this.globName).set(value);
return value;
case GLOB:
if (value.value instanceof RuntimeIO) {
// *STDOUT = $new_handle
GlobalVariable.getGlobalIO(this.globName).set(value);
} else if (value.value instanceof RuntimeGlob sourceGlob) {
// *dest = *source - copy all slots from source glob to dest glob
String sourceGlobName = sourceGlob.globName;
// Copy all slots from source to destination
this.set(GlobalVariable.getGlobalCodeRef(sourceGlobName));
this.set(GlobalVariable.getGlobalIO(sourceGlobName));
this.set(GlobalVariable.getGlobalArray(sourceGlobName).createReference());
this.set(GlobalVariable.getGlobalHash(sourceGlobName).createReference());
this.set(GlobalVariable.getGlobalVariable(sourceGlobName).createReference());
this.set(GlobalVariable.getGlobalFormatRef(sourceGlobName));
}
return value;
// Handle the case where a typeglob is assigned a reference to an array
case HASHREFERENCE:
if (value.value instanceof RuntimeHash) {
GlobalVariable.getGlobalHash(this.globName).setFromList(((RuntimeHash) value.value).getList());
}
return value;
case UNDEF:
return value;
case STRING:
case BYTE_STRING:
// Assigning a string to a stash entry sets the prototype for that symbol
// e.g., $::{foo} = '$$' sets the prototype of &foo to '$$'
RuntimeScalar codeRef = GlobalVariable.getGlobalCodeRef(this.globName);
if (codeRef.type == CODE) {
((RuntimeCode) codeRef.value).prototype = value.toString();
} else {
// Create a new RuntimeCode with the prototype
RuntimeCode code = new RuntimeCode(value.toString(), null);
codeRef.set(new RuntimeScalar(code));
}
return value;
case GLOBREFERENCE:
// `*foo = \*bar` creates a constant subroutine returning the glob
if (value.value instanceof RuntimeGlob glob) {
RuntimeCode code = new RuntimeCode("", null);
code.constantValue = new RuntimeList(new RuntimeScalar(glob));
GlobalVariable.getGlobalCodeRef(this.globName).set(
new RuntimeScalar(code));
}
return value;
}
throw new IllegalStateException("typeglob assignment not implemented for " + value.type);
}
/**
* Sets the current RuntimeScalar object to the values associated with the given RuntimeGlob.
* This method effectively implements the behavior of assigning one typeglob to another,
* similar to Perl's typeglob assignment.
*
* @param value The RuntimeGlob object whose associated values are to be assigned.
* @return The scalar value associated with the provided RuntimeGlob.
*/
public RuntimeScalar set(RuntimeStashEntry value) {
type = RuntimeScalarType.GLOB;
// Retrieve the name of the glob from the provided RuntimeGlob object.
String globName = value.globName;
// Set the current scalar to the global code reference associated with the glob name.
this.set(GlobalVariable.getGlobalCodeRef(globName));
// Set the current scalar to the global IO (input/output) reference associated with the glob name.
this.set(GlobalVariable.getGlobalIO(globName));
// Set the current scalar to a reference of the global array associated with the glob name.
this.set(GlobalVariable.getGlobalArray(globName).createReference());
// Set the current scalar to a reference of the global hash associated with the glob name.
this.set(GlobalVariable.getGlobalHash(globName).createReference());
// Set the current scalar to a reference of the global variable associated with the glob name.
this.set(GlobalVariable.getGlobalVariable(globName).createReference());
// Set the current scalar to the global format reference associated with the glob name.
this.set(GlobalVariable.getGlobalFormatRef(globName));
// Return the scalar value associated with the provided RuntimeGlob.
return value.scalar();
}
/**
* Returns a boolean indicating whether the typeglob is defined.
*
* @return Always true, as typeglobs are always considered defined.
*/
public boolean getDefinedBoolean() {
// System.out.println("Stash Entry getDefinedBoolean: " + (type == RuntimeScalarType.UNDEF ? "undef" : "defined"));
return type != RuntimeScalarType.UNDEF;
}
/**
* Retrieves the boolean value of the typeglob.
*
* @return Always true, indicating the presence of the typeglob.
*/
public boolean getBoolean() {
// System.out.println("Stash Entry getBoolean: " + (type == RuntimeScalarType.UNDEF ? "undef" : "defined"));
return getDefinedBoolean();
}
public RuntimeScalar defined() {
// System.out.println("Stash Entry defined: " + (type == RuntimeScalarType.UNDEF ? "undef" : "defined"));
return getDefinedBoolean() ? scalarTrue : scalarFalse;
}
/**
* Sets itself from a RuntimeList.
*
* @param value The RuntimeList from which this typeglob will be set.
* @return The updated RuntimeArray.
*/
public RuntimeArray setFromList(RuntimeList value) {
return new RuntimeArray(this.set(value.scalar()));
}
/**
* Undefines the elements of the typeglob.
* This method clears all slots (CODE, FORMAT, SCALAR, ARRAY, HASH) and invalidates the method resolution cache.
*
* @return The current RuntimeGlob instance after undefining its elements.
*/
public RuntimeStashEntry undefine() {
// Undefine CODE
GlobalVariable.getGlobalCodeRef(this.globName).set(new RuntimeScalar());
// Undefine FORMAT
GlobalVariable.getGlobalFormatRef(this.globName).undefineFormat();
// Undefine SCALAR
GlobalVariable.getGlobalVariable(this.globName).set(new RuntimeScalar());
// Undefine ARRAY - create empty array
GlobalVariable.globalArrays.put(this.globName, new RuntimeArray());
// Undefine HASH - create empty hash
GlobalVariable.globalHashes.put(this.globName, new RuntimeHash());
// Invalidate the method resolution cache
InheritanceResolver.invalidateCache();
type = RuntimeScalarType.UNDEF;
return this;
}
/**
* Returns a string representation of the stash entry.
* For defined stash entries, returns the glob representation.
* For undefined stash entries, returns undef.
*
* @return A string representation of the stash entry.
*/
@Override
public String toString() {
// For stash entries, always return the glob representation
// This matches Perl's behavior where stash entries stringify to "*PackageName::symbol"
return "*" + this.globName;
}
}