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;}"; 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(); }
final Function fnAggregate = context.compileFunction(scope, aggregate, "aggregate", 1, null); final Function fnReset = context.compileFunction(scope, reset, "reset", 1, null); final Function fnCombine = context.compileFunction(scope, combine, "combine", 1, null); Context.exit();
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, 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; } }; }
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; } }; }
public Function compileFunction(final Context context, ScriptableObject scope) throws JSONException { return context.compileFunction(scope, getFunction(), null, 0, null); }
final Function compileFunction(Scriptable scope, String source, Evaluator compiler, ErrorReporter compilationErrorReporter, String sourceName, int lineno, Object securityDomain) { try { return (Function) compileImpl(scope, null, source, sourceName, lineno, securityDomain, true, compiler, compilationErrorReporter); } catch (IOException ioe) { // Should never happen because we just made the reader // from a String throw new RuntimeException(); } }
final Function compileFunction(Scriptable scope, String source, Evaluator compiler, ErrorReporter compilationErrorReporter, String sourceName, int lineno, Object securityDomain) { try { return (Function) compileImpl(scope, null, source, sourceName, lineno, securityDomain, true, compiler, compilationErrorReporter); } catch (IOException ioe) { // Should never happen because we just made the reader // from a String throw new RuntimeException(); } }
@Override public Object createInstance(NitDesc nitDesc) throws NitException { Context context = Context.enter(); Scriptable scope = context.initStandardObjects(); // scope //source //sourcename //linenumber // security domain Function function = context.compileFunction(scope, nitDesc.getScript(), "nit-source", 1, null); return function; }
public JavaScriptPredicate(final String script) { Preconditions.checkNotNull(script, "script must not be null"); this.script = script; final Context cx = Context.enter(); try { cx.setOptimizationLevel(9); scope = cx.initStandardObjects(); fnApply = cx.compileFunction(scope, script, "script", 1, null); } 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(); } }
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(); } }
/** * get the (cached) handler Function for this event Handler * on first access compile the function * @return */ Function getHandler() { if (_handler == null) { String attribute = _baseElement.getAttributeWithNoDefault( _handlerName ); if (attribute != null && Context.getCurrentContext() != null) { _handler = Context.getCurrentContext().compileFunction( _baseElement, "function " + AbstractDomComponent.createAnonymousFunctionName() + "() { " + attribute + "}", "httpunit", 0, null ); } } return _handler; } }
/** * get the (cached) handler Function for this event Handler * on first access compile the function * @return */ Function getHandler() { if (_handler == null) { String attribute = _baseElement.getAttributeWithNoDefault( _handlerName ); if (attribute != null && Context.getCurrentContext() != null) { _handler = Context.getCurrentContext().compileFunction( _baseElement, "function " + AbstractDomComponent.createAnonymousFunctionName() + "() { " + attribute + "}", "httpunit", 0, null ); } } return _handler; } }
/** * get the (cached) handler Function for this event Handler * on first access compile the function * @return */ Function getHandler() { if (_handler == null) { String attribute = _baseElement.getAttributeWithNoDefault( _handlerName ); if (attribute != null && Context.getCurrentContext() != null) { _handler = Context.getCurrentContext().compileFunction( _baseElement, "function " + AbstractDomComponent.createAnonymousFunctionName() + "() { " + attribute + "}", "httpunit", 0, null ); } } return _handler; } }
private Reducer(String reduceTxt, Context cx) { Scriptable scope = new ImporterTopLevel(cx); cx.evaluateString(scope, REDUCE_JS, "reduce.js", 1, null); Scriptable builtins = (Scriptable) scope.get("BUILTIN_REDUCERS", scope); if (builtins.has(reduceTxt, builtins)) { reduceFunc = (Function)builtins.get(reduceTxt, builtins); } else { reduceFunc = cx.compileFunction(scope, reduceTxt, "reduce", 1, null); } }
private Reducer(String reduceTxt, Context cx) { Scriptable scope = new ImporterTopLevel(cx); cx.evaluateString(scope, REDUCE_JS, "reduce.js", 1, null); Scriptable builtins = (Scriptable) scope.get("BUILTIN_REDUCERS", scope); if (builtins.has(reduceTxt, builtins)) { reduceFunc = (Function)builtins.get(reduceTxt, builtins); } else { reduceFunc = cx.compileFunction(scope, reduceTxt, "reduce", 1, null); } }
private Indexer(String mapTxt, Context cx) { scope = new ImporterTopLevel(cx); cx.evaluateString(scope, INDEX_JS, "index.js", 1, null); // Index source cx.evaluateString(scope, JavascriptRun.getCollateJS(), "collate.js", 1, null); // Collation mapFunction = cx.compileFunction(scope, mapTxt, "map", 1, null); // var index = new Index() indexResults = cx.newObject(scope, "Index"); indexFunction = (Function) indexResults.getPrototype().get("indexDoc", indexResults); // var emit = index.emit Function emitFunc = (Function) indexResults.getPrototype().get("emit", indexResults); emitFunc = new BoundFunction(cx, scope, emitFunc, indexResults, NO_ARGS); scope.put("emit", scope, emitFunc); }
private Indexer(String mapTxt, Context cx) { scope = new ImporterTopLevel(cx); cx.evaluateString(scope, INDEX_JS, "index.js", 1, null); // Index source cx.evaluateString(scope, JavascriptRun.getCollateJS(), "collate.js", 1, null); // Collation mapFunction = cx.compileFunction(scope, mapTxt, "map", 1, null); // var index = new Index() indexResults = cx.newObject(scope, "Index"); indexFunction = (Function) indexResults.getPrototype().get("indexDoc", indexResults); // var emit = index.emit Function emitFunc = (Function) indexResults.getPrototype().get("emit", indexResults); emitFunc = new BoundFunction(cx, scope, emitFunc, indexResults, NO_ARGS); scope.put("emit", scope, emitFunc); }