src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java

Print this page




 563         }
 564 
 565         this.engine = eng;
 566         init(eng);
 567     }
 568 
 569     /**
 570      * Wrap a Java object as corresponding script object
 571      *
 572      * @param obj object to wrap
 573      * @return    wrapped object
 574      */
 575     public Object wrapAsObject(final Object obj) {
 576         if (obj instanceof Boolean) {
 577             return new NativeBoolean((Boolean)obj, this);
 578         } else if (obj instanceof Number) {
 579             return new NativeNumber(((Number)obj).doubleValue(), this);
 580         } else if (obj instanceof String || obj instanceof ConsString) {
 581             return new NativeString((CharSequence)obj, this);
 582         } else if (obj instanceof Object[]) { // extension
 583             return new NativeArray((Object[])obj);
 584         } else if (obj instanceof double[]) { // extension
 585             return new NativeArray((double[])obj);
 586         } else if (obj instanceof long[]) {
 587             return new NativeArray((long[])obj);
 588         } else if (obj instanceof int[]) {
 589             return new NativeArray((int[])obj);
 590         } else {
 591             // FIXME: more special cases? Map? List?
 592             return obj;
 593         }
 594     }
 595 
 596     /**
 597      * Lookup helper for JS primitive types
 598      *
 599      * @param request the link request for the dynamic call site.
 600      * @param self     self reference
 601      *
 602      * @return guarded invocation
 603      */
 604     public static GuardedInvocation primitiveLookup(final LinkRequest request, final Object self) {
 605         if (self instanceof String || self instanceof ConsString) {
 606             return NativeString.lookupPrimitive(request, self);
 607         } else if (self instanceof Number) {
 608             return NativeNumber.lookupPrimitive(request, self);
 609         } else if (self instanceof Boolean) {




 563         }
 564 
 565         this.engine = eng;
 566         init(eng);
 567     }
 568 
 569     /**
 570      * Wrap a Java object as corresponding script object
 571      *
 572      * @param obj object to wrap
 573      * @return    wrapped object
 574      */
 575     public Object wrapAsObject(final Object obj) {
 576         if (obj instanceof Boolean) {
 577             return new NativeBoolean((Boolean)obj, this);
 578         } else if (obj instanceof Number) {
 579             return new NativeNumber(((Number)obj).doubleValue(), this);
 580         } else if (obj instanceof String || obj instanceof ConsString) {
 581             return new NativeString((CharSequence)obj, this);
 582         } else if (obj instanceof Object[]) { // extension
 583             return new NativeArray(ArrayData.allocate((Object[])obj), this);
 584         } else if (obj instanceof double[]) { // extension
 585             return new NativeArray(ArrayData.allocate((double[])obj), this);
 586         } else if (obj instanceof long[]) {
 587             return new NativeArray(ArrayData.allocate((long[])obj), this);
 588         } else if (obj instanceof int[]) {
 589             return new NativeArray(ArrayData.allocate((int[]) obj), this);
 590         } else {
 591             // FIXME: more special cases? Map? List?
 592             return obj;
 593         }
 594     }
 595 
 596     /**
 597      * Lookup helper for JS primitive types
 598      *
 599      * @param request the link request for the dynamic call site.
 600      * @param self     self reference
 601      *
 602      * @return guarded invocation
 603      */
 604     public static GuardedInvocation primitiveLookup(final LinkRequest request, final Object self) {
 605         if (self instanceof String || self instanceof ConsString) {
 606             return NativeString.lookupPrimitive(request, self);
 607         } else if (self instanceof Number) {
 608             return NativeNumber.lookupPrimitive(request, self);
 609         } else if (self instanceof Boolean) {