use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.
the class JsEnvironment method runResource.
public AsyncContainer runResource(Resource scriptResource, Bindings globalBindings, Bindings arguments) {
AsyncContainer asyncContainer = new AsyncContainer();
runResource(scriptResource, globalBindings, arguments, asyncContainer.createCompletionCallback());
return asyncContainer;
}
use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.
the class JsValueAdapter method forceAsync.
private Object forceAsync(Object jsValue) {
AsyncContainer asyncContainer = new AsyncContainer();
asyncExtractor.extract(jsValue, asyncContainer.createCompletionCallback());
return asyncContainer.getResult();
}
use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.
the class JsUseProvider method provide.
@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
Bindings globalBindings = renderContext.getBindings();
if (!Utils.isJsScript(identifier)) {
return ProviderOutcome.failure();
}
ScriptEngine jsEngine = scriptEngineManager.getEngineByName(JS_ENGINE_NAME);
if (jsEngine == null) {
return ProviderOutcome.failure(new SightlyException("No JavaScript engine was defined."));
}
SlingScriptHelper scriptHelper = Utils.getHelper(globalBindings);
JsEnvironment environment = null;
try {
environment = new JsEnvironment(jsEngine);
environment.initialize();
ResourceResolver slingScriptingResolver = scriptingResourceResolverProvider.getRequestScopedResourceResolver();
Resource callerScript = slingScriptingResolver.getResource(scriptHelper.getScript().getScriptResource().getPath());
Resource scriptResource = Utils.getScriptResource(callerScript, identifier, globalBindings);
globalBindings.put(ScriptEngine.FILENAME, scriptResource.getPath());
proxyAsyncScriptableFactory.registerProxies(globalBindings);
AsyncContainer asyncContainer = environment.runResource(scriptResource, globalBindings, arguments);
return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
} finally {
if (environment != null) {
environment.cleanup();
}
}
}
use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.
the class SlyBindingsValuesProvider method loadFactory.
private Function loadFactory(ResourceResolver resolver, JsEnvironment jsEnvironment, String path, Bindings bindings) {
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new SightlyException("Sly namespace loader could not find the following script: " + path);
}
AsyncContainer container = jsEnvironment.runResource(resource, createBindings(bindings), new SimpleBindings());
Object obj = container.getResult();
if (!(obj instanceof Function)) {
throw new SightlyException("Script " + path + " was expected to return a function.");
}
return (Function) obj;
}
use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.
the class UseFunction method use.
private Object use(List<String> depNames, final Function callback, final Context cx, final Scriptable scope) {
final AsyncContainer asyncContainer = new AsyncContainer();
if (depNames.isEmpty()) {
callImmediate(callback, asyncContainer, cx, scope);
} else {
final int[] counter = { depNames.size() };
final Object[] dependencies = new Object[depNames.size()];
for (int i = 0; i < depNames.size(); i++) {
final int dependencyPos = i;
dependencyResolver.resolve(depNames.get(i), new UnaryCallback() {
@Override
public void invoke(Object arg) {
counter[0]--;
dependencies[dependencyPos] = arg;
if (counter[0] == 0) {
Object result = JsUtils.callFn(callback, cx, scope, thisObj, dependencies);
asyncContainer.complete(result);
}
}
});
}
}
return asyncContainer;
}
Aggregations