Package org.mozilla.javascript

Examples of org.mozilla.javascript.BaseFunction


    }
  public static Function createFunction(final String source, String name) {
    Context context = PersevereContextFactory.getContext();
    try {
      BaseFunction func = (BaseFunction) context.evaluateString(GlobalData.getGlobalScope(), "(" + source + ")", name, 1, null);
      // store the source so we can retain comments et al
      func.put("source", func, source);
      ScriptRuntime.setObjectProtoAndParent(func,GlobalData.getGlobalScope());
      return func;
    }
    catch (Exception e) {
      System.err.println(e.getMessage());
View Full Code Here


        if (!UserSecurity.hasPermission(SystemPermission.javaScriptCoding))
          throw new SecurityException("User is not permitted to execute code on the server");
        return cx.evaluateString(GlobalData.getGlobalScope(), (String) args[0], "console", 0, null);
      }     
    });
    BaseFunction putHandler = new PersevereNativeFunction() {
      @Override
      public Object call(final Context cx, final Scriptable scope,
          final Scriptable thisObj, Object[] args) {
        // check to make sure all the objects are valid after a schema change
        // TODO: The performance of this could be greatly improved with more information from the put
        PersistableClass schema = (PersistableClass) thisObj;
        if (schema != null) {
          Object propertiesDefinitions = schema.get("properties");
          if (propertiesDefinitions instanceof Persistable && schema.getPrototypeProperty() instanceof Persistable) {
            Persistable prototype = (Persistable) schema.getPrototypeProperty();
              for (Object key : prototype.getIds())
                if (key instanceof String)
                  enforceSchemaForProperty(schema, prototype, (String) key, prototype.get((String) key), true, true, false);
              List<Persistable> instances = (List) schema.get("instances");
              Object[] propertyIds = ((Persistable)propertiesDefinitions).getIds();
              List<String> stringList = new ArrayList();
              for (Object property : propertyIds){
                if (property instanceof String)
                  stringList.add((String) property);
              }
              String[] stringKeys = new String[stringList.size()];
              stringList.toArray(stringKeys);
              for (String key : stringKeys) {
                Object propertyDef = ((Persistable)propertiesDefinitions).get(key);
                //TODO: Allow it to point to another a schema's properties object
                if (propertyDef instanceof Persistable && !(propertyDef instanceof PersistableClass)) {
                  if (((Persistable)propertyDef).get("properties") instanceof Persistable)
                    throw new RuntimeException("Can not create an object validator that does not reference an existing schema");
                }
              }
              if(Boolean.TRUE.equals(schema.get("checkAllInstancesOnSchemaChange", schema))) {
                for (Persistable instance : instances){
                  for (String key : stringKeys) {
                    enforceSchemaForProperty(schema, instance, key, ScriptableObject.getProperty(instance, key), true, true, false);
                  }
                }
              }
          }
        }
        return true;
        }
      public String toString() {
        return "function(resource){/*native code*/}";
      }
    };
    putHandler.put("source", putHandler, "function(){[Native code]}");
    pjsLibrary.put("putHandler", pjsLibrary, putHandler);
    putHandler.setPrototype(ScriptableObject.getFunctionPrototype(GlobalData.getGlobalScope()));
  }
View Full Code Here

          }
          final Function newFunction = ((Query) key).conditionFunction;
          final Function oldFunction = ((Query) oldQuery).conditionFunction;
          if (oldFunction != null) {
            // create a new function that combines the other two
            ((Query) oldQuery).conditionFunction = new BaseFunction() {
              public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                return ScriptRuntime.toBoolean(newFunction.call(cx,  scope, thisObj, args)) ||
                  ScriptRuntime.toBoolean(oldFunction.call(cx, scope, thisObj, args));
              }
            };
View Full Code Here

  /**
   * ECMA 11.4.3 says that typeof on host object is Implementation-dependent
   */
  public void test0() throws Exception
  {
        final Function f = new BaseFunction()
        {
          @Override
          public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj,
              Object[] _args)
          {
View Full Code Here

  /**
   * ECMA 11.4.3 says that typeof on host object is Implementation-dependent
   */
  public void test0() throws Exception
  {
        final Function f = new BaseFunction()
        {
          @Override
          public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj,
              Object[] _args)
          {
View Full Code Here

    static final String CLASSNAME = "ScriptableList";

    // Set up a custom constructor, for this class is somewhere between a host class and
    // a native wrapper, for which no standard constructor class exists
    public static void init(Scriptable scope) throws NoSuchMethodException {
        BaseFunction ctor = new BaseFunction(scope, ScriptableObject.getFunctionPrototype(scope)) {
            @Override
            public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
                if (args.length > 1) {
                    throw new EvaluatorException("ScriptableList() requires a java.util.List argument");
                }
View Full Code Here

    final static String CLASSNAME = "ScriptableMap";

    // Set up a custom constructor, for this class is somewhere between a host class and
    // a native wrapper, for which no standard constructor class exists
    public static void init(Scriptable scope) throws NoSuchMethodException {
        BaseFunction ctor = new BaseFunction(scope, ScriptableObject.getFunctionPrototype(scope)) {
            @Override
            public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
                boolean reflect = false;
                if (args.length > 2) {
                    throw new EvaluatorException("ScriptableMap() called with too many arguments");
View Full Code Here

    private void initJsProcessor() {
        this.cx = org.mozilla.javascript.Context.enter();
        this.scope = new ImporterTopLevel(cx);

        if (jsFunctions != null) {
            scope.defineProperty("count", new BaseFunction() {
                @Override
                public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                    if (args.length == 0 || !(args[0] instanceof String)) {
                        throw new IllegalArgumentException("Should take string argument");
                    }
                    return jsFunctions.count((String)args[0]);
                }
            }, ScriptableObject.DONTENUM);

            scope.defineProperty("find", new BaseFunction() {
                @Override
                public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                    if (args.length == 0 || !(args[0] instanceof String)) {
                        throw new IllegalArgumentException("Should take string argument");
                    }
View Full Code Here

  /**
   * ECMA 11.4.3 says that typeof on host object is Implementation-dependent
   */
  public void test0() throws Exception
  {
        final Function f = new BaseFunction()
        {
          @Override
          public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj,
              Object[] _args)
          {
View Full Code Here

TOP

Related Classes of org.mozilla.javascript.BaseFunction

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.