private Scriptable setupScope(Context cx, ScriptContext context) { ScriptableObject scriptable = new ImporterTopLevel(cx); Scriptable scope = cx.initStandardObjects(scriptable); //ScriptableObject.putProperty(scope, "argv", Context.javaToJS(args, scope)); return scope; } }
String script = "function abc(x,y) {return x+y;}"; Context context = Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); Scriptable that = context.newObject(scope); Function fct = context.compileFunction(scope, script, "script", 1, null); Object result = fct.call( context, scope, that, new Object[] {2, 3}); System.out.println(Context.jsToJava(result, int.class)); } finally { Context.exit(); }
String script = "function abc(x,y) {return x+y;}" + "function def(u,v) {return u-v;}"; Context context = Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, script, "script", 1, null); Function fct = (Function)scope.get("abc", scope); Object result = fct.call( context, scope, scope, new Object[] {2, 3}); System.out.println(Context.jsToJava(result, int.class)); } finally { Context.exit(); }
public JavaScriptPredicateFactory(final String script, final ExtractionFn extractionFn) { Preconditions.checkNotNull(script, "script must not be null"); this.script = script; this.extractionFn = extractionFn; final Context cx = Context.enter(); try { cx.setOptimizationLevel(9); scope = cx.initStandardObjects(); fnApply = cx.compileFunction(scope, script, "script", 1, null); } finally { Context.exit(); } }
NativeObject scriptableObject = (NativeObject) context.initStandardObjects();
String[] names = {"print"}; shell.defineFunctionProperties(names, Shell.class, ScriptableObject.DONTENUM); Scriptable scope = context.initStandardObjects(shell); context.evaluateString(scope, "print('Hello, world!')", null, 0, null); } finally {
private @NonNull ScriptableObject initJsScope(@NonNull Context jsContext) { // Set the main Rhino goodies ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext); ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false); ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope)); try { importClasses(jsContext, scope); importPackages(jsContext, scope); importConsole(scope); importVariables(scope); importFunctions(scope); } catch (StethoJsException e) { String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e)); LogUtil.e(e, message); CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message); } return scope; }
@BeforeClass public static void setUp() throws Exception { ctx = Context.enter(); scope = ctx.initStandardObjects(); }
private static Function compile(String function) { final ContextFactory contextFactory = ContextFactory.getGlobal(); final Context context = contextFactory.enterContext(); context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL); final ScriptableObject scope = context.initStandardObjects(); final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null); Context.exit(); return new Function() { @Override public double apply(Object[] args) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } return Context.toNumber(fn.call(cx, scope, scope, args)); } }; }
private static Function<Object, String> compile(String function) { final ContextFactory contextFactory = ContextFactory.getGlobal(); final Context context = contextFactory.enterContext(); context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL); final ScriptableObject scope = context.initStandardObjects(); final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null); Context.exit(); return new Function<Object, String>() { @Override public String apply(Object input) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } final Object res = fn.call(cx, scope, scope, new Object[]{input}); return res != null ? Context.toString(res) : null; } }; }
private static Function<Object, Object> compile(String function) { final ContextFactory contextFactory = ContextFactory.getGlobal(); final Context context = contextFactory.enterContext(); context.setOptimizationLevel(9); final ScriptableObject scope = context.initStandardObjects(); final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null); Context.exit(); return new Function<Object, Object>() { @Override public Object apply(Object input) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } final Object res = fn.call(cx, scope, scope, new Object[]{input}); return res != null ? Context.toObject(res, scope) : null; } }; }
@Before public void setUp() { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); jsContext = ContextFactory.getGlobal().enterContext(); jsScope = jsContext.initStandardObjects(); view = new MappingJackson2XmlView(); }
@Before public void setUp() { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); jsContext = ContextFactory.getGlobal().enterContext(); jsScope = jsContext.initStandardObjects(); view = new MappingJackson2JsonView(); }
Context cx = factory.enterContext(); ScriptableObject scriptable = new ImporterTopLevel(cx); Scriptable scope = cx.initStandardObjects(scriptable);
context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL); final ScriptableObject scope = context.initStandardObjects();
scope = cx.initStandardObjects( null );
jsscope = jscx.initStandardObjects( null, false ); try { jscx.setOptimizationLevel( Integer.valueOf( transMeta.environmentSubstitute( optimizationLevel ) ) );
jsscope = jscx.initStandardObjects( null, false );
jsscope = jscx.initStandardObjects( null, false );
data.scope = data.cx.initStandardObjects( null, false );