summaryrefslogtreecommitdiff
path: root/sources/scala/runtime/RunTime.java
blob: f5bfe0bf5f85814368afb4826356dd0810d759e9 (plain) (blame)
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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002, LAMP/EPFL                  **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $OldId: RunTime.java,v 1.13 2002/11/19 12:01:40 paltherr Exp $
// $Id$

package scala.runtime;

import scala.Unit;
import scala.Boolean;
import scala.Byte;
import scala.Short;
import scala.Char;
import scala.Int;
import scala.Long;
import scala.Float;
import scala.Double;
import scala.Array;

public abstract class RunTime {

    //########################################################################
    // Private Constants

    private static final int BITS        = 8;
    private static final int ARRAY_SIZE  = 2 << BITS;
    private static final int INDEX_MASK  = ARRAY_SIZE - 1;
    private static final int CHECK_MASK  = ~(ARRAY_SIZE / 2 - 1);

    private static final UValue uvalue   = new UValue();
    private static final ZValue zvalue_f = new ZValue(false);
    private static final ZValue zvalue_t = new ZValue(true);
    private static final BValue bvalue[] = new BValue[256];
    private static final SValue svalue[] = new SValue[ARRAY_SIZE];
    private static final CValue cvalue[] = new CValue[ARRAY_SIZE / 2];
    private static final IValue ivalue[] = new IValue[ARRAY_SIZE];
    private static final LValue lvalue[] = new LValue[ARRAY_SIZE];

    static {
        for (int i = 0; i < bvalue.length; i++)
            bvalue[i] = new BValue((byte)i);
        for (int i = 0; i < ARRAY_SIZE / 2; i++) {
            svalue[i] = new SValue((short)i);
            cvalue[i] = new CValue((char )i);
            ivalue[i] = new IValue((int  )i);
            lvalue[i] = new LValue((long )i);
            svalue[i + ARRAY_SIZE / 2] = new SValue((short)(CHECK_MASK | i));
            ivalue[i + ARRAY_SIZE / 2] = new IValue((int  )(CHECK_MASK | i));
            lvalue[i + ARRAY_SIZE / 2] = new LValue((long )(CHECK_MASK | i));
        }
    }

    //########################################################################
    // Private Variables

    private static ClassLoader loader = ClassLoader.getSystemClassLoader();

    //########################################################################
    // Public Functions - Getting & setting class loader

    public static ClassLoader getClassLoader() {
        return loader;
    }

    public static void setClassLoader(ClassLoader loader) {
        RunTime.loader = loader;
    }

    //########################################################################
    // Public Functions - Boxing primitives

    public static Unit    box_uvalue(         ) {
	return uvalue;
    }

    public static Boolean box_zvalue(boolean x) {
        return x ? zvalue_t : zvalue_f;
    }

    public static Byte    box_bvalue(byte    x) {
        return bvalue[x & 0x000000FF];
    }

    public static Short   box_svalue(short   x) {
        int c = x & CHECK_MASK;
        if (c == 0 || c == CHECK_MASK) return svalue[x & INDEX_MASK];
        return new SValue(x);
    }

    public static Char    box_cvalue(char    x) {
        int c = (int)x & CHECK_MASK;
        if (c == 0) return cvalue[(int)x & INDEX_MASK];
        return new CValue(x);
    }

    public static Int     box_ivalue(int     x) {
        int c = x & CHECK_MASK;
        if (c == 0 || c == CHECK_MASK) return ivalue[x & INDEX_MASK];
        return new IValue(x);
    }

    public static Long    box_lvalue(long    x) {
        long c = x & CHECK_MASK;
        if (c == 0 || c == CHECK_MASK) return lvalue[(int)x & INDEX_MASK];
        return new LValue(x);
    }

    public static Float   box_fvalue(float   x) {
        return new FValue(x);
    }

    public static Double  box_dvalue(double  x) {
        return new DValue(x);
    }

    /** @meta method (scala.Array[scala.Boolean]) scala.Array[scala.Boolean];*/
    public static Array   box_zarray(boolean[] xs) {
	return new ZArray(xs);
    }

    /** @meta method (scala.Array[scala.Byte]) scala.Array[scala.Byte]; */
    public static Array   box_barray(byte   [] xs) {
	return new BArray(xs);
    }

    /** @meta method (scala.Array[scala.Short]) scala.Array[scala.Short]; */
    public static Array   box_sarray(short  [] xs) {
	return new SArray(xs);
    }

    /** @meta method (scala.Array[scala.Char]) scala.Array[scala.Char]; */
    public static Array   box_carray(char   [] xs) {
	return new CArray(xs);
    }

    /** @meta method (scala.Array[scala.Int]) scala.Array[scala.Int]; */
    public static Array   box_iarray(int    [] xs) {
	return new IArray(xs);
    }

    /** @meta method (scala.Array[scala.Long]) scala.Array[scala.Long]; */
    public static Array   box_larray(long   [] xs) {
	return new LArray(xs);
    }

    /** @meta method (scala.Array[scala.Float]) scala.Array[scala.Float]; */
    public static Array   box_farray(float  [] xs) {
	return new FArray(xs);
    }

    /** @meta method (scala.Array[scala.Double]) scala.Array[scala.Double]; */
    public static Array   box_darray(double [] xs) {
	return new DArray(xs);
    }

    /** @meta method [?T < scala.AnyRef](scala.Array[?T]) scala.Array[?T]; */
    public static Array   box_oarray(Object [] xs) {
	return new OArray(xs);
    }

    /** @meta method [?T](scala.Array[?T]) scala.Array[?T]; */
    public static Array   box__array(Object    xs) {
        if (xs == null             ) return box_oarray((Object [])xs);
        if (xs instanceof boolean[]) return box_zarray((boolean[])xs);
        if (xs instanceof byte   []) return box_barray((byte   [])xs);
        if (xs instanceof short  []) return box_sarray((short  [])xs);
        if (xs instanceof char   []) return box_carray((char   [])xs);
        if (xs instanceof int    []) return box_iarray((int    [])xs);
        if (xs instanceof long   []) return box_larray((long   [])xs);
        if (xs instanceof float  []) return box_farray((float  [])xs);
        if (xs instanceof double []) return box_darray((double [])xs);
        if (xs instanceof Object []) return box_oarray((Object [])xs);
        throw new ClassCastException(xs.getClass() + " is not an array class");
    }

    //########################################################################
    // Public Functions - Unboxing primitives

    public static void      unbox_uvalue(Unit    x) {        x.value(); }
    public static boolean   unbox_zvalue(Boolean x) { return x.value  ; }
    public static byte      unbox_bvalue(Byte    x) { return x.value  ; }
    public static short     unbox_svalue(Short   x) { return x.value  ; }
    public static char      unbox_cvalue(Char    x) { return x.value  ; }
    public static int       unbox_ivalue(Int     x) { return x.value  ; }
    public static long      unbox_lvalue(Long    x) { return x.value  ; }
    public static float     unbox_fvalue(Float   x) { return x.value  ; }
    public static double    unbox_dvalue(Double  x) { return x.value  ; }

    /** @meta method (scala.Array[scala.Boolean]) scala.Array[scala.Boolean];*/
    public static boolean[] unbox_zarray(Array xs) {
        return xs == null ? null : xs.asBooleanArray();
    }
    /** @meta method (scala.Array[scala.Byte]) scala.Array[scala.Byte]; */
    public static byte   [] unbox_barray(Array xs) {
        return xs == null ? null : xs.asByteArray   ();
    }
    /** @meta method (scala.Array[scala.Short]) scala.Array[scala.Short]; */
    public static short  [] unbox_sarray(Array xs) {
        return xs == null ? null : xs.asShortArray  ();
    }
    /** @meta method (scala.Array[scala.Char]) scala.Array[scala.Char]; */
    public static char   [] unbox_carray(Array xs) {
        return xs == null ? null : xs.asCharArray   ();
    }
    /** @meta method (scala.Array[scala.Int]) scala.Array[scala.Int]; */
    public static int    [] unbox_iarray(Array xs) {
        return xs == null ? null : xs.asIntArray    ();
    }
    /** @meta method (scala.Array[scala.Long]) scala.Array[scala.Long]; */
    public static long   [] unbox_larray(Array xs) {
        return xs == null ? null : xs.asLongArray   ();
    }
    /** @meta method (scala.Array[scala.Float]) scala.Array[scala.Float]; */
    public static float  [] unbox_farray(Array xs) {
        return xs == null ? null : xs.asFloatArray  ();
    }
    /** @meta method (scala.Array[scala.Double]) scala.Array[scala.Double]; */
    public static double [] unbox_darray(Array xs) {
        return xs == null ? null : xs.asDoubleArray ();
    }
    /** @meta method [?T < scala.AnyRef](scala.Array[?T]) scala.Array[?T]; */
    public static Object [] unbox_oarray(Array xs) {
        return xs == null ? null : xs.asObjectArray ();
    }
    /** @meta method [?T](scala.Array[?T]) scala.Array[?T]; */
    public static Object    unbox__array(Array xs) {
        return xs == null ? null : xs.asArray       ();
    }

    //########################################################################
    // Public Functions - Conversion primitives

    public static byte   b2b(byte   x) { return (byte  )x; }
    public static short  b2s(byte   x) { return (short )x; }
    public static char   b2c(byte   x) { return (char  )x; }
    public static int    b2i(byte   x) { return (int   )x; }
    public static long   b2l(byte   x) { return (long  )x; }
    public static float  b2f(byte   x) { return (float )x; }
    public static double b2d(byte   x) { return (double)x; }
    public static byte   s2b(short  x) { return (byte  )x; }
    public static short  s2s(short  x) { return (short )x; }
    public static char   s2c(short  x) { return (char  )x; }
    public static int    s2i(short  x) { return (int   )x; }
    public static long   s2l(short  x) { return (long  )x; }
    public static float  s2f(short  x) { return (float )x; }
    public static double s2d(short  x) { return (double)x; }
    public static byte   c2b(char   x) { return (byte  )x; }
    public static short  c2s(char   x) { return (short )x; }
    public static char   c2c(char   x) { return (char  )x; }
    public static int    c2i(char   x) { return (int   )x; }
    public static long   c2l(char   x) { return (long  )x; }
    public static float  c2f(char   x) { return (float )x; }
    public static double c2d(char   x) { return (double)x; }
    public static byte   i2b(int    x) { return (byte  )x; }
    public static short  i2s(int    x) { return (short )x; }
    public static char   i2c(int    x) { return (char  )x; }
    public static int    i2i(int    x) { return (int   )x; }
    public static long   i2l(int    x) { return (long  )x; }
    public static float  i2f(int    x) { return (float )x; }
    public static double i2d(int    x) { return (double)x; }
    public static byte   l2b(long   x) { return (byte  )x; }
    public static short  l2s(long   x) { return (short )x; }
    public static char   l2c(long   x) { return (char  )x; }
    public static int    l2i(long   x) { return (int   )x; }
    public static long   l2l(long   x) { return (long  )x; }
    public static float  l2f(long   x) { return (float )x; }
    public static double l2d(long   x) { return (double)x; }
    public static byte   f2b(float  x) { return (byte  )x; }
    public static short  f2s(float  x) { return (short )x; }
    public static char   f2c(float  x) { return (char  )x; }
    public static int    f2i(float  x) { return (int   )x; }
    public static long   f2l(float  x) { return (long  )x; }
    public static float  f2f(float  x) { return (float )x; }
    public static double f2d(float  x) { return (double)x; }
    public static byte   d2b(double x) { return (byte  )x; }
    public static short  d2s(double x) { return (short )x; }
    public static char   d2c(double x) { return (char  )x; }
    public static int    d2i(double x) { return (int   )x; }
    public static long   d2l(double x) { return (long  )x; }
    public static float  d2f(double x) { return (float )x; }
    public static double d2d(double x) { return (double)x; }

    //########################################################################
    // Public Functions - Array primitives

    public static boolean[] zarray(int length) { return new boolean[length]; }
    public static byte   [] barray(int length) { return new byte   [length]; }
    public static short  [] sarray(int length) { return new short  [length]; }
    public static char   [] carray(int length) { return new char   [length]; }
    public static int    [] iarray(int length) { return new int    [length]; }
    public static long   [] larray(int length) { return new long   [length]; }
    public static float  [] farray(int length) { return new float  [length]; }
    public static double [] darray(int length) { return new double [length]; }
    public static Object    oarray(int length, String classname) {
        try {
            Class clasz = Class.forName(classname, false, loader);
            return java.lang.reflect.Array.newInstance(clasz, length);
        } catch (ClassNotFoundException exception) {
            throw new Error(exception.toString());
        }
    }

    public static int zarray_length(boolean[] xs) { return xs.length; }
    public static int barray_length(byte   [] xs) { return xs.length; }
    public static int sarray_length(short  [] xs) { return xs.length; }
    public static int carray_length(char   [] xs) { return xs.length; }
    public static int iarray_length(int    [] xs) { return xs.length; }
    public static int larray_length(long   [] xs) { return xs.length; }
    public static int farray_length(float  [] xs) { return xs.length; }
    public static int darray_length(double [] xs) { return xs.length; }
    public static int oarray_length(Object [] xs) { return xs.length; }

    public static boolean zarray_get(boolean[] xs, int i) { return xs[i]; }
    public static byte    barray_get(byte   [] xs, int i) { return xs[i]; }
    public static short   sarray_get(short  [] xs, int i) { return xs[i]; }
    public static char    carray_get(char   [] xs, int i) { return xs[i]; }
    public static int     iarray_get(int    [] xs, int i) { return xs[i]; }
    public static long    larray_get(long   [] xs, int i) { return xs[i]; }
    public static float   farray_get(float  [] xs, int i) { return xs[i]; }
    public static double  darray_get(double [] xs, int i) { return xs[i]; }
    public static Object  oarray_get(Object [] xs, int i) { return xs[i]; }

    public static void zarray_set(boolean[] xs, int i, boolean x) { xs[i] = x;}
    public static void barray_set(byte   [] xs, int i, byte    x) { xs[i] = x;}
    public static void sarray_set(short  [] xs, int i, short   x) { xs[i] = x;}
    public static void carray_set(char   [] xs, int i, char    x) { xs[i] = x;}
    public static void iarray_set(int    [] xs, int i, int     x) { xs[i] = x;}
    public static void larray_set(long   [] xs, int i, long    x) { xs[i] = x;}
    public static void farray_set(float  [] xs, int i, float   x) { xs[i] = x;}
    public static void darray_set(double [] xs, int i, double  x) { xs[i] = x;}
    public static void oarray_set(Object [] xs, int i, Object  x) { xs[i] = x;}

    //########################################################################
    // Public Functions - Synchronization primitives

    /** @meta method [?T](scala.AnyRef,?T) ?T; */
    public static Object synchronised(Object lock, Object body) {
        return body;
    }

    //########################################################################
}

// These classes may not be defined in class RunTime because inner
// classes confuse pico which then attributes the metadata to the
// wrong members.

class UValue extends Unit    { public UValue(         ) { super( ); } }
class ZValue extends Boolean { public ZValue(boolean x) { super(x); } }
class BValue extends Byte    { public BValue(byte    x) { super(x); } }
class SValue extends Short   { public SValue(short   x) { super(x); } }
class CValue extends Char    { public CValue(char    x) { super(x); } }
class IValue extends Int     { public IValue(int     x) { super(x); } }
class LValue extends Long    { public LValue(long    x) { super(x); } }
class FValue extends Float   { public FValue(float   x) { super(x); } }
class DValue extends Double  { public DValue(double  x) { super(x); } }

class ZArray extends Array {
    private final boolean[] xs;
    public ZArray(boolean[] xs) { this.xs = xs; }
    public boolean[] asBooleanArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_zvalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Boolean)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}

class BArray extends Array {
    private final byte[] xs;
    public BArray(byte[] xs) { this.xs = xs; }
    public byte[] asByteArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_bvalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Byte)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}

class SArray extends Array {
    private final short[] xs;
    public SArray(short[] xs) { this.xs = xs; }
    public short[] asShortArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_svalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Short)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}

class CArray extends Array {
    private final char[] xs;
    public CArray(char[] xs) { this.xs = xs; }
    public char[] asCharArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_cvalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Char)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf((Object)xs); }
}

class IArray extends Array {
    private final int[] xs;
    public IArray(int[] xs) { this.xs = xs; }
    public int[] asIntArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_ivalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Int)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}

class LArray extends Array {
    private final long[] xs;
    public LArray(long[] xs) { this.xs = xs; }
    public long[] asLongArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_lvalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Long)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}

class FArray extends Array {
    private final float[] xs;
    public FArray(float[] xs) { this.xs = xs; }
    public float[] asFloatArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_fvalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Float)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}

class DArray extends Array {
    private final double[] xs;
    public DArray(double[] xs) { this.xs = xs; }
    public double[] asDoubleArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return RunTime.box_dvalue(xs[i]); }
    public void update(int i, Object x) { xs[i] = ((Double)x).value; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}

class OArray extends Array {
    private final Object[] xs;
    public OArray(Object[] xs) { this.xs = xs; }
    public Object[] asObjectArray() { return xs; }
    public Object asArray() { return xs; }
    public Object apply(int i) { return xs[i]; }
    public void update(int i, Object x) { xs[i] = x; }
    public int length() { return xs.length; }
    public String toString() { return String.valueOf(xs); }
}