KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > sc > Values


1 /***
2  * Values.java
3  *
4  * Description: SWF Action Code instruction opcode constants
5  */

6
7 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
8 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
9 * Use is subject to license terms. *
10 * J_LZ_COPYRIGHT_END *********************************************************/

11
12 package org.openlaszlo.sc;
13 import java.io.*;
14
15 public class Values {
16   /***
17    * These do double duty as identifiers, and as the typecodes
18    * that are used by PUSH.
19    */

20   public static class PushTypes {
21     public static final byte String = 0;
22     public static final byte Float = 1;
23     public static final byte Null = 2;
24     public static final byte Undefined = 3;
25     public static final byte Register = 4;
26     public static final byte Boolean = 5;
27     public static final byte Integer = 7;
28     public static final byte Double = 6;
29     public static final byte CONSTANT_INDEX8 = 8;
30     public static final byte CONSTANT_INDEX16 = 9;
31   }
32
33   // TODO: [2004-03-24 ptw] Generalize to any number of registers
34
public static Value r0 = Value.make("r:0", PushTypes.Register, (byte)0);
35   public static Value r1 = Value.make("r:1", PushTypes.Register, (byte)1);
36   public static Value r2 = Value.make("r:2", PushTypes.Register, (byte)2);
37   public static Value r3 = Value.make("r:3", PushTypes.Register, (byte)3);
38
39   public static Value Register(int n) {
40     switch (n) {
41       case 0:
42         return r0;
43       case 1:
44         return r1;
45       case 2:
46         return r2;
47       case 3:
48         return r3;
49       default:
50         return Value.make("r:" + n, PushTypes.Register, (byte)n);
51     }
52   }
53
54   public static boolean isRegister(Object JavaDoc v) {
55     return v instanceof Value && ((Value)v).type == PushTypes.Register;
56   }
57
58   public static boolean isInteger(Object JavaDoc v) {
59     return v instanceof Number JavaDoc && ((Number JavaDoc)v).intValue() == ((Number JavaDoc)v).doubleValue();
60   }
61
62   /***
63    * Runtime Types
64    */

65
66   public static class Value implements Serializable {
67     public String JavaDoc name;
68     public byte type;
69
70     public static Value make(String JavaDoc name, byte type) {
71       return new Value(name, type);
72     }
73
74     public static Value make(String JavaDoc name, byte type, byte value) {
75       return new ParameterizedValue(name, type, value);
76     }
77
78     private Value(String JavaDoc name, byte type) {
79       this.name = name.intern();
80       this.type = type;
81     }
82
83     public String JavaDoc toString() {
84       return this.name;
85     }
86
87     public Object JavaDoc readResolve() throws ObjectStreamException {
88         switch (this.type) {
89         case PushTypes.Undefined:
90             return Values.Undefined;
91         case PushTypes.Null:
92             return Values.Null;
93         default:
94             return this;
95         }
96     }
97   }
98
99   public static class ParameterizedValue extends Value {
100     public byte value;
101
102     private ParameterizedValue(String JavaDoc name, byte type, byte value) {
103       super(name, type);
104       this.value = value;
105     }
106
107     public Object JavaDoc readResolve() throws ObjectStreamException {
108         switch (this.type) {
109         case PushTypes.Boolean:
110             return this.value == 0 ? Values.False : Values.True;
111         case PushTypes.Register:
112             return Values.Register(this.value);
113         default:
114             return super.readResolve();
115         }
116     }
117   }
118
119   public static Value True = Value.make("TRUE", PushTypes.Boolean, (byte)1);
120   public static Value False = Value.make("FALSE", PushTypes.Boolean, (byte)0);
121   public static Value Undefined = Value.make("UNDEF", PushTypes.Undefined);
122   public static Value Null = Value.make("NULL", PushTypes.Null);
123
124   // Integers are represented as instances of Long
125

126   // Strings are represented as instances of String
127
}
128
129
Popular Tags