Пример #1
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);
  }
Пример #2
0
  static {
    _functions.set("toFixed", toFixed);

    _functions.set(
        "to_f",
        new JSFunctionCalls0() {
          public Object call(Scope s, Object foo[]) {
            return ((Number) s.getThis()).doubleValue();
          }
        });

    _functions.set(
        "round",
        new JSFunctionCalls0() {
          public Object call(Scope s, Object foo[]) {
            return (int) (((Number) s.getThis()).doubleValue() + .5);
          }
        });

    _functions.set(
        "times",
        new JSFunctionCalls1() {
          public Object call(Scope s, Object func, Object foo[]) {
            final int t = ((Number) s.getThis()).intValue();
            final JSFunction f = (JSFunction) func;

            return _loop(s, f, 0, t);
          }
        });

    _functions.set(
        "upto",
        new JSFunctionCalls2() {
          public Object call(Scope s, Object num, Object func, Object foo[]) {
            final int start = ((Number) s.getThis()).intValue();
            final int end = ((Number) num).intValue() + 1;
            final JSFunction f = (JSFunction) func;

            return _loop(s, f, start, end);
          }
        });

    _functions.set(
        "chr",
        new JSFunctionCalls0() {
          public Object call(Scope s, Object foo[]) {
            return (char) (((Number) s.getThis()).intValue());
          }
        });

    _functions.set(
        "zero_q_",
        new JSFunctionCalls0() {
          public Object call(Scope s, Object foo[]) {
            return ((Number) s.getThis()).doubleValue() == 0;
          }
        });
    _functions.set(
        "valueOf",
        new JSFunctionCalls0() {
          public Object call(Scope s, Object foo[]) {
            Object o = s.getThis();
            if (o instanceof JSObjectBase && ((JSObjectBase) o).getConstructor() instanceof Cons)
              return ((Cons) ((JSObjectBase) o).getConstructor())._parse(null, null);

            if (o instanceof Number) return o;

            if (!(o instanceof JSNumber))
              throw new JSException("TypeError: valueOf must be called on a Number");

            return ((JSNumber) o)._val;
          }
        });
    _functions.set(
        "toString",
        new JSFunctionCalls0() {
          public Object call(Scope s, Object foo[]) {
            return new JSString(s.getThis().toString());
          }
        });

    _functions.set("kilobytes", new Conversion(1024));
    _functions.set("megabytes", new Conversion(1024 * 1024));

    _functions.set("seconds", new Conversion(1000));
    _functions.set("minutes", new Conversion(1000 * 60));
    _functions.set("hours", new Conversion(1000 * 60 * 60));
    _functions.set("days", new Conversion(1000 * 60 * 60 * 24));
    _functions.set("weeks", new Conversion(1000 * 60 * 60 * 24 * 7));
    _functions.set("years", new Conversion(1000 * 60 * 60 * 24 * 365.25));
  }
Пример #3
0
 public JSFunction getFunction(String name) {
   return JSObjectBase.getFunction(this, name);
 }
Пример #4
0
  private void _setupScope() {
    if (_inScopeSetup) return;

    final Scope saveTLPref = _scope.getTLPreferred();
    _scope.setTLPreferred(null);

    final Scope saveTL = Scope.getThreadLocal();
    _scope.makeThreadLocal();

    _inScopeSetup = true;

    try {
      Object fo = getConfigObject("framework");

      if (fo != null) {

        Framework f = null;

        if (fo instanceof JSString) {
          f = Framework.byName(fo.toString(), null); // we allow people to just specify name
          _logger.info("Setting framework by name [" + fo.toString() + "]");
        } else if (fo instanceof JSObjectBase) {

          JSObjectBase obj = (JSObjectBase) fo;

          if (obj.containsKey("name")) {

            String s = obj.getAsString("version");

            f = Framework.byName(obj.getAsString("name"), s);
            _logger.info("Setting framework by name [" + obj.getAsString("name") + "]");
          } else if (obj.containsKey("custom")) {

            Object o = obj.get("custom");

            if (o instanceof JSObjectBase) {
              f = Framework.byCustom((JSObjectBase) o);
              _logger.info("Setting framework by custom [" + o + "]");
            } else {
              throw new RuntimeException("Error - custom framework wasn't an object [" + o + "]");
            }
          } else if (obj.containsKey("classname")) {
            f = Framework.byClass(obj.getAsString("classname"));
            _logger.info("Setting framework by class [" + obj.getAsString("classname") + "]");
          }
        }

        if (f == null) {
          throw new RuntimeException("Error : can't find framework [" + fo + "]");
        }

        f.install(this);
      }

      _runInitFiles(INIT_FILES);

      if (_adminContext != null) {
        _adminContext._scope.set("siteScope", _scope);
        _adminContext._setLocalObject(_localObject);
      }

      _lastScopeInitTime = _getScopeTime();
    } catch (RuntimeException re) {
      _scopeInited = false;
      throw re;
    } catch (Exception e) {
      _scopeInited = false;
      throw new RuntimeException(e);
    } finally {
      _inScopeSetup = false;
      _scope.setTLPreferred(saveTLPref);

      if (saveTL != null) saveTL.makeThreadLocal();

      this.approxSize(_rootContextReachable);
    }
  }