@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; } };
@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; } };
@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 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)); } }; }
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(); } }
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(); }
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(); }
@Before public void setUp() { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); jsContext = ContextFactory.getGlobal().enterContext(); jsScope = jsContext.initStandardObjects(); view = new MappingJackson2JsonView(); }
public static void LoadScriptFromTab( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { for ( int i = 0; i < ArgList.length; i++ ) { // don't worry about "undefined" arguments String strToLoad = Context.toString( ArgList[i] ); String strScript = actualObject.get( strToLoad, actualObject ).toString(); actualContext.evaluateString( actualObject, strScript, "_" + strToLoad + "_", 0, null ); } } catch ( Exception e ) { // System.out.println(e.toString()); } }
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; } }
public static Object getTransformationName( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { Object objTranName = Context.toString( actualObject.get( "_TransformationName_", actualObject ) ); return objTranName; } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } }
public void putObject(String name, Object object) { ScriptableObject.putProperty(scope, name, Context.javaToJS(object, scope)); }
private void validateResult() throws Exception { Object xmlResult = jsContext.evaluateString(jsScope, "(" + response.getContentAsString() + ")", "XML Stream", 1, null); assertNotNull("XML Result did not eval as valid JavaScript", xmlResult); assertEquals("application/xml", response.getContentType()); }
public JsConsole(ScriptableObject scope) { setParentScope(scope); Object ctor = ScriptRuntime.getTopLevelProp(scope, "Console"); if (ctor != null && ctor instanceof Scriptable) { Scriptable scriptable = (Scriptable) ctor; setPrototype((Scriptable) scriptable.get("prototype", scriptable)); } }
Script script = cx.compileString(scriptText, "<command>", 1, null); if (script != null) { script.exec(cx, getShellScope()); // <- just an eval }
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(); }
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; } }; }