src/jdk/nashorn/api/scripting/ScriptObjectMirror.java

Print this page




 584         });
 585     }
 586 
 587     /**
 588      * Utility to check if given object is ECMAScript undefined value
 589      *
 590      * @param obj object to check
 591      * @return true if 'obj' is ECMAScript undefined value
 592      */
 593     public static boolean isUndefined(final Object obj) {
 594         return obj == ScriptRuntime.UNDEFINED;
 595     }
 596 
 597     /**
 598      * Make a script object mirror on given object if needed. Also converts ConsString instances to Strings.
 599      *
 600      * @param obj object to be wrapped/converted
 601      * @param homeGlobal global to which this object belongs. Not used for ConsStrings.
 602      * @return wrapped/converted object
 603      */
 604     public static Object wrap(final Object obj, final ScriptObject homeGlobal) {
 605         if(obj instanceof ScriptObject) {
 606             return homeGlobal != null ? new ScriptObjectMirror((ScriptObject)obj, homeGlobal) : obj;
 607         }
 608         if(obj instanceof ConsString) {
 609             return obj.toString();
 610         }
 611         return obj;
 612     }
 613 
 614     /**
 615      * Unwrap a script object mirror if needed.
 616      *
 617      * @param obj object to be unwrapped
 618      * @param homeGlobal global to which this object belongs
 619      * @return unwrapped object
 620      */
 621     public static Object unwrap(final Object obj, final ScriptObject homeGlobal) {
 622         if (obj instanceof ScriptObjectMirror) {
 623             final ScriptObjectMirror mirror = (ScriptObjectMirror)obj;
 624             return (mirror.global == homeGlobal)? mirror.sobj : obj;
 625         }
 626 
 627         return obj;
 628     }
 629 
 630     /**
 631      * Wrap an array of object to script object mirrors if needed.
 632      *
 633      * @param args array to be unwrapped
 634      * @param homeGlobal global to which this object belongs
 635      * @return wrapped array
 636      */
 637     public static Object[] wrapArray(final Object[] args, final ScriptObject homeGlobal) {
 638         if (args == null || args.length == 0) {
 639             return args;
 640         }
 641 
 642         final Object[] newArgs = new Object[args.length];
 643         int index = 0;
 644         for (final Object obj : args) {
 645             newArgs[index] = wrap(obj, homeGlobal);
 646             index++;
 647         }
 648         return newArgs;
 649     }
 650 
 651     /**
 652      * Unwrap an array of script object mirrors if needed.
 653      *
 654      * @param args array to be unwrapped
 655      * @param homeGlobal global to which this object belongs
 656      * @return unwrapped array
 657      */
 658     public static Object[] unwrapArray(final Object[] args, final ScriptObject homeGlobal) {
 659         if (args == null || args.length == 0) {
 660             return args;
 661         }
 662 
 663         final Object[] newArgs = new Object[args.length];
 664         int index = 0;
 665         for (final Object obj : args) {
 666             newArgs[index] = unwrap(obj, homeGlobal);
 667             index++;
 668         }
 669         return newArgs;
 670     }
 671 
 672     // package-privates below this.
 673 
 674     ScriptObjectMirror(final ScriptObject sobj, final ScriptObject global) {
 675         assert sobj != null : "ScriptObjectMirror on null!";
 676         assert global instanceof GlobalObject : "global is not a GlobalObject";
 677 
 678         this.sobj = sobj;




 584         });
 585     }
 586 
 587     /**
 588      * Utility to check if given object is ECMAScript undefined value
 589      *
 590      * @param obj object to check
 591      * @return true if 'obj' is ECMAScript undefined value
 592      */
 593     public static boolean isUndefined(final Object obj) {
 594         return obj == ScriptRuntime.UNDEFINED;
 595     }
 596 
 597     /**
 598      * Make a script object mirror on given object if needed. Also converts ConsString instances to Strings.
 599      *
 600      * @param obj object to be wrapped/converted
 601      * @param homeGlobal global to which this object belongs. Not used for ConsStrings.
 602      * @return wrapped/converted object
 603      */
 604     public static Object wrap(final Object obj, final Object homeGlobal) {
 605         if(obj instanceof ScriptObject) {
 606             return homeGlobal instanceof ScriptObject ? new ScriptObjectMirror((ScriptObject)obj, (ScriptObject)homeGlobal) : obj;
 607         }
 608         if(obj instanceof ConsString) {
 609             return obj.toString();
 610         }
 611         return obj;
 612     }
 613 
 614     /**
 615      * Unwrap a script object mirror if needed.
 616      *
 617      * @param obj object to be unwrapped
 618      * @param homeGlobal global to which this object belongs
 619      * @return unwrapped object
 620      */
 621     public static Object unwrap(final Object obj, final Object homeGlobal) {
 622         if (obj instanceof ScriptObjectMirror) {
 623             final ScriptObjectMirror mirror = (ScriptObjectMirror)obj;
 624             return (mirror.global == homeGlobal)? mirror.sobj : obj;
 625         }
 626 
 627         return obj;
 628     }
 629 
 630     /**
 631      * Wrap an array of object to script object mirrors if needed.
 632      *
 633      * @param args array to be unwrapped
 634      * @param homeGlobal global to which this object belongs
 635      * @return wrapped array
 636      */
 637     public static Object[] wrapArray(final Object[] args, final Object homeGlobal) {
 638         if (args == null || args.length == 0) {
 639             return args;
 640         }
 641 
 642         final Object[] newArgs = new Object[args.length];
 643         int index = 0;
 644         for (final Object obj : args) {
 645             newArgs[index] = wrap(obj, homeGlobal);
 646             index++;
 647         }
 648         return newArgs;
 649     }
 650 
 651     /**
 652      * Unwrap an array of script object mirrors if needed.
 653      *
 654      * @param args array to be unwrapped
 655      * @param homeGlobal global to which this object belongs
 656      * @return unwrapped array
 657      */
 658     public static Object[] unwrapArray(final Object[] args, final Object homeGlobal) {
 659         if (args == null || args.length == 0) {
 660             return args;
 661         }
 662 
 663         final Object[] newArgs = new Object[args.length];
 664         int index = 0;
 665         for (final Object obj : args) {
 666             newArgs[index] = unwrap(obj, homeGlobal);
 667             index++;
 668         }
 669         return newArgs;
 670     }
 671 
 672     // package-privates below this.
 673 
 674     ScriptObjectMirror(final ScriptObject sobj, final ScriptObject global) {
 675         assert sobj != null : "ScriptObjectMirror on null!";
 676         assert global instanceof GlobalObject : "global is not a GlobalObject";
 677 
 678         this.sobj = sobj;