RhinoScriptHandler.java
16.3 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.mozilla.javascript.Context
* org.mozilla.javascript.EvaluatorException
* org.mozilla.javascript.JavaScriptException
* org.mozilla.javascript.RhinoException
* org.mozilla.javascript.Script
* org.mozilla.javascript.Scriptable
* org.mozilla.javascript.Undefined
* org.mozilla.javascript.WrappedException
*/
package com.adobe.xfa.scripthandler.rhino;
import com.adobe.xfa.AppModel;
import com.adobe.xfa.Arg;
import com.adobe.xfa.Element;
import com.adobe.xfa.Obj;
import com.adobe.xfa.ScriptDebugger;
import com.adobe.xfa.ScriptHandler;
import com.adobe.xfa.TextNode;
import com.adobe.xfa.XFA;
import com.adobe.xfa.scripthandler.rhino.LiveComponent;
import com.adobe.xfa.scripthandler.rhino.LiveObject;
import com.adobe.xfa.scripthandler.rhino.RhinoEngine;
import com.adobe.xfa.scripthandler.rhino.ScriptHandlerIF;
import com.adobe.xfa.scripthandler.rhino.ScriptObject;
import com.adobe.xfa.ut.ExFull;
import com.adobe.xfa.ut.MsgFormat;
import com.adobe.xfa.ut.ResId;
import java.util.IdentityHashMap;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.WrappedException;
public class RhinoScriptHandler
extends ScriptHandler
implements ScriptHandlerIF {
public static final String INPTERPRET_SCRIPT = "interpretScript";
private boolean interpretScript = false;
private final RhinoScriptHandler mScriptHost;
private final AppModel mAppModel;
private final LiveObject mAppModelLiveObject;
private Exception mError;
private int mJavaScriptTimeout;
private final IdentityHashMap<Obj, LiveObject> mObjectMap;
protected RhinoScriptHandler() {
this.mScriptHost = this;
this.mAppModel = null;
this.mAppModelLiveObject = null;
this.mObjectMap = new IdentityHashMap();
}
public RhinoScriptHandler(AppModel appModel) {
this(appModel, null);
}
public RhinoScriptHandler(AppModel appModel, ScriptDebugger scriptDebugger) {
this(appModel, null, 0);
}
public RhinoScriptHandler(AppModel appModel, ScriptDebugger scriptDebugger, int javaScriptTimeout) {
super(scriptDebugger);
this.mAppModel = appModel;
this.mScriptHost = this;
this.mJavaScriptTimeout = javaScriptTimeout;
if (this.mJavaScriptTimeout < 0) {
this.mJavaScriptTimeout = 0;
}
LiveComponent oLiveComponent = this.mScriptHost.newLiveComponent(this, this.mAppModel);
RhinoEngine.setTopLevelScope(oLiveComponent);
this.mAppModelLiveObject = this.mScriptHost.newLiveObject(this, this.mAppModel);
this.mObjectMap = new IdentityHashMap();
}
protected RhinoScriptHandler(AppModel appModel, RhinoScriptHandler scriptHost, LiveObject liveObject, ScriptDebugger scriptDebugger, IdentityHashMap<Obj, LiveObject> objectMap) {
this(appModel, scriptHost, liveObject, scriptDebugger, 0, objectMap);
}
protected RhinoScriptHandler(AppModel appModel, RhinoScriptHandler scriptHost, LiveObject liveObject, ScriptDebugger scriptDebugger, int javaScriptTimeout, IdentityHashMap<Obj, LiveObject> objectMap) {
super(scriptDebugger);
this.mAppModel = appModel;
this.mScriptHost = scriptHost;
this.mJavaScriptTimeout = javaScriptTimeout;
if (this.mJavaScriptTimeout < 0) {
this.mJavaScriptTimeout = 0;
}
this.mAppModelLiveObject = liveObject;
this.mObjectMap = objectMap;
}
@Override
public RhinoScriptHandler clone() {
RhinoScriptHandler oNewScriptHandler = new RhinoScriptHandler(this.mAppModel, this.mScriptHost, this.mAppModelLiveObject, this.getDebugger(), this.mObjectMap);
oNewScriptHandler.mError = this.mError;
oNewScriptHandler.interpretScript = this.interpretScript;
return oNewScriptHandler;
}
@Override
public LiveObject newLiveObject(RhinoScriptHandler handler, Obj xfaObject) {
return new LiveObject(handler, xfaObject);
}
@Override
public LiveComponent newLiveComponent(RhinoScriptHandler handler, Obj xfaObject) {
return new LiveComponent(handler, xfaObject);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void executeOrSyntaxCheck(String sScript, Arg oReturnCode, int eReason, boolean bSyntaxCheckOnly, String locale) {
boolean bSuccess;
ScriptDebugger oDebugger;
block38 : {
Scriptable topScope;
Object oResult;
this.clearError();
int nScriptID = -1;
oDebugger = this.getDebugger();
if (oDebugger != null && !$assertionsDisabled) {
throw new AssertionError();
}
String oScriptID = "";
if (oDebugger != null) {
oScriptID = "#" + nScriptID;
}
bSuccess = false;
if (this.interpretScript && !bSyntaxCheckOnly) {
Context ctx = RhinoEngine.getThreadLocalRuntimeContext();
int nLevel = ctx.getOptimizationLevel();
try {
ctx.setOptimizationLevel(-1);
topScope = RhinoEngine.getTopLevelScope();
oResult = ctx.evaluateString(topScope, sScript, oScriptID, 0, (Object)null);
if (!(oResult instanceof Undefined)) {
oReturnCode.assign(this.variantToArg(oResult));
bSuccess = true;
}
break block38;
}
catch (RhinoException f) {
this.setError((Exception)f);
}
catch (ExFull g) {
this.setError(g);
}
finally {
ctx.setOptimizationLevel(nLevel);
}
}
Script oScript = null;
try {
oScript = RhinoEngine.getThreadLocalRuntimeContext().compileString(sScript, oScriptID, 1, (Object)null);
}
catch (RhinoException oError) {
this.setError((Exception)oError);
}
boolean bl = bSuccess = oScript != null;
if (oScript != null && !bSyntaxCheckOnly) {
if (oDebugger != null) {
assert (false);
} else {
bSuccess = false;
try {
Context ctx = RhinoEngine.getThreadLocalRuntimeContext();
topScope = RhinoEngine.getTopLevelScope();
oResult = oScript.exec(ctx, topScope);
if (!(oResult instanceof Undefined)) {
oReturnCode.assign(this.variantToArg(oResult));
bSuccess = true;
}
}
catch (RhinoException f) {
this.setError((Exception)f);
}
catch (ExFull g) {
this.setError(g);
}
}
}
}
if (!bSuccess) {
StringBuilder sError;
String sRhinoPrefix;
Exception error = this.getError();
String sErrorText = null;
if (error instanceof JavaScriptException) {
sError = new StringBuilder(((JavaScriptException)error).getMessage());
sErrorText = sError.toString();
} else if (error instanceof WrappedException) {
sError = new StringBuilder(((WrappedException)error).getMessage());
sRhinoPrefix = "Wrapped ";
int nErrorPrefix = sError.indexOf("Wrapped ");
if (nErrorPrefix >= 0) {
sError.replace(0, nErrorPrefix + "Wrapped ".length(), "");
}
String sRhinoSuffix = "\n (#";
int nLineNumberSuffix = sError.indexOf("\n (#");
if (nLineNumberSuffix >= 0) {
sError.delete(nLineNumberSuffix + 1, sError.length());
}
if (sError.charAt(sError.length() - 1) == '\n') {
sError.setLength(sError.length() - 1);
}
sErrorText = sError.toString();
} else if (error instanceof RhinoException) {
int lineNumberIndex;
String searchString;
sError = new StringBuilder(((RhinoException)error).getMessage());
sRhinoPrefix = "Error: ";
int nErrorPrefix = sError.indexOf("Error: ");
if (nErrorPrefix >= 0) {
sError.delete(0, nErrorPrefix + "Error: ".length());
}
int nErrorLine = ((RhinoException)error).lineNumber();
if (this.interpretScript && !bSyntaxCheckOnly && (lineNumberIndex = sError.indexOf(searchString = "#" + String.valueOf(nErrorLine))) >= 0) {
sError.replace(lineNumberIndex, lineNumberIndex + searchString.length(), "#" + String.valueOf(nErrorLine + 1));
}
sErrorText = sError.toString();
if (bSyntaxCheckOnly) {
MsgFormat oFmt = new MsgFormat(ResId.ScriptHandlerError, sErrorText);
ScriptHandler.ScriptException oScriptEx = new ScriptHandler.ScriptException(oFmt, nErrorLine, RhinoScriptHandler.RhinoErrorIdToErrorCode(0));
throw oScriptEx;
}
} else if (error instanceof ExFull) {
sErrorText = error.toString();
}
if (sErrorText != null) {
Arg arg = this.variantToArg(sErrorText);
oReturnCode.assign(arg);
throw new ExFull(new MsgFormat(ResId.ScriptHandlerError, sErrorText));
}
}
if (bSyntaxCheckOnly) {
return;
}
if (oDebugger != null && !$assertionsDisabled) {
throw new AssertionError();
}
}
public AppModel getAppModel() {
return this.mAppModel;
}
protected RhinoScriptHandler getScriptHost() {
return this.mScriptHost;
}
protected IdentityHashMap<Obj, LiveObject> getObjectMap() {
return this.mObjectMap;
}
protected LiveObject getAppModelLiveObject() {
return this.mAppModelLiveObject;
}
@Override
public String languageName() {
return "javascript";
}
public void throwError(ExFull oError) {
StringBuilder sErrorBuf = new StringBuilder(oError.toString());
int nLen = sErrorBuf.length();
while (sErrorBuf.charAt(nLen - 1) == '\n') {
--nLen;
}
sErrorBuf.setLength(nLen);
RhinoEngine.throwException(sErrorBuf.toString());
}
@Override
public boolean wasFatalError() {
return false;
}
@Override
protected void clearExecutionContexts() {
this.mObjectMap.clear();
RhinoEngine.destroy();
}
void clearError() {
this.setError(null);
}
Exception getError() {
return this.mError;
}
void setError(Exception error) {
this.mError = error;
}
LiveObject ObjectToLiveObject(Obj object) {
LiveObject liveObject = this.lookupObject(object);
if (liveObject == null) {
liveObject = this.mScriptHost.newLiveObject(this, object);
this.addObject(object, liveObject);
}
return liveObject;
}
LiveObject lookupObject(Obj object) {
if (object == this.mAppModel) {
return this.mAppModelLiveObject;
}
return this.mObjectMap.get(object);
}
void addObject(Obj object, LiveObject liveObject) {
TextNode oText;
Element oNode;
Element oParent;
assert (object != this.mAppModel);
this.mObjectMap.put(object, liveObject);
if (object.isSameClass(XFA.SCRIPTTAG) && (oParent = (oNode = (Element)object).getXFAParent()) != null && oParent.isSameClass(XFA.VARIABLESTAG) && (oText = (TextNode)oNode.getProperty(XFA.TEXTNODETAG, 0)) != null) {
String sScript = oText.getValue();
this.compileObject(object, sScript);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
boolean compileObject(Obj object, String sScript) {
this.clearError();
Script oScript = null;
Context ctx = RhinoEngine.getThreadLocalRuntimeContext();
int nLevel = ctx.getOptimizationLevel();
try {
ctx.setOptimizationLevel(-1);
oScript = ctx.compileString(sScript, null, 1, (Object)null);
}
catch (RhinoException oError) {
this.setError((Exception)oError);
}
finally {
ctx.setOptimizationLevel(nLevel);
}
boolean bSuccess = oScript != null;
boolean bRet = false;
if (!bSuccess) {
String sError = "";
Exception error = this.getError();
if (error instanceof RhinoException) {
sError = ((RhinoException)error).getMessage();
}
throw new ExFull(new MsgFormat(ResId.ScriptHandlerError, sError));
}
LiveObject liveObject = this.lookupObject(object);
ScriptObject oScriptObj = new ScriptObject(oScript);
liveObject.setScriptObject(oScriptObj);
bRet = true;
return bRet;
}
public Object argToVariant(Arg arg) {
Object variant = null;
switch (arg.getArgType()) {
case 1: {
variant = Undefined.instance;
break;
}
case 2: {
variant = null;
break;
}
case 3: {
variant = (boolean)arg.getBool();
break;
}
case 4: {
variant = (int)arg.getInteger();
break;
}
case 5: {
variant = new Double(arg.getDouble(false));
break;
}
case 6: {
variant = arg.getString();
break;
}
case 7: {
Obj oObj = arg.getObject();
if (oObj == null) {
variant = null;
break;
}
LiveObject oXFALiveObject = this.ObjectToLiveObject(oObj);
variant = oXFALiveObject;
break;
}
case 8: {
throw new EvaluatorException(arg.getException().toString());
}
default: {
assert (false);
break;
}
}
return variant;
}
public Arg variantToArg(Object variant) {
Arg arg = new Arg();
if (variant == null) {
arg.setNull();
} else if (variant instanceof Undefined) {
arg.empty();
} else if (variant instanceof Boolean) {
arg.setBool((boolean)((Boolean)variant));
} else if (variant instanceof Integer) {
arg.setInteger((int)((Integer)variant));
} else if (variant instanceof Double) {
arg.setDouble(new Double((Double)variant));
} else if (variant instanceof String) {
arg.setString((String)variant);
} else if (variant instanceof LiveObject) {
arg.setObject(((LiveObject)((Object)variant)).getXFAObject());
} else {
arg.setVoid(variant);
}
return arg;
}
static int RhinoErrorIdToErrorCode(int nErrorCode) {
return 8;
}
@Override
public void setOption(String sOptionName, String sOptionValue) {
if ("interpretScript".equals(sOptionName)) {
this.interpretScript = Boolean.valueOf(sOptionValue);
} else {
super.setOption(sOptionName, sOptionValue);
}
}
@Override
public String getOption(String sOptionName) {
if ("interpretScript".equals(sOptionName)) {
return String.valueOf(this.interpretScript);
}
return super.getOption(sOptionName);
}
}