示例#1
0
  private void _loadConfig() {
    try {

      _configScope.set("__instance__", this);

      _loadConfigFromCloudObject(getSiteObject());
      _loadConfigFromCloudObject(getEnvironmentObject());

      File f;
      if (!_admin) {
        f = getFileSafe("_config.js");
        if (f == null || !f.exists()) f = getFileSafe("_config");
      } else
        f =
            new File(
                Module.getModule("core-modules/admin").getRootFile(getVersionForLibrary("admin")),
                "_config.js");

      _libraryLogger.info("config file [" + f + "] exists:" + f.exists());

      if (f == null || !f.exists()) return;

      Convert c = new Convert(f);
      JSFunction func = c.get();
      func.setUsePassedInScope(true);
      func.call(_configScope);

      _logger.debug("config things " + _configScope.keySet());
    } catch (Exception e) {
      throw new RuntimeException("couldn't load config", e);
    }
  }
  public boolean isTestClass(@NotNull JSClass clazz, boolean allowSuite) {
    if (clazz instanceof XmlBackedJSClassImpl) return false;
    if (clazz.getAttributeList() == null) return false;
    if (clazz.getAttributeList().getAccessType() != JSAttributeList.AccessType.PUBLIC) return false;

    if (allowSuite && isSuite(clazz)) return true;

    final boolean flexUnit1Subclass = isFlexUnit1Subclass(clazz);
    if (!flexUnit1Subclass && !flexUnit4Present) return false;

    if (getCustomRunner(clazz) == null) {
      for (JSFunction method : clazz.getFunctions()) {
        if (method.getKind() == JSFunction.FunctionKind.CONSTRUCTOR
            && ValidateTypesUtil.hasRequiredParameters(method)) return false;
      }
    }

    if (!flexUnit1Subclass && !isFlunitSubclass(clazz)) {
      boolean hasTests = false;
      for (JSFunction method : clazz.getFunctions()) {
        if (isTestMethod(method)) {
          hasTests = true;
          break;
        }
      }
      if (!hasTests) return false;
    }
    return true;
  }
示例#3
0
  public ExecutionContext createFunctionExecutionContext(
      Object functionReference, JSFunction function, Object thisArg, Object... arguments) {
    // 10.4.3
    Object thisBinding = null;
    if (function.isStrict()) {
      thisBinding = thisArg;
    } else {
      if (thisArg == null || thisArg == Types.NULL || thisArg == Types.UNDEFINED) {
        thisBinding = this.getLexicalEnvironment().getGlobalObject();
      } else if (!(thisArg instanceof JSObject)) {
        // thisBinding = Types.toObject(this, thisArg);
        thisBinding = Types.toThisObject(this, thisArg);
      } else {
        thisBinding = thisArg;
      }
    }

    LexicalEnvironment scope = function.getScope();
    LexicalEnvironment localEnv = LexicalEnvironment.newDeclarativeEnvironment(scope);

    ExecutionContext context =
        new ExecutionContext(this, localEnv, localEnv, thisBinding, function.isStrict());
    context.performDeclarationBindingInstantiation(function, arguments);
    context.fileName = function.getFileName();
    // System.err.println( "debug null: " + ( function.getDebugContext() == null ? function : "not
    // null") );
    context.debugContext = function.getDebugContext();
    context.functionReference = functionReference;
    return context;
  }
示例#4
0
  public Object construct(JSFunction function, Object... args) {
    if (!function.isConstructor()) {
      throw new ThrowException(this, createTypeError("not a constructor"));
    }

    // 13.2.2
    // 1. create the new object
    JSObject obj = function.createNewObject(this);
    // 2. set internal methods per 8.12 [DynObject]
    // 3. set class name [DynObject subclass (defaults true)]
    // 4. Set Extensible [DynObject subclass (defaults true)]
    // 5. Get the function's prototype
    // 6. If prototype is an object make that the new object's prototype
    // 7. If prototype is not an object set to the standard builtin object prototype 15.2.4
    // [AbstractJavascriptFunction]
    // [AbstractJavascriptFunction] handles #7, subclasses may handle #6 if necessary (see
    // BuiltinArray#createNewObject)
    Object p = function.get(this, "prototype");
    if (p != Types.UNDEFINED && p instanceof JSObject) {
      obj.setPrototype((JSObject) p);
    } else {
      JSObject defaultObjectProto = getPrototypeFor("Object");
      obj.setPrototype(defaultObjectProto);
    }

    // 8. Call the function with obj as self
    Object result = internalCall(null, function, obj, args);
    // 9. If result is a JSObject return it
    if (result instanceof JSObject) {
      return (JSObject) result;
    }
    // Otherwise return obj
    return obj;
  }
示例#5
0
  private void _runInitFiles(String[] files) throws IOException {

    if (files == null) return;

    for (JSFunction func : _initRefreshHooks) {
      func.call(_initScope, null);
    }

    for (int i = 0; i < files.length; i++) runInitFile(files[i].replaceAll("PREFIX", _codePrefix));
  }
示例#6
0
  private void _runInitFile(File f) throws IOException {
    if (f == null) return;

    if (!f.exists()) return;

    _initFlies.add(f);
    JxpSource s = getSource(f);
    JSFunction func = s.getFunction();
    func.setUsePassedInScope(true);
    func.call(_initScope);
  }
示例#7
0
  @Test
  public void testFunctionDeclaration() {
    eval("function foo() { return 42.0 };");
    Reference foo = getContext().resolve("foo");
    assertThat(foo).isNotNull();
    assertThat(foo.isUnresolvableReference()).isFalse();
    JSFunction fn = (JSFunction) foo.getValue(getContext());
    assertThat(fn).isNotNull();

    Object result = fn.call(getContext());
    assertThat(result).isEqualTo(42L);
  }
示例#8
0
  /** @unexpose */
  static Object _loop(final Scope s, final JSFunction f, final int start, final int end) {
    Object blah = s.getParent().getThis();
    s.setThis(blah);

    Boolean old = f.setUsePassedInScopeTL(true);

    for (int i = start; i < end; i++) f.call(s, i);

    f.setUsePassedInScopeTL(old);

    s.clearThisNormal(null);

    return null;
  }
示例#9
0
  public void onMessage(
      String channel, String sender, String login, String hostname, String message) {

    JSFunction onMessage = (JSFunction) _things.get("onMessage");
    if (onMessage == null) return;

    JSObjectBase o = new JSObjectBase();
    o.set("channel", channel);
    o.set("sender", sender);
    o.set("login", login);
    o.set("hostname", hostname);
    o.set("message", message);

    onMessage.call(onMessage.getScope(), o);
  }
 @Override
 public Object runFunction(ScriptRunner runner, IFunction func, Object... args)
     throws InternalScriptingException {
   Object ret = null;
   try {
     enterContext();
     setScriptRunner(runner);
     if (func instanceof JSFunction) {
       ((JSFunction) func)
           .getFunction()
           .call(mcJavascriptContext, mcJavascriptScope, mcJavascriptScope, args);
     } else {
       throw new InternalScriptingException("Invalid Function");
     }
     setScriptRunner(null);
   } catch (EcmaError e) {
     throw new InternalScriptingException(e.getMessage());
   } catch (EvaluatorException e) {
     throw new InternalScriptingException(e.getMessage());
   } catch (Error e) {
     throw new InternalScriptingException(e.getMessage());
   } finally {
     exitContext();
   }
   return ret;
 }
示例#11
0
  private void performDeclarationBindingInstantiation(JSFunction function, Object[] arguments) {
    // 10.5 (Functions)
    String[] names = function.getFormalParameters();

    Object v = null;

    DeclarativeEnvironmentRecord env =
        (DeclarativeEnvironmentRecord) this.variableEnvironment.getRecord();

    // * 4
    for (int i = 0; i < names.length; ++i) {
      if ((i + 1) > arguments.length) {
        v = Types.UNDEFINED;
      } else {
        v = arguments[i];
      }

      if (!env.hasBinding(this, names[i])) {
        env.createMutableBinding(this, names[i], false);
      }

      env.setMutableBinding(this, names[i], v, function.isStrict());
    }

    // * 5
    performFunctionDeclarationBindings(function, false);

    // * 6
    if (!env.hasBinding(this, "arguments")) {
      // * 7
      Arguments argsObj = createArgumentsObject(function, arguments);

      if (function.isStrict()) {
        env.createImmutableBinding("arguments");
        env.initializeImmutableBinding("arguments", argsObj);
      } else {
        env.createMutableBinding(this, "arguments", false);
        env.setMutableBinding(this, "arguments", argsObj, false);
      }
    }

    // * 8
    performVariableDeclarationBindings(function, false);
  }
  @Override
  public int execute(
      final String js,
      final String[] args,
      final int type,
      final int event,
      final char keyPressed) {

    if (event == ActionHandler.FOCUS_EVENT && (type == KEYSTROKE || type == FORMAT)) {
      // format added for file baseline_screens\forms\Testdokument PDF.pdf
      // F action found this AFTime_FormatEx("HH:MM") unknown command
      //        	if(type==FORMAT){
      //        		System.out.println("AFTime.execute focus format js="+js);
      //	        	org.jpedal.objects.acroforms.utils.ConvertToString.printStackTrace(7);

      //        	}
      final String validatedValue = validateMask(args, ":", false);

      if (validatedValue == null) {

        final Object[] errArgs = new Object[1];
        errArgs[0] = formObject.getObjectRefAsString();

        maskAlert(ErrorCodes.JSInvalidFormat, errArgs); // chris unformat
        execute(js, args, type, event, keyPressed);
      } else {
        // be sure to get the current value before we change it

        formObject.setLastValidValue(validatedValue);
        formObject.updateValue(validatedValue, false, true);
      }

    } else if (type == KEYSTROKE) { // just ignore and process on focus lost
      JSFunction.debug("AFTime(keystroke)=" + js);
    } else if (type == FORMAT) {
      JSFunction.debug("AFTime(format)=" + js);
    } else {
      JSFunction.debug("Unknown command " + js);
    }

    return 0;
  }
示例#13
0
  private void performFunctionDeclarationBindings(
      final JSCode code, final boolean configurableBindings) {
    // 10.5 Function Declaration Binding
    List<FunctionDeclaration> decls = code.getFunctionDeclarations();

    EnvironmentRecord env = this.variableEnvironment.getRecord();
    for (FunctionDeclaration each : decls) {
      String identifier = each.getIdentifier();
      if (!env.hasBinding(this, identifier)) {
        env.createMutableBinding(this, identifier, configurableBindings);
      } else if (env.isGlobal()) {
        JSObject globalObject = ((ObjectEnvironmentRecord) env).getBindingObject();
        PropertyDescriptor existingProp =
            (PropertyDescriptor) globalObject.getProperty(this, identifier);
        if (existingProp.isConfigurable()) {
          PropertyDescriptor newProp =
              new PropertyDescriptor() {
                {
                  set("Value", Types.UNDEFINED);
                  set("Writable", true);
                  set("Enumerable", true);
                  set("Configurable", configurableBindings);
                }
              };
          globalObject.defineOwnProperty(this, identifier, newProp, true);
        } else if (existingProp.isAccessorDescriptor()
            || (!existingProp.isWritable() && !existingProp.isEnumerable())) {
          throw new ThrowException(
              this, createTypeError("unable to bind function '" + identifier + "'"));
        }
      }
      JSFunction function =
          getCompiler()
              .compileFunction(
                  this, identifier, each.getFormalParameters(), each.getBlock(), each.isStrict());
      function.setDebugContext(identifier);
      env.setMutableBinding(this, identifier, function, code.isStrict());
    }
  }
  public boolean isPotentialTestMethod(JSFunction method) {
    if (method.getKind() == JSFunction.FunctionKind.CONSTRUCTOR) return false;

    PsiElement parent = method.getParent();
    if (parent instanceof JSClass
        && (isFlunitSubclass((JSClass) parent) || isFlexUnit1Subclass((JSClass) parent))) {
      if (method.getName() != null && method.getName().startsWith("test")) return true;
    }

    if (method.getAttributeList() != null
        && method.getAttributeList().getAttributesByName(TEST_ATTRIBUTE).length > 0
        && method.getAttributeList().getAttributesByName(IGNORE_ATTRIBUTE).length == 0) {
      return true;
    }
    return false;
  }
示例#15
0
 public Object internalCall(
     Object functionReference, JSFunction function, Object self, Object... args) {
   // 13.2.1
   try {
     ExecutionContext fnContext =
         createFunctionExecutionContext(functionReference, function, self, args);
     ThreadContextManager.pushContext(fnContext);
     try {
       Object value = function.call(fnContext);
       if (value == null) {
         return Types.NULL;
       }
       return value;
     } catch (ThrowException e) {
       throw e;
     } catch (Throwable e) {
       throw new ThrowException(fnContext, e);
     }
   } finally {
     ThreadContextManager.popContext();
   }
 }
示例#16
0
 public void appendScript(Appendable target) throws IOException {
   target.append("new ");
   super.appendScript(target);
 }
  public boolean isTestMethod(JSFunction method) {
    if (!(method.getParent() instanceof JSClass)
        || method.getParent() instanceof XmlBackedJSClassImpl) return false;

    JSClass clazz = (JSClass) method.getParent();

    // FlexUnit 1: flexunit.framework.TestCase.getTestMethodNames()
    // Flunit: net.digitalprimates.fluint.tests.defaultFilterFunction()
    // FlexUnit 4: org.flexunit.runners.BlockFlexUnit4ClassRunner
    if (method.getName() == null) return false;

    if (flexUnit4Present && getCustomRunner(clazz) != null) return true;

    if (method.getAttributeList() == null) return false;
    if (method.getAttributeList().getAccessType() != JSAttributeList.AccessType.PUBLIC)
      return false;
    if (method.getKind() != JSFunction.FunctionKind.SIMPLE) return false;
    if (method.getAttributeList().hasModifier(JSAttributeList.ModifierType.STATIC)) return false;
    if (ValidateTypesUtil.hasRequiredParameters(method)) return false;

    if (isFlexUnit1Subclass(clazz)) {
      if (!method.getName().startsWith("test")) return false;
    } else if (isFlunitSubclass(clazz)) {
      if (!method.getName().startsWith("test")
          && method.getAttributeList().getAttributesByName(TEST_ATTRIBUTE).length == 0)
        return false;
    } else {
      if (!flexUnit4Present) return false;

      final JSType returnType = method.getReturnType();
      if (returnType != null && !(returnType instanceof JSVoidType)) return false;

      if (method.getAttributeList().getAttributesByName(IGNORE_ATTRIBUTE).length > 0) return false;
      if (method.getAttributeList().getAttributesByName(TEST_ATTRIBUTE).length == 0) return false;
    }
    return true;
  }
示例#18
0
  private Arguments createArgumentsObject(final JSFunction function, final Object[] arguments) {
    // 10.6

    Arguments obj = new Arguments(getGlobalObject());
    PropertyDescriptor desc =
        new PropertyDescriptor() {
          {
            set("Value", arguments.length);
            set("Writable", true);
            set("Enumerable", false);
            set("Configurable", true);
          }
        };
    obj.defineOwnProperty(this, "length", desc, false);

    String[] names = function.getFormalParameters();

    JSObject map = new DynObject(getGlobalObject());
    List<String> mappedNames = new ArrayList<>();

    final LexicalEnvironment env = getVariableEnvironment();

    for (int i = 0; i < arguments.length; ++i) {
      final Object val = arguments[i];
      desc =
          new PropertyDescriptor() {
            {
              set("Value", val);
              set("Writable", true);
              set("Enumerable", true);
              set("Configurable", true);
            }
          };

      obj.defineOwnProperty(this, "" + i, desc, false);

      if (i < names.length) {
        if (!function.isStrict()) {
          final String name = names[i];
          if (i < names.length) {
            if (!mappedNames.contains(name)) {
              mappedNames.add(name);

              desc =
                  new PropertyDescriptor() {
                    {
                      set("Set", new ArgSetter(env, name));
                      set("Get", new ArgGetter(env, name));
                      set("Configurable", true);
                    }
                  };
              map.defineOwnProperty(this, "" + i, desc, false);
            }
          }
        }
      }
    }

    if (!mappedNames.isEmpty()) {
      obj.setParameterMap(map);
    }

    if (function.isStrict()) {
      final JSFunction thrower = (JSFunction) getGlobalObject().get(this, "__throwTypeError");

      obj.defineOwnProperty(
          this,
          "caller",
          new PropertyDescriptor() {
            {
              set("Get", thrower);
              set("Set", thrower);
              set("Enumerable", false);
              set("Configurable", false);
            }
          },
          false);

      obj.defineOwnProperty(
          this,
          "callee",
          new PropertyDescriptor() {
            {
              set("Get", thrower);
              set("Set", thrower);
              set("Enumerable", false);
              set("Configurable", false);
            }
          },
          false);

    } else {
      obj.defineOwnProperty(
          this,
          "callee",
          new PropertyDescriptor() {
            {
              set("Value", function);
              set("Writable", true);
              set("Enumerable", false);
              set("Configurable", true);
            }
          },
          false);
    }

    return obj;
  }
示例#19
0
  protected void onDisconnect() {
    JSFunction onDisconnect = (JSFunction) _things.get("onDisconnect");
    if (onDisconnect == null) return;

    onDisconnect.call(onDisconnect.getScope());
  }