Example usage for jdk.nashorn.api.scripting ScriptObjectMirror delete

List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror delete

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting ScriptObjectMirror delete.

Prototype

public boolean delete(final Object key) 

Source Link

Document

Delete a property from this object.

Usage

From source file:com.mckoi.webplatform.nashorn.NashornInstanceGlobalFactory.java

License:Open Source License

/**
 * Initializes the NashornInternal object.
 * /*w ww  .  jav  a 2s.c o  m*/
 * @param cl the ClassLoader to use, or null for default.
 */
public void init(final ClassLoader cl) {

    // Incase we call 'init' more than once,
    if (PRIV_nashorn_ctx != null) {
        return;
    }

    // Hacky way to return properties from the Nashorn engine,
    final Object[] inner_data = new Object[5];

    // Run initialization in a privileged security context,
    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        @Override
        public Object run() {

            // We make a VM static NashornScriptEngine that we use to compile
            // scripts and fork new JavaScript instances.

            NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
            NashornScriptEngine engine;
            if (cl == null) {
                engine = (NashornScriptEngine) factory.getScriptEngine(engine_args);
            } else {
                engine = (NashornScriptEngine) factory.getScriptEngine(engine_args, cl);
            }

            // We add some functions to the global object,
            ScriptObjectMirror global_object = (ScriptObjectMirror) engine
                    .getBindings(ScriptContext.ENGINE_SCOPE);

            // A function that performs startup. We execute this to obtain
            // information about the engine such as the Global and Context object.
            // It seems the only way to get at this information is from code
            // running within the engine.
            global_object.put("engineStartup", new EngineStartupFunction(inner_data));

            // Invoke the 'engineStartup' function to get at the priviledged
            // information.
            try {
                engine.invokeFunction("engineStartup", new Object[0]);
            } catch (ScriptException | NoSuchMethodException ex) {
                // Oops,
                throw new RuntimeException(ex);
            } finally {
                // Don't leave this function around, just incase,
                global_object.delete("engineStartup");
            }

            return null;
        }
    });

    PRIV_nashorn_ctx = (Context) inner_data[0];
    //    base_nashorn_global = (Global) inner_data[1];

}