Java Code Examples for javax.script.ScriptContext#getAttributesScope()

The following examples show how to use javax.script.ScriptContext#getAttributesScope() . 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: NashornScriptEngine.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This hook is used to search js global variables exposed from Java code.
 *
 * @param self 'this' passed from the script
 * @param ctxt current ScriptContext in which name is searched
 * @param name name of the variable searched
 * @return the value of the named variable
 */
public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) {
    if (ctxt != null) {
        final int scope = ctxt.getAttributesScope(name);
        final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
        if (scope != -1) {
            return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal);
        }

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

    return UNDEFINED;
}
 
Example 2
Source File: NashornScriptEngine.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This hook is used to search js global variables exposed from Java code.
 *
 * @param self 'this' passed from the script
 * @param ctxt current ScriptContext in which name is searched
 * @param name name of the variable searched
 * @return the value of the named variable
 */
public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) {
    if (ctxt != null) {
        final int scope = ctxt.getAttributesScope(name);
        final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
        if (scope != -1) {
            return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal);
        }

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

    return UNDEFINED;
}
 
Example 3
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 4
Source File: FastJavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String getClassPath(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(CLASSPATH);
	if (scope != -1) {
		return ctx.getAttribute(CLASSPATH).toString();
	} else {
		// look for "com.sun.script.java.classpath"
		String res = System.getProperty(SYSPROP_PREFIX + CLASSPATH);
		if (res == null) {
			res = System.getProperty("java.class.path");
		}
		return res;
	}
}
 
Example 5
Source File: FastJavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String getSourcePath(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(SOURCEPATH);
	if (scope != -1) {
		return ctx.getAttribute(SOURCEPATH).toString();
	} else {
		// look for "com.sun.script.java.sourcepath"
		return System.getProperty(SYSPROP_PREFIX + SOURCEPATH);
	}		
}
 
Example 6
Source File: FastJavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String getFileName(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(ScriptEngine.FILENAME);
	if (scope != -1) {
		return ctx.getAttribute(ScriptEngine.FILENAME, scope).toString();
	} else {
		return "$unnamed.java";
	}
}
 
Example 7
Source File: FastJavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String getMainClassName(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(MAINCLASS);
	if (scope != -1) {
		return ctx.getAttribute(MAINCLASS).toString();
	} else {
		// look for "com.sun.script.java.mainClass"
		return System.getProperty(SYSPROP_PREFIX + MAINCLASS);
	}		
}
 
Example 8
Source File: JavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String getSourcePath(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(SOURCEPATH);
	if (scope != -1) {
		return ctx.getAttribute(SOURCEPATH).toString();
	} else {
		// look for "com.sun.script.java.sourcepath"
		return System.getProperty(SYSPROP_PREFIX + SOURCEPATH);
	}		
}
 
Example 9
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 10
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 11
Source File: JavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String getFileName(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(ScriptEngine.FILENAME);
	if (scope != -1) {
		return ctx.getAttribute(ScriptEngine.FILENAME, scope).toString();
	} else {
		return "$unnamed.java";
	}
}
 
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: FastJavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static ClassLoader getParentLoader(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(PARENTLOADER);
	if (scope != -1) {
		Object loader = ctx.getAttribute(PARENTLOADER);
		if (loader instanceof ClassLoader) {
			return (ClassLoader) loader;
		} // else fall through..
	}
	return null;	   
}
 
Example 14
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 15
Source File: JavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String getMainClassName(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(MAINCLASS);
	if (scope != -1) {
		return ctx.getAttribute(MAINCLASS).toString();
	} else {
		// look for "com.sun.script.java.mainClass"
		return System.getProperty(SYSPROP_PREFIX + MAINCLASS);
	}		
}
 
Example 16
Source File: JavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static String[] getArguments(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(ARGUMENTS);
	if (scope != -1) {
		Object obj =  ctx.getAttribute(ARGUMENTS, scope);
		if (obj instanceof String[]) {
			return (String[])obj;
		}
	}
	// return zero length array
	return EMPTY_STRING_ARRAY;
}
 
Example 17
Source File: JavaScriptEngine.java    From kite with Apache License 2.0 5 votes vote down vote up
private static ClassLoader getParentLoader(ScriptContext ctx) {
	int scope = ctx.getAttributesScope(PARENTLOADER);
	if (scope != -1) {
		Object loader = ctx.getAttribute(PARENTLOADER);
		if (loader instanceof ClassLoader) {
			return (ClassLoader) loader;
		} // else fall through..
	}
	return null;	   
}
 
Example 18
Source File: ScopeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testScriptContext_NPE_IAE() throws Exception {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext c = e.getContext();
    try {
        c.getAttribute("");
        throw new AssertionError("should have thrown IAE");
    } catch (final IllegalArgumentException iae1) {}

    try {
        c.getAttribute(null);
        throw new AssertionError("should have thrown NPE");
    } catch (final NullPointerException npe1) {}

    try {
        c.getAttribute("", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (final IllegalArgumentException iae2) {}

    try {
        c.getAttribute(null, ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (final NullPointerException npe2) {}

    try {
        c.removeAttribute("", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (final IllegalArgumentException iae3) {}

    try {
        c.removeAttribute(null, ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (final NullPointerException npe3) {}

    try {
        c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (final IllegalArgumentException iae4) {}

    try {
        c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (final NullPointerException npe4) {}

    try {
        c.getAttributesScope("");
        throw new AssertionError("should have thrown IAE");
    } catch (final IllegalArgumentException iae5) {}

    try {
        c.getAttributesScope(null);
        throw new AssertionError("should have thrown NPE");
    } catch (final NullPointerException npe5) {}
}
 
Example 19
Source File: ScopeTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testScriptContext_NPE_IAE() throws Exception {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext c = e.getContext();
    try {
        c.getAttribute("");
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae1) {}

    try {
        c.getAttribute(null);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe1) {}

    try {
        c.getAttribute("", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae2) {}

    try {
        c.getAttribute(null, ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe2) {}

    try {
        c.removeAttribute("", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae3) {}

    try {
        c.removeAttribute(null, ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe3) {}

    try {
        c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae4) {}

    try {
        c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe4) {}

    try {
        c.getAttributesScope("");
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae5) {}

    try {
        c.getAttributesScope(null);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe5) {}
}
 
Example 20
Source File: ScopeTest.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testScriptContext_NPE_IAE() throws Exception {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext c = e.getContext();
    try {
        c.getAttribute("");
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae1) {}

    try {
        c.getAttribute(null);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe1) {}

    try {
        c.getAttribute("", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae2) {}

    try {
        c.getAttribute(null, ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe2) {}

    try {
        c.removeAttribute("", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae3) {}

    try {
        c.removeAttribute(null, ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe3) {}

    try {
        c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae4) {}

    try {
        c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe4) {}

    try {
        c.getAttributesScope("");
        throw new AssertionError("should have thrown IAE");
    } catch (IllegalArgumentException iae5) {}

    try {
        c.getAttributesScope(null);
        throw new AssertionError("should have thrown NPE");
    } catch (NullPointerException npe5) {}
}