Beispiel #1
0
  /**
   * Initialize the JavaScript context using given parent scope.
   *
   * @param scPrototype Parent scope object. If it's null, use default scope.
   */
  public final void init(Scriptable scPrototype) throws ChartException {
    final Context cx = Context.enter();
    try {
      if (scPrototype == null) // NO PROTOTYPE
      {
        // scope = cx.initStandardObjects();
        scope = new ImporterTopLevel(cx);
      } else {
        scope = cx.newObject(scPrototype);
        scope.setPrototype(scPrototype);
        // !don't reset the parent scope here.
        // scope.setParentScope( null );
      }

      // final Scriptable scopePrevious = scope;
      // !deprecated, remove this later. use script context instead.
      // registerExistingScriptableObject( this, "chart" ); //$NON-NLS-1$
      // scope = scopePrevious; // RESTORE

      // !deprecated, remove this later, use logger from script context
      // instead.
      // ADD LOGGING CAPABILITIES TO JAVASCRIPT ACCESS
      final Object oConsole = Context.javaToJS(getLogger(), scope);
      scope.put("logger", scope, oConsole); // $NON-NLS-1$
    } catch (RhinoException jsx) {
      throw convertException(jsx);
    } finally {
      Context.exit();
    }
  }
Beispiel #2
0
 protected void addToScope(Scriptable scope) {
   Object value = exportingBundle.lookup(name);
   StringTokenizer tokenizer = new StringTokenizer(name, "."); // $NON-NLS-1$
   while (true) {
     String token = tokenizer.nextToken();
     Object current = scope.get(token, scope);
     if (!tokenizer.hasMoreTokens()) {
       if (current == Scriptable.NOT_FOUND) {
         if (value instanceof NativeObject) {
           Scriptable wrapped = Context.getCurrentContext().newObject(scope);
           wrapped.setPrototype((Scriptable) value);
           value = wrapped;
         }
         scope.put(token, scope, value);
         return;
       }
       throw new IllegalStateException(
           "Resolve error: "
               + name
               + " already exists for "
               + this.toString()); // $NON-NLS-1$//$NON-NLS-2$	
     }
     if (current == Scriptable.NOT_FOUND) {
       current = ScriptableObject.getProperty(scope, token);
       if (current == Scriptable.NOT_FOUND) current = Context.getCurrentContext().newObject(scope);
       else if (current instanceof NativeObject) {
         // we need to wrap this object from the prototype
         Scriptable wrapped = Context.getCurrentContext().newObject(scope);
         wrapped.setPrototype((Scriptable) current);
         current = wrapped;
       } else
         throw new IllegalStateException(
             "Resolve error: "
                 + name
                 + "-"
                 + token
                 + " already exists for "
                 + this.toString()); // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
       scope.put(token, scope, current);
     }
     scope = (Scriptable) current;
   }
 }
 /**
  * Validate that a regex is correct
  *
  * @param regex the regex to validate
  * @return true if the regex is valid
  */
 public static boolean regexIsValid(final String regex) {
   final Context context = Context.enter();
   try {
     final Scriptable scope = context.newObject(sharedScope);
     scope.setPrototype(sharedScope);
     scope.setParentScope(null);
     return (Boolean) regexIsValid.call(context, scope, scope, new Object[] {regex});
   } finally {
     Context.exit();
   }
 }
Beispiel #4
0
 public Scriptable initialState(Map<String, Object> zero) {
   Context context = ContextFactory.getGlobal().enterContext();
   // Always interpret, since otherwise we have difficulty serialising.
   context.setOptimizationLevel(-1);
   // this arrangement means _global isn't mutable, and
   // variables go in _topLevel.
   ScriptableObject global = context.initStandardObjects(null, true);
   global.sealObject();
   _global = global;
   _topLevel = context.newObject(_global);
   _topLevel.setPrototype(_global);
   _topLevel.setParentScope(null);
   // TODO complain if it's not there
   String script = zero.get("script").toString();
   context.evaluateString(_topLevel, script, "<script>", 1, null);
   context.exit();
   return _topLevel;
 }
Beispiel #5
0
  public JsRunner get() {
    /*
     * Create a new thread/request local javascript scope for the JsRules,
     * based on the preinitialized global one (which contains our js rules).
     */
    // Avoid a write lock if we can
    if (!rulesCurator.getUpdated().equals(this.updated)) {
      compileRules(this.rulesCurator);
    }
    Scriptable rulesScope;
    scriptLock.readLock().lock();
    try {
      Context context = Context.enter();
      rulesScope = context.newObject(scope);
      rulesScope.setPrototype(scope);
      rulesScope.setParentScope(null);
      Context.exit();
    } finally {
      scriptLock.readLock().unlock();
    }

    return new JsRunner(rulesScope);
  }