/
IntrinsicMethod.java
337 lines (310 loc) · 9.86 KB
/
IntrinsicMethod.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
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
327
328
329
330
331
332
333
334
335
336
337
/*
* This file is part of the Jikes RVM project (http://jikesrvm.org).
*
* This file is licensed to You under the Common Public License (CPL);
* You may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.opensource.org/licenses/cpl1.0.php
*
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership.
*/
package org.mmtk.harness.lang.ast;
import java.util.ArrayList;
import java.util.List;
import org.mmtk.harness.lang.Env;
import org.mmtk.harness.lang.Trace;
import org.mmtk.harness.lang.Visitor;
import org.mmtk.harness.lang.Trace.Item;
import org.mmtk.harness.lang.runtime.BoolValue;
import org.mmtk.harness.lang.runtime.IntValue;
import org.mmtk.harness.lang.runtime.ObjectValue;
import org.mmtk.harness.lang.runtime.StringValue;
import org.mmtk.harness.lang.runtime.Value;
/**
* A method that is implemented directly in Java rather than in the scripting language.
*
* This class also contains definitions of the built-in intrinsic methods.
*/
public class IntrinsicMethod extends Method {
/** The actual Java method */
private final java.lang.reflect.Method method;
/** The Java signature of the method */
private final Class<?>[] signature;
/************************************************************************
* helper methods for the constructors
*
*/
/**
* Find the appropriate scripting language type for a given Java type
*
* @param externalType The java type
* @return
*/
private static Type internalType(Class<?> externalType) {
if (externalType.equals(int.class) ||
externalType.equals(Integer.class) ||
externalType.equals(IntValue.class)) {
return Type.INT;
} else if (externalType.equals(String.class) ||
externalType.equals(StringValue.class)) {
return Type.STRING;
} else if (externalType.equals(ObjectValue.class)) {
return Type.OBJECT;
} else if (externalType.equals(boolean.class) ||
externalType.equals(Boolean.class) ||
externalType.equals(BoolValue.class)) {
return Type.BOOLEAN;
} else if (externalType.equals(void.class)) {
return Type.VOID;
}
return null;
}
/**
* Do a reflective method lookup. We look for a method with a first parameter
* of 'Env env', which is hidden from the script-language specification of the
* method.
*
* @param className
* @param methodName
* @param signature
* @return
*/
private static java.lang.reflect.Method getJavaMethod(String className,
String methodName, Class<?>[] signature) {
try {
Class<?> klass = Class.forName(className);
Class<?>[] realSignature = new Class<?>[signature.length+1];
realSignature[0] = Env.class;
System.arraycopy(signature, 0, realSignature, 1, signature.length);
return klass.getDeclaredMethod(methodName, realSignature);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Turn a string into a class object, applying some simple mappings for
* primitives etc.
* @param param
* @return
*/
private static Class<?> classForParam(String param) {
Class<?> r;
if (param.equals("int")) {
r = int.class;
} else if (param.equals("long")) {
r = long.class;
} else if (param.equals("byte")) {
r = byte.class;
} else if (param.equals("short")) {
r = short.class;
} else if (param.equals("char")) {
r = char.class;
} else if (param.equals("boolean")) {
r = boolean.class;
} else try {
r = Class.forName(param);
} catch (ClassNotFoundException e) {
// As a last chance, try looking for the class in java.lang
try {
r = Class.forName("java.lang."+param);
} catch (ClassNotFoundException f) {
throw new RuntimeException(e);
}
}
return r;
}
/**
* Turn an array of strings (class names) into an array of Class objects
* @param params
* @return
*/
private static Class<?>[] classesForParams(List<String> params) {
Class<?>[] result = new Class<?>[params.size()];
for (int i=0; i < params.size(); i++) {
result[i] = classForParam(params.get(i));
}
return result;
}
/************************************************************************
*
* Constructors
*
*/
/**
* Constructor
*/
public IntrinsicMethod(String name, String className, String methodName, List<String> params) {
this(name,className, methodName,classesForParams(params));
}
/**
* Create an intrinsic that calls a static method.
* @param name Script language name of the method
* @param className Java class in which the intrinsic occurs
* @param methodName Java name of the method
* @param signature Java types of the parameters, excluding the mandatory Env first parameter
*/
public IntrinsicMethod(String name, String className, String methodName, Class<?>[] signature) {
this(name,getJavaMethod(className, methodName,signature),signature);
}
/**
* Create an intrinsic that calls a static method with no parameters
*
* @param name Script language name of the method
* @param className Java class in which the intrinsic occurs
* @param methodName Java name of the method
*/
public IntrinsicMethod(String name, String className, String methodName) {
this(name,className,methodName,new Class<?>[] {});
}
/**
* Internal 'helper' constructor
* @param name Script-language name of the method
* @param method Java Method
* @param signature Java signature (without the mandatory Env parameter)
*/
private IntrinsicMethod(String name, java.lang.reflect.Method method, Class<?>[] signature) {
this(name,method,internalType(method.getReturnType()),signature);
}
/**
* Internal constructor - ultimately all constructors call this one.
* @param name Script-language name of the method
* @param method Java Method
* @param returnType Script-language return type
* @param signature Java signature (without the mandatory Env parameter)
*/
private IntrinsicMethod(String name, java.lang.reflect.Method method, Type returnType, Class<?>[] signature) {
super(name,signature.length,returnType);
this.method = method;
this.signature = signature;
}
/**
* Take a script-language value and return the corresponding Java value.
* This can only be done for a limited number of types ...
*
* @param v The script-language value
* @param klass The Java target type
* @return An object of type Class<klass>.
*/
private Object marshall(Value v, Class<?> klass) {
switch (v.type()) {
case INT:
if (klass.isAssignableFrom(IntValue.class)) {
return v;
} else {
return Integer.valueOf(v.getIntValue());
}
case BOOLEAN:
if (klass.isAssignableFrom(BoolValue.class)) {
return v;
} else {
return Boolean.valueOf(v.getBoolValue());
}
case STRING:
if (klass.isAssignableFrom(StringValue.class)) {
return v;
} else {
return v.getStringValue();
}
case OBJECT:
if (klass.isAssignableFrom(ObjectValue.class)) {
return v;
} else {
throw new RuntimeException("Can't marshall an object into a Java Object");
}
default:
throw new RuntimeException("Unknown type in intrinsic call");
}
}
/**
* Marshall an array of values, adding the mandatory Env value.
* @param env
* @param params
* @return
*/
private Object[] marshall(Env env, Value[] params) {
assert params.length == signature.length : "Signature doesn't match params";
Object[] marshalled = new Object[params.length+1];
marshalled[0] = env;
for (int i=0; i < params.length; i++) {
marshalled[i+1] = marshall(params[i], signature[i]);
}
return marshalled;
}
/**
* Convert a return type from a Java type to a scripting language Value.
* @param obj
* @return
*/
private Value unMarshall(Object obj) {
if (returnType == Type.VOID) {
return null;
} else if (obj instanceof Integer) {
assert returnType == Type.INT : "mismatched return types";
return IntValue.valueOf(((Integer)obj).intValue());
} else if (obj instanceof String) {
assert returnType == Type.STRING : "mismatched return types";
return new StringValue((String)obj);
} else if (obj instanceof Boolean) {
assert returnType == Type.BOOLEAN : "mismatched return types";
return BoolValue.valueOf(((Boolean)obj).booleanValue());
}
throw new RuntimeException("Can't unmarshall a "+obj.getClass().getCanonicalName());
}
/**
* Invoke the intrinsic method.
* @param env
* @param values
* @return
*/
Object invoke(Env env, Value[] values) {
Trace.trace(Item.INTRINSIC,"Executing "+toString());
try {
Object result = method.invoke(null, marshall(env,values));
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Evaluate the method as an expression
* @see Method{@link #eval(Env, Value...)}
*
* Also used by the visitor to evaluate ...
*/
public Value eval(Env env, Value...values) {
return unMarshall(invoke(env,values));
}
/**
* Execute the method as a statement
*/
public void exec(Env env, Value...values) {
invoke(env,values);
}
/**
* Convert to a string
*/
@Override
public String toString() {
return method.getName();
}
/**
* Accept visitors
*/
@Override
public void accept(Visitor v) {
v.visit(this);
}
/**
* Return parameter types, in script-language terms.
*/
@Override
public List<Type> getParamTypes() {
List<Type> result = new ArrayList<Type>(params);
for (Class<?> paramClass : signature) {
result.add(internalType(paramClass));
}
return result;
}
}