/
RubyAPI.java
670 lines (550 loc) · 22.3 KB
/
RubyAPI.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/**
* Copyright 2005-2007 Xue Yong Zhi, Ye Zheng
* Distributed under the BSD License
*/
package com.xruby.runtime.lang;
//import j2me.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
import j2me.util.Iterator;
import j2me.lang.AssertMe;
import com.rho.RhoEmptyLogger;
import com.rho.RhoLogger;
import com.xruby.runtime.builtin.ObjectFactory;
import com.xruby.runtime.builtin.RubyArray;
import com.xruby.runtime.builtin.RubyMethodValue;
import com.xruby.runtime.builtin.RubyProc;
import com.xruby.runtime.builtin.RubyString;
//import com.xruby.runtime.javasupport.JavaClass;
import com.xruby.runtime.builtin.RubyTypesUtil;
public class RubyAPI {
private static final RhoLogger LOG = RhoLogger.RHO_STRIP_LOG ? new RhoEmptyLogger() :
new RhoLogger("RubyAPI");
public static RubyClass defineClass(String name, RubyClass superclass) {
return ClassFactory.defineClass(name, superclass);
}
/* For compiler */
public static RubyClass defineClass(String name, RubyValue superclass) {
if (null != superclass && !(superclass instanceof RubyClass)) {
throw new RubyException(RubyRuntime.TypeErrorClass, "superclass must be a Class (" + superclass.getRubyClass().getName() + " given)");
}
RubyValue value = RubyRuntime.ObjectClass.getConstant(name);
if (value != null) {
if (!(value instanceof RubyClass)) {
throw new RubyException(RubyRuntime.TypeErrorClass, name + " is not a class");
}
RubyClass klass = (RubyClass)value;
if (superclass != null) {
if (superclass != klass.getSuperClass().getRealClass()) {
throw new RubyException(RubyRuntime.TypeErrorClass, "superclass mismatch for class " + name);
}
}
klass.setAccessPublic();
return klass;
}
return defineClass(name, null == superclass ? null : (RubyClass)superclass);
}
public static RubyModule defineModule(String name) {
return ClassFactory.defineModule(name);
}
public static boolean testCaseEqual(RubyValue value1, RubyValue value2) {
return RubyAPI.callPublicOneArgMethod(value1, value2, null, RubyID.longEqualID).isTrue();
}
public static boolean testCaseEqual(RubyArray values, RubyValue value2) {
// for (RubyValue value1 : values) {
for (Iterator iter = values.iterator(); iter.hasNext();) {
RubyValue value1 = (RubyValue)iter.next();
if (RubyAPI.callPublicOneArgMethod(value1, value2, null, RubyID.longEqualID).isTrue()) {
return true;
}
}
return false;
}
public static boolean testEqual(RubyValue value1, RubyValue value2) {
return RubyAPI.callPublicOneArgMethod(value1, value2, null, RubyID.equalID).isTrue();
}
public static boolean testCaseEqualNotNil(RubyValue value1, RubyValue value2)
{
return value1.isTrue();
}
public static boolean testExceptionType(RubyArray classes_to_compare, RubyException e) {
RubyValue value = e.getRubyValue();
// for (RubyValue class_to_compare : classes_to_compare) {
for (Iterator iter = classes_to_compare.iterator(); iter.hasNext();) {
RubyValue class_to_compare = (RubyValue)iter.next();
if (RubyAPI.isKindOf(class_to_compare, value)) {
return true;
}
}
return false;
}
public static boolean isKindOf(RubyValue class_to_compare, RubyValue value) {
if (class_to_compare == value) {
return true;
} else if (!(class_to_compare instanceof RubyModule)) {
//TODO should test include relationship with Module
return false;
} else {
return value.getRubyClass().isKindOf((RubyModule) class_to_compare);
}
}
//e.g. defined? super
public static RubyValue isDefinedSuperMethod(RubyValue receiver, String method_name, RubyMethod current_method) {
RubyClass c = (RubyClass) current_method.getScope();
RubyID mid = RubyID.intern(method_name);
RubyMethod m = c.findSuperMethod(mid);
if (null == m || UndefMethod.isUndef(m)) {
return RubyConstant.QNIL;
}
return ObjectFactory.createString("super");
}
//e.g. defined? a.f
//TODO bad name, should be isDefinedMethodWithReceiver
public static RubyValue isDefinedPublicMethod(RubyValue receiver, RubyModule owner, String method_name) {
if (receiver.getRubyClass() == owner) {
//TODO do not have to be equal
return isDefinedMethod(receiver, method_name);
}
RubyID mid = RubyID.intern(method_name);
RubyMethod m = receiver.findPublicMethod(mid);
if (null == m || UndefMethod.isUndef(m)) {
return RubyConstant.QNIL;
}
return ObjectFactory.createString("method");
}
//e.g. defined? f
public static RubyValue isDefinedMethod(RubyValue receiver, String method_name) {
RubyID mid = RubyID.intern(method_name);
RubyMethod m = receiver.findMethod(mid);
if (null == m || UndefMethod.isUndef(m)) {
return RubyConstant.QNIL;
}
return ObjectFactory.createString("method");
}
//e.g. defined? @m
public static RubyValue isDefinedInstanceVariable(RubyValue receiver, String method_name) {
RubyID mid = RubyID.intern(method_name);
RubyValue var = receiver.getInstanceVariable(mid);
if (null == var || var == RubyConstant.QNIL) {
return RubyConstant.QNIL;
}
return ObjectFactory.createString("instance-variable");
}
public static RubyValue isDefinedNonPrivateMethod(RubyValue receiver, String method_name) {
RubyID mid = RubyID.intern(method_name);
RubyMethod m = receiver.findMethod(mid);
if (null == m || UndefMethod.isUndef(m) || m.getAccess() == RubyMethod.PRIVATE) {
return RubyConstant.QNIL;
}
return ObjectFactory.createString("method");
}
public static RubyValue isDefinedYield(RubyBlock block) {
if (null == block) {
return RubyConstant.QNIL;
} else {
return ObjectFactory.createString("yield");
}
}
private static RubyValue callMethodMissing(RubyValue receiver, RubyArray args, RubyBlock block, RubyID mid) {
RubyMethod m = receiver.findMethod(RubyID.methodMissingId);
if (null != m && !UndefMethod.isUndef(m)) {
if (null == args) {
args = new RubyArray(ObjectFactory.createSymbol(mid));
} else {
args.insert(0, ObjectFactory.createSymbol(mid));
}
return m.invoke(receiver, args, block);
}
RubyClass klass = receiver.getRubyClass();
klass = (klass != null) ? klass.getRealClass() : null;
throw new RubyException(RubyRuntime.NoMethodErrorClass, "undefined method '" + mid.toString() + "' for " + klass.getName());
}
private static RubyValue processException( Exception e, RubyValue receiver, RubyID mid )throws RubyException
{
if ( e instanceof RubyException )
{
RubyException exc = (RubyException)e;
if ( exc.getRubyValue().getRubyClass() == RubyRuntime.EOFErrorClass )
throw exc;
}
String className = "Unknown::";
if ( receiver instanceof RubyClass)
className = ((RubyClass)receiver).getName() + "::";
else
{
if ( receiver.getRubyClass() != null )
className = receiver.getRubyClass().getName() + ".";
}
String strTraceMsg = className + mid.toString();
String strErrMsg = strTraceMsg + " failed.";
if ( e instanceof RubyException )
{
RubyException exc = (RubyException)e;
exc.getRubyValue().addBacktrace(strTraceMsg);
LOG.ERROR( strTraceMsg + " failed: " + e.getMessage() );
}else
LOG.ERROR( strErrMsg, e);
throw (e instanceof RubyException ? (RubyException)e : new RubyException( strErrMsg + e.getMessage()));
}
//receiver is implicit self
public static RubyValue callMethod(RubyValue receiver, RubyArray args, RubyBlock block, RubyID mid)
{
try{
AssertMe.rho_assert(null == args || args.size() > 1);//use callOneArgMethod if has only one arg
RubyMethod m = receiver.findMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, args, block);
}
return callMethodMissing(receiver, args, block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
public static RubyValue callNoArgMethod(RubyValue receiver, RubyBlock block, RubyID mid)
{
try{
RubyMethod m = receiver.findMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, block);
}
return callMethodMissing(receiver, null, block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
//method call with *one* argument and no block (use the other one if no arg (arg == null)!)
//This make code (especially reverse engineered ones) more readable.
public static RubyValue callOneArgMethod(RubyValue receiver, RubyValue arg, RubyBlock block, RubyID mid)
{
try{
AssertMe.rho_assert(null != arg);
RubyMethod m = receiver.findMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, arg, block);
}
return callMethodMissing(receiver, new RubyArray(arg), block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
public static RubyValue callTwoArgMethod(RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block, RubyID mid)
{
try{
AssertMe.rho_assert(null != arg0 && null != arg1);
RubyMethod m = receiver.findMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, arg0, arg1, block);
}
return callMethodMissing(receiver, new RubyArray(arg0, arg1), block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
public static RubyValue callPublicNoArgMethod(RubyValue receiver, RubyBlock block, RubyID mid)
{
try{
RubyValue res = receiver.rhom_processProperty(mid);
if ( res != null )
return res;
RubyMethod m = receiver.findPublicMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, block);
}
return callMethodMissing(receiver, null, block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
//method call with *one* argument and no block (use the other one if no arg (arg == null)!)
//This make code (especially reverse engineered ones) more readable.
public static RubyValue callPublicOneArgMethod(RubyValue receiver, RubyValue arg, RubyBlock block, RubyID mid)
{
try{
AssertMe.rho_assert(null != arg);
RubyMethod m = receiver.findPublicMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, arg, block);
}
return callMethodMissing(receiver, new RubyArray(arg), block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
public static RubyValue callPublicTwoArgMethod(RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block, RubyID mid)
{
try{
AssertMe.rho_assert(null != arg0 && null != arg1);
RubyMethod m = receiver.findPublicMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, arg0, arg1, block);
}
return callMethodMissing(receiver, new RubyArray(arg0, arg1), block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
//TODO should pass owner to work with protected method
public static RubyValue callPublicMethod(RubyValue receiver, RubyArray args, RubyBlock block, RubyID mid) {
//RHO
//AssertMe.rho_assert(null == args || args.size() > 1);//use callPublicOneArgMethod if has only one arg
//RHO
try{
RubyMethod m = receiver.findPublicMethod(mid);
if (null != m && !UndefMethod.isUndef(m)) {
return m.invoke(receiver, args, block);
}
return callMethodMissing(receiver, args, block, mid);
}catch(Exception e)
{
return processException(e,receiver,mid);
}
}
public static RubyValue callSuperNoArgMethod(RubyValue receiver, RubyBlock block, MethodBlockBase mbb)
{
try{
RubyClass c = (RubyClass) mbb.getScope();
RubyMethod m = c.findSuperMethod(mbb.getID());
if (null == m || UndefMethod.isUndef(m)) {
throw new RubyException(RubyRuntime.NoMethodErrorClass, "super method '" + mbb.getID() + "' can not be found in '" + c.getName() + "'");
}
return m.invoke(receiver, block);
}catch(Exception e)
{
return processException(e,receiver,mbb.getID());
}
}
public static RubyValue callSuperOneArgMethod(RubyValue receiver, RubyValue arg, RubyBlock block, MethodBlockBase mbb)
{
try{
AssertMe.rho_assert(null != arg);
RubyClass c = (RubyClass) mbb.getScope();
RubyMethod m = c.findSuperMethod(mbb.getID());
if (null == m || UndefMethod.isUndef(m)) {
throw new RubyException(RubyRuntime.NoMethodErrorClass, "super method '" + mbb.getID() + "' can not be found in '" + c.getName() + "'");
}
return m.invoke(receiver, arg, block);
}catch(Exception e)
{
return processException(e,receiver,mbb.getID());
}
}
public static RubyValue callSuperTwoArgMethod(RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block, MethodBlockBase mbb)
{
try{
AssertMe.rho_assert(null != arg0 && null != arg1);
RubyClass c = (RubyClass) mbb.getScope();
RubyMethod m = c.findSuperMethod(mbb.getID());
if (null == m || UndefMethod.isUndef(m)) {
throw new RubyException(RubyRuntime.NoMethodErrorClass, "super method '" + mbb.getID() + "' can not be found in '" + c.getName() + "'");
}
return m.invoke(receiver, arg0, arg1, block);
}catch(Exception e)
{
return processException(e,receiver,mbb.getID());
}
}
public static RubyValue callSuperMethod(RubyValue receiver, RubyArray args, RubyBlock block, MethodBlockBase mbb)
{
try{
AssertMe.rho_assert(null == args || args.size() > 1);//use callSuperOneArgMethod if has only one arg
RubyClass c = (RubyClass) mbb.getScope();
RubyMethod m = c.findSuperMethod(mbb.getID());
if (null == m || UndefMethod.isUndef(m)) {
throw new RubyException(RubyRuntime.NoMethodErrorClass, "super method '" + mbb.getID() + "' can not be found in '" + c.getName() + "'");
}
return m.invoke(receiver, args, block);
}catch(Exception e)
{
return processException(e,receiver,mbb.getID());
}
}
public static RubyValue operatorNot(RubyValue value) {
return value.isTrue() ? RubyConstant.QFALSE : RubyConstant.QTRUE;
}
public static boolean isWindows() {
return false;
/* String strOS = System.getProperty("os.name");
if ( strOS == null )
return false;
return (strOS.toUpperCase().indexOf("WINDOWS") >= 0);*/
}
public static RubyValue runCommandAndCaptureOutput(String value) {
//RHO_COMMENT: runCommandAndCaptureOutput
return ObjectFactory.createString(new StringBuffer());
//some commands are builtin within the shell. e,g. echo, copy...
/* String[] cmdarray = new String[3];
cmdarray[2] = value;
if (isWindows()) {
cmdarray[0] = "cmd";
cmdarray[1] = "/c";
} else {
cmdarray[0] = "/bin/sh";
cmdarray[1] = "-c";
}
try {
Process p = Runtime.getRuntime().exec(cmdarray);
StringBuffer output = new StringBuffer();
BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
int c;
while ((c = stderr.read()) != -1) {
if (c != '\r') {
output.append((char) c);
}
}
stderr.close();
BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((c = stdout.read()) != -1) {
if (c != '\r') {
output.append((char) c);
}
}
stdout.close();
return ObjectFactory.createString(output);
} catch (IOException e) {
throw new RubyException(RubyRuntime.ExceptionClass, e.toString());
}*/
}
public static RubyValue expandArrayIfThereIsZeroOrOneValue(RubyArray a) {
if (a.size() <= 1) {
return a.get(0);
}
return a;
}
public static RubyValue expandArrayIfThereIsZeroOrOneValue(RubyValue v) {
if (v instanceof RubyArray) {
RubyArray a = (RubyArray) v;
if (a.isSingleAsterisk()) {
return expandArrayIfThereIsZeroOrOneValue(a);
}
}
return v;
}
public static RubyArray expandArrayIfThereIsOnlyOneRubyArray(RubyArray a) {
if (a.size() == 1 &&
a.isSingleRhs() &&
a.get(0) instanceof RubyArray) {
return (RubyArray) a.get(0);
}
return a;
}
public static RubyArray convertToArrayIfNotYet(RubyValue v) {
if (v instanceof RubyArray) {
return (RubyArray) v;
}
return new RubyArray(v);
}
public static RubyBlock convertRubyValue2RubyBlock(RubyValue v) {
if (RubyConstant.QNIL == v) {
return null;
} else if (v instanceof RubyMethodValue) {
return ((RubyMethodValue) v).convertToRubyProc().getBlock();
} else if (v instanceof RubyProc) {
return ((RubyProc) v).getBlock();
} else {
throw new RubyException(RubyRuntime.TypeErrorClass, "wrong argument type " + v.getRubyClass().getName() + " (expected Proc) ");
}
}
public static RubyValue convertRubyException2RubyValue(RubyException e) {
return e.getRubyValue();
}
public static RubyValue setTopLevelConstant(RubyValue value, String name) {
return RubyRuntime.ObjectClass.setConstant(name, value);
}
public static RubyValue getCurrentNamespaceConstant(RubyModule receiver, String name) {
RubyValue v = receiver.getConstant(name);
if (null != v) {
return v;
}
v = RubyRuntime.ObjectClass.getConstant(name);
if (null == v) {
//RHO_COMMENT: RubyRuntime.javaIsSupported()
/* if(RubyRuntime.javaIsSupported()){
// for(String pkgName :JavaClass.getPackeageList()){
for(int i = 0; i < JavaClass.getPackeageList().length;i++){
String pkgName = JavaClass.getPackeageList()[i];
try {
Class clazz = Class.forName(pkgName+"."+name);
return JavaClass.createJavaClass(clazz);
} catch (ClassNotFoundException e) {
//do nothing
}
}
}*/
throwUninitializedConstant(receiver, name);
}
return v;
}
public static RubyValue isDefinedCurrentNamespaceConstant(RubyValue receiver, String name) {
RubyValue v = ((RubyModule) receiver).getConstant(name);
if (null == v) {
return RubyConstant.QNIL;
}
return ObjectFactory.createString("constant");
}
public static RubyValue getConstant(RubyValue receiver, String name) {
throwTypeErrorIfNotClassModule(receiver);
RubyModule m = (RubyModule) receiver;
RubyValue v = m.getConstant(name);
if (null == v) {
throwUninitializedConstant(m, name);
}
return v;
}
//RHO_COMMENT: isConstantDefined
public static RubyValue isConstantDefined(RubyValue receiver, String name) {
throwTypeErrorIfNotClassModule(receiver);
RubyModule m = (RubyModule) receiver;
RubyValue v = m.getConstant(name);
if (null == v)
return RubyConstant.QFALSE;
return RubyConstant.QTRUE;
}
private static void throwUninitializedConstant(RubyModule m, String name) {
RubyString str = ObjectFactory.createString();
if (m != RubyRuntime.ObjectClass) {
m.to_s(str);
if (str.length() > 0) {
str.appendString("::");
}
}
str.appendString(name);
throw new RubyException(RubyRuntime.NameErrorClass, "uninitialized constant " + str.toString());
}
public static RubyValue setConstant(RubyValue value, RubyValue receiver, String name) {
throwTypeErrorIfNotClassModule(receiver);
return ((RubyModule) receiver).setConstant(name, value);
}
private static void throwTypeErrorIfNotClassModule(RubyValue receiver) {
if (!(receiver instanceof RubyClass) &&
!(receiver instanceof RubyModule)) {
RubyValue v = RubyAPI.callPublicNoArgMethod(receiver, null, RubyID.toSID);
String s = v.toString();
throw new RubyException(RubyRuntime.TypeErrorClass, s + " is not a class/module");
}
}
private static final RubyID ASERT = RubyID.intern("[]=");
public static void callArraySet(RubyValue value, RubyValue index, RubyValue receiver) {
callPublicTwoArgMethod(receiver, index, value, null, ASERT);
}
public static RubyValue initializeAsteriskParameter(RubyArray args, int argc) {
if (null == args) {
return new RubyArray();
} else {
return args.collect(argc);
}
}
public static RubyValue initializeBlockParameter(RubyBlock block) {
if (null == block) {
return RubyConstant.QNIL;
} else {
return ObjectFactory.createProc(block);
}
}
}