Package org.mozilla.javascript

Examples of org.mozilla.javascript.Scriptable


                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


        Session session = request.getSession(false);

        if (session == null) {
            return null;
        }
        Scriptable scope;
        HashMap userScopes = (HashMap)session.getAttribute(USER_GLOBAL_SCOPE);

        if (userScopes == null) {
            return null;
        }
View Full Code Here

        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),
                                                        ScriptableObject.DONTENUM |
                                                        ScriptableObject.PERMANENT);

        } else {
            cocoon = (JSCocoon)thrScope.get("cocoon", thrScope);
            lastExecTime = ((Long)thrScope.get(LAST_EXEC_TIME,
                                               thrScope)).longValue();

        }
        // We need to setup the JSCocoon object according to the current
        // request. Everything else remains the same.
        cocoon.setContext(manager, environment);

        // Check if we need to compile and/or execute scripts
        synchronized (compiledScripts) {
            List execList = new ArrayList();
            boolean needsRefresh = false;
            if (reloadScripts) {
                long now = System.currentTimeMillis();
                if (now >= lastTimeCheck + checkTime) {
                    needsRefresh = true;
                }
                lastTimeCheck = now;
            }
            // If we've never executed scripts in this scope or
            // if reload-scripts is true and the check interval has expired
            // or if new scripts have been specified in the sitemap,
            // then create a list of scripts to compile/execute
            if (lastExecTime == 0 || needsRefresh || needResolve.size() > 0) {
                topLevelScripts.addAll(needResolve);
                if (!newScope && !needsRefresh) {
                    execList.addAll(needResolve);
                } else {
                    execList.addAll(topLevelScripts);
                }
                needResolve.clear();
            }
            thrScope.put(LAST_EXEC_TIME, thrScope,
                         new Long(System.currentTimeMillis()));
            // Compile all the scripts first. That way you can set breakpoints
            // in the debugger before they execute.
            for (int i = 0, size = execList.size(); i < size; i++) {
                String sourceURI = (String)execList.get(i);
View Full Code Here

     */
    public void callFunction(String funName, List params,
                             Environment environment)
        throws Exception
    {
        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

        // JSCocoon object associated in the dynamic scope of the saved
        // continuation with the environment and context objects.
        JSWebContinuation jswk = (JSWebContinuation)wk.getUserObject();
        JSCocoon cocoon = jswk.getJSCocoon();
        cocoon.setContext(manager, environment);
        final Scriptable kScope = cocoon.getParentScope();
        if (enableDebugger) {
            getDebugger().setVisible(true);
        }

        // We can now resume the processing from the state saved by the
        // continuation object. Setup the JavaScript Context object.
        Object handleContFunction = kScope.get("handleContinuation", kScope);
        if (handleContFunction == Scriptable.NOT_FOUND)
            throw new RuntimeException("Cannot find 'handleContinuation' "
                                       + "(system.js not loaded?)");

        Object args[] = { jswk };
View Full Code Here

    @SuppressWarnings("unchecked")
    @Override
    public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
            Object javaObject, Class staticType) {

        Scriptable result = null;
        try {
            String hostObjectName = getHostObjectName(staticType);

            if (hostObjectName == null) {
                hostObjectName = getHostObjectName(javaObject.getClass());
View Full Code Here

            SlingScriptHelper.class);
        if (sling == null) {
            throw new NullPointerException(SlingBindings.SLING);
        }

        Scriptable globalScope = ScriptableObject.getTopLevelScope(thisObj);

        Resource scriptResource = sling.getScript().getScriptResource();
        ResourceResolver resolver = scriptResource.getResourceResolver();

        // the path of the current script to resolve realtive paths
View Full Code Here

      boolean result = false;

      // now evaluate the condition using JavaScript
      Context cx = Context.enter();
      try {
          Scriptable scope = cx.initStandardObjects(null);
          Object cxResultObject =
            cx.evaluateString(scope, cond
          /*** conditionString ***/
          , "<cmd>", 1, null);
          resultStr = Context.toString(cxResultObject);
View Full Code Here

  @Override
  public String compile (String coffeeScriptSource) {
        Context context = Context.enter();
        try {
            Scriptable compileScope = context.newObject(globalScope);
            compileScope.setParentScope(globalScope);
            compileScope.put("coffeeScriptSource", compileScope, coffeeScriptSource);
            try {
                return (String)context.evaluateString(compileScope, String.format("CoffeeScript.compile(coffeeScriptSource, %s);", options.toJavaScript()),
                        "JCoffeeScriptCompiler", 0, null);
            } catch (JavaScriptException e) {
                throw new CoffeeScriptCompileException(e);
View Full Code Here

        public XLoper execute(IFunctionContext context, XLoper[] args) throws RequestException {
            Context ctx = Context.enter();
            Object[] oargs = converter.convert(args, BSFScript.createArgHints(args));
            ScriptableObject so = ctx.initStandardObjects();
            Scriptable argsObj = ctx.newArray(so, oargs);
            so.defineProperty("args", argsObj, ScriptableObject.DONTENUM);
            try {
                return converter.createFrom(script.exec(ctx, so));
            } catch (Throwable t) {
                throw new RequestException(t.getMessage());
View Full Code Here

TOP

Related Classes of org.mozilla.javascript.Scriptable

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.