Search in sources :

Example 1 with CommonJsModule

use of org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule in project sling by apache.

the class JsEnvironment method runResource.

public void runResource(Resource scriptResource, Bindings globalBindings, Bindings arguments, UnaryCallback callback) {
    ScriptContext scriptContext = new SimpleScriptContext();
    CommonJsModule module = new CommonJsModule();
    Bindings scriptBindings = buildBindings(scriptResource, globalBindings, arguments, module);
    scriptContext.setBindings(scriptBindings, ScriptContext.ENGINE_SCOPE);
    scriptContext.setAttribute(ScriptEngine.FILENAME, scriptResource.getPath(), ScriptContext.ENGINE_SCOPE);
    runScript(scriptResource, scriptContext, callback, module);
}
Also used : CommonJsModule(org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 2 with CommonJsModule

use of org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule in project sling by apache.

the class SlyBindingsValuesProvider method createQInstance.

private Object createQInstance(Context context, Script qScript) {
    CommonJsModule module = new CommonJsModule();
    Scriptable tempScope = context.newObject(qScope);
    ScriptableObject.putProperty(tempScope, Variables.MODULE, module);
    ScriptableObject.putProperty(tempScope, Variables.EXPORTS, module.getExports());
    qScript.exec(context, tempScope);
    return module.getExports();
}
Also used : CommonJsModule(org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

CommonJsModule (org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule)2 Bindings (javax.script.Bindings)1 ScriptContext (javax.script.ScriptContext)1 SimpleBindings (javax.script.SimpleBindings)1 SimpleScriptContext (javax.script.SimpleScriptContext)1 Scriptable (org.mozilla.javascript.Scriptable)1