Source:
public static JavaScriptAggregator.ScriptAggregator compileScript(final String script) { final ContextFactory contextFactory = ContextFactory.getGlobal(); Context context = contextFactory.enterContext(); context.setOptimizationLevel(9); final ScriptableObject scope = context.initStandardObjects(); Script compiledScript = context.compileString(script, "script", 1, null); compiledScript.exec(context, scope); final Function fnAggregate = getScriptFunction("aggregate", scope); final Function fnReset = getScriptFunction("reset", scope); final Function fnCombine = getScriptFunction("combine", scope); Context.exit(); return new JavaScriptAggregator.ScriptAggregator() { @Override public double aggregate(final double current, final FloatMetricSelector[] selectorList) { Context cx = Context.getCurrentContext(); if(cx == null) cx = contextFactory.enterContext(); final int size = selectorList.length; final Object[] args = new Object[size + 1]; args[0] = current; int i = 0; while (i < size) { args[i + 1] = selectorList[i++].get(); } final Object res = fnAggregate.call(cx, scope, scope, args); return Context.toNumber(res); } @Override public double combine(final double a, final double b) { final Object res = contextFactory.call( new ContextAction() { @Override public Object run(final Context cx) { return fnCombine.call(cx, scope, scope, new Object[]{a, b}); } } ); return Context.toNumber(res); } @Override public double reset() { final Object res = contextFactory.call( new ContextAction() { @Override public Object run(final Context cx) { return fnReset.call(cx, scope, scope, new Object[]{}); } } ); return Context.toNumber(res); } @Override public void close() { if(Context.getCurrentContext() != null) Context.exit(); } }; }

Target:
public static JavaScriptAggregator.ScriptAggregator compileScript(final String script) { final ContextFactory contextFactory = ContextFactory.getGlobal(); Context context = contextFactory.enterContext(); context.setOptimizationLevel(9); final ScriptableObject scope = context.initStandardObjects(); Script compiledScript = context.compileString(script, "script", 1, null); compiledScript.exec(context, scope); final Function fnAggregate = getScriptFunction("aggregate", scope); final Function fnReset = getScriptFunction("reset", scope); final Function fnCombine = getScriptFunction("combine", scope); Context.exit(); return new JavaScriptAggregator.ScriptAggregator() { @Override public double aggregate(final double current, final FloatMetricSelector[] selectorList) { Context cx = Context.getCurrentContext(); if(cx == null) cx = contextFactory.enterContext(); final int size = selectorList.length; final Object[] args = new Object[size + 1]; args[0] = current; int i = 0; while (i < size) { args[i + 1] = selectorList[i++].get(); } final Object res = fnAggregate.call(cx, scope, scope, args); return Context.toNumber(res); } @Override public double combine(final double a, final double b) { final Object res = contextFactory.call( new ContextAction() { @Override public Object run(final Context cx) { return fnCombine.call(cx, scope, scope, new Object[]{a, b}); } } ); return Context.toNumber(res); } @Override public double reset() { final Object res = contextFactory.call( new ContextAction() { @Override public Object run(final Context cx) { return fnReset.call(cx, scope, scope, new Object[]{}); } } ); return Context.toNumber(res); } @Override public void close() { if(Context.getCurrentContext() != null) { Context.exit(); } } }; }