Debian Codegraph @

rhino/src/org/mozilla/javascript/optimizer/OptRuntime.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
6 package org.mozilla.javascript.optimizer;
7
8 import org.mozilla.javascript.*;
9
10 public final class OptRuntime extends ScriptRuntime
11 {
12
13 public static final Double zeroObj = new Double(0.0);
14 public static final Double oneObj = new Double(1.0);
15 public static final Double minusOneObj = new Double(-1.0);
16
17 /**
18 * Implement ....() call shrinking optimizer code.
19 */
20 public static Object call0(Callable fun, Scriptable thisObj,
21 Context cx, Scriptable scope)
22 {
23 return fun.call(cx, scope, thisObj, ScriptRuntime.emptyArgs);
24 }
25
26 /**
27 * Implement ....(arg) call shrinking optimizer code.
28 */
29 public static Object call1(Callable fun, Scriptable thisObj, Object arg0,
30 Context cx, Scriptable scope)
31 {
32 return fun.call(cx, scope, thisObj, new Object[] { arg0 } );
33 }
34
35 /**
36 * Implement ....(arg0, arg1) call shrinking optimizer code.
37 */
38 public static Object call2(Callable fun, Scriptable thisObj,
39 Object arg0, Object arg1,
40 Context cx, Scriptable scope)
41 {
42 return fun.call(cx, scope, thisObj, new Object[] { arg0, arg1 });
43 }
44
45 /**
46 * Implement ....(arg0, arg1, ...) call shrinking optimizer code.
47 */
48 public static Object callN(Callable fun, Scriptable thisObj,
49 Object[] args,
50 Context cx, Scriptable scope)
51 {
52 return fun.call(cx, scope, thisObj, args);
53 }
54
55 /**
56 * Implement name(args) call shrinking optimizer code.
57 */
58 public static Object callName(Object[] args, String name,
59 Context cx, Scriptable scope)
60 {
61 Callable f = getNameFunctionAndThis(name, cx, scope);
62 Scriptable thisObj = lastStoredScriptable(cx);
63 return f.call(cx, scope, thisObj, args);
64 }
65
66 /**
67 * Implement name() call shrinking optimizer code.
68 */
69 public static Object callName0(String name,
70 Context cx, Scriptable scope)
71 {
72 Callable f = getNameFunctionAndThis(name, cx, scope);
73 Scriptable thisObj = lastStoredScriptable(cx);
74 return f.call(cx, scope, thisObj, ScriptRuntime.emptyArgs);
75 }
76
77 /**
78 * Implement x.property() call shrinking optimizer code.
79 */
80 public static Object callProp0(Object value, String property,
81 Context cx, Scriptable scope)
82 {
83 Callable f = getPropFunctionAndThis(value, property, cx, scope);
84 Scriptable thisObj = lastStoredScriptable(cx);
85 return f.call(cx, scope, thisObj, ScriptRuntime.emptyArgs);
86 }
87
88 public static Object add(Object val1, double val2)
89 {
90 if (val1 instanceof Scriptable)
91 val1 = ((Scriptable) val1).getDefaultValue(null);
92 if (!(val1 instanceof CharSequence))
93 return wrapDouble(toNumber(val1) + val2);
94 return new ConsString((CharSequence)val1, toString(val2));
95 }
96
97 public static Object add(double val1, Object val2)
98 {
99 if (val2 instanceof Scriptable)
100 val2 = ((Scriptable) val2).getDefaultValue(null);
101 if (!(val2 instanceof CharSequence))
102 return wrapDouble(toNumber(val2) + val1);
103 return new ConsString(toString(val1), (CharSequence)val2);
104 }
105
106 /**
107 * @deprecated Use {@link #elemIncrDecr(Object, double, Context, Scriptable, int)} instead
108 */
109 @Deprecated
110 public static Object elemIncrDecr(Object obj, double index,
111 Context cx, int incrDecrMask)
112 {
113 return elemIncrDecr(obj, index, cx, getTopCallScope(cx), incrDecrMask);
114 }
115
116 public static Object elemIncrDecr(Object obj, double index,
117 Context cx, Scriptable scope,
118 int incrDecrMask)
119 {
120 return ScriptRuntime.elemIncrDecr(obj, new Double(index), cx, scope,
121 incrDecrMask);
122 }
123
124 public static Object[] padStart(Object[] currentArgs, int count) {
125 Object[] result = new Object[currentArgs.length + count];
126 System.arraycopy(currentArgs, 0, result, count, currentArgs.length);
127 return result;
128 }
129
130 public static void initFunction(NativeFunction fn, int functionType,
131 Scriptable scope, Context cx)
132 {
133 ScriptRuntime.initFunction(cx, scope, fn, functionType, false);
134 }
135
136 public static Function bindThis(NativeFunction fn, Context cx, Scriptable scope, Scriptable thisObj)
137 {
138 return new ArrowFunction(cx, scope, fn, thisObj);
139 }
140
141 public static Object callSpecial(Context cx, Callable fun,
142 Scriptable thisObj, Object[] args,
143 Scriptable scope,
144 Scriptable callerThis, int callType,
145 String fileName, int lineNumber)
146 {
147 return ScriptRuntime.callSpecial(cx, fun, thisObj, args, scope,
148 callerThis, callType,
149 fileName, lineNumber);
150 }
151
152 public static Object newObjectSpecial(Context cx, Object fun,
153 Object[] args, Scriptable scope,
154 Scriptable callerThis, int callType)
155 {
156 return ScriptRuntime.newSpecial(cx, fun, args, scope, callType);
157 }
158
159 public static Double wrapDouble(double num)
160 {
161 if (num == 0.0) {
162 if (1 / num > 0) {
163 // +0.0
164 return zeroObj;
165 }
166 } else if (num == 1.0) {
167 return oneObj;
168 } else if (num == -1.0) {
169 return minusOneObj;
170 } else if (num != num) {
171 return NaNobj;
172 }
173 return new Double(num);
174 }
175
176 static String encodeIntArray(int[] array)
177 {
178 // XXX: this extremely inefficient for small integers
179 if (array == null) { return null; }
180 int n = array.length;
181 char[] buffer = new char[1 + n * 2];
182 buffer[0] = 1;
183 for (int i = 0; i != n; ++i) {
184 int value = array[i];
185 int shift = 1 + i * 2;
186 buffer[shift] = (char)(value >>> 16);
187 buffer[shift + 1] = (char)value;
188 }
189 return new String(buffer);
190 }
191
192 private static int[] decodeIntArray(String str, int arraySize)
193 {
194 // XXX: this extremely inefficient for small integers
195 if (arraySize == 0) {
196 if (str != null) throw new IllegalArgumentException();
197 return null;
198 }
199 if (str.length() != 1 + arraySize * 2 && str.charAt(0) != 1) {
200 throw new IllegalArgumentException();
201 }
202 int[] array = new int[arraySize];
203 for (int i = 0; i != arraySize; ++i) {
204 int shift = 1 + i * 2;
205 array[i] = (str.charAt(shift) << 16) | str.charAt(shift + 1);
206 }
207 return array;
208 }
209
210 public static Scriptable newArrayLiteral(Object[] objects,
211 String encodedInts,
212 int skipCount,
213 Context cx,
214 Scriptable scope)
215 {
216 int[] skipIndexces = decodeIntArray(encodedInts, skipCount);
217 return newArrayLiteral(objects, skipIndexces, cx, scope);
218 }
219
220 public static void main(final Script script, final String[] args)
221 {
222 ContextFactory.getGlobal().call(new ContextAction() {
223 public Object run(Context cx)
224 {
225 ScriptableObject global = getGlobal(cx);
226
227 // get the command line arguments and define "arguments"
228 // array in the top-level object
229 Object[] argsCopy = new Object[args.length];
230 System.arraycopy(args, 0, argsCopy, 0, args.length);
231 Scriptable argsObj = cx.newArray(global, argsCopy);
232 global.defineProperty("arguments", argsObj,
233 ScriptableObject.DONTENUM);
234 script.exec(cx, global);
235 return null;
236 }
237 });
238 }
239
240 public static void throwStopIteration(Object obj) {
241 throw new JavaScriptException(
242 NativeIterator.getStopIterationObject((Scriptable)obj), "", 0);
243 }
244
245 public static Scriptable createNativeGenerator(NativeFunction funObj,
246 Scriptable scope,
247 Scriptable thisObj,
248 int maxLocals,
249 int maxStack)
250 {
251 return new NativeGenerator(scope, funObj,
252 new GeneratorState(thisObj, maxLocals, maxStack));
253 }
254
255 public static Object[] getGeneratorStackState(Object obj) {
256 GeneratorState rgs = (GeneratorState) obj;
257 if (rgs.stackState == null)
258 rgs.stackState = new Object[rgs.maxStack];
259 return rgs.stackState;
260 }
261
262 public static Object[] getGeneratorLocalsState(Object obj) {
263 GeneratorState rgs = (GeneratorState) obj;
264 if (rgs.localsState == null)
265 rgs.localsState = new Object[rgs.maxLocals];
266 return rgs.localsState;
267 }
268
269 public static class GeneratorState {
270 static final String CLASS_NAME =
271 "org/mozilla/javascript/optimizer/OptRuntime$GeneratorState";
272
273 public int resumptionPoint;
274 static final String resumptionPoint_NAME = "resumptionPoint";
275 static final String resumptionPoint_TYPE = "I";
276
277 public Scriptable thisObj;
278 static final String thisObj_NAME = "thisObj";
279 static final String thisObj_TYPE =
280 "Lorg/mozilla/javascript/Scriptable;";
281
282 Object[] stackState;
283 Object[] localsState;
284 int maxLocals;
285 int maxStack;
286
287 GeneratorState(Scriptable thisObj, int maxLocals, int maxStack) {
288 this.thisObj = thisObj;
289 this.maxLocals = maxLocals;
290 this.maxStack = maxStack;
291 }
292 }
293 }