Search in sources :

Example 1 with ImporterTopLevel

use of org.mozilla.javascript.ImporterTopLevel in project sling by apache.

the class RhinoJavaScriptEngineFactory method getRootScope.

@SuppressWarnings("unchecked")
private Scriptable getRootScope() {
    if (rootScope == null) {
        final Context rhinoContext = Context.enter();
        try {
            rhinoContext.setOptimizationLevel(optimizationLevel);
            Scriptable tmpScope = rhinoContext.initStandardObjects(new ImporterTopLevel(rhinoContext), false);
            // default classes
            addHostObjects(tmpScope, (Class<? extends ScriptableObject>[]) HOSTOBJECT_CLASSES);
            // provided classes
            for (RhinoHostObjectProvider provider : hostObjectProvider) {
                addHostObjects(tmpScope, provider.getHostObjectClasses());
                addImportedClasses(rhinoContext, tmpScope, provider.getImportedClasses());
                addImportedPackages(rhinoContext, tmpScope, provider.getImportedPackages());
            }
            // only assign the root scope when complete set up
            rootScope = tmpScope;
        } finally {
            // ensure the context is exited after setting up the
            // the new root scope
            Context.exit();
        }
    }
    return rootScope;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) BundleContext(org.osgi.framework.BundleContext) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoHostObjectProvider(org.apache.sling.scripting.javascript.RhinoHostObjectProvider) NativeJavaClass(org.mozilla.javascript.NativeJavaClass) Scriptable(org.mozilla.javascript.Scriptable)

Example 2 with ImporterTopLevel

use of org.mozilla.javascript.ImporterTopLevel in project stetho by facebook.

the class JsRuntimeReplFactoryBuilder method initJsScope.

@NonNull
private ScriptableObject initJsScope(@NonNull Context jsContext) {
    // Set the main Rhino goodies
    ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext);
    ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false);
    ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope));
    try {
        importClasses(jsContext, scope);
        importPackages(jsContext, scope);
        importConsole(scope);
        importVariables(scope);
        importFunctions(scope);
    } catch (StethoJsException e) {
        String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e));
        LogUtil.e(e, message);
        CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message);
    }
    return scope;
}
Also used : ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) ScriptableObject(org.mozilla.javascript.ScriptableObject) NonNull(android.support.annotation.NonNull)

Example 3 with ImporterTopLevel

use of org.mozilla.javascript.ImporterTopLevel in project neo4j by neo4j.

the class JavascriptExecutor method createPrototype.

private Scriptable createPrototype(Context cx) {
    Scriptable proto = cx.initStandardObjects();
    Scriptable topLevel = new ImporterTopLevel(cx);
    proto.setParentScope(topLevel);
    return proto;
}
Also used : ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) Scriptable(org.mozilla.javascript.Scriptable)

Example 4 with ImporterTopLevel

use of org.mozilla.javascript.ImporterTopLevel in project jmeter by apache.

the class BSFJavaScriptEngine method initialize.

/**
     * Initialize the engine.
     * Put the manager into the context-manager
     * map hashtable too.
     */
@Override
public void initialize(BSFManager mgr, String lang, // superclass does not support types
@SuppressWarnings("rawtypes") Vector declaredBeans) throws BSFException {
    super.initialize(mgr, lang, declaredBeans);
    // Initialize context and global scope object
    try {
        Context cx = Context.enter();
        global = new ImporterTopLevel(cx);
        Scriptable bsf = Context.toObject(new BSFFunctions(mgr, this), global);
        global.put("bsf", global, bsf);
        // superclass does not support types
        @SuppressWarnings("unchecked") final Vector<BSFDeclaredBean> beans = declaredBeans;
        for (BSFDeclaredBean declaredBean : beans) {
            declareBean(declaredBean);
        }
    } catch (Throwable t) {
        // NOSONAR We handle correctly Error case in function
        handleError(t);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) Scriptable(org.mozilla.javascript.Scriptable) BSFFunctions(org.apache.bsf.util.BSFFunctions) BSFDeclaredBean(org.apache.bsf.BSFDeclaredBean)

Aggregations

ImporterTopLevel (org.mozilla.javascript.ImporterTopLevel)4 Scriptable (org.mozilla.javascript.Scriptable)3 Context (org.mozilla.javascript.Context)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 NonNull (android.support.annotation.NonNull)1 BSFDeclaredBean (org.apache.bsf.BSFDeclaredBean)1 BSFFunctions (org.apache.bsf.util.BSFFunctions)1 RhinoHostObjectProvider (org.apache.sling.scripting.javascript.RhinoHostObjectProvider)1 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)1 BundleContext (org.osgi.framework.BundleContext)1 ComponentContext (org.osgi.service.component.ComponentContext)1