001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package js.core;
021 
022 import js.ObjectLike;
023 import js.Value;
024 
025 /**
026  * <p>An <b>opaque</b> class representing JavaScript <tt>Error</tt>.</p>
027  * <p>Instances of the <tt>Error</tt> represent errors or exceptions and are typically used 
028  * with the <tt>throw</tt> and <tt>try/catch</tt> statements in JavaScript. The <tt>name</tt> 
029  * property specifies the type of the exception, and the <tt>message</tt> property can 
030  * provide human-readable details about the exception.</p>
031  * <p>The JavaScript interpreter never throws <tt>Error</tt> objects directly; instead, it 
032  * throws instances of one of the <tt>Error</tt> subclasses, such as <tt>SyntaxError</tt> 
033  * or <tt>RangeError</tt>. In your own JavaScript code, you may find it convenient to 
034  * throw <tt>Error</tt> objects to signal exceptions, or you may prefer to simply throw an 
035  * error message or error code as a primitive string or number value.</p>
036  * <p>Note that the ECMAScript specification defines a <tt>toString()</tt> method for the 
037  * <tt>Error</tt> class (it is inherited by each of the subclasses of <tt>Error</tt>) but 
038  * that it does not require this <tt>toString()</tt> method to return a string that contains 
039  * the contents of the <tt>message</tt> property. Therefore, you should not expect the 
040  * <tt>toString()</tt> method to convert an <tt>Error</tt> object to a meaningful, 
041  * human-readable string. To display an error message to a user, you should explicitly use 
042  * the <tt>name</tt> and <tt>message</tt> properties of the <tt>Error</tt> object.</p>
043  * <p>In JS, to throw a value or object as a <tt>RuntimeException</tt>, see {@link js.Js#err(Object)}.</p>
044  *
045  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
046  * @see js.Js#err(Object)
047  * 
048  * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be 
049  * generated into the target codes. Re-compilers must exit with error on the operations of 
050  * accessing that kind of class objects.
051  * The <tt>checkcast</tt> operation to the class literal of this interface must be ignored 
052  * and <tt>instanceof</tt> to it always <tt>true</tt>.
053  */
054 public class JsError extends JsGlobal.Error.Prototype
055 {
056     /**
057      * <p>An <b>internal</b> class containing membership data for its enclosing 
058      * opaque class.</p>
059      * <p>This class is only used inside of <b>opaque</b> or <b>internal</b> classes or 
060      * class members.</p>
061      *
062      * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
063      * 
064      * @javascript Re-compilers must report error on resolving an <b>internal</b> class. 
065      */
066     protected static abstract class Members extends JsGlobal.Error.Prototype.Members
067     {
068         /**
069          * <p>An <b>internal</b> static field defining a member ID of the field name and 
070          * used internally to define a member of the same name.</p>
071          * @since 1.0
072          * @see JsError#message
073          * @see Member#message
074          * @javascript Re-compilers must report error on accessing an <b>internal</b> field. 
075          */
076         public final static Mid message = id("message");
077         /**
078          * <p>An <b>internal</b> static field defining a member ID of the field name and 
079          * used internally to define a member of the same name.</p>
080          * @since 1.0
081          * @see JsError#name
082          * @see Member#name
083          * @javascript Re-compilers must report error on accessing an <b>internal</b> field. 
084          */
085         public final static Mid name    = id("name"   );
086     }
087 
088     /**
089      * <p>An <b>opaque</b> class representing members of its enclosing <b>opaque</b> type.</p>
090      * <p>Note that, this class is <b>opaque</b> but its constructors are all <b>internal</b>. 
091      * This class and the subclasses of this class are used to declare either <b>opaque</b> 
092      * <tt>public</tt> instance fields of the opaque type {@link js.Var.Member} or the 
093      * <b>opaque</b> <tt>public</tt> static fields of other <b>opaque</b> types while their 
094      * constructors are used to define the fields inside <b>opaque</b> classes. Under 
095      * either circumstance, the field names must be exactly same as the member names, as 
096      * the <b>opaque</b> fields of <b>opaque</b> types are resolved by re-compilers directly 
097      * based on the field names.</p>
098      *
099      * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
100      * 
101      * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be created 
102      * in the target codes. Re-compilers must exit with error on operations accessing that kind 
103      * of class objects.
104      * Re-compilers must resolve an <b>opaque</b> instance field declared by this class in 
105      * {@link js.Var.Member} or its subclasses to the JavaScript identifier: 
106      * <pre>q.m</pre>
107      * where <tt>m</tt> is the identifier of the field name and <tt>q</tt> is the identifier 
108      * resolved from the instance of the enclosing member. Re-compilers must resolve an 
109      * <b>opaque</b> static field declared by this class in <b>opaque</b> types other than 
110      * {@link js.Var.Member} and its subclasses to the JavaScript identifier: 
111      * <pre>m</pre>
112      * where <tt>m</tt> is the identifier of the field name. And re-compilers must report 
113      * error on the access to <b>opaque</b> fields declared by this class under any other 
114      * circumstances.
115      */
116     public static class Member extends JsGlobal.Error.Prototype.Member
117     {
118         /**
119          * <p>Internally constructs a member based on a qualifying member.</p>
120          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> 
121          * or <b>internal</b> classes or class members.</p>
122          * <p>Note that, this constructor is <b>internal</b> but its declaring class is 
123          * <b>opaque</b>. This constructor is used to define <b>opaque</b> instance fields 
124          * declared in the declaring class of this constructor itself or its subclasses. 
125          * Under this circumstance, the field names must be exactly same as the member 
126          * names, as the <b>opaque</b> instance fields of the <b>opaque</b> type 
127          * {@link js.Var.Member} or its subclasses are resolved by re-compilers directly 
128          * to their names appending to the name resolved from the specified qualifying 
129          * member with a dot in between.</p>
130          * @param q A qualifying member
131          * @param mid The ID of the member to construct
132          * @since 1.0
133          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
134          */
135         public Member(JsObject.Member q, Mid mid) {
136             super(q, mid);
137         }
138         /**
139          * <p>Internally constructs a member without a qualifying member.</p>
140          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> 
141          * or <b>internal</b> classes or class members.</p>
142          * <p>Note that, this constructor is <b>internal</b> but its declaring class is 
143          * <b>opaque</b>. This constructor is used to define <b>opaque</b> static fields, 
144          * declared in <b>opaque</b> types other than the declaring class of this constructor 
145          * itself and its subclasses. Under this circumstance, the field names must be 
146          * exactly same as the member names, as the <b>opaque</b> static fields of <b>opaque</b> 
147          * types are generally resolved by re-compilers directly to identifiers of their names.</p>
148          * @param mid The ID of the member to construct
149          * @since 1.0
150          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
151          */
152         public Member(Mid mid) {
153             super(mid);
154         }
155         /**
156          * <p>Evaluates the property, represented by the current member instance, of the 
157          * argument object.</p>
158          * @param o The argument object
159          * @return The value of the current member based on the object argument.
160          * @since 1.0
161          * @javascript Re-compilers must convert the instance invocation of this method into 
162          * the JavaScript expression: 
163          * <pre>o.m</pre>
164          * where <tt>m</tt> is the identifier name resolved from the current member 
165          * instance of the invocation.
166          */
167         @Override
168         public JsError with(ObjectLike o) {
169             return new JsError(super.with(o));
170         }
171 
172         /**
173          * <p>An <b>opaque</b> instance field defining a sub-member that is named by the 
174          * name of this field, qualified by the current member instance of the field, and 
175          * to access the property of the name on an object.</p>
176          * <p>The <tt>constructor</tt> property of an object is a reference to the function 
177          * that was the constructor for that object.</p>
178          * <p>A common use of the <tt>constructor</tt> property is to determine the type of unknown 
179          * objects. Given an unknown value, you can use the {@link js.Js#typeof(Object)} operation 
180          * to determine whether it is a primitive value or an object. If it is an object, you can 
181          * use the <tt>constructor</tt> property to determine what type of object it is.</p>
182          * <p>Note, however, that while this technique works for the objects built into core 
183          * JavaScript, it is not guaranteed to work with host objects such as the <tt>window</tt> 
184          * object of client-side JavaScript. The default implementation of the <tt>toString()</tt> 
185          * method provides another way to determine the type of an unknown object.</p>
186          * @since 1.0
187          * @javascript Re-compilers must resolve the member of this instance field to the 
188          * identifier of the field name appending to the identifier resolved from its 
189          * qualifying member with a dot in between.
190          */
191         public final JsFunction.Member<JsError> constructor = new JsFunction.Member<JsError>(this, Members.constructor, Generic.get(JsError.class));
192         /**
193          * <p>An <b>opaque</b> instance field defining a sub-member that is named by the 
194          * name of this field, qualified by the current member instance of the field, and 
195          * to access the property of the name on an object.</p>
196          * <p>The <tt>message</tt> property of an <tt>Error</tt> object (or of an instance of any 
197          * subclass of <tt>Error</tt>) is intended to contain a human-readable string that provides 
198          * details about the error or exception that occurred. If a <tt>message</tt> argument is 
199          * passed to the <tt>Error()</tt> constructor, this message becomes the value of the <tt>message</tt> 
200          * property. If no <tt>message</tt> argument is passed, an <tt>Error</tt> object inherits 
201          * an implementation-defined default value (which may be the empty string) for this 
202          * property.</p>
203          * @since 1.0
204          * @javascript Re-compilers must resolve the member of this instance field to the 
205          * identifier of the field name appending to the identifier resolved from its 
206          * qualifying member with a dot in between.
207          */
208         public final Value.String.Member message = new Value.String.Member(this, Members.message);
209         /**
210          * <p>An <b>opaque</b> instance field defining a sub-member that is named by the 
211          * name of this field, qualified by the current member instance of the field, and 
212          * to access the property of the name on an object.</p>
213          * <p>The <tt>name</tt> property of an <tt>Error</tt> object (or of an instance of 
214          * any subclass of <tt>Error</tt>) specifies the type of error or exception that 
215          * occurred. All <tt>Error</tt> objects inherit this property from their constructor. 
216          * The value of the property is the same as the name of the constructor. Thus 
217          * <tt>SyntaxError</tt> objects have a <tt>name</tt> property of "SyntaxError", and 
218          * <tt>EvalError</tt> objects have a <tt>name</tt> of "EvalError".</p>
219          * @since 1.0
220          * @javascript Re-compilers must resolve the member of this instance field to the 
221          * identifier of the field name appending to the identifier resolved from its 
222          * qualifying member with a dot in between.
223          */
224         public final Value.String.Member name    = new Value.String.Member(this, Members.name   );
225     }
226 
227     /**
228      * <p>Casts an <b>opaque</b> object to the current <b>opaque</b> type by wrapping it 
229      * with the wrapping constructor.</p>
230      * @param var The argument of an <b>opaque</b> object.
231      * @since 1.0
232      * @javascript Re-compilers must ignore the construction operation of this constructor, 
233      * that is, replacing it with its only argument.
234      */
235     public JsError(JsObject var) {
236         super(var);
237     }
238 
239     /**
240      * <p>An <b>opaque</b> static field defining a member that is named by the field name 
241      * without a qualifying member and to access the property of the name on an object.</p>
242      * <p>The <tt>constructor</tt> property of an object is a reference to the function 
243      * that was the constructor for that object.</p>
244      * <p>A common use of the <tt>constructor</tt> property is to determine the type of unknown 
245      * objects. Given an unknown value, you can use the {@link js.Js#typeof(Object)} operation 
246      * to determine whether it is a primitive value or an object. If it is an object, you can 
247      * use the <tt>constructor</tt> property to determine what type of object it is.</p>
248      * <p>Note, however, that while this technique works for the objects built into core 
249      * JavaScript, it is not guaranteed to work with host objects such as the <tt>window</tt> 
250      * object of client-side JavaScript. The default implementation of the <tt>toString()</tt> 
251      * method provides another way to determine the type of an unknown object.</p>
252      * @since 1.0
253      * @javascript Re-compilers must resolve the member of this static field to the 
254      * identifier of the field name.
255      */
256     public static final JsFunction.Member<JsError> constructor = new JsFunction.Member<JsError>(Members.constructor, Generic.get(JsError.class));
257     /**
258      * <p>An <b>opaque</b> static field defining a member that is named by the field name 
259      * without a qualifying member and to access the property of the name on an object.</p>
260      * <p>The <tt>message</tt> property of an <tt>Error</tt> object (or of an instance of any 
261      * subclass of <tt>Error</tt>) is intended to contain a human-readable string that provides 
262      * details about the error or exception that occurred. If a <tt>message</tt> argument is 
263      * passed to the <tt>Error()</tt> constructor, this message becomes the value of the <tt>message</tt> 
264      * property. If no <tt>message</tt> argument is passed, an <tt>Error</tt> object inherits 
265      * an implementation-defined default value (which may be the empty string) for this 
266      * property.</p>
267      * @since 1.0
268      * @javascript Re-compilers must resolve the member of this static field to the 
269      * identifier of the field name.
270      */
271     public static final Value.String.Member message = new Value.String.Member(Members.message);
272     /**
273      * <p>An <b>opaque</b> static field defining a member that is named by the field name 
274      * without a qualifying member and to access the property of the name on an object.</p>
275      * <p>The <tt>name</tt> property of an <tt>Error</tt> object (or of an instance of 
276      * any subclass of <tt>Error</tt>) specifies the type of error or exception that 
277      * occurred. All <tt>Error</tt> objects inherit this property from their constructor. 
278      * The value of the property is the same as the name of the constructor. Thus 
279      * <tt>SyntaxError</tt> objects have a <tt>name</tt> property of "SyntaxError", and 
280      * <tt>EvalError</tt> objects have a <tt>name</tt> of "EvalError".</p>
281      * @since 1.0
282      * @javascript Re-compilers must resolve the member of this static field to the 
283      * identifier of the field name.
284      */
285     public static final Value.String.Member name    = new Value.String.Member(Members.name   );
286 
287     /**
288      * <p>Returns the primitive value associated with the current instance, if there is one. 
289      * This invocation simply returns the instance itself for the current instance is an 
290      * object and there is no primitive value for it.</p>
291      * @return The current object itself.
292      * @since 1.0
293      * @javascript Re-compilers must convert the instance invocation of this method directly 
294      * into a JavaScript invocation on its current object instance without changing the 
295      * method name, but expanding variable arguments, if any, into comma-separated values. 
296      */
297     @Override
298     public JsError valueOf() {
299         return new JsError((JsObject)var().valueOf());
300     }
301 }