本文整理汇总了Java中jdk.nashorn.internal.objects.Global类的典型用法代码示例。如果您正苦于以下问题:Java Global类的具体用法?Java Global怎么用?Java Global使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Global类属于jdk.nashorn.internal.objects包,在下文中一共展示了Global类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evalImpl
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
private static Object evalImpl(final Context.MultiGlobalCompiledScript mgcs, final ScriptContext ctxt, final Global ctxtGlobal) throws ScriptException {
final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != ctxtGlobal);
try {
if (globalChanged) {
Context.setGlobal(ctxtGlobal);
}
final ScriptFunction script = mgcs.getFunction(ctxtGlobal);
ctxtGlobal.setScriptContext(ctxt);
return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
} catch (final Exception e) {
throwAsScriptException(e, ctxtGlobal);
throw new AssertionError("should not reach here");
} finally {
if (globalChanged) {
Context.setGlobal(oldGlobal);
}
}
}
示例2: throwAsScriptException
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
private static void throwAsScriptException(final Exception e, final Global global) throws ScriptException {
if (e instanceof ScriptException) {
throw (ScriptException)e;
} else if (e instanceof NashornException) {
final NashornException ne = (NashornException)e;
final ScriptException se = new ScriptException(
ne.getMessage(), ne.getFileName(),
ne.getLineNumber(), ne.getColumnNumber());
ne.initEcmaError(global);
se.initCause(e);
throw se;
} else if (e instanceof RuntimeException) {
throw (RuntimeException)e;
} else {
// wrap any other exception as ScriptException
throw new ScriptException(e);
}
}
示例3: compileImpl
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
private ScriptFunction compileImpl(final Source source, final Global newGlobal) throws ScriptException {
final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != newGlobal);
try {
if (globalChanged) {
Context.setGlobal(newGlobal);
}
return nashornContext.compileScript(source, newGlobal);
} catch (final Exception e) {
throwAsScriptException(e, newGlobal);
throw new AssertionError("should not reach here");
} finally {
if (globalChanged) {
Context.setGlobal(oldGlobal);
}
}
}
示例4: run
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
/**
* Run method logic.
*
* @param in input stream for Shell
* @param out output stream for Shell
* @param err error stream for Shell
* @param args arguments to Shell
*
* @return exit code
*
* @throws IOException if there's a problem setting up the streams
*/
protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
final Context context = makeContext(in, out, err, args);
if (context == null) {
return COMMANDLINE_ERROR;
}
final Global global = context.createGlobal();
final ScriptEnvironment env = context.getEnv();
final List<String> files = env.getFiles();
if (files.isEmpty()) {
return readEvalPrint(context, global);
}
if (env._compile_only) {
return compileScripts(context, global, files);
}
if (env._fx) {
return runFXScripts(context, global, files);
}
return runScripts(context, global, files);
}
示例5: toPropertyDescriptor
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
/**
* ECMA 8.10.5 ToPropertyDescriptor ( Obj )
*
* @return property descriptor
*/
public final PropertyDescriptor toPropertyDescriptor() {
final Global global = Context.getGlobal();
final PropertyDescriptor desc;
if (isDataDescriptor()) {
if (has(SET) || has(GET)) {
throw typeError(global, "inconsistent.property.descriptor");
}
desc = global.newDataDescriptor(UNDEFINED, false, false, false);
} else if (isAccessorDescriptor()) {
if (has(VALUE) || has(WRITABLE)) {
throw typeError(global, "inconsistent.property.descriptor");
}
desc = global.newAccessorDescriptor(UNDEFINED, UNDEFINED, false, false);
} else {
desc = global.newGenericDescriptor(false, false);
}
return desc.fillFrom(this);
}
示例6: evalTest
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
@Test
public void evalTest() {
final Options options = new Options("");
final ErrorManager errors = new ErrorManager();
final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
final Global oldGlobal = Context.getGlobal();
Context.setGlobal(cx.createGlobal());
try {
String code = "22 + 10";
assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue());
code = "obj = { js: 'nashorn' }; obj.js";
assertEquals(eval(cx, "<evalTest2>", code), "nashorn");
} finally {
Context.setGlobal(oldGlobal);
}
}
示例7: createNashornGlobal
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
private Global createNashornGlobal() {
final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
@Override
public Global run() {
try {
return nashornContext.newGlobal();
} catch (final RuntimeException e) {
if (Context.DEBUG) {
e.printStackTrace();
}
throw e;
}
}
}, CREATE_GLOBAL_ACC_CTXT);
nashornContext.initGlobal(newGlobal, this);
return newGlobal;
}
示例8: checkLinkRequest
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
private static void checkLinkRequest(final LinkRequest request) {
final Global global = Context.getGlobal();
final ClassFilter cf = global.getClassFilter();
if (cf != null) {
throw typeError("no.reflection.with.classfilter");
}
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
final Object self = request.getReceiver();
// allow 'static' access on Class objects representing public classes of non-restricted packages
if ((self instanceof Class) && Modifier.isPublic(((Class<?>)self).getModifiers())) {
final CallSiteDescriptor desc = request.getCallSiteDescriptor();
if ("static".equals(NashornCallSiteDescriptor.getOperand(desc)) && NashornCallSiteDescriptor.contains(desc, StandardOperation.GET, StandardNamespace.PROPERTY)) {
if (Context.isAccessibleClass((Class<?>)self) && !isReflectionClass((Class<?>)self)) {
// If "GET:PROPERTY:static" passes access checks, allow access.
return;
}
}
}
checkReflectionPermission(sm);
}
}
示例9: call
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
@Override
public Object call(final Object thiz, final Object... args) {
final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
}
if (sobj instanceof ScriptFunction) {
final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args;
final Object self = globalChanged? wrap(thiz, oldGlobal) : thiz;
return wrap(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global)), global);
}
throw new RuntimeException("not a function: " + toString());
} catch (final NashornException ne) {
throw ne.initEcmaError(global);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
} finally {
if (globalChanged) {
Context.setGlobal(oldGlobal);
}
}
}
示例10: toString
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
/**
* Converts {@code result} to a printable string. The reason we don't use {@link JSType#toString(Object)}
* or {@link ScriptRuntime#safeToString(Object)} is that we want to be able to render Symbol values
* even if they occur within an Array, and therefore have to implement our own Array to String
* conversion.
*
* @param result the result
* @param global the global object
* @return the string representation
*/
protected static String toString(final Object result, final Global global) {
if (result instanceof Symbol) {
// Normal implicit conversion of symbol to string would throw TypeError
return result.toString();
}
if (result instanceof NativeSymbol) {
return JSType.toPrimitive(result).toString();
}
if (isArrayWithDefaultToString(result, global)) {
// This should yield the same string as Array.prototype.toString but
// will not throw if the array contents include symbols.
final StringBuilder sb = new StringBuilder();
final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(result, true);
while (iter.hasNext()) {
final Object obj = iter.next();
if (obj != null && obj != ScriptRuntime.UNDEFINED) {
sb.append(toString(obj, global));
}
if (iter.hasNext()) {
sb.append(',');
}
}
return sb.toString();
}
return JSType.toString(result);
}
示例11: wrap
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
/**
* Make a script object mirror on given object if needed. Also converts ConsString instances to Strings.
*
* @param obj object to be wrapped/converted
* @param homeGlobal global to which this object belongs. Not used for ConsStrings.
* @return wrapped/converted object
*/
public static Object wrap(final Object obj, final Object homeGlobal) {
if(obj instanceof ScriptObject) {
return homeGlobal instanceof Global ? new ScriptObjectMirror((ScriptObject)obj, (Global)homeGlobal) : obj;
}
if(obj instanceof ConsString) {
return obj.toString();
}
return obj;
}
示例12: globalFilter
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
@SuppressWarnings("unused")
private static Object globalFilter(final Object object) {
ScriptObject sobj = (ScriptObject) object;
while (sobj != null && !(sobj instanceof Global)) {
sobj = sobj.getProto();
}
return sobj;
}
示例13: asCompiledScript
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
private CompiledScript asCompiledScript(final Source source) throws ScriptException {
final Context.MultiGlobalCompiledScript mgcs;
final ScriptFunction func;
final Global oldGlobal = Context.getGlobal();
final Global newGlobal = getNashornGlobalFrom(context);
final boolean globalChanged = (oldGlobal != newGlobal);
try {
if (globalChanged) {
Context.setGlobal(newGlobal);
}
mgcs = nashornContext.compileScript(source);
func = mgcs.getFunction(newGlobal);
} catch (final Exception e) {
throwAsScriptException(e, newGlobal);
throw new AssertionError("should not reach here");
} finally {
if (globalChanged) {
Context.setGlobal(oldGlobal);
}
}
return new CompiledScript() {
@Override
public Object eval(final ScriptContext ctxt) throws ScriptException {
final Global globalObject = getNashornGlobalFrom(ctxt);
// Are we running the script in the same global in which it was compiled?
if (func.getScope() == globalObject) {
return evalImpl(func, ctxt, globalObject);
}
// different global
return evalImpl(mgcs, ctxt, globalObject);
}
@Override
public ScriptEngine getEngine() {
return NashornScriptEngine.this;
}
};
}
示例14: getGuardedInvocation
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest origRequest, final LinkerServices linkerServices)
throws Exception {
final LinkRequest request = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context
final Object self = request.getReceiver();
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor) request.getCallSiteDescriptor();
return Bootstrap.asTypeSafeReturn(Global.primitiveLookup(request, self), linkerServices, desc);
}
示例15: checkLinkRequest
import jdk.nashorn.internal.objects.Global; //导入依赖的package包/类
private static void checkLinkRequest(final LinkRequest origRequest) {
final Global global = Context.getGlobal();
final ClassFilter cf = global.getClassFilter();
if (cf != null) {
throw typeError("no.reflection.with.classfilter");
}
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
final LinkRequest requestWithoutContext = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context
final Object self = requestWithoutContext.getReceiver();
// allow 'static' access on Class objects representing public classes of non-restricted packages
if ((self instanceof Class) && Modifier.isPublic(((Class<?>)self).getModifiers())) {
final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
if(CallSiteDescriptorFactory.tokenizeOperators(desc).contains("getProp")) {
if (desc.getNameTokenCount() > CallSiteDescriptor.NAME_OPERAND &&
"static".equals(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND))) {
if (Context.isAccessibleClass((Class<?>)self) && !isReflectionClass((Class<?>)self)) {
// If "getProp:static" passes access checks, allow access.
return;
}
}
}
}
checkReflectionPermission(sm);
}
}