Exemple #1
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));
}
Exemple #2
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);

	}
}
Exemple #3
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);
	}
}
Exemple #4
0
void sj_invoke_cb2(struct v7 *v7, v7_val_t cb, 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_create_array(v7);
  v7_own(v7, &args);
  v7_array_push(v7, args, arg1);
  v7_array_push(v7, args, arg2);
  sj_invoke_cb(v7, cb, v7_get_global(v7), args);
  v7_disown(v7, &args);
  v7_disown(v7, &arg2);
  v7_disown(v7, &arg1);
  v7_disown(v7, &cb);
}
Exemple #5
0
V7_PRIVATE val_t rx_exec(struct v7 *v7, val_t rx, val_t str, int lind) {
  if (v7_is_regexp(rx)) {
    val_t s = to_string(v7, str);
    size_t len;
    struct slre_loot sub;
    struct slre_cap *ptok = sub.caps;
    char *const str = (char *) v7_to_string(v7, &s, &len);
    const char *const end = str + len;
    const char *begin = str;
    struct v7_regexp *rp = v7_to_regexp(rx);
    int flag_g = slre_get_flags(rp->compiled_regexp) & SLRE_FLAG_G;
    if (rp->lastIndex < 0) rp->lastIndex = 0;
    if (flag_g || lind) begin = utfnshift(str, rp->lastIndex);

    if (!slre_exec(rp->compiled_regexp, 0, begin, end, &sub)) {
      int i;
      val_t arr = v7_create_array(v7);

      for (i = 0; i < sub.num_captures; i++, ptok++)
        v7_array_push(v7, arr, v7_create_string(v7, ptok->start,
                                                ptok->end - ptok->start, 1));
      if (flag_g) rp->lastIndex = utfnlen(str, sub.caps->end - str);
      v7_set_property(v7, arr, "index", 5, V7_PROPERTY_READ_ONLY,
                      v7_create_number(utfnlen(str, sub.caps->start - str)));
      return arr;
    } else
      rp->lastIndex = 0;
  }
  return v7_create_null();
}
Exemple #6
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);
}
Exemple #7
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);
	}
}
Exemple #8
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);
	}
}
Exemple #9
0
static void gpio_intr_handler_proxy(int pin, int level) {
  char prop_name[15];
  int len;
  v7_val_t args;

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

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

  if (!v7_is_function(cb)) {
    return;
  }

  args = v7_create_array(s_v7);
  v7_array_push(s_v7, args, v7_create_number(pin));
  v7_array_push(s_v7, args, v7_create_number(level));

  v7_apply(s_v7, cb, v7_create_undefined(), args);
}
Exemple #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);
}
Exemple #11
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");
  }
}
Exemple #12
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);
	}
}
Exemple #13
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);
	}
}
Exemple #14
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);
}
Exemple #15
0
void sj_wifi_scan_done(const char **ssids) {
  struct v7 *v7 = s_v7;
  v7_val_t cb = v7_get(v7, s_wifi, "_scb", ~0);
  v7_val_t res = v7_create_undefined();
  const char **p;
  if (!v7_is_function(cb)) return;

  v7_own(v7, &res);
  if (ssids != NULL) {
    res = v7_create_array(v7);
    for (p = ssids; *p != NULL; p++) {
      v7_array_push(v7, res, v7_create_string(v7, *p, strlen(*p), 1));
    }
  } else {
    res = v7_create_undefined();
  }

  sj_invoke_cb1(v7, cb, res);

  v7_disown(v7, &res);
  v7_set(v7, s_wifi, "_scb", ~0, V7_PROPERTY_DONT_ENUM | V7_PROPERTY_HIDDEN,
         v7_create_undefined());
}