/
RakudoCodeRef.java
174 lines (148 loc) · 5.96 KB
/
RakudoCodeRef.java
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
package Rakudo.Metamodel.Representations;
import java.util.HashMap;
import Rakudo.Runtime.Context;
import Rakudo.Runtime.Exceptions.Handler;
import Rakudo.Runtime.ThreadContext;
import Rakudo.Runtime.Lexpad;
import Rakudo.Runtime.Parameter;
import Rakudo.Runtime.Signature;
import Rakudo.Metamodel.SharedTable;
import Rakudo.Metamodel.Hints;
import Rakudo.Metamodel.Representation;
import Rakudo.Metamodel.RakudoObject;
import Rakudo.Serialization.SerializationContext;
/// <summary>
/// A representation for low-level code references. This is something
/// specific to this Rakudo backend, not something standard accross all
/// Rakudo backends.
/// </summary>
public final class RakudoCodeRef implements Representation
{
// Interface for the purpose of constructing anonymous classes that
// implement it, as the Java equivalent of a C# lambda expression.
// Used in for example: CodeObjectUtility
public interface IFunc_Body {
public RakudoObject Invoke(ThreadContext tc, RakudoObject meth, RakudoObject capt);
} // C# has public Func<ThreadContext, RakudoObject, RakudoObject, RakudoObject>; // TODO: why 4 parameters and not 3?
/// <summary>
/// This is how the boxed form of a P6str looks.
/// </summary>
public final class Instance extends RakudoObject
{
/// <summary>
/// The code body - the thing that actually runs instructions.
/// </summary>
public RakudoCodeRef.IFunc_Body Body; // IFunc_Body is defined above Instance
/// <summary>
/// The static lexpad.
/// </summary>
public Lexpad StaticLexPad;
/// <summary>
/// Our static outer block.
/// </summary>
public Instance OuterBlock;
/// <summary>
/// Signature object.
/// </summary>
public Signature Sig;
/// <summary>
/// The context currently using this sub.
/// </summary>
public Context CurrentContext;
/// <summary>
/// Exception handlers this block has, if any.
/// </summary>
public Handler[] Handlers;
/// <summary>
/// Constructor.
/// </summary>
public Instance(SharedTable sharedTable)
{
this.setSTable(sharedTable);
}
}
/// <summary>
/// Create a new type object.
/// </summary>
/// <param name="MetaPackage"></param>
/// <returns></returns>
public RakudoObject type_object_for(ThreadContext tc, RakudoObject MetaPackage)
{
// Do the usual bits of setup for the type-object.
SharedTable sTable = new SharedTable();
sTable.HOW = MetaPackage;
sTable.REPR = this;
sTable.WHAT = new Instance(sTable);
// Also twiddle the S-Table's Invoke to invoke the contained
// function.
sTable.Invoke = new IFunc_Body() { // the C# version has a lambda
public RakudoObject Invoke( ThreadContext tci, RakudoObject meth, RakudoObject capt )
{
return ((RakudoCodeRef.Instance)meth).Body.Invoke(tci, meth, capt);
}
};
return sTable.WHAT;
}
/// <summary>
/// Creates an instance of the type with the given type object.
/// </summary>
/// <param name="WHAT"></param>
/// <returns></returns>
public RakudoObject instance_of(ThreadContext tc, RakudoObject rakudoObject)
{
return new Instance(rakudoObject.getSTable());
}
/// <summary>
/// Determines if the representation is defined or not.
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
public boolean defined(ThreadContext tc, RakudoObject rakudoObject)
{
return ((Instance)rakudoObject).Body != null;
}
public RakudoObject get_attribute(ThreadContext tc, RakudoObject Object, RakudoObject ClassHandle, String Name)
{
throw new UnsupportedOperationException("RakudoCodeRef objects cannot store additional attributes.");
}
public RakudoObject get_attribute_with_hint(ThreadContext tc, RakudoObject Object, RakudoObject ClassHandle, String Name, int Hint)
{
throw new UnsupportedOperationException("RakudoCodeRef objects cannot store additional attributes.");
}
public void bind_attribute(ThreadContext tc, RakudoObject Object, RakudoObject ClassHandle, String Name, RakudoObject Value)
{
throw new UnsupportedOperationException("RakudoCodeRef objects cannot store additional attributes.");
}
public void bind_attribute_with_hint(ThreadContext tc, RakudoObject Object, RakudoObject ClassHandle, String Name, int Hint, RakudoObject Value)
{
throw new UnsupportedOperationException("RakudoCodeRef objects cannot store additional attributes.");
}
public int hint_for(ThreadContext tc, RakudoObject ClassHandle, String Name)
{
return Hints.NO_HINT;
}
public void set_int(ThreadContext tc, RakudoObject Object, int Value)
{
throw new UnsupportedOperationException("This type of representation cannot box a native int");
}
public int get_int(ThreadContext tc, RakudoObject Object)
{
throw new UnsupportedOperationException("This type of representation cannot unbox to a native int");
}
public void set_num(ThreadContext tc, RakudoObject Object, double Value)
{
throw new UnsupportedOperationException("This type of representation cannot box a native num");
}
public double get_num(ThreadContext tc, RakudoObject Object)
{
throw new UnsupportedOperationException("This type of representation cannot unbox to a native num");
}
public void set_str(ThreadContext tc, RakudoObject Object, String Value)
{
throw new UnsupportedOperationException("This type of representation cannot box a native string");
}
public String get_str(ThreadContext tc, RakudoObject Object)
{
throw new UnsupportedOperationException("This type of representation cannot unbox to a native string");
}
}