Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeCallSite.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 /**
10 * This class is used by the V8 extension "Error.prepareStackTrace." It is passed to
11 * that function, which may then use it to format the stack as it sees fit.
12 */
13
14 public class NativeCallSite extends IdScriptableObject
15 {
16 private static final String CALLSITE_TAG = "CallSite";
17
18 private ScriptStackElement element;
19
20 static void init(Scriptable scope, boolean sealed)
21 {
22 NativeCallSite cs = new NativeCallSite();
23 cs.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
24 }
25
26 static NativeCallSite make(Scriptable scope, Scriptable ctorObj)
27 {
28 NativeCallSite cs = new NativeCallSite();
29 Scriptable proto = (Scriptable)(ctorObj.get("prototype", ctorObj));
30 cs.setParentScope(scope);
31 cs.setPrototype(proto);
32 return cs;
33 }
34
35 private NativeCallSite()
36 {
37 }
38
39 void setElement(ScriptStackElement elt)
40 {
41 this.element = elt;
42 }
43
44 @Override
45 public String getClassName()
46 {
47 return "CallSite";
48 }
49
50 @Override
51 protected void initPrototypeId(int id)
52 {
53 String s;
54 int arity;
55 switch (id) {
56 case Id_constructor: arity = 0; s="constructor"; break;
57 case Id_getThis: arity = 0; s="getThis"; break;
58 case Id_getTypeName: arity = 0; s="getTypeName"; break;
59 case Id_getFunction: arity = 0; s="getFunction"; break;
60 case Id_getFunctionName: arity = 0; s="getFunctionName"; break;
61 case Id_getMethodName: arity = 0; s="getMethodName"; break;
62 case Id_getFileName: arity = 0; s="getFileName"; break;
63 case Id_getLineNumber: arity = 0; s="getLineNumber"; break;
64 case Id_getColumnNumber: arity = 0; s="getColumnNumber"; break;
65 case Id_getEvalOrigin: arity = 0; s="getEvalOrigin"; break;
66 case Id_isToplevel: arity = 0; s="isToplevel"; break;
67 case Id_isEval: arity = 0; s="isEval"; break;
68 case Id_isNative: arity = 0; s="isNative"; break;
69 case Id_isConstructor: arity = 0; s="isConstructor"; break;
70 case Id_toString: arity = 0; s="toString"; break;
71 default: throw new IllegalArgumentException(String.valueOf(id));
72 }
73 initPrototypeMethod(CALLSITE_TAG, id, s, arity);
74 }
75
76 @Override
77 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
78 Scriptable thisObj, Object[] args)
79 {
80 if (!f.hasTag(CALLSITE_TAG)) {
81 return super.execIdCall(f, cx, scope, thisObj, args);
82 }
83 int id = f.methodId();
84 switch (id) {
85 case Id_constructor:
86 return make(scope, f);
87 case Id_getFunctionName:
88 return getFunctionName(thisObj);
89 case Id_getFileName:
90 return getFileName(thisObj);
91 case Id_getLineNumber:
92 return getLineNumber(thisObj);
93 case Id_getThis:
94 case Id_getTypeName:
95 case Id_getFunction:
96 case Id_getColumnNumber:
97 return getUndefined();
98 case Id_getMethodName:
99 return getNull();
100 case Id_getEvalOrigin:
101 case Id_isEval:
102 case Id_isConstructor:
103 case Id_isNative:
104 case Id_isToplevel:
105 return getFalse();
106 case Id_toString:
107 return js_toString(thisObj);
108 default:
109 throw new IllegalArgumentException(String.valueOf(id));
110 }
111 }
112
113 @Override
114 public String toString()
115 {
116 if (element == null) {
117 return "";
118 }
119 return element.toString();
120 }
121
122 private Object js_toString(Scriptable obj)
123 {
124 while(obj != null && !(obj instanceof NativeCallSite)) {
125 obj = obj.getPrototype();
126 }
127 if (obj == null) {
128 return NOT_FOUND;
129 }
130 NativeCallSite cs = (NativeCallSite)obj;
131 StringBuilder sb = new StringBuilder();
132 cs.element.renderJavaStyle(sb);
133 return sb.toString();
134 }
135
136 private Object getUndefined()
137 {
138 return Undefined.instance;
139 }
140
141 private Object getNull()
142 {
143 return null;
144 }
145
146 private Object getFalse()
147 {
148 return Boolean.FALSE;
149 }
150
151 private Object getFunctionName(Scriptable obj)
152 {
153 while(obj != null && !(obj instanceof NativeCallSite)) {
154 obj = obj.getPrototype();
155 }
156 if (obj == null) {
157 return NOT_FOUND;
158 }
159 NativeCallSite cs = (NativeCallSite)obj;
160 return (cs.element == null ? null : cs.element.functionName);
161 }
162
163 private Object getFileName(Scriptable obj)
164 {
165 while(obj != null && !(obj instanceof NativeCallSite)) {
166 obj = obj.getPrototype();
167 }
168 if (obj == null) {
169 return NOT_FOUND;
170 }
171 NativeCallSite cs = (NativeCallSite)obj;
172 return (cs.element == null ? null : cs.element.fileName);
173 }
174
175 private Object getLineNumber(Scriptable obj)
176 {
177 while(obj != null && !(obj instanceof NativeCallSite)) {
178 obj = obj.getPrototype();
179 }
180 if (obj == null) {
181 return NOT_FOUND;
182 }
183 NativeCallSite cs = (NativeCallSite)obj;
184 if ((cs.element == null) || (cs.element.lineNumber < 0)) {
185 return Undefined.instance;
186 }
187 return cs.element.lineNumber;
188 }
189
190 // #string_id_map#
191
192 @Override
193 protected int findPrototypeId(String s)
194 {
195 int id;
196 // #generated# Last update: 2015-03-02 23:42:12 PST
197 L0: { id = 0; String X = null; int c;
198 L: switch (s.length()) {
199 case 6: X="isEval";id=Id_isEval; break L;
200 case 7: X="getThis";id=Id_getThis; break L;
201 case 8: c=s.charAt(0);
202 if (c=='i') { X="isNative";id=Id_isNative; }
203 else if (c=='t') { X="toString";id=Id_toString; }
204 break L;
205 case 10: X="isToplevel";id=Id_isToplevel; break L;
206 case 11: switch (s.charAt(4)) {
207 case 'i': X="getFileName";id=Id_getFileName; break L;
208 case 't': X="constructor";id=Id_constructor; break L;
209 case 'u': X="getFunction";id=Id_getFunction; break L;
210 case 'y': X="getTypeName";id=Id_getTypeName; break L;
211 } break L;
212 case 13: switch (s.charAt(3)) {
213 case 'E': X="getEvalOrigin";id=Id_getEvalOrigin; break L;
214 case 'L': X="getLineNumber";id=Id_getLineNumber; break L;
215 case 'M': X="getMethodName";id=Id_getMethodName; break L;
216 case 'o': X="isConstructor";id=Id_isConstructor; break L;
217 } break L;
218 case 15: c=s.charAt(3);
219 if (c=='C') { X="getColumnNumber";id=Id_getColumnNumber; }
220 else if (c=='F') { X="getFunctionName";id=Id_getFunctionName; }
221 break L;
222 }
223 if (X!=null && X!=s && !X.equals(s)) id = 0;
224 break L0;
225 }
226 // #/generated#
227 return id;
228 }
229
230 private static final int
231 Id_constructor = 1,
232 Id_getThis = 2,
233 Id_getTypeName = 3,
234 Id_getFunction = 4,
235 Id_getFunctionName = 5,
236 Id_getMethodName = 6,
237 Id_getFileName = 7,
238 Id_getLineNumber = 8,
239 Id_getColumnNumber = 9,
240 Id_getEvalOrigin = 10,
241 Id_isToplevel = 11,
242 Id_isEval = 12,
243 Id_isNative = 13,
244 Id_isConstructor = 14,
245 Id_toString = 15,
246 MAX_PROTOTYPE_ID = 15;
247 // #/string_id_map#
248 }