Exemplo n.º 1
0
void esp_sj_uart_init(struct v7 *v7) {
  sj_us[0].v7 = sj_us[1].v7 = v7;
  os_timer_setfn(&sj_us[0].timer, esp_sj_uart_dispatcher, (void *) 0);
  os_timer_setfn(&sj_us[1].timer, esp_sj_uart_dispatcher, (void *) 1);
  s_uart_proto = v7_mk_object(v7);
  v7_set_method(v7, s_uart_proto, "configure", UART_configure);
  v7_set_method(v7, s_uart_proto, "onRecv", UART_onRecv);
  v7_set_method(v7, s_uart_proto, "recv", UART_recv);
  v7_set_method(v7, s_uart_proto, "setRXEnabled", UART_setRXEnabled);
  v7_set_method(v7, s_uart_proto, "onTXEmpty", UART_onTXEmpty);
  v7_set_method(v7, s_uart_proto, "sendAvail", UART_sendAvail);
  v7_set_method(v7, s_uart_proto, "send", UART_send);
  v7_set_method(v7, v7_get_global(v7), "UART", UART_get);

  sj_us[0].uart_no = 0;
  sj_us[0].obj = v7_mk_object(v7);
  v7_set_proto(v7, sj_us[0].obj, s_uart_proto);
  v7_set(v7, sj_us[0].obj, "_u", ~0, v7_mk_number(0));
  v7_own(v7, &sj_us[0].obj);

  sj_us[1].uart_no = 1;
  sj_us[1].obj = v7_mk_object(v7);
  v7_set_proto(v7, sj_us[1].obj, s_uart_proto);
  v7_set(v7, sj_us[1].obj, "_u", ~0, v7_mk_number(1));
  v7_own(v7, &sj_us[1].obj);
}
Exemplo n.º 2
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.º 3
0
static enum v7_err Sys_prof(struct v7 *v7, v7_val_t *res) {
  *res = v7_mk_object(v7);

  v7_set(v7, *res, "sysfree", 7, v7_mk_number(sj_get_free_heap_size()));
  v7_set(v7, *res, "used_by_js", 10,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_HEAP_USED)));
  v7_set(v7, *res, "used_by_fs", 10, v7_mk_number(sj_get_fs_memory_usage()));

  return V7_OK;
}
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::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.º 6
0
Arquivo: js_oop.c Projeto: QUSIR/v7
int main(void) {
    enum v7_err rcode = V7_OK;
    struct v7 *v7 = v7_create();
    v7_val_t ctor_func, proto, eval_result;

    proto = v7_mk_object(v7);
    ctor_func = v7_mk_function_with_proto(v7, MyThing_ctor, proto);
    v7_def(v7, ctor_func, "MY_CONST", ~0,
           (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(123));
    v7_set_method(v7, proto, "myMethod", &MyThing_myMethod);
    v7_set(v7, v7_get_global(v7), "MyThing", ~0, ctor_func);

    rcode = v7_exec(v7,
                    "\
      print('MyThing.MY_CONST = ', MyThing.MY_CONST); \
      var t = new MyThing(456); \
      print('t.MY_CONST = ', t.MY_CONST); \
      print('t.myMethod = ', t.myMethod); \
      print('t.myMethod() = ', t.myMethod());",
                    &eval_result);
    if (rcode != V7_OK) {
        fprintf(stderr, "exec error: %d\n", (int) rcode);
    }

    v7_destroy(v7);
    return (int) rcode;
}
Exemplo n.º 7
0
static enum v7_err jsSetTimeout(struct v7* v7, v7_val_t* res) {
	v7_val_t* func = new v7_val_t;
	v7_own(v7, func);
	*func = v7_arg(v7, 0);
	v7_val_t inv = v7_arg(v7, 1);

	if (!v7_is_callable(v7, *func)) {
		//TODO: catch error if 1st argument not callable code
		return V7_OK;
	}

	if (!v7_is_number(inv)) {
		//TODO: catch error if 2nd argument not a number
		return V7_OK;
	}

	double ms = v7_to_number(inv);
	long lms = floor(ms);

	//v7_apply(v7, func, _activeBot->getV7Obj(), v7_mk_undefined(), NULL);

	TIMER* newTimer = new TIMER;
	newTimer->pToFunc = func;
	newTimer->delay = lms;
	newTimer->timeBegin = GetTickCount();
	newTimer->id = (long)newTimer;

	_activeBot->_timers.Add(newTimer);

	double vOut = (double)newTimer->id;

	*res = v7_mk_number(vOut);

	return V7_OK;
}
Exemplo n.º 8
0
static enum v7_err UART_sendAvail(struct v7 *v7, v7_val_t *res) {
  struct esp_sj_uart_state *us;
  enum v7_err ret = esp_sj_uart_get_state(v7, &us);
  if (ret != V7_OK) return ret;
  *res = v7_mk_number(esp_uart_tx_buf(us->uart_no)->avail);
  return V7_OK;
}
Exemplo n.º 9
0
static enum v7_err js_sum(struct v7 *v7, v7_val_t *res) {
  double arg0 = v7_to_number(v7_arg(v7, 0));
  double arg1 = v7_to_number(v7_arg(v7, 1));
  double result = sum(arg0, arg1);

  *res = v7_mk_number(result);
  return V7_OK;
}
Exemplo n.º 10
0
static int notify_js(enum js_update_status us, const char *info) {
#ifndef CS_DISABLE_JS
  if (!v7_is_undefined(s_updater_notify_cb)) {
    if (info == NULL) {
      sj_invoke_cb1(s_v7, s_updater_notify_cb, v7_mk_number(s_v7, us));
    } else {
      sj_invoke_cb2(s_v7, s_updater_notify_cb, v7_mk_number(s_v7, us),
                    v7_mk_string(s_v7, info, ~0, 1));
    };

    return 1;
  }
#else
  (void) us;
  (void) info;
#endif

  return 0;
}
Exemplo n.º 11
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.º 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
void init_updater_clubby(struct v7 *v7) {
  (void) v7;
#ifndef DISABLE_C_CLUBBY

#ifndef CS_DISABLE_JS
  s_v7 = v7;
  v7_val_t updater = v7_mk_object(v7);
  v7_val_t sys = v7_get(v7, v7_get_global(v7), "Sys", ~0);
  s_updater_notify_cb = V7_UNDEFINED;
  v7_own(v7, &s_updater_notify_cb);

  v7_def(v7, sys, "updater", ~0, V7_DESC_ENUMERABLE(0), updater);
  v7_set_method(v7, updater, "notify", Updater_notify);
  v7_set_method(v7, updater, "start", Updater_startupdate);

  v7_def(s_v7, updater, "GOT_REQUEST", ~0,
         (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)),
         v7_mk_number(v7, UJS_GOT_REQUEST));

  v7_def(s_v7, updater, "COMPLETED", ~0,
         (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)),
         v7_mk_number(v7, UJS_COMPLETED));

  v7_def(s_v7, updater, "NOTHING_TODO", ~0,
         (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)),
         v7_mk_number(v7, UJS_NOTHING_TODO));

  v7_def(s_v7, updater, "FAILED", ~0,
         (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)),
         v7_mk_number(v7, UJS_ERROR));
#endif

  sj_clubby_register_global_command("/v1/SWUpdate.Update", handle_update_req,
                                    NULL);

  sj_clubby_register_global_command(clubby_cmd_ready, handle_clubby_ready,
                                    NULL);
#endif
}
Exemplo n.º 14
0
void sj_wifi_api_setup(struct v7 *v7) {
  v7_val_t s_wifi = v7_mk_object(v7);

  v7_own(v7, &s_wifi);

  v7_set_method(v7, s_wifi, "setup", sj_Wifi_setup);
  v7_set_method(v7, s_wifi, "connect", Wifi_connect);
  v7_set_method(v7, s_wifi, "disconnect", Wifi_disconnect);
  v7_set_method(v7, s_wifi, "status", Wifi_status);
  v7_set_method(v7, s_wifi, "show", Wifi_show);
  v7_set_method(v7, s_wifi, "ip", Wifi_ip);
  v7_set_method(v7, s_wifi, "changed", Wifi_changed);
  v7_set_method(v7, s_wifi, "scan", Wifi_scan);
  v7_set_method(v7, s_wifi, "ready", Wifi_ready);
  v7_set(v7, v7_get_global(v7), "Wifi", ~0, s_wifi);

  v7_set(v7, s_wifi, "CONNECTED", ~0, v7_mk_number(v7, SJ_WIFI_CONNECTED));
  v7_set(v7, s_wifi, "DISCONNECTED", ~0,
         v7_mk_number(v7, SJ_WIFI_DISCONNECTED));
  v7_set(v7, s_wifi, "GOTIP", ~0, v7_mk_number(v7, SJ_WIFI_IP_ACQUIRED));

  v7_disown(v7, &s_wifi);
}
Exemplo n.º 15
0
V7_PRIVATE
v7_val_t mk_cfunction_obj(struct v7 *v7, v7_cfunction_t *f, int num_args) {
  val_t obj = mk_object(v7, v7->vals.function_prototype);
  struct gc_tmp_frame tf = new_tmp_frame(v7);
  tmp_stack_push(&tf, &obj);
  v7_def(v7, obj, "", 0, _V7_DESC_HIDDEN(1), v7_mk_cfunction(f));
  if (num_args >= 0) {
    v7_def(v7, obj, "length", 6, (V7_DESC_ENUMERABLE(0) | V7_DESC_WRITABLE(0) |
                                  V7_DESC_CONFIGURABLE(0)),
           v7_mk_number(v7, num_args));
  }
  tmp_frame_cleanup(&tf);
  return obj;
}
Exemplo n.º 16
0
static enum v7_err ADC_readVoltage(struct v7 *v7, v7_val_t *res) {
  v7_val_t pinv = v7_arg(v7, 0);
  int pin;

  if (!v7_is_number(pinv)) {
    printf("non-numeric pin\n");
    *res = v7_mk_undefined();
  } else {
    pin = v7_to_number(pinv);
    *res = v7_mk_number(sj_adc_read_voltage(pin));
  }

  return V7_OK;
}
Exemplo n.º 17
0
SJ_PRIVATE enum v7_err GPIO_read(struct v7 *v7, v7_val_t *res) {
  v7_val_t pinv = v7_arg(v7, 0);
  int pin;

  if (!v7_is_number(pinv)) {
    printf("non-numeric pin\n");
    *res = v7_mk_undefined();
  } else {
    pin = v7_to_number(pinv);
    *res = v7_mk_number(sj_gpio_read(pin));
  }

  return V7_OK;
}
Exemplo n.º 18
0
SJ_PRIVATE enum v7_err GPIO_read(struct v7 *v7, v7_val_t *res) {
  v7_val_t pinv = v7_arg(v7, 0);
  int pin;

  if (!v7_is_number(pinv)) {
    printf("non-numeric pin\n");
    *res = V7_UNDEFINED;
  } else {
    pin = v7_get_double(v7, pinv);
    *res = v7_mk_number(v7, sj_gpio_read(pin));
  }

  return V7_OK;
}
Exemplo n.º 19
0
SJ_PRIVATE enum v7_err sj_set_interval_or_timeout(struct v7 *v7, v7_val_t *res,
                                                  int repeat) {
  v7_val_t msecsv = v7_arg(v7, 1);
  int msecs;
  (void) res;

  if (!v7_is_callable(v7, v7_arg(v7, 0))) {
    printf("cb is not a function\n");
  } else if (!v7_is_number(msecsv)) {
    printf("msecs is not a number\n");
  } else {
    v7_val_t cb = v7_arg(v7, 0);
    msecs = v7_get_double(v7, msecsv);
    *res = v7_mk_number(v7, sj_set_js_timer(msecs, repeat, v7, cb));
  }

  return V7_OK;
}
Exemplo n.º 20
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.º 21
0
static enum v7_err UART_send(struct v7 *v7, v7_val_t *res) {
  struct esp_sj_uart_state *us;
  enum v7_err ret = esp_sj_uart_get_state(v7, &us);
  if (ret != V7_OK) return ret;

  v7_val_t arg0 = v7_arg(v7, 0);
  if (!v7_is_string(arg0)) {
    return v7_throwf(v7, "Error", "String arg required");
  }
  size_t len = 0;
  const char *data = v7_get_string_data(v7, &arg0, &len);
  if (data != NULL && len > 0) {
    cs_rbuf_t *txb = esp_uart_tx_buf(us->uart_no);
    len = MIN(len, txb->avail);
    cs_rbuf_append(txb, (uint8_t *) data, len);
    esp_sj_uart_schedule_dispatcher(us->uart_no);
  }
  *res = v7_mk_number(len);
  return V7_OK;
}
Exemplo n.º 22
0
v7_val_t v7_file_to_val(int fd) {
  return v7_mk_number(fd);
}
Exemplo n.º 23
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);
}
Exemplo n.º 24
0
SJ_PRIVATE enum v7_err Sys_fs_getFreeSpace(struct v7 *v7, v7_val_t *res) {
  *res = v7_mk_number(v7, sj_get_storage_free_space());
  return V7_OK;
}
Exemplo n.º 25
0
static enum v7_err Sys_time(struct v7 *v7, v7_val_t *res) {
  (void) v7;
  *res = v7_mk_number(v7, cs_time());
  return V7_OK;
}
Exemplo n.º 26
0
void sj_gpio_api_setup(struct v7 *v7) {
  v7_val_t gpio = v7_mk_object(v7);
  v7_set(v7, v7_get_global(v7), "GPIO", ~0, gpio);
  v7_set_method(v7, gpio, "setMode", GPIO_setMode);
  v7_set_method(v7, gpio, "read", GPIO_read);
  v7_set_method(v7, gpio, "write", GPIO_write);
  v7_set_method(v7, gpio, "setISR", GPIO_setISR);

  v7_set(v7, gpio, "INOUT", ~0, v7_mk_number(v7, GPIO_MODE_INOUT));
  v7_set(v7, gpio, "IN", ~0, v7_mk_number(v7, GPIO_MODE_INPUT));
  v7_set(v7, gpio, "OUT", ~0, v7_mk_number(v7, GPIO_MODE_OUTPUT));

  v7_set(v7, gpio, "FLOAT", ~0, v7_mk_number(v7, GPIO_PULL_FLOAT));
  v7_set(v7, gpio, "PULLUP", ~0, v7_mk_number(v7, GPIO_PULL_PULLUP));
  v7_set(v7, gpio, "PULLDOWN", ~0, v7_mk_number(v7, GPIO_PULL_PULLDOWN));

  v7_set(v7, gpio, "OFF", ~0, v7_mk_number(v7, GPIO_INTR_OFF));
  v7_set(v7, gpio, "POSEDGE", ~0, v7_mk_number(v7, GPIO_INTR_POSEDGE));
  v7_set(v7, gpio, "NEGEDGE", ~0, v7_mk_number(v7, GPIO_INTR_NEGEDGE));
  v7_set(v7, gpio, "ANYEDGE", ~0, v7_mk_number(v7, GPIO_INTR_ANYEDGE));
  v7_set(v7, gpio, "LOLEVEL", ~0, v7_mk_number(v7, GPIO_INTR_LOLEVEL));
  v7_set(v7, gpio, "HILEVEL", ~0, v7_mk_number(v7, GPIO_INTR_HILEVEL));
  /*
   * TODO(mkm): figure out what to do with this "esp specific" mode.
   * It's not really ESP specific, but the soft debouncer is currently
   * implemented in esp8266 platform code.
   */
  v7_set(v7, gpio, "CLICK", ~0,
         v7_mk_number(v7, 6 /* GPIO_INTR_TYPE_ONCLICK */));

  if (v7_exec_file(v7, "gpio.js", NULL) != V7_OK) {
    /* TODO(mkm): make setup functions return an error code */
    abort();
  }
}
Exemplo n.º 27
0
/*
 * Parse URL; used for:
 *
 * - `URL.parse()`
 * - `Http.request()` and `Http.get()`, when provided `opts` is a string.
 */
static enum v7_err sj_url_parse(struct v7 *v7, v7_val_t url_v, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  v7_val_t opts, protocol_v;
  size_t i, j, len;
  int state = 0;
  const char *url;

  if (!v7_is_string(url_v)) {
    rcode = v7_throwf(v7, "TypeError", "URL must be a string");
    goto clean;
  }

  url = v7_get_string_data(v7, &url_v, &len);
  opts = v7_mk_object(v7);
  for (i = j = 0; j < len; j++) {
    switch (state) {
      case 0:
        if (url[j] == '/') {
          protocol_v = v7_mk_string(v7, url + i, j - i - 1, 1);
          v7_set(v7, opts, "protocol", ~0, protocol_v);
          j += 1;
          i = j + 1;
          state = 1;
        }
        break;
      case 1:
        if (url[j] == '/' || (j > i && url[j] == ':') || j == len - 1) {
          int hl = j - i;
          if (j == len - 1 && url[j] != '/' && url[j] != ':') hl++;
          v7_set(v7, opts, "hostname", ~0, v7_mk_string(v7, url + i, hl, 1));
          if (url[j] == '/' || j == len - 1) {
            const char *protocol = v7_to_cstring(v7, &protocol_v);
            int port = strcasecmp(protocol, "https") == 0 ? 443 : 80;
            v7_set(v7, opts, "port", ~0, v7_mk_number(port));
            i = j;
            if (j == len - 1) j--;
            state = 3;
          } else {
            i = j + 1;
            state = 2;
          }
        }
        break;
      case 2:
        if (url[j] == '/' || j == len - 1) {
          char ps[6];
          size_t l = j - i;
          if (j == len - 1) l++;
          if (l > sizeof(ps) - 1) l = sizeof(ps) - 1;
          memcpy(ps, url + i, l);
          ps[l] = '\0';
          v7_set(v7, opts, "port", ~0, v7_mk_number(atoi(ps)));
          i = j;
          if (j == len - 1) j--;
          state = 3;
        }
        break;
      case 3:
        if (j == len - 1) {
          v7_val_t path_v = j - i > 0 ? v7_mk_string(v7, url + i, j - i + 1, 1)
                                      : v7_mk_string(v7, "/", 1, 1);
          v7_set(v7, opts, "path", ~0, path_v);
        }
        break;
    }
  }
  *res = opts;

clean:
  return rcode;
}
Exemplo n.º 28
0
/*
 * Returns an object describing the free memory.
 *
 * sysfree: free system heap bytes
 * jssize: size of JS heap in bytes
 * jsfree: free JS heap bytes
 * strres: size of reserved string heap in bytes
 * struse: portion of string heap with used data
 * objnfree: number of free object slots in js heap
 * propnfree: number of free property slots in js heap
 * funcnfree: number of free function slots in js heap
 */
SJ_PRIVATE enum v7_err GC_stat(struct v7 *v7, v7_val_t *res) {
  /* take a snapshot of the stats that would change as we populate the result */
  size_t sysfree = sj_get_free_heap_size();
  size_t jssize = v7_heap_stat(v7, V7_HEAP_STAT_HEAP_SIZE);
  size_t jsfree = jssize - v7_heap_stat(v7, V7_HEAP_STAT_HEAP_USED);
  size_t strres = v7_heap_stat(v7, V7_HEAP_STAT_STRING_HEAP_RESERVED);
  size_t struse = v7_heap_stat(v7, V7_HEAP_STAT_STRING_HEAP_USED);
  size_t objfree = v7_heap_stat(v7, V7_HEAP_STAT_OBJ_HEAP_FREE);
  size_t propnfree = v7_heap_stat(v7, V7_HEAP_STAT_PROP_HEAP_FREE);
  *res = v7_mk_object(v7);

  v7_set(v7, *res, "sysfree", ~0, v7_mk_number(sysfree));
  v7_set(v7, *res, "jssize", ~0, v7_mk_number(jssize));
  v7_set(v7, *res, "jsfree", ~0, v7_mk_number(jsfree));
  v7_set(v7, *res, "strres", ~0, v7_mk_number(strres));
  v7_set(v7, *res, "struse", ~0, v7_mk_number(struse));
  v7_set(v7, *res, "objfree", ~0, v7_mk_number(objfree));
  v7_set(v7, *res, "objncell", ~0,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_OBJ_HEAP_CELL_SIZE)));
  v7_set(v7, *res, "propnfree", ~0, v7_mk_number(propnfree));
  v7_set(v7, *res, "propncell", ~0,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_PROP_HEAP_CELL_SIZE)));
  v7_set(v7, *res, "funcnfree", ~0,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_FUNC_HEAP_FREE)));
  v7_set(v7, *res, "funcncell", ~0,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_FUNC_HEAP_CELL_SIZE)));
  v7_set(v7, *res, "astsize", ~0,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_FUNC_AST_SIZE)));
  v7_set(v7, *res, "owned", ~0,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_FUNC_OWNED)));
  v7_set(v7, *res, "owned_max", ~0,
         v7_mk_number(v7_heap_stat(v7, V7_HEAP_STAT_FUNC_OWNED_MAX)));

  return V7_OK;
}
Exemplo n.º 29
0
void sj_wifi_on_change_js(enum sj_wifi_status event, void *arg) {
  struct wifi_cb_arg *cba = (struct wifi_cb_arg *) arg;
  if (v7_is_callable(cba->v7, cba->v)) {
    sj_invoke_cb1(cba->v7, cba->v, v7_mk_number(cba->v7, event));
  }
}