Exemplo n.º 1
0
void Bot::onJoin(bot_exchange_format& f) {
	
	long l = _onJoin.GetLength();
	if (l == 0) return;

	v7_val_t user = v7_mk_object(v7);

	v7_val_t userName = v7_mk_string(v7, std::string(f[1]).c_str(), ~0, 1);
	v7_set(v7, user, "name", ~0, userName);

	v7_val_t flags = v7_mk_number((u_long)f[2]);
	v7_set(v7, user, "flags", ~0, flags);

	v7_val_t age = v7_mk_number((u_char)f[3]);
	v7_set(v7, user, "age", ~0, age);

	v7_val_t count = v7_mk_number((int)f[4]);
	v7_set(v7, user, "count", ~0, count);
	
	v7_val_t gifts = v7_mk_number((u_long)f[5]);
	v7_set(v7, user, "gifts", ~0, gifts);

	long i;
	v7_val_t* func;
	v7_val_t args;
	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onJoin.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, user);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Exemplo n.º 2
0
void sj_invoke_cb0_this(struct v7 *v7, v7_val_t cb, v7_val_t this_obj) {
  v7_val_t args;
  v7_own(v7, &cb);
  args = v7_mk_array(v7);
  v7_own(v7, &args);
  sj_invoke_cb(v7, cb, this_obj, args);
  v7_disown(v7, &args);
  v7_disown(v7, &cb);
}
Exemplo n.º 3
0
void Bot::onText(bot_exchange_format& f) {
	long i, l;
	l = _onTexts.GetLength();
	std::string nick = std::string(f[1]);
	std::string text = std::string(f[2]);

	v7_val_t attr;

	if ((u_char)f[3] == 0) {
		attr = v7_mk_undefined();
	}
	else {
		attr = v7_mk_object(v7);

		v7_val_t size = v7_mk_number((u_char)f[4]);
		v7_set(v7, attr, "size", ~0, size);

		COLORREF col = (u_long)f[5];
		int r = GetRValue(col);
		int g = GetGValue(col);
		int b = GetBValue(col);
		char strCol[8];
		sprintf(strCol, "#%02X%02X%02X", r, g, b);
		v7_val_t color = v7_mk_string(v7, strCol, ~0, 1);
		v7_set(v7, attr, "color", ~0, color);

		v7_val_t effects = v7_mk_number((u_long)f[6]);
		v7_set(v7, attr, "effects", ~0, effects);

		v7_val_t charset = v7_mk_number((u_char)f[7]);
		v7_set(v7, attr, "charset", ~0, charset);

		v7_val_t pitch = v7_mk_number((u_char)f[8]);
		v7_set(v7, attr, "pitch", ~0, pitch);

		std::string font = std::string(f[9]);
		v7_val_t fontName = v7_mk_string(v7, font.c_str(), ~0, 1);
		v7_set(v7, attr, "font", ~0, fontName);
	}

	v7_val_t nickName = v7_mk_string(v7, nick.c_str(), ~0, 0);
	v7_val_t fullText = v7_mk_string(v7, text.c_str(), ~0, 0);
	for (i = 0; i < l; i++) {
		v7_val_t* func = (v7_val_t*)_onTexts.GetAt(i);
		v7_val_t args;
		args = v7_mk_array(v7);
		v7_array_push(v7, args, nickName);
		v7_array_push(v7, args, fullText);
		v7_array_push(v7, args, attr);
		v7_apply(v7, *func, *v7Obj, args, NULL);

	}
}
Exemplo n.º 4
0
static void call_sum(struct v7 *v7) {
  v7_val_t func, result, args;

  func = v7_get(v7, v7_get_global(v7), "sum", 3);

  args = v7_mk_array(v7);
  v7_array_push(v7, args, v7_mk_number(123.0));
  v7_array_push(v7, args, v7_mk_number(456.789));

  v7_apply(v7, func, v7_mk_undefined(), args, &result);
  printf("Result: %g\n", v7_to_number(result));
}
Exemplo n.º 5
0
void Bot::onMotd(TString& motd) {
	v7_val_t theMotd = v7_mk_string(v7, motd.GetAsChar(), ~0, 1);
	v7_val_t* func;
	v7_val_t args;

	long i;
	long l = _onMotd.GetLength();

	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onMotd.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, theMotd);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Exemplo n.º 6
0
void Bot::onLeft(TString& nick) {
	long l = _onLeft.GetLength();
	if (l == 0) return;

	long i;
	v7_val_t* func;
	v7_val_t args;
	v7_val_t userName = v7_mk_string(v7, nick.GetAsChar(), ~0, 1);
	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onLeft.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, userName);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Exemplo n.º 7
0
void sj_invoke_cb2_this(struct v7 *v7, v7_val_t cb, v7_val_t this_obj,
                        v7_val_t arg1, v7_val_t arg2) {
  v7_val_t args;
  v7_own(v7, &cb);
  v7_own(v7, &arg1);
  v7_own(v7, &arg2);

  args = v7_mk_array(v7);
  v7_own(v7, &args);
  v7_array_push(v7, args, arg1);
  v7_array_push(v7, args, arg2);
  sj_invoke_cb(v7, cb, this_obj, args);
  v7_disown(v7, &args);
  v7_disown(v7, &arg2);
  v7_disown(v7, &arg1);
  v7_disown(v7, &cb);
}
Exemplo n.º 8
0
void Bot::onIm(TString& nick, TString& text) {
	v7_val_t theNick = v7_mk_string(v7, nick.GetAsChar(), ~0, 1);
	v7_val_t theText = v7_mk_string(v7, text.GetAsChar(), ~0, 1);
	v7_val_t* func;
	v7_val_t args;

	long i;
	long l = _onMotd.GetLength();

	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onIm.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, theNick);
		v7_array_push(v7, args, theText);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Exemplo n.º 9
0
void Bot::onTalk(TString& nick, unsigned char flag) {
	long l = _onTalk.GetLength();
	if (l == 0) return;

	long i;
	v7_val_t* func;
	v7_val_t args;
	v7_val_t userName = v7_mk_string(v7, nick.GetAsChar(), ~0, 1);
	v7_val_t theFlag = v7_mk_number(flag);

	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onTalk.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, userName);
		v7_array_push(v7, args, theFlag);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Exemplo n.º 10
0
static void gpio_intr_handler_proxy(int pin, enum gpio_level level, void *arg) {
  struct v7 *v7 = (struct v7 *) arg;
  char prop_name[15];
  int len;

  len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin);

  v7_val_t cb = v7_get(v7, v7_get_global(v7), prop_name, len);

  if (!v7_is_callable(v7, cb)) {
    return;
  }

  /* Forwarding call to common cbs queue */
  v7_val_t args = v7_mk_array(v7);
  v7_array_push(v7, args, v7_mk_number(v7, pin));
  v7_array_push(v7, args, v7_mk_number(v7, level));
  sj_invoke_cb2(v7, s_isr_cb_proxy_v, cb, args);
}
Exemplo n.º 11
0
void sj_wifi_scan_done(const char **ssids, void *arg) {
  struct wifi_cb_arg *cba = (struct wifi_cb_arg *) arg;
  struct v7 *v7 = cba->v7;
  v7_val_t res = v7_mk_undefined();
  const char **p;

  v7_own(v7, &res);
  if (ssids != NULL) {
    res = v7_mk_array(v7);
    for (p = ssids; *p != NULL; p++) {
      v7_array_push(v7, res, v7_mk_string(v7, *p, strlen(*p), 1));
    }
  }

  /* Free the struct in case callback launches a new scan. */
  cba->v7 = NULL;
  v7_disown(v7, &cba->v);

  sj_invoke_cb1(v7, cba->v, res);
  v7_disown(v7, &res);
}
Exemplo n.º 12
0
static void gpio_intr_handler_proxy(int pin, int level) {
  char prop_name[15];
  int len;
  v7_val_t res, args;

  len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin);

  v7_val_t cb = v7_get(s_v7, v7_get_global(s_v7), prop_name, len);

  if (!v7_is_callable(s_v7, cb)) {
    return;
  }

  args = v7_mk_array(s_v7);
  v7_array_push(s_v7, args, v7_mk_number(pin));
  v7_array_push(s_v7, args, v7_mk_number(level));

  if (v7_apply(s_v7, cb, v7_mk_undefined(), args, &res) != V7_OK) {
    /* TODO(mkm): make it print stack trace */
    fprintf(stderr, "cb threw an exception\n");
  }
}
Exemplo n.º 13
0
/* Callback for json_walk() */
static void frozen_cb(void *data, const char *name, size_t name_len,
                      const char *path, const struct json_token *token) {
  struct json_parse_ctx *ctx = (struct json_parse_ctx *) data;
  v7_val_t v = V7_UNDEFINED;

  (void) path;

  v7_own(ctx->v7, &v);

  switch (token->type) {
    case JSON_TYPE_STRING:
      v = v7_mk_string(ctx->v7, token->ptr, token->len, 1 /* copy */);
      break;
    case JSON_TYPE_NUMBER:
      v = v7_mk_number(ctx->v7, cs_strtod(token->ptr, NULL));
      break;
    case JSON_TYPE_TRUE:
      v = v7_mk_boolean(ctx->v7, 1);
      break;
    case JSON_TYPE_FALSE:
      v = v7_mk_boolean(ctx->v7, 0);
      break;
    case JSON_TYPE_NULL:
      v = V7_NULL;
      break;
    case JSON_TYPE_OBJECT_START:
      v = v7_mk_object(ctx->v7);
      break;
    case JSON_TYPE_ARRAY_START:
      v = v7_mk_array(ctx->v7);
      break;

    case JSON_TYPE_OBJECT_END:
    case JSON_TYPE_ARRAY_END: {
      /* Object or array has finished: deallocate its frame */
      ctx->frame = free_json_frame(ctx, ctx->frame);
    } break;

    default:
      LOG(LL_ERROR, ("Wrong token type %d\n", token->type));
      break;
  }

  if (!v7_is_undefined(v)) {
    if (name != NULL && name_len != 0) {
      /* Need to define a property on the current object/array */
      if (v7_is_object(ctx->frame->val)) {
        v7_set(ctx->v7, ctx->frame->val, name, name_len, v);
      } else if (v7_is_array(ctx->v7, ctx->frame->val)) {
        /*
         * TODO(dfrank): consult name_len. Currently it's not a problem due to
         * the implementation details of frozen, but it might change
         */
        int idx = (int) cs_strtod(name, NULL);
        v7_array_set(ctx->v7, ctx->frame->val, idx, v);
      } else {
        LOG(LL_ERROR, ("Current value is neither object nor array\n"));
      }
    } else {
      /* This is a root value */
      assert(ctx->frame == NULL);

      /*
       * This value will also be the overall result of JSON parsing
       * (it's already owned by the `v7_alt_json_parse()`)
       */
      ctx->result = v;
    }

    if (token->type == JSON_TYPE_OBJECT_START ||
        token->type == JSON_TYPE_ARRAY_START) {
      /* New object or array has just started, so we need to allocate a frame
       * for it */
      struct json_parse_frame *new_frame = alloc_json_frame(ctx, v);
      new_frame->up = ctx->frame;
      ctx->frame = new_frame;
    }
  }

  v7_disown(ctx->v7, &v);
}