Java Code Examples for jdk.nashorn.api.scripting.ScriptObjectMirror#unwrap()

The following examples show how to use jdk.nashorn.api.scripting.ScriptObjectMirror#unwrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Context.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final Global oldGlobal = getGlobal();
    final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
       @Override
       public Global run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobal(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobal(oldGlobal);
    }
}
 
Example 2
Source File: ScriptRuntime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Entering a {@code with} node requires new scope. This is the implementation. When exiting the with statement,
 * use {@link ScriptObject#getProto()} on the scope.
 *
 * @param scope      existing scope
 * @param expression expression in with
 *
 * @return {@link WithObject} that is the new scope
 */
public static ScriptObject openWith(final ScriptObject scope, final Object expression) {
    final Global global = Context.getGlobal();
    if (expression == UNDEFINED) {
        throw typeError(global, "cant.apply.with.to.undefined");
    } else if (expression == null) {
        throw typeError(global, "cant.apply.with.to.null");
    }

    if (expression instanceof ScriptObjectMirror) {
        final Object unwrapped = ScriptObjectMirror.unwrap(expression, global);
        if (unwrapped instanceof ScriptObject) {
            return new WithObject(scope, (ScriptObject)unwrapped);
        }
        // foreign ScriptObjectMirror
        final ScriptObject exprObj = global.newObject();
        NativeObject.bindAllProperties(exprObj, (ScriptObjectMirror)expression);
        return new WithObject(scope, exprObj);
    }

    final Object wrappedExpr = JSType.toScriptObject(global, expression);
    if (wrappedExpr instanceof ScriptObject) {
        return new WithObject(scope, (ScriptObject)wrappedExpr);
    }

    throw typeError(global, "cant.apply.with.to.non.scriptobject");
}
 
Example 3
Source File: ScriptRuntime.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Entering a {@code with} node requires new scope. This is the implementation. When exiting the with statement,
 * use {@link ScriptObject#getProto()} on the scope.
 *
 * @param scope      existing scope
 * @param expression expression in with
 *
 * @return {@link WithObject} that is the new scope
 */
public static ScriptObject openWith(final ScriptObject scope, final Object expression) {
    final Global global = Context.getGlobal();
    if (expression == UNDEFINED) {
        throw typeError(global, "cant.apply.with.to.undefined");
    } else if (expression == null) {
        throw typeError(global, "cant.apply.with.to.null");
    }

    if (expression instanceof ScriptObjectMirror) {
        final Object unwrapped = ScriptObjectMirror.unwrap(expression, global);
        if (unwrapped instanceof ScriptObject) {
            return new WithObject(scope, (ScriptObject)unwrapped);
        }
        // foreign ScriptObjectMirror
        final ScriptObject exprObj = global.newObject();
        NativeObject.bindAllProperties(exprObj, (ScriptObjectMirror)expression);
        return new WithObject(scope, exprObj);
    }

    final Object wrappedExpr = JSType.toScriptObject(global, expression);
    if (wrappedExpr instanceof ScriptObject) {
        return new WithObject(scope, (ScriptObject)wrappedExpr);
    }

    throw typeError(global, "cant.apply.with.to.non.scriptobject");
}
 
Example 4
Source File: Context.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final Global oldGlobal = getGlobal();
    final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
       @Override
       public Global run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobal(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobal(oldGlobal);
    }
}
 
Example 5
Source File: Context.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final ScriptObject oldGlobal = getGlobalTrusted();
    final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
       @Override
       public ScriptObject run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobalTrusted(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobalTrusted(oldGlobal);
    }
}
 
Example 6
Source File: Context.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final ScriptObject oldGlobal = getGlobalTrusted();
    final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
       @Override
       public ScriptObject run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobalTrusted(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobalTrusted(oldGlobal);
    }
}
 
Example 7
Source File: Global.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hook to search missing variables in ScriptContext if available
 * @param self used to detect if scope call or not (this function is 'strict')
 * @param name name of the variable missing
 * @return value of the missing variable or undefined (or TypeError for scope search)
 */
public static Object __noSuchProperty__(final Object self, final Object name) {
    final Global global = Global.instance();
    final ScriptContext sctxt = global.currentContext();
    final String nameStr = name.toString();

    if (sctxt != null) {
        final int scope = sctxt.getAttributesScope(nameStr);
        if (scope != -1) {
            return ScriptObjectMirror.unwrap(sctxt.getAttribute(nameStr, scope), global);
        }
    }

    if ("context".equals(nameStr)) {
        return sctxt;
    } else if ("engine".equals(nameStr)) {
        // expose "engine" variable only when there is no security manager
        // or when no class filter is set.
        if (System.getSecurityManager() == null || global.getClassFilter() == null) {
            return global.engine;
        }
    }

    if (self == UNDEFINED) {
        // scope access and so throw ReferenceError
        throw referenceError(global, "not.defined", nameStr);
    }

    return UNDEFINED;
}
 
Example 8
Source File: Global.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hook to search missing variables in ScriptContext if available
 * @param self used to detect if scope call or not (this function is 'strict')
 * @param name name of the variable missing
 * @return value of the missing variable or undefined (or TypeError for scope search)
 */
public static Object __noSuchProperty__(final Object self, final Object name) {
    final Global global = Global.instance();
    final ScriptContext sctxt = global.currentContext();
    final String nameStr = name.toString();

    if (sctxt != null) {
        final int scope = sctxt.getAttributesScope(nameStr);
        if (scope != -1) {
            return ScriptObjectMirror.unwrap(sctxt.getAttribute(nameStr, scope), global);
        }
    }

    if ("context".equals(nameStr)) {
        return sctxt;
    } else if ("engine".equals(nameStr)) {
        // expose "engine" variable only when there is no security manager
        // or when no class filter is set.
        if (System.getSecurityManager() == null || global.getClassFilter() == null) {
            return global.engine;
        }
    }

    if (self == UNDEFINED) {
        // scope access and so throw ReferenceError
        throw referenceError(global, "not.defined", nameStr);
    }

    return UNDEFINED;
}
 
Example 9
Source File: Context.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final Global oldGlobal = getGlobal();
    final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
       @Override
       public Global run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobal(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobal(oldGlobal);
    }
}
 
Example 10
Source File: ScriptRuntime.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Entering a {@code with} node requires new scope. This is the implementation. When exiting the with statement,
 * use {@link ScriptObject#getProto()} on the scope.
 *
 * @param scope      existing scope
 * @param expression expression in with
 *
 * @return {@link WithObject} that is the new scope
 */
public static ScriptObject openWith(final ScriptObject scope, final Object expression) {
    final Global global = Context.getGlobal();
    if (expression == UNDEFINED) {
        throw typeError(global, "cant.apply.with.to.undefined");
    } else if (expression == null) {
        throw typeError(global, "cant.apply.with.to.null");
    }

    if (expression instanceof ScriptObjectMirror) {
        final Object unwrapped = ScriptObjectMirror.unwrap(expression, global);
        if (unwrapped instanceof ScriptObject) {
            return new WithObject(scope, (ScriptObject)unwrapped);
        }
        // foreign ScriptObjectMirror
        final ScriptObject exprObj = global.newObject();
        NativeObject.bindAllProperties(exprObj, (ScriptObjectMirror)expression);
        return new WithObject(scope, exprObj);
    }

    final Object wrappedExpr = JSType.toScriptObject(global, expression);
    if (wrappedExpr instanceof ScriptObject) {
        return new WithObject(scope, (ScriptObject)wrappedExpr);
    }

    throw typeError(global, "cant.apply.with.to.non.scriptobject");
}
 
Example 11
Source File: Context.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final Global oldGlobal = getGlobal();
    final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
       @Override
       public Global run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobal(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobal(oldGlobal);
    }
}
 
Example 12
Source File: Global.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hook to search missing variables in ScriptContext if available
 * @param self used to detect if scope call or not (this function is 'strict')
 * @param name name of the variable missing
 * @return value of the missing variable or undefined (or TypeError for scope search)
 */
public static Object __noSuchProperty__(final Object self, final Object name) {
    final Global global = Global.instance();
    final ScriptContext sctxt = global.currentContext();
    final String nameStr = name.toString();

    if (sctxt != null) {
        final int scope = sctxt.getAttributesScope(nameStr);
        if (scope != -1) {
            return ScriptObjectMirror.unwrap(sctxt.getAttribute(nameStr, scope), global);
        }
    }

    switch (nameStr) {
    case "context":
        return sctxt;
    case "engine":
        return global.engine;
    default:
        break;
    }

    if (self == UNDEFINED) {
        // scope access and so throw ReferenceError
        throw referenceError(global, "not.defined", nameStr);
    }

    return UNDEFINED;
}
 
Example 13
Source File: Context.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final Global oldGlobal = getGlobal();
    final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
       @Override
       public Global run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobal(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobal(oldGlobal);
    }
}
 
Example 14
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hook to search missing variables in ScriptContext if available
 * @param self used to detect if scope call or not (this function is 'strict')
 * @param name name of the variable missing
 * @return value of the missing variable or undefined (or TypeError for scope search)
 */
public static Object __noSuchProperty__(final Object self, final Object name) {
    final Global global = Global.instance();
    final ScriptContext sctxt = global.currentContext();
    final String nameStr = name.toString();

    if (sctxt != null) {
        final int scope = sctxt.getAttributesScope(nameStr);
        if (scope != -1) {
            return ScriptObjectMirror.unwrap(sctxt.getAttribute(nameStr, scope), global);
        }
    }

    switch (nameStr) {
    case "context":
        return sctxt;
    case "engine":
        return global.engine;
    default:
        break;
    }

    if (self == UNDEFINED) {
        // scope access and so throw ReferenceError
        throw referenceError(global, "not.defined", nameStr);
    }

    return UNDEFINED;
}
 
Example 15
Source File: Global.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hook to search missing variables in ScriptContext if available
 * @param self used to detect if scope call or not (this function is 'strict')
 * @param name name of the variable missing
 * @return value of the missing variable or undefined (or TypeError for scope search)
 */
public static Object __noSuchProperty__(final Object self, final Object name) {
    final Global global = Global.instance();
    final ScriptContext sctxt = global.currentContext();
    final String nameStr = name.toString();

    if (sctxt != null) {
        final int scope = sctxt.getAttributesScope(nameStr);
        if (scope != -1) {
            return ScriptObjectMirror.unwrap(sctxt.getAttribute(nameStr, scope), global);
        }
    }

    switch (nameStr) {
    case "context":
        return sctxt;
    case "engine":
        return global.engine;
    default:
        break;
    }

    if (self == UNDEFINED) {
        // scope access and so throw ReferenceError
        throw referenceError(global, "not.defined", nameStr);
    }

    return UNDEFINED;
}
 
Example 16
Source File: JSONListAdapter.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Unwraps this adapter into its underlying non-JSObject representative.
 * @param homeGlobal the home global for unwrapping
 * @return either the unwrapped object or this if it should not be unwrapped in the specified global.
 */
public Object unwrap(final Object homeGlobal) {
    final Object unwrapped = ScriptObjectMirror.unwrap(obj, homeGlobal);
    return unwrapped != obj ? unwrapped : this;
}
 
Example 17
Source File: JSONListAdapter.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Unwraps this adapter into its underlying non-JSObject representative.
 * @param homeGlobal the home global for unwrapping
 * @return either the unwrapped object or this if it should not be unwrapped in the specified global.
 */
public Object unwrap(final Object homeGlobal) {
    final Object unwrapped = ScriptObjectMirror.unwrap(obj, homeGlobal);
    return unwrapped != obj ? unwrapped : this;
}
 
Example 18
Source File: JSONListAdapter.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Unwraps this adapter into its underlying non-JSObject representative.
 * @param homeGlobal the home global for unwrapping
 * @return either the unwrapped object or this if it should not be unwrapped in the specified global.
 */
public Object unwrap(final Object homeGlobal) {
    final Object unwrapped = ScriptObjectMirror.unwrap(obj, homeGlobal);
    return unwrapped != obj ? unwrapped : this;
}
 
Example 19
Source File: JSONListAdapter.java    From jdk8u_nashorn with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Unwraps this adapter into its underlying non-JSObject representative.
 * @param homeGlobal the home global for unwrapping
 * @return either the unwrapped object or this if it should not be unwrapped in the specified global.
 */
public Object unwrap(final Object homeGlobal) {
    final Object unwrapped = ScriptObjectMirror.unwrap(obj, homeGlobal);
    return unwrapped != obj ? unwrapped : this;
}
 
Example 20
Source File: JSONListAdapter.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Unwraps this adapter into its underlying non-JSObject representative.
 * @param homeGlobal the home global for unwrapping
 * @return either the unwrapped object or this if it should not be unwrapped in the specified global.
 */
public Object unwrap(final Object homeGlobal) {
    final Object unwrapped = ScriptObjectMirror.unwrap(obj, homeGlobal);
    return unwrapped != obj ? unwrapped : this;
}