Package org.mozilla.javascript

Examples of org.mozilla.javascript.Context


     * @param contexts a Map of name-value pairs which are added to the wire Scope to enable the script to access
     *        the values by using the variable in name.
     * @return the function return value.
     */
    public Object invoke(String functionName, Object[] args, Class responseClass, Map contexts) {
        Context cx = Context.enter();
        try {
            Function function = getFunction(scriptScope, functionName);
            Scriptable invocationScope = getInvocationScope(cx, contexts);
            Object[] jsArgs = processArgs(functionName, args, invocationScope);
            Object jsResponse = function.call(cx, invocationScope, invocationScope, jsArgs);
View Full Code Here


    /**
     * Create a Rhino scope and compile the script into it
     */
    protected void initScriptScope(String fileName, String scriptCode, Map context, ClassLoader cl) {
        Context cx = Context.enter();
        try {
           
            if (cl != null) {
               cx.setApplicationClassLoader(cl);
            }
            this.scriptScope = new ImporterTopLevel( cx, true );
            Script compiledScript = cx.compileString(scriptCode, fileName, 1, null);
            compiledScript.exec(cx, scriptScope);
            addContexts(scriptScope, context);

        } finally {
            Context.exit();
View Full Code Here

    /**
     * Initializes the shared scope
     */
    protected void initSharedScope() {
        Context cx = Context.enter();
        try {

            this.sharedScope = cx.newObject(scriptScope);
            sharedScope.setPrototype(scriptScope);
            sharedScope.setParentScope(null);

        } finally {
            Context.exit();
View Full Code Here

            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Flow debugger enabled, creating");
            }
            getDebugger().doBreak();
        }
        Context context = Context.enter();
        context.setOptimizationLevel(OPTIMIZATION_LEVEL);
        context.setCompileFunctionsWithDynamicScope(true);
        context.setGeneratingDebug(true);
        // add support for Rhino objects to JXPath
        JXPathIntrospector.registerDynamicClass(org.mozilla.javascript.Scriptable.class,
                                                ScriptablePropertyHandler.class);
        JXPathContextReferenceImpl.addNodePointerFactory(new ScriptablePointerFactory());

        try {
            scope = new JSGlobal(context);

            // Register some handy classes with JavaScript, so we can make
            // use of them from the flow layer.

            // Access to the Cocoon log
            ScriptableObject.defineClass(scope, JSLog.class);

            // Access to Cocoon internal objects
            ScriptableObject.defineClass(scope, JSCocoon.class);

            // Wrapper for WebContinuation
            ScriptableObject.defineClass(scope, JSWebContinuation.class);

            // Define some functions on the top level scope
            String[] names = { "print" };
            try {
                scope.defineFunctionProperties(names, JSGlobal.class,
                                               ScriptableObject.DONTENUM);
            } catch (PropertyException e) {
                throw new Error(e.getMessage());
            }

            // Define some global variables in JavaScript
            Object args[] = {};
            Scriptable log = context.newObject(scope, "Log", args);
            ((JSLog)log).enableLogging(getLogger());
            scope.put("log", scope, log);
            errorReporter = new JSErrorReporter(getLogger());
        }
        catch (Exception e) {
View Full Code Here

     * @exception Exception if an error occurs
     */
    protected Scriptable enterContext(Environment environment)
        throws Exception
    {
        Context context = Context.enter();
        context.setOptimizationLevel(OPTIMIZATION_LEVEL);
        context.setGeneratingDebug(true);
        context.setCompileFunctionsWithDynamicScope(true);
        context.setErrorReporter(errorReporter);
        Scriptable thrScope = null;


        // Try to retrieve the scope object from the session instance. If
        // no scope is found, we create a new one, but don't place it in
        // the session.
        //
        // When a user script "creates" a session using
        // cocoon.createSession() in JavaScript, the thrScope is placed in
        // the session object, where it's later retrieved from here. This
        // behaviour allows multiple JavaScript functions to share the
        // same global scope.
        thrScope = getSessionScope(environment);

        // The Cocoon object exported to JavaScript needs to be setup here
        JSCocoon cocoon;
        boolean newScope = false;
        long lastExecTime = 0;
        if (thrScope == null) {

            newScope = true;

            thrScope = context.newObject(scope);

            thrScope.setPrototype(scope);
            // We want 'thrScope' to be a new top-level scope, so set its
            // parent scope to null. This means that any variables created
            // by assignments will be properties of "thrScope".
            thrScope.setParentScope(null);

            // Put in the thread scope the Cocoon object, which gives access
            // to the interpreter object, and some Cocoon objects. See
            // JSCocoon for more details.
            Object args[] = {};
            cocoon = (JSCocoon)context.newObject(thrScope, "Cocoon", args);
            cocoon.setInterpreter(this);
            cocoon.setParentScope(thrScope);
            thrScope.put("cocoon", thrScope, cocoon);
            ((ScriptableObject)thrScope).defineProperty(LAST_EXEC_TIME,
                                                        new Long(0),
View Full Code Here

    {
        Scriptable thrScope = null;
        try {
            thrScope = enterContext(environment);

            Context context = Context.getCurrentContext();
            JSCocoon cocoon = (JSCocoon)thrScope.get("cocoon", thrScope);
            if (enableDebugger) {
                if (!getDebugger().isVisible()) {
                    // only raise the debugger window if it isn't already visible
                    getDebugger().setVisible(true);
View Full Code Here

            p.add(new Interpreter.Argument("kontId", id));
            callFunction("handleInvalidContinuation", p, environment);
            return;
        }

        Context context = Context.enter();
        context.setOptimizationLevel(OPTIMIZATION_LEVEL);
        context.setGeneratingDebug(true);
        context.setCompileFunctionsWithDynamicScope(true);

        // Obtain the JS continuation object from it, and setup the
        // JSCocoon object associated in the dynamic scope of the saved
        // continuation with the environment and context objects.
        JSWebContinuation jswk = (JSWebContinuation)wk.getUserObject();
View Full Code Here

    public RhinoJavaScriptEngineFactory() {

        // initialize the Rhino Context Factory
        SlingContextFactory.setup(this);

        Context cx = Context.enter();
        setEngineName(getEngineName() + " (" + cx.getImplementationVersion()
            + ")");
        languageVersion = String.valueOf(cx.getLanguageVersion());
        Context.exit();

        setExtensions(ECMA_SCRIPT_EXTENSION, ESP_SCRIPT_EXTENSION);
        setMimeTypes("text/javascript", "application/ecmascript",
            "application/javascript");
View Full Code Here

        return getRootScope();
    }

    private Scriptable getRootScope() {
        if (rootScope == null) {
            final Context rhinoContext = Context.enter();
            rootScope = rhinoContext.initStandardObjects();

            for (Class<?> clazz : HOSTOBJECT_CLASSES) {
                try {

                    // register the host object
View Full Code Here

        }

        // create a rhino Context and execute the script
        try {
           
            final Context rhinoContext = Context.enter();
            final ScriptableObject scope = new NativeObject();

            // Set the global scope to be our prototype
            scope.setPrototype(rootScope);

            // We want "scope" to be a new top-level scope, so set its parent
            // scope to null. This means that any variables created by assignments
            // will be properties of "scope".
            scope.setParentScope(null);

            // setup the context for use
            rhinoContext.setWrapFactory(SlingWrapFactory.INSTANCE);

            // add initial properties to the scope
            for (Object entryObject : bindings.entrySet()) {
                Entry<?, ?> entry = (Entry<?, ?>) entryObject;
                Object wrapped = ScriptRuntime.toObject(scope, entry.getValue());
                ScriptableObject.putProperty(scope, (String) entry.getKey(),
                    wrapped);
            }

            final int lineNumber = 1;
            final Object securityDomain = null;

            return rhinoContext.evaluateReader(scope, scriptReader, scriptName,
                lineNumber, securityDomain);

        } catch (JavaScriptException t) {
           
            final ScriptException se = new ScriptException(t.details(),
View Full Code Here

TOP

Related Classes of org.mozilla.javascript.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.