001 
002 /*
003  *  JScripter Simulation 1.0 - For Java To Script
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 org.jscripter.sim.core;
021 
022 import java.util.Iterator;
023 
024 import org.jscripter.sim.Simul;
025 
026 
027 import js.*;
028 import js.core.*;
029 import js.user.*;
030 
031 /**
032  * <p>An <b>internal</b> class for JS Core Simulation.</p>
033  * <p>This <b>internal</b> class is only used or extended internally in JS Simulation Library.</p>
034  *
035  * @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>
036  * 
037  * @javascript Re-compilers must report error on resolving an <tt>internal</tt> class. 
038  */
039 public class Core extends Simul
040 {
041     private static Core singleton;
042 
043     /**
044      * <p>Internally determines whether a JS Core Simulation is running.</p>
045      * @return <tt>true</tt> if there is a JS Core Simulation running; <tt>false</tt> otherwise.
046      * @since 1.0
047      * 
048      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
049      */
050     public final static synchronized boolean isRunning() {
051         return singleton != null;
052     }
053 
054     /**
055      * <p>Internally gets a JS Core Simulation.</p>
056      * @return A Js Core Simulation.
057      * @since 1.0
058      * 
059      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
060      */
061     public final static synchronized Core get() {
062         if (singleton == null) {
063             singleton = new Core();
064         }
065         return singleton;
066     }
067 
068     /**
069      * <p>Internally constructs a JS Core Simulation.</p>
070      * @since 1.0
071      * 
072      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
073      */
074     protected Core() {}
075 
076     /**
077      * <p>Converts a numeric value to a number object and returns it.</p>
078      * @param n A numeric value to be converted to number object.
079      * @return The number object.
080      * @see js.core.JsNumber#JsNumber(Number)
081      * @see js.core.JsNumber#JsNumber(Value)
082      * @see js.core.JsGlobal.Number#create(Object)
083      * @see js.core.JsGlobal.Number#create(Vars)
084      * @see js.core.JsGlobal.Number#invoke(Object)
085      * @see js.core.JsGlobal.Number#invoke(Vars)
086      * @since 1.0
087      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
088      */
089     @Override
090     protected final <T extends Number> NumberLike<T> numberLike(T n) {
091         return super.numberLike(n);
092     }
093 
094     /**
095      * <p>Converts a string value to a string object and returns it.</p>
096      * @param s A string value to be converted to string object.
097      * @return The string object.
098      * @see js.core.JsString#JsString(String)
099      * @see js.core.JsString#JsString(Var)
100      * @see js.core.JsGlobal.String#create(Object)
101      * @see js.core.JsGlobal.String#create(Vars)
102      * @see js.core.JsGlobal.String#invoke(Object)
103      * @see js.core.JsGlobal.String#invoke(Vars)
104      * @since 1.0
105      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
106      */
107     @Override
108     protected final StringLike stringLike(String s) {
109         return super.stringLike(s);
110     }
111 
112     /**
113      * <p>Internally checks if the simulation is limited to JavaScript core features only.</p>
114      * <p>This method always returns <tt>true</tt>.</p>
115      * @return <tt>true</tt> if this is a core simulation; <tt>false</tt>, otherwise;
116      * @since 1.0
117      * 
118      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
119      */
120     @Override
121     protected final boolean isCore() {
122         return true;
123     }
124 
125     /**
126      * <p>Internally creates function object from a function definition.</p>
127      * @param f A {@link Function} definition.
128      * @return The function object.
129      * @since 1.0
130      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
131      */
132     @Override
133     protected <T> JsFunction<T> var(Function<T> f) {
134         return new JscFunction<T>(f);
135     }
136 
137     /**
138      * <p>Returns the opaque global object with the client-side support.</p>
139      * <p>This method simply throws an {@link UnsupportedOperationException} exception.</p>
140      * @return The opaque global object with the client-side support.
141      * @see Js#win()
142      * @see jsx.client.Client
143      * @since 1.0
144      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
145      */
146     @Override
147     protected JsWin win() {
148         throw new UnsupportedOperationException("Core only!");
149     }
150 
151     private JscGlobal g = null;
152 
153     /**
154      * <p>Returns the opaque global object with the core support only.</p>
155      * <p>This method returns exactly the same as {@link #win()}.</p>
156      * @return The opaque global object with the core support only.
157      * @see Js#core()
158      * @see jsx.client.Global
159      * @since 1.0
160      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
161      */
162     @Override
163     protected JsGlobal core() {
164         if (g == null) {
165             g = new JscGlobal();
166             g.init();
167         }
168         return g;
169     }
170 
171     /**
172      * <p>Internally down cast an object to array type.</p>
173      * <p>This method is internal and only called inside of opaque or internal classes 
174      * or class members.</p>
175      * @param o The object
176      * @since 1.0
177      * @return The array representation of an object
178      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
179      */
180     @SuppressWarnings("unchecked")
181     @Override
182     protected JsArray toJsArray(JsObject o) {
183         Unwrappable<JscObject> u = o == null ? null : (Unwrappable<JscObject>)o.var();
184         return u == null ? null : new JscArray(u.unwrap());
185     }
186 
187     /**
188      * <p>Internally down cast an object to boolean type.</p>
189      * <p>This method is internal and only called inside of opaque or internal classes 
190      * or class members.</p>
191      * @param o The object
192      * @since 1.0
193      * @return The boolean representation of an object
194      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
195      */
196     @SuppressWarnings("unchecked")
197     @Override
198     protected JsBoolean toJsBoolean(JsObject o) {
199         Unwrappable<JscObject> u = o == null ? null : (Unwrappable<JscObject>)o.var();
200         return u == null ? null : new JscBoolean(u.unwrap());
201     }
202 
203     /**
204      * <p>Internally down cast an object to date type.</p>
205      * <p>This method is internal and only called inside of opaque or internal classes 
206      * or class members.</p>
207      * @param o The object
208      * @since 1.0
209      * @return The date representation of an object
210      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
211      */
212     @SuppressWarnings("unchecked")
213     @Override
214     protected JsDate toJsDate(JsObject o) {
215         Unwrappable<JscObject> u = o == null ? null : (Unwrappable<JscObject>)o.var();
216         return u == null ? null : new JscDate(u.unwrap());
217     }
218 
219     /**
220      * <p>Internally down cast an object to function type.</p>
221      * <p>This method is internal and only called inside of opaque or internal classes 
222      * or class members.</p>
223      * @param o The object
224      * @since 1.0
225      * @return The function representation of an object
226      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
227      */
228     @SuppressWarnings("unchecked")
229     @Override
230     protected <T> JsFunction<T> toJsFunction(JsObject o) {
231         Unwrappable<JscObject> u = o == null ? null : (Unwrappable<JscObject>)o.var();
232         return u == null ? null : new JscFunction<T>(u.unwrap());
233     }
234 
235     /**
236      * <p>Internally down cast an object to function type.</p>
237      * <p>This method is internal and only called inside of opaque or internal classes 
238      * or class members.</p>
239      * @param o The object
240      * @param w A wrapper to perform the cast
241      * @since 1.0
242      * @return The function representation of an object
243      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
244      */
245     @Override
246     protected <T> JsFunction<T> toJsFunction(JsObject o, Generic<T> w) {
247         return toJsFunction(o);
248     }
249 
250     /**
251      * <p>Internally down cast an object to number type.</p>
252      * <p>This method is internal and only called inside of opaque or internal classes 
253      * or class members.</p>
254      * @param o The object
255      * @since 1.0
256      * @return The number representation of an object
257      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
258      */
259     @SuppressWarnings("unchecked")
260     @Override
261     protected JsNumber toJsNumber(JsObject o) {
262         Unwrappable<JscObject> u = o == null ? null : (Unwrappable<JscObject>)o.var();
263         return u == null ? null : new JscNumber(u.unwrap());
264     }
265 
266     /**
267      * <p>Internally down cast an object to regular expression type.</p>
268      * <p>This method is internal and only called inside of opaque or internal classes 
269      * or class members.</p>
270      * @param o The object
271      * @since 1.0
272      * @return The regular expression representation of an object
273      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
274      */
275     @SuppressWarnings("unchecked")
276     @Override
277     protected JsRegExp toJsRegExp(JsObject o) {
278         Unwrappable<JscObject> u = o == null ? null : (Unwrappable<JscObject>)o.var();
279         return u == null ? null : new JscRegExp(u.unwrap());
280     }
281 
282     /**
283      * <p>Internally down cast an object to string type.</p>
284      * <p>This method is internal and only called inside of opaque or internal classes 
285      * or class members.</p>
286      * @param o The object
287      * @since 1.0
288      * @return The string representation of an object
289      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
290      */
291     @SuppressWarnings("unchecked")
292     @Override
293     protected JsString toJsString(JsObject o) {
294         Unwrappable<JscObject> u = o == null ? null : (Unwrappable<JscObject>)o.var();
295         return u == null ? null : new JscString(u.unwrap());
296     }
297 
298     /**
299      * <p>Internally creates a variable list from a value or object.</p>
300      * @param args A value or object.
301      * @return The variable list created.
302      * @since 1.0
303      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
304      */
305     @Override
306     protected Vars<?> vars(Object args) {
307         Vars<Object> vars = new Vars<Object>();
308         if (args instanceof ArrayLike<?>) {
309             ArrayLike<?> a = (ArrayLike<?>)args;
310             for (int l = a.length(), i = 0; i < l; i++) {
311                 vars.add(a.get(i));
312             }
313         } else if (args.getClass().isArray()) {
314             Object[] a = (Object[])args;
315             for (int l = a.length, i = 0; i < l; i++) {
316                 vars.add(a[i]);
317             }
318         } else {
319             vars.add(args);
320         }
321         return vars;
322     }
323 
324     /**
325      * <p>Gets the list of the property names of an object.</p>
326      * @param o The object.
327      * @return The list of the property names of the object.
328      * @since 1.0
329      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
330      */
331     @Override
332     protected ArrayLike<String> keys(JsObject o) {
333         ArrayLike<String> ret = new Vars<String>().var();
334         Iterator<Mid> it = ((JscObject)((Unwrappable<?>)o.var()).unwrap()).iterator();
335         while (it.hasNext()) {
336             ret.push(it.next().toString());
337         }
338         return ret;
339     }
340 
341     /**
342      * <p>Copies all the properties of <tt>a</tt> to <tt>o</tt>.</p>
343      * @param o The object that the properties being copied to.
344      * @param a The object that the properties being copied from.
345      * @since 1.0
346      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
347      */
348     @Override
349     protected void apply(JsObject o, ObjectLike a) {
350         throw new UnsupportedOperationException();
351     }
352 
353     /**
354      * <p>Creates a JavaScript function.</p>
355      * <p>A function is a fundamental data-type in JavaScript. Note that although functions 
356      * may be created with this method, this is not efficient, and the preferred way to 
357      * define functions, in most cases, is to extend {@link Function}.</p>
358      * @param args A list of the argument values, each naming one or more arguments of the 
359      * function being created, ended with a string that specifies the body of the function.
360      * @return A newly created function. Invoking it executes the JavaScript code specified 
361      * by last string value of the variable argument <tt>args</tt>.
362      * @throws RuntimeException JavaScript throws a <tt>SyntaxError</tt> if there was a 
363      * JavaScript syntax error in one of the value of the variable arguments. See {@link Js#err(Object)} 
364      * and {@link js.core.JsSyntaxError} for JS Simulation.
365      * @see Function
366      * @since 1.0
367      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
368      */
369     @Override
370     protected <T> JsFunction<T> function(Vars<String> args) {
371         ArrayLike<String> a = args.var();
372         if (a.length() < 1) {
373             return new Function<T>() {
374                 @Override
375                 protected T function(Object jsthis, Call<T> callee) {
376                     return null;
377                 }
378             }.var();
379         }
380         throw new UnsupportedOperationException();
381     }
382 
383     /**
384      * <p>Gets the hash code of a value.</p>
385      * @param o Any value.
386      * @return The hash code of a value.
387      * @since 1.0
388      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
389      */
390     @Override
391     protected final int hashCode(Object o) {
392         return o.hashCode();
393     }
394 
395     /**
396      * <p>Checks if a value equals another object.</p>
397      * @param v Any value
398      * @param o Any value
399      * @return <tt>true</tt> if <tt>v</tt> equals <tt>o</tt>; false, otherwise.
400      * @since 1.0
401      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
402      */
403     @Override
404     protected final boolean equals(Object v, Object o) {
405         return eqs(v, o);
406     }
407 
408     /**
409      * <p>Checks whether the two operands are "equal" using a more relaxed definition of 
410      * sameness that allows type conversions, resembling the equality operator in 
411      * JavaScript.</p>
412      * <p>The equality and identity operations check whether two values are the same, using 
413      * two different definitions of sameness. Both operations accept operands of any type, 
414      * and both return <tt>true</tt> if their operands are the same and <tt>false</tt> 
415      * if they are different. The identity operation checks whether its two operands are 
416      * "identical" using a strict definition of sameness. The equality operation checks 
417      * whether its two operands are "equal" using a more relaxed definition of sameness 
418      * that allows type conversions.</p>
419      * <p>The identity operation is standardized by ECMAScript v3 and implemented in 
420      * JavaScript 1.3 and later. Be sure you understand the differences between the 
421      * assignment, equality, and identity operations, and be careful to use the correct one 
422      * when coding! Although it is tempting to call all three operations "equals," it may 
423      * help to reduce confusion if you read "gets or is assigned" for assignment operation, 
424      * "is equal to" for equality operation, and "is identical to" for identity operation.</p>
425      * <p>In JavaScript, numbers, strings, and boolean values are compared by value. In this 
426      * case, two separate values are involved, and the equality and identity operations 
427      * check that these two values are identical. This means that two variables are equal 
428      * or identical only if they contain the same value. For example, two strings are equal 
429      * only if they each contain exactly the same characters.</p>
430      * <p>On the other hand, objects, arrays, and functions are compared by reference. This 
431      * means that two variables are equal only if they refer to the same object. Two 
432      * separate arrays are never equal or identical, even if they contain equal or identical 
433      * elements. Two variables that contain references to objects, arrays, or functions are 
434      * equal only if they refer to the same object, array, or function. If you want to test 
435      * that two distinct objects contain the same properties or that two distinct arrays 
436      * contain the same elements, you'll have to check the properties or elements individually 
437      * for equality or identity. And, if any of the properties or elements are themselves 
438      * objects or arrays, you'll have to decide how deep you want the comparison to go.</p>
439      * <p>The following rules determine whether two values are equal according to the 
440      * equality operation:
441      * <ul>
442      * <li>If the two values have the same type, test them for identity. If the values are 
443      * identical, they are equal; if they are not identical, they are not equal.</li>
444      * <li>If the two values do not have the same type, they may still be equal. Use the 
445      * following rules and type conversions to check for equality:</li>
446      * <ul>
447      * <li>If one value is null and the other is undefined, they are equal.</li>
448      * <li>If one value is a number and the other is a string, convert the string to a 
449      * number and try the comparison again, using the converted value.</li>
450      * <li>If either value is <tt>true</tt>, convert it to 1 and try the comparison 
451      * again. If either value is <tt>false</tt>, convert it to 0 and try the comparison 
452      * again.</li>
453      * <li>If one value is an object and the other is a number or string, convert the 
454      * object to a primitive and try the comparison again. An object is converted to a 
455      * primitive value by either its <tt>toString()</tt> method or its <tt>valueOf()</tt> 
456      * method. The built-in classes of core JavaScript attempt <tt>valueOf()</tt> 
457      * conversion before <tt>toString()</tt> conversion, except for the Date class, 
458      * which performs <tt>toString()</tt> conversion. Objects that are not part of core 
459      * JavaScript may convert themselves to primitive values in an implementation-defined 
460      * way.</li>
461      * <li>Any other combinations of values are not equal.</li>
462      * </ul>
463      * </ul>
464      * @param v Any value or object.
465      * @param o Any value or object.
466      * @return <tt>true</tt> if the first operand equals the second; <tt>false</tt>, 
467      * otherwise;
468      * @since 1.0
469      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
470      */
471     @Override
472     protected final boolean eq(Object v, Object o) {
473         v = v instanceof Var<?> ? ((Var<?>)v).var() : v;
474         o = o instanceof Var<?> ? ((Var<?>)o).var() : o;
475         boolean nv = Js.undefined(v);
476         boolean no = Js.undefined(o);
477         if (nv && no) {
478             return true;
479         } else if (nv || no) {
480             return false;
481         }
482         return eqv(Js.valueOf(v), Js.valueOf(o));
483     }
484 
485     /**
486      * <p>Checks whether the two operands are "identical" using a strict definition of 
487      * sameness, resembling the identity operator in JavaScript.</p>
488      * <p>The equality and identity operations check whether two values are the same, using 
489      * two different definitions of sameness. Both operations accept operands of any type, 
490      * and both return <tt>true</tt> if their operands are the same and <tt>false</tt> 
491      * if they are different. The identity operation checks whether its two operands are 
492      * "identical" using a strict definition of sameness. The equality operation checks 
493      * whether its two operands are "equal" using a more relaxed definition of sameness 
494      * that allows type conversions.</p>
495      * <p>The identity operation is standardized by ECMAScript v3 and implemented in 
496      * JavaScript 1.3 and later. Be sure you understand the differences between the 
497      * assignment, equality, and identity operations, and be careful to use the correct one 
498      * when coding! Although it is tempting to call all three operations "equals," it may 
499      * help to reduce confusion if you read "gets or is assigned" for assignment operation, 
500      * "is equal to" for equality operation, and "is identical to" for identity operation.</p>
501      * <p>In JavaScript, numbers, strings, and boolean values are compared by value. In this 
502      * case, two separate values are involved, and the equality and identity operations 
503      * check that these two values are identical. This means that two variables are equal 
504      * or identical only if they contain the same value. For example, two strings are equal 
505      * only if they each contain exactly the same characters.</p>
506      * <p>On the other hand, objects, arrays, and functions are compared by reference. This 
507      * means that two variables are equal only if they refer to the same object. Two 
508      * separate arrays are never equal or identical, even if they contain equal or identical 
509      * elements. Two variables that contain references to objects, arrays, or functions are 
510      * equal only if they refer to the same object, array, or function. If you want to test 
511      * that two distinct objects contain the same properties or that two distinct arrays 
512      * contain the same elements, you'll have to check the properties or elements individually 
513      * for equality or identity. And, if any of the properties or elements are themselves 
514      * objects or arrays, you'll have to decide how deep you want the comparison to go.</p>
515      * <p>The following rules determine whether two values are identical according to the identity operation:
516      * <ul>
517      * <li>If the two values have different types, they are not identical.</li>
518      * <li>If both values are numbers and have the same value, they are identical, unless 
519      * either or both values are <tt>NaN</tt>, in which case they are not identical. 
520      * The <tt>NaN</tt> value is never identical to any other value, including itself! 
521      * To check whether a value is <tt>NaN</tt>, use the global {@link Js#isNaN(Object)} 
522      * function.</li>
523      * <li>If both values are strings and contain exactly the same characters in the same 
524      * positions, they are identical. If the strings differ in length or content, they are 
525      * not identical. Note that in some cases, the Unicode standard allows more than one 
526      * way to encode the same string. For efficiency, however, JavaScript's string 
527      * comparison compares strictly on a character-by-character basis, and it assumes that 
528      * all strings have been converted to a "normalized form" before they are compared. 
529      * See the {@link StringLike#localeCompare(Object)} for another way to compare strings.</li>
530      * <li>If both values are the boolean value <tt>true</tt> or both are the boolean 
531      * value <tt>false</tt>, they are identical.</li>
532      * <li>If both values refer to the same object, array, or function, they are identical. 
533      * If they refer to different objects (or arrays or functions) they are not identical, 
534      * even if both objects have identical properties or both arrays have identical elements.</li>
535      * <li>If both values are null or both values are undefined, they are identical.</li>
536      * </ul>
537      * @param v Any value or object.
538      * @param o Any value or object.
539      * @return <tt>true</tt> if the first operand is identical to the second; 
540      * <tt>false</tt>, otherwise;
541      * @since 1.0
542      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
543      */
544     @Override
545     protected final boolean eqs(Object v, Object o) {
546         v = v instanceof Var<?> ? ((Var<?>)v).var() : v;
547         o = o instanceof Var<?> ? ((Var<?>)o).var() : o;
548         boolean nv = Js.undefined(v);
549         boolean no = Js.undefined(o);
550         if (nv && no) {
551             return true;
552         } else if (nv || no) {
553             return false;
554         }
555         boolean lv = v instanceof Var<?>;
556         boolean lo = o instanceof Var<?>;
557         if (lv && lo) {
558             return ((Unwrappable<?>)v).unwrap() == ((Unwrappable<?>)o).unwrap();
559         }
560         return !lv && !lo && v.equals(o);
561     }
562 
563     /**
564      * <p>Checks if the first operand is the name of a property of the second, resembling 
565      * the <tt>in</tt> operator in JavaScript.</p>
566      * <p>This operation converts the first operand to a string and expects the second 
567      * operand be an object (or array).</p>
568      * @param v Any value that is or can be converted to a string.
569      * @param o Any object or array.
570      * @return <tt>true</tt> if the first operand is the name of a property of the 
571      * second operand; <tt>false</tt>, otherwise;
572      * @since 1.0
573      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
574      */
575     @Override
576     protected boolean in(Object v, Object o) {
577         v = v instanceof Var<?> ? ((Var<?>)v).var() : v;
578         o = o instanceof Var<?> ? ((Var<?>)o).var() : o;
579         return o instanceof JsObject && Js.be(((JsObject)o).var(v.toString()));
580     }
581 
582     /**
583      * <p>Checks if the first operand is an instance of the second, resembling the 
584      * <tt>instanceof</tt> operator in JavaScript.</p>
585      * <p>This operation expects the first operand be an object and the second be a class 
586      * of objects. In JavaScript, classes of objects are defined by the constructor function 
587      * that initializes them.</p>
588      * @param v Any value or object.
589      * @param o A constructor function.
590      * @return <tt>true</tt> if the first operand is an instance of the second; 
591      * <tt>false</tt>, otherwise;
592      * @since 1.0
593      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
594      */
595     @Override
596     protected boolean instanceOf(Object v, Object o) {
597         v = v instanceof Var<?> ? ((Var<?>)v).var() : v;
598         o = o instanceof Var<?> ? ((Var<?>)o).var() : o;
599         return v instanceof JsObject && o instanceof JsFunction<?> &&
600             equals(JsObject.constructor.with((JsObject)v), o);
601     }
602 
603     /**
604      * <p>Gets the value or object contained in a <tt>RuntimeException</tt> object 
605      * thrown by JS Simulation.</p>
606      * @param e The <tt>RuntimeException</tt> object thrown by JS Simulation.
607      * @return The value or object contained in the <tt>RuntimeException</tt> object.
608      * @since 1.0
609      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
610      */
611     @Override
612     protected Object err(RuntimeException e) {
613         return e;
614     }
615 
616     /**
617      * <p>Checks if a variable represents a Java object.</p>
618      * @param v Any value or object.
619      * @return <tt>true</tt> if <tt>v</tt> is Java; <tt>false</tt>, otherwise.
620      * @since 1.0
621      * @javascript Re-compilers must report error on the invocation to an <b>internal</b> method. 
622      */
623     @Override
624     protected boolean isJavaObject(Object v) {
625         return !(v instanceof JscObject) && !v.getClass().isPrimitive();
626     }
627 }