Debian Codegraph @

rhino/src/org/mozilla/javascript/IdFunctionObject.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 // API class
8
9 package org.mozilla.javascript;
10
11 public class IdFunctionObject extends BaseFunction
12 {
13
14 static final long serialVersionUID = -5332312783643935019L;
15
16 public IdFunctionObject(IdFunctionCall idcall, Object tag, int id, int arity)
17 {
18 if (arity < 0)
19 throw new IllegalArgumentException();
20
21 this.idcall = idcall;
22 this.tag = tag;
23 this.methodId = id;
24 this.arity = arity;
25 if (arity < 0) throw new IllegalArgumentException();
26 }
27
28 public IdFunctionObject(IdFunctionCall idcall, Object tag, int id,
29 String name, int arity, Scriptable scope)
30 {
31 super(scope, null);
32
33 if (arity < 0)
34 throw new IllegalArgumentException();
35 if (name == null)
36 throw new IllegalArgumentException();
37
38 this.idcall = idcall;
39 this.tag = tag;
40 this.methodId = id;
41 this.arity = arity;
42 this.functionName = name;
43 }
44
45 public void initFunction(String name, Scriptable scope)
46 {
47 if (name == null) throw new IllegalArgumentException();
48 if (scope == null) throw new IllegalArgumentException();
49 this.functionName = name;
50 setParentScope(scope);
51 }
52
53 public final boolean hasTag(Object tag)
54 {
55 return tag == null ? this.tag == null : tag.equals(this.tag);
56 }
57
58 public Object getTag() {
59 return tag;
60 }
61
62 public final int methodId()
63 {
64 return methodId;
65 }
66
67 public final void markAsConstructor(Scriptable prototypeProperty)
68 {
69 useCallAsConstructor = true;
70 setImmunePrototypeProperty(prototypeProperty);
71 }
72
73 public final void addAsProperty(Scriptable target)
74 {
75 ScriptableObject.defineProperty(target, functionName, this,
76 ScriptableObject.DONTENUM);
77 }
78
79 public void exportAsScopeProperty()
80 {
81 addAsProperty(getParentScope());
82 }
83
84 @Override
85 public Scriptable getPrototype()
86 {
87 // Lazy initialization of prototype: for native functions this
88 // may not be called at all
89 Scriptable proto = super.getPrototype();
90 if (proto == null) {
91 proto = getFunctionPrototype(getParentScope());
92 setPrototype(proto);
93 }
94 return proto;
95 }
96
97 @Override
98 public Object call(Context cx, Scriptable scope, Scriptable thisObj,
99 Object[] args)
100 {
101 return idcall.execIdCall(this, cx, scope, thisObj, args);
102 }
103
104 @Override
105 public Scriptable createObject(Context cx, Scriptable scope)
106 {
107 if (useCallAsConstructor) {
108 return null;
109 }
110 // Throw error if not explicitly coded to be used as constructor,
111 // to satisfy ECMAScript standard (see bugzilla 202019).
112 // To follow current (2003-05-01) SpiderMonkey behavior, change it to:
113 // return super.createObject(cx, scope);
114 throw ScriptRuntime.typeError1("msg.not.ctor", functionName);
115 }
116
117 @Override
118 String decompile(int indent, int flags)
119 {
120 StringBuilder sb = new StringBuilder();
121 boolean justbody = (0 != (flags & Decompiler.ONLY_BODY_FLAG));
122 if (!justbody) {
123 sb.append("function ");
124 sb.append(getFunctionName());
125 sb.append("() { ");
126 }
127 sb.append("[native code for ");
128 if (idcall instanceof Scriptable) {
129 Scriptable sobj = (Scriptable)idcall;
130 sb.append(sobj.getClassName());
131 sb.append('.');
132 }
133 sb.append(getFunctionName());
134 sb.append(", arity=");
135 sb.append(getArity());
136 sb.append(justbody ? "]\n" : "] }\n");
137 return sb.toString();
138 }
139
140 @Override
141 public int getArity()
142 {
143 return arity;
144 }
145
146 @Override
147 public int getLength() { return getArity(); }
148
149 @Override
150 public String getFunctionName()
151 {
152 return (functionName == null) ? "" : functionName;
153 }
154
155 public final RuntimeException unknown()
156 {
157 // It is program error to call id-like methods for unknown function
158 return new IllegalArgumentException(
159 "BAD FUNCTION ID="+methodId+" MASTER="+idcall);
160 }
161
162 private final IdFunctionCall idcall;
163 private final Object tag;
164 private final int methodId;
165 private int arity;
166 private boolean useCallAsConstructor;
167 private String functionName;
168 }