/** * Setups a proper javascript context so that it can run javascript code properly under android. * For android we need to disable bytecode generation since the android vms don't understand JVM bytecode. * @return a proper javascript context */ static @NonNull Context enterJsContext() { final Context jsContext = Context.enter(); // If we cause the context to throw a runtime exception from this point // we need to make sure that exit the context. try { jsContext.setLanguageVersion(Context.VERSION_1_8); // We can't let Rhino to optimize the JS and to use a JIT because it would generate JVM bytecode // and android runs on DEX bytecode. Instead we need to go in interpreted mode. jsContext.setOptimizationLevel(-1); } catch (RuntimeException e) { // Something bad happened to the javascript context but it might still be usable. // The first thing to do is to exit the context and then propagate the error. Context.exit(); throw e; } return jsContext; } }
public DocumentConverter(final Context context, final View view) throws IOException, JSONException { this.context = context; scope = context.initStandardObjects(); context.setLanguageVersion(Context.VERSION_1_8); // Allow custom document helper class. try { ScriptableObject.defineClass(scope, RhinoDocument.class); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } // Add a log object ScriptableObject.putProperty(scope, "log", new JSLog()); // Compile user-specified function try { viewFun = view.compileFunction(context, scope); } catch (final RhinoException e) { LOG.error("View code for " + view + " does not compile."); throw e; } }
/** * Create a new Context. * * Note that the Context must be associated with a thread before * it can be used to execute a script. * * @see #enter() * @see #call(ContextAction) */ public Context() { setLanguageVersion(VERSION_DEFAULT); optimizationLevel = codegenClass != null ? 0 : -1; maximumInterpreterStackDepth = Integer.MAX_VALUE; }
/** * Creates a new context. Provided as a preferred super constructor for * subclasses in place of the deprecated default public constructor. * @param factory the context factory associated with this context (most * likely, the one that created the context). Can not be null. The context * features are inherited from the factory, and the context will also * otherwise use its factory's services. * @throws IllegalArgumentException if factory parameter is null. */ protected Context(ContextFactory factory) { if(factory == null) { throw new IllegalArgumentException("factory == null"); } this.factory = factory; setLanguageVersion(VERSION_DEFAULT); optimizationLevel = codegenClass != null ? 0 : -1; maximumInterpreterStackDepth = Integer.MAX_VALUE; }
/** * Associate a context with the current thread. This calls Context.enter() and sets the language * version to 1.8. * * @return a Context associated with the thread */ public static Context enterContext() { Context cx = Context.enter(); cx.setLanguageVersion(Context.VERSION_1_8); return cx; } }
@Override protected Context makeContext() { Context context = new SlingContext(); context.setLanguageVersion(languageVersion); return context; }
Context ctx = Context.enter(); ctx.setLanguageVersion(Context.VERSION_1_7); try { CompilerEnvirons compEnv = new CompilerEnvirons(); compEnv.initFromContext(ctx); ... } finally { Context.exit(); }
int optimisationLevel = 3; int languageVersion = Context.VERSION_1_7; try { Context cx = Context.enter(); cx.setOptimizationLevel(optimisationLevel); cx.setLanguageVersion(languageVersion); ImporterTopLevel scope = new ImporterTopLevel(cx); cx.evaluateString(scope, TEST_SCRIPT1, "doTest", 1, null); for (int i = 0; i < 10; i++) cx.evaluateString(scope, "doTest();", "", 1, null); } finally { Context.exit(); }
/** * Get and set the language version. * * This method is defined as a JavaScript function. */ public static double version(Context cx, Scriptable thisObj, Object[] args, Function funObj) { double result = cx.getLanguageVersion(); if (args.length > 0) { double d = Context.toNumber(args[0]); cx.setLanguageVersion((int) d); } return result; }
/** * Get and set the language version. * * This method is defined as a JavaScript function. */ public static double version(Context cx, Scriptable thisObj, Object[] args, Function funObj) { double result = cx.getLanguageVersion(); if (args.length > 0) { double d = Context.toNumber(args[0]); cx.setLanguageVersion((int) d); } return result; }
/** * Get and set the language version. * * This method is defined as a JavaScript function. */ public static double version(Context cx, Scriptable thisObj, Object[] args, Function funObj) { double result = cx.getLanguageVersion(); if (args.length > 0) { double d = Context.toNumber(args[0]); cx.setLanguageVersion((int) d); } return result; }
public class MyContextFactory extends ContextFactory { protected Context makeContext() { Context cx = new MyContext(this); cx.setOptimizationLevel(0); cx.setLanguageVersion(Context.VERSION_1_8); cx.setInstructionObserverThreshold(100000); return cx; } }
/** * Initialize the context. */ private ScriptableObject createContext(final ScriptableObject initialScope) { final Context context = getContext(); context.setOptimizationLevel(-1); // TODO redirect errors from System.err to LOG.error() context.setErrorReporter(new ToolErrorReporter(false)); context.setLanguageVersion(Context.VERSION_1_8); InputStream script = null; final ScriptableObject scriptCommon = (ScriptableObject) context.initStandardObjects(initialScope); try { script = getClass().getResourceAsStream("commons.js"); context.evaluateReader(scriptCommon, new InputStreamReader(script), "commons.js", 1, null); } catch (final IOException e) { throw new RuntimeException("Problem while evaluationg commons script.", e); } finally { IOUtils.closeQuietly(script); } return scriptCommon; }
protected Context enterContext() { Context context = Context.enter(); context.setOptimizationLevel(-1); // TODO redirect errors from System.err to LOG.error() context.setErrorReporter(new ToolErrorReporter(false)); context.setLanguageVersion(Context.VERSION_1_8); return context; }
@Override protected void onContextCreated(Context cx) { cx.setLanguageVersion(languageVersion); cx.setOptimizationLevel(optimizationLevel); if (errorReporter != null) { cx.setErrorReporter(errorReporter); } cx.setGeneratingDebug(generatingDebug); super.onContextCreated(cx); }
@Override protected void onContextCreated(Context cx) { cx.setLanguageVersion(languageVersion); cx.setOptimizationLevel(optimizationLevel); if (errorReporter != null) { cx.setErrorReporter(errorReporter); } cx.setGeneratingDebug(generatingDebug); super.onContextCreated(cx); }
public Rhino1_7R3Context() { Context ctx = Context.enter(); try { ctx.setOptimizationLevel(1); ctx.setLanguageVersion(Context.VERSION_1_7); // Global global = new Global(); global.init(ctx); this.global = global; this.ctx = ctx; this.scope = ctx.initStandardObjects(global); this.classCache = new HashMap<String, Class<?>>(); this.codeCache = new HashMap<String, byte[]>(); } finally { Context.exit(); } }
public Rhino1_7R3Context() { Context ctx = Context.enter(); try { ctx.setOptimizationLevel(1); ctx.setLanguageVersion(Context.VERSION_1_7); // Global global = new Global(); global.init(ctx); this.global = global; this.ctx = ctx; this.scope = ctx.initStandardObjects(global); this.classCache = new HashMap<String, Class<?>>(); this.codeCache = new HashMap<String, byte[]>(); } finally { Context.exit(); } }
/** * Create new Context instance to be associated with the current thread. */ @Override protected Context makeContext() { Context cx = super.makeContext(); cx.setLanguageVersion(languageVersion); cx.setOptimizationLevel(optimizationLevel); cx.setClassShutter(RhinoClassShutter.getInstance()); cx.setWrapFactory(RhinoWrapFactory.getInstance()); return cx; }
private void init(final Class<?> clazz) { final Context context = Context.enter(); context.setOptimizationLevel(this.optimizationLevel); context.setLanguageVersion(Context.VERSION_1_7); final ScriptableObject scope = context.initStandardObjects(); final Require require = new Require(Context.getCurrentContext(), scope, getModuleScriptProvider(clazz), null, null, false); require.install(scope); try { this.moduleScope = new ModuleScope(scope, new URI("./" + this.name), null); } catch (final URISyntaxException e) { throw new SmallerException("Failed to create moduleScope", e); } addGlobalFunction("print", LOGGER, "info"); }