Package org.mozilla.javascript

Examples of org.mozilla.javascript.Context


     * To be used by <code>EventTargetWrapper</code>.
     */
    void callHandler(Function handler,
                     Object arg)
        throws JavaScriptException {
        Context ctx = Context.enter();
        ctx.setWrapHandler(wrapHandler);
        try {
            arg = Context.toObject(arg, globalObject);
            Object[] args = {arg};
            handler.call(ctx, globalObject, globalObject, args);
        } finally {
View Full Code Here


     * To be used by <code>WindowWrapper</code>.
     */
    void callHandler(Function handler,
                     Object[] args)
        throws JavaScriptException {
        Context ctx = Context.enter();
        ctx.setWrapHandler(wrapHandler);
        try {
            handler.call(ctx, globalObject, globalObject, args);
        } finally {
            Context.exit();
        }
View Full Code Here

  JSGlobal scope;

  public void initialize()
    throws Exception
  {
    Context context = Context.enter();
    context.setOptimizationLevel(OPTIMIZATION_LEVEL);

    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 {
        ((ScriptableObject)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).setLogger(getLogger());
      scope.put("log", scope, log);

    } catch (Exception e) {
      context.exit();
      System.out.println("problem initializing JavaScriptInterpreter: ");
      e.printStackTrace();
      throw e;
    }
  }
View Full Code Here

  }

  protected Scriptable enterContext(Environment environment, InvokeContext ctx)
    throws Exception
  {
    Context context = Context.enter();
    context.setOptimizationLevel(OPTIMIZATION_LEVEL);
    context.setCompileFunctionsWithDynamicScope(true);
    Scriptable 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[] = {};
    Scriptable cocoon = context.newObject(scope, "Cocoon", args);
    ((JSCocoon)cocoon).setInterpreter(this);
    ((JSCocoon)cocoon).setContext(manager, environment, ctx);
    ((JSCocoon)cocoon).setContinuationsManager(continuationsMgr);
    ((JSCocoon)cocoon).setScope(thrScope);
    thrScope.put("cocoon", thrScope, cocoon);
View Full Code Here

    try {
      thrScope = enterContext(environment, null);
      source = environment.resolve(sourceName);
      InputStream inputStream = source.getInputStream();
      Reader reader = new BufferedReader(new InputStreamReader(inputStream));
      Context ctx = Context.getCurrentContext();
      ctx.evaluateReader(scope, reader, sourceName, 1, null);
    }
    catch (Exception ex) {
      ex.printStackTrace();
      throw ex;
    }
View Full Code Here

      if (fun == Scriptable.NOT_FOUND)
        throw new RuntimeException("'" + funName + "' is undefined!");
      if (!(fun instanceof Function))
        throw new RuntimeException("'" + funName + "' is not a function!");

      Context cx = Context.getCurrentContext();

      int size = (params != null ? params.size() : 0);
      Object[] funArgs = new Object[size];
      if (size != 0) {
        for (int i = 0; i < size; i++) {
View Full Code Here

    WebContinuation wk = continuationsMgr.lookupWebContinuation(id);

    if (wk == null)
      throw new RuntimeException("No continuation with id " + id);

    Context context = Context.enter();
    context.setOptimizationLevel(OPTIMIZATION_LEVEL);
    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();
    JSCocoon cocoon = jswk.getJSCocoon();
    cocoon.setContext(manager, environment, ctx);
    Scriptable kScope = cocoon.getScope();

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

    Object args[] = { jswk };
    try {
      ((Function)handleContFunction).call(context, kScope, kScope, args);
    }
    finally {
      context.exit();
    }
  }
View Full Code Here

                throw new ScriptFrameworkErrorException(
                    nsc.getMessage(), null,
                    metaData.getLanguageName(), metaData.getLanguage(),
                    ScriptFrameworkErrorType.UNKNOWN );
            }
            Context ctxt = null;

            try
            {
                String editorURL = sourceUrl.toString();
                Object result = null;
                String source = null;
                ScriptEditorForJavaScript editor =
                    ScriptEditorForJavaScript.getEditor(
                        metaData.getSourceURL() );

                if (editor != null)
                {
                    editorURL = editor.getURL();
                    result = editor.execute();
                    if ( result != null  &&
                         result.getClass().getName().equals( "org.mozilla.javascript.Undefined" ) )
                    {
                        // Always return a string
                        // TODO revisit
                        return Context.toString( result );
                    }
   
                }

                if (editor != null && editor.isModified() == true)
                {
                    LogUtils.DEBUG("GOT A MODIFIED SOURCE");
                    source = editor.getText();
                }
                else
                {
                    metaData.loadSource();
                    source =  metaData.getSource();

                }

                if ( source == null || source.length() == 0 ) {
                    throw new ScriptFrameworkErrorException(
                        "Failed to read source data for script", null,
                        metaData.getLanguageName(), metaData.getLanguage(),
                        ScriptFrameworkErrorType.UNKNOWN );
                }

                /* Set the context ClassLoader on the current thread to
                   be our custom ClassLoader. This is the suggested method
                   for setting up a ClassLoader to be used by the Rhino
                   interpreter
                 */
                if (cl != null) {
                    Thread.currentThread().setContextClassLoader(cl);
                }

                // Initialize a Rhino Context object
                ctxt = Context.enter();

                /* The ImporterTopLevel ensures that importClass and
                   importPackage statements work in Javascript scripts
                   Make the XScriptContext available as a global variable
                   to the script
                 */
                ImporterTopLevel scope = new ImporterTopLevel(ctxt);

                Scriptable jsCtxt = Context.toObject(
                   ScriptContext.createContext(
                       m_xModel, m_xInvocContext, m_xContext,
                       m_xMultiComponentFactory), scope);
                scope.put("XSCRIPTCONTEXT", scope, jsCtxt);

                Scriptable jsArgs = Context.toObject(params, scope);
                scope.put("ARGUMENTS", scope, jsArgs);

                result = ctxt.evaluateString(scope,
                        source, "<stdin>", 1, null);
                result = ctxt.toString(result);
        return result;
            }
            catch (JavaScriptException jse) {
                LogUtils.DEBUG( "Caught JavaScriptException exception for JavaScript type = " + jse.getClass() );
                String message = jse.getMessage();
View Full Code Here

        }
       
    }
    private Scriptable getScope(XScriptContext xsctxt )
    {
        Context ctxt = Context.enter();
        ImporterTopLevel scope = new ImporterTopLevel(ctxt);

        Scriptable jsCtxt = Context.toObject(xsctxt, scope);
        scope.put("XSCRIPTCONTEXT", scope, jsCtxt);
View Full Code Here

      String resultStr = "";
      boolean result = false;

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

          if (resultStr.equals("false")) {
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.