Ejemplo n.º 1
0
Archivo: regex.c Proyecto: CoryXie/v7
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();
}
Ejemplo n.º 2
0
static void ws_ev_handler(struct ns_connection *nc, int ev, void *ev_data) {
  struct websocket_message *wm = (struct websocket_message *) ev_data;
  struct user_data *ud = (struct user_data *) nc->user_data;
  struct v7 *v7 = ud->v7;

  switch (ev) {
    case NS_CONNECT:
      if (*(int *) ev_data == 0) {
        char *proto = NULL;
        if (ud->proto != NULL) {
          int tmp = asprintf(&proto, "Sec-WebSocket-Protocol: %s\n", ud->proto);
          (void) tmp; /* Shutup compiler */
        }
        ns_send_websocket_handshake(nc, "/", proto);
        if (proto != NULL) {
          free(proto);
        }
      } else {
        invoke_cb(ud, "onerror", v7_create_null());
      }
      break;
    case NS_WEBSOCKET_HANDSHAKE_DONE:
      v7_set(v7, ud->ws, "_nc", ~0, V7_PROPERTY_HIDDEN, v7_create_foreign(nc));
      invoke_cb(ud, "onopen", v7_create_null());
      break;
    case NS_WEBSOCKET_FRAME: {
      v7_val_t ev, data;
      ev = v7_create_object(v7);
      v7_own(v7, &ev);
      data = v7_create_string(v7, (char *) wm->data, wm->size, 1);
      v7_set(v7, ev, "data", ~0, 0, data);
      invoke_cb(ud, "onmessage", ev);
      v7_disown(v7, &ev);
      break;
    }
    case NS_CLOSE:
      invoke_cb(ud, "onclose", v7_create_null());
      nc->user_data = NULL;
      v7_set(v7, ud->ws, "_nc", ~0, V7_PROPERTY_HIDDEN, v7_create_undefined());
      v7_disown(v7, &ud->ws);
      free(ud);
      break;
  }
}
Ejemplo n.º 3
0
static v7_val_t DHT11_read(struct v7 *v7) {
  int pin, temp, rh;
  v7_val_t pinv = v7_arg(v7, 0), result;

  if (!v7_is_number(pinv)) {
    printf("non-numeric pin\n");
    return v7_create_undefined();
  }
  pin = v7_to_number(pinv);

  if (!dht11_read(pin, &temp, &rh)) {
    return v7_create_null();
  }

  result = v7_create_object(v7);
  v7_own(v7, &result);
  v7_set(v7, result, "temp", 4, 0, v7_create_number(temp));
  v7_set(v7, result, "rh", 2, 0, v7_create_number(rh));
  v7_disown(v7, &result);
  return result;
}
Ejemplo n.º 4
0
ICACHE_FLASH_ATTR
static v7_val_t DHT11_read(struct v7 *v7, v7_val_t this_obj, v7_val_t args) {
  int pin, temp, rh;
  v7_val_t pinv = v7_array_get(v7, args, 0), result;

  if (!v7_is_double(pinv)) {
    printf("non-numeric pin\n");
    return v7_create_undefined();
  }
  pin = v7_to_double(pinv);

  if (!dht11_read(pin, &temp, &rh)) {
    return v7_create_null();
  }

  result = v7_create_object(v7);
  v7_set(v7, result, "temp", 4, 0, v7_create_number(temp));
  v7_set(v7, result, "rh", 2, 0, v7_create_number(rh));
  /* prevent the object from being potentially GCed */
  v7_set(v7, args, "_tmp", 4, 0, result);
  return result;
}
Ejemplo n.º 5
0
/*
 * Prints message to current debug output
 */
ICACHE_FLASH_ATTR v7_val_t
Debug_print(struct v7 *v7, v7_val_t this_obj, v7_val_t args) {
  char *p, buf[512];
  int i, num_args = v7_array_length(v7, args);

  (void) this_obj;
  for (i = 0; i < num_args; i++) {
    v7_val_t arg = v7_array_get(v7, args, i);
    if (v7_is_string(arg)) {
      size_t n;
      const char *s = v7_to_string(v7, &arg, &n);
      os_printf("%s", s);
    } else {
      p = v7_to_json(v7, arg, buf, sizeof(buf));
      os_printf("%s", p);
      if (p != buf) {
        free(p);
      }
    }
  }
  os_printf("\n");

  return v7_create_null();
}
Ejemplo n.º 6
0
static enum v7_err DHT11_read(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  int pin, temp, rh;
  v7_val_t pinv = v7_arg(v7, 0);

  if (!v7_is_number(pinv)) {
    printf("non-numeric pin\n");
    *res = v7_create_undefined();
    goto clean;
  }
  pin = v7_to_number(pinv);

  if (!dht11_read(pin, &temp, &rh)) {
    *res = v7_create_null();
    goto clean;
  }

  *res = v7_create_object(v7);
  v7_set(v7, *res, "temp", 4, 0, v7_create_number(temp));
  v7_set(v7, *res, "rh", 2, 0, v7_create_number(rh));

clean:
  return rcode;
}
Ejemplo n.º 7
0
Archivo: regex.c Proyecto: CoryXie/v7
static val_t Regex_exec(struct v7 *v7, val_t this_obj, val_t args) {
  if (v7_array_length(v7, args) > 0) {
    return rx_exec(v7, this_obj, v7_array_get(v7, args, 0), 0);
  }
  return v7_create_null();
}