| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.io.Serializable; |
| 10 |
import java.lang.reflect.Method; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
final class NativeError extends IdScriptableObject |
| 19 |
{ |
| 20 |
static final long serialVersionUID = -5338413581437645187L; |
| 21 |
|
| 22 |
private static final Object ERROR_TAG = "Error"; |
| 23 |
|
| 24 |
private static final Method ERROR_DELEGATE_GET_STACK; |
| 25 |
private static final Method ERROR_DELEGATE_SET_STACK; |
| 26 |
|
| 27 |
static { |
| 28 |
try { |
| 29 |
|
| 30 |
ERROR_DELEGATE_GET_STACK = NativeError.class.getMethod("getStackDelegated", Scriptable.class); |
| 31 |
ERROR_DELEGATE_SET_STACK = NativeError.class.getMethod("setStackDelegated", Scriptable.class, Object.class); |
| 32 |
} catch (NoSuchMethodException nsm) { |
| 33 |
throw new RuntimeException(nsm); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
public static final int DEFAULT_STACK_LIMIT = -1; |
| 39 |
|
| 40 |
|
| 41 |
private static final String STACK_HIDE_KEY = "_stackHide"; |
| 42 |
|
| 43 |
private RhinoException stackProvider; |
| 44 |
|
| 45 |
static void init(Scriptable scope, boolean sealed) |
| 46 |
{ |
| 47 |
NativeError obj = new NativeError(); |
| 48 |
ScriptableObject.putProperty(obj, "name", "Error"); |
| 49 |
ScriptableObject.putProperty(obj, "message", ""); |
| 50 |
ScriptableObject.putProperty(obj, "fileName", ""); |
| 51 |
ScriptableObject.putProperty(obj, "lineNumber", Integer.valueOf(0)); |
| 52 |
obj.setAttributes("name", ScriptableObject.DONTENUM); |
| 53 |
obj.setAttributes("message", ScriptableObject.DONTENUM); |
| 54 |
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); |
| 55 |
NativeCallSite.init(obj, sealed); |
| 56 |
} |
| 57 |
|
| 58 |
static NativeError make(Context cx, Scriptable scope, |
| 59 |
IdFunctionObject ctorObj, Object[] args) |
| 60 |
{ |
| 61 |
Scriptable proto = (Scriptable)(ctorObj.get("prototype", ctorObj)); |
| 62 |
|
| 63 |
NativeError obj = new NativeError(); |
| 64 |
obj.setPrototype(proto); |
| 65 |
obj.setParentScope(scope); |
| 66 |
|
| 67 |
int arglen = args.length; |
| 68 |
if (arglen >= 1) { |
| 69 |
if (args[0] != Undefined.instance) { |
| 70 |
ScriptableObject.putProperty(obj, "message", |
| 71 |
ScriptRuntime.toString(args[0])); |
| 72 |
} |
| 73 |
if (arglen >= 2) { |
| 74 |
ScriptableObject.putProperty(obj, "fileName", args[1]); |
| 75 |
if (arglen >= 3) { |
| 76 |
int line = ScriptRuntime.toInt32(args[2]); |
| 77 |
ScriptableObject.putProperty(obj, "lineNumber", |
| 78 |
Integer.valueOf(line)); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
return obj; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected void fillConstructorProperties(IdFunctionObject ctor) |
| 87 |
{ |
| 88 |
addIdFunctionProperty(ctor, ERROR_TAG, ConstructorId_captureStackTrace, |
| 89 |
"captureStackTrace", 2); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
ProtoProps protoProps = new ProtoProps(); |
| 95 |
associateValue(ProtoProps.KEY, protoProps); |
| 96 |
|
| 97 |
|
| 98 |
ctor.defineProperty("stackTraceLimit", protoProps, |
| 99 |
ProtoProps.GET_STACK_LIMIT, ProtoProps.SET_STACK_LIMIT, 0); |
| 100 |
ctor.defineProperty("prepareStackTrace", protoProps, |
| 101 |
ProtoProps.GET_PREPARE_STACK, ProtoProps.SET_PREPARE_STACK, 0); |
| 102 |
|
| 103 |
super.fillConstructorProperties(ctor); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public String getClassName() |
| 108 |
{ |
| 109 |
return "Error"; |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public String toString() |
| 114 |
{ |
| 115 |
|
| 116 |
Object toString = js_toString(this); |
| 117 |
return toString instanceof String ? (String) toString : super.toString(); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
protected void initPrototypeId(int id) |
| 122 |
{ |
| 123 |
String s; |
| 124 |
int arity; |
| 125 |
switch (id) { |
| 126 |
case Id_constructor: arity=1; s="constructor"; break; |
| 127 |
case Id_toString: arity=0; s="toString"; break; |
| 128 |
case Id_toSource: arity=0; s="toSource"; break; |
| 129 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 130 |
} |
| 131 |
initPrototypeMethod(ERROR_TAG, id, s, arity); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 136 |
Scriptable thisObj, Object[] args) |
| 137 |
{ |
| 138 |
if (!f.hasTag(ERROR_TAG)) { |
| 139 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 140 |
} |
| 141 |
int id = f.methodId(); |
| 142 |
switch (id) { |
| 143 |
case Id_constructor: |
| 144 |
return make(cx, scope, f, args); |
| 145 |
|
| 146 |
case Id_toString: |
| 147 |
return js_toString(thisObj); |
| 148 |
|
| 149 |
case Id_toSource: |
| 150 |
return js_toSource(cx, scope, thisObj); |
| 151 |
|
| 152 |
case ConstructorId_captureStackTrace: |
| 153 |
js_captureStackTrace(cx, thisObj, args); |
| 154 |
return Undefined.instance; |
| 155 |
} |
| 156 |
throw new IllegalArgumentException(String.valueOf(id)); |
| 157 |
} |
| 158 |
|
| 159 |
public void setStackProvider(RhinoException re) { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if (stackProvider == null) { |
| 165 |
stackProvider = re; |
| 166 |
defineProperty("stack", this, |
| 167 |
ERROR_DELEGATE_GET_STACK, ERROR_DELEGATE_SET_STACK, |
| 168 |
DONTENUM); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public Object getStackDelegated(Scriptable target) { |
| 173 |
if (stackProvider == null) { |
| 174 |
return NOT_FOUND; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
int limit = DEFAULT_STACK_LIMIT; |
| 179 |
Function prepare = null; |
| 180 |
NativeError cons = (NativeError)getPrototype(); |
| 181 |
ProtoProps pp = (ProtoProps)cons.getAssociatedValue(ProtoProps.KEY); |
| 182 |
|
| 183 |
if (pp != null) { |
| 184 |
limit = pp.getStackTraceLimit(); |
| 185 |
prepare = pp.getPrepareStackTrace(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
String hideFunc = (String)getAssociatedValue(STACK_HIDE_KEY); |
| 190 |
ScriptStackElement[] stack = stackProvider.getScriptStack(limit, hideFunc); |
| 191 |
|
| 192 |
|
| 193 |
Object value; |
| 194 |
if (prepare == null) { |
| 195 |
value = RhinoException.formatStackTrace(stack, stackProvider.details()); |
| 196 |
} else { |
| 197 |
value = callPrepareStack(prepare, stack); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
setStackDelegated(target, value); |
| 203 |
return value; |
| 204 |
} |
| 205 |
|
| 206 |
public void setStackDelegated(Scriptable target, Object value) { |
| 207 |
target.delete("stack"); |
| 208 |
stackProvider = null; |
| 209 |
target.put("stack", target, value); |
| 210 |
} |
| 211 |
|
| 212 |
private Object callPrepareStack(Function prepare, ScriptStackElement[] stack) |
| 213 |
{ |
| 214 |
Context cx = Context.getCurrentContext(); |
| 215 |
Object[] elts = new Object[stack.length]; |
| 216 |
|
| 217 |
|
| 218 |
for (int i = 0; i < stack.length; i++) { |
| 219 |
NativeCallSite site = (NativeCallSite)cx.newObject(this, "CallSite"); |
| 220 |
site.setElement(stack[i]); |
| 221 |
elts[i] = site; |
| 222 |
} |
| 223 |
|
| 224 |
Scriptable eltArray = cx.newArray(this, elts); |
| 225 |
return prepare.call(cx, prepare, this, new Object[] { this, eltArray }); |
| 226 |
} |
| 227 |
|
| 228 |
private static Object js_toString(Scriptable thisObj) { |
| 229 |
Object name = ScriptableObject.getProperty(thisObj, "name"); |
| 230 |
if (name == NOT_FOUND || name == Undefined.instance) { |
| 231 |
name = "Error"; |
| 232 |
} else { |
| 233 |
name = ScriptRuntime.toString(name); |
| 234 |
} |
| 235 |
Object msg = ScriptableObject.getProperty(thisObj, "message"); |
| 236 |
if (msg == NOT_FOUND || msg == Undefined.instance) { |
| 237 |
msg = ""; |
| 238 |
} else { |
| 239 |
msg = ScriptRuntime.toString(msg); |
| 240 |
} |
| 241 |
if (name.toString().length() == 0) { |
| 242 |
return msg; |
| 243 |
} else if (msg.toString().length() == 0) { |
| 244 |
return name; |
| 245 |
} else { |
| 246 |
return ((String) name) + ": " + ((String) msg); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
private static String js_toSource(Context cx, Scriptable scope, |
| 251 |
Scriptable thisObj) |
| 252 |
{ |
| 253 |
|
| 254 |
Object name = ScriptableObject.getProperty(thisObj, "name"); |
| 255 |
Object message = ScriptableObject.getProperty(thisObj, "message"); |
| 256 |
Object fileName = ScriptableObject.getProperty(thisObj, "fileName"); |
| 257 |
Object lineNumber = ScriptableObject.getProperty(thisObj, "lineNumber"); |
| 258 |
|
| 259 |
StringBuilder sb = new StringBuilder(); |
| 260 |
sb.append("(new "); |
| 261 |
if (name == NOT_FOUND) { |
| 262 |
name = Undefined.instance; |
| 263 |
} |
| 264 |
sb.append(ScriptRuntime.toString(name)); |
| 265 |
sb.append("("); |
| 266 |
if (message != NOT_FOUND |
| 267 |
|| fileName != NOT_FOUND |
| 268 |
|| lineNumber != NOT_FOUND) |
| 269 |
{ |
| 270 |
if (message == NOT_FOUND) { |
| 271 |
message = ""; |
| 272 |
} |
| 273 |
sb.append(ScriptRuntime.uneval(cx, scope, message)); |
| 274 |
if (fileName != NOT_FOUND || lineNumber != NOT_FOUND) { |
| 275 |
sb.append(", "); |
| 276 |
if (fileName == NOT_FOUND) { |
| 277 |
fileName = ""; |
| 278 |
} |
| 279 |
sb.append(ScriptRuntime.uneval(cx, scope, fileName)); |
| 280 |
if (lineNumber != NOT_FOUND) { |
| 281 |
int line = ScriptRuntime.toInt32(lineNumber); |
| 282 |
if (line != 0) { |
| 283 |
sb.append(", "); |
| 284 |
sb.append(ScriptRuntime.toString(line)); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
sb.append("))"); |
| 290 |
return sb.toString(); |
| 291 |
} |
| 292 |
|
| 293 |
private static void js_captureStackTrace(Context cx, Scriptable thisObj, Object[] args) |
| 294 |
{ |
| 295 |
ScriptableObject obj = (ScriptableObject)ScriptRuntime.toObjectOrNull(cx, args[0], thisObj); |
| 296 |
Function func = null; |
| 297 |
if (args.length > 1) { |
| 298 |
func = (Function)ScriptRuntime.toObjectOrNull(cx, args[1], thisObj); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
NativeError err = (NativeError)cx.newObject(thisObj, "Error"); |
| 303 |
|
| 304 |
err.setStackProvider(new EvaluatorException("[object Object]")); |
| 305 |
|
| 306 |
|
| 307 |
if (func != null) { |
| 308 |
Object funcName = func.get("name", func); |
| 309 |
if ((funcName != null) && !Undefined.instance.equals(funcName)) { |
| 310 |
err.associateValue(STACK_HIDE_KEY, Context.toString(funcName)); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
obj.defineProperty("stack", err, |
| 318 |
ERROR_DELEGATE_GET_STACK, ERROR_DELEGATE_SET_STACK, 0); |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
protected int findPrototypeId(String s) |
| 323 |
{ |
| 324 |
int id; |
| 325 |
|
| 326 |
|
| 327 |
L0: { id = 0; String X = null; int c; |
| 328 |
int s_length = s.length(); |
| 329 |
if (s_length==8) { |
| 330 |
c=s.charAt(3); |
| 331 |
if (c=='o') { X="toSource";id=Id_toSource; } |
| 332 |
else if (c=='t') { X="toString";id=Id_toString; } |
| 333 |
} |
| 334 |
else if (s_length==11) { X="constructor";id=Id_constructor; } |
| 335 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 336 |
break L0; |
| 337 |
} |
| 338 |
|
| 339 |
return id; |
| 340 |
} |
| 341 |
|
| 342 |
private static final int |
| 343 |
Id_constructor = 1, |
| 344 |
Id_toString = 2, |
| 345 |
Id_toSource = 3, |
| 346 |
ConstructorId_captureStackTrace = -1, |
| 347 |
|
| 348 |
MAX_PROTOTYPE_ID = 3; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
private static final class ProtoProps |
| 357 |
implements Serializable |
| 358 |
{ |
| 359 |
static final String KEY = "_ErrorPrototypeProps"; |
| 360 |
|
| 361 |
static final Method GET_STACK_LIMIT; |
| 362 |
static final Method SET_STACK_LIMIT; |
| 363 |
static final Method GET_PREPARE_STACK; |
| 364 |
static final Method SET_PREPARE_STACK; |
| 365 |
|
| 366 |
static { |
| 367 |
try { |
| 368 |
GET_STACK_LIMIT = ProtoProps.class.getMethod("getStackTraceLimit", Scriptable.class); |
| 369 |
SET_STACK_LIMIT = ProtoProps.class.getMethod("setStackTraceLimit", Scriptable.class, Object.class); |
| 370 |
GET_PREPARE_STACK = ProtoProps.class.getMethod("getPrepareStackTrace", Scriptable.class); |
| 371 |
SET_PREPARE_STACK = ProtoProps.class.getMethod("setPrepareStackTrace", Scriptable.class, Object.class); |
| 372 |
} catch (NoSuchMethodException nsm) { |
| 373 |
throw new RuntimeException(nsm); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
private static final long serialVersionUID = 1907180507775337939L; |
| 378 |
|
| 379 |
private int stackTraceLimit = DEFAULT_STACK_LIMIT; |
| 380 |
private Function prepareStackTrace; |
| 381 |
|
| 382 |
public Object getStackTraceLimit(Scriptable thisObj) { |
| 383 |
if (stackTraceLimit >= 0) { |
| 384 |
return stackTraceLimit; |
| 385 |
} else { |
| 386 |
return Double.POSITIVE_INFINITY; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
public int getStackTraceLimit() { |
| 391 |
return stackTraceLimit; |
| 392 |
} |
| 393 |
|
| 394 |
public void setStackTraceLimit(Scriptable thisObj, Object value) { |
| 395 |
double limit = Context.toNumber(value); |
| 396 |
if (Double.isNaN(limit) || Double.isInfinite(limit)) { |
| 397 |
stackTraceLimit = -1; |
| 398 |
} else { |
| 399 |
stackTraceLimit = (int)limit; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
public Object getPrepareStackTrace(Scriptable thisObj) |
| 404 |
{ |
| 405 |
Object ps = getPrepareStackTrace(); |
| 406 |
return (ps == null ? Undefined.instance : ps); |
| 407 |
} |
| 408 |
|
| 409 |
public Function getPrepareStackTrace() { |
| 410 |
return prepareStackTrace; |
| 411 |
} |
| 412 |
|
| 413 |
public void setPrepareStackTrace(Scriptable thisObj, Object value) { |
| 414 |
if ((value == null) || Undefined.instance.equals(value)) { |
| 415 |
prepareStackTrace = null; |
| 416 |
} else if (value instanceof Function) { |
| 417 |
prepareStackTrace = (Function)value; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |