コード例 #1
0
ファイル: sj_simple_http.c プロジェクト: victorino80/smart.js
void sj_http_success_callback(struct v7 *v7, v7_val_t cb, const char *data,
                              size_t data_len) {
  v7_val_t datav, cb_args;
  v7_val_t res;

  cb_args = v7_create_object(v7);
  v7_own(v7, &cb_args);

  datav = v7_create_string(v7, data, data_len, 1);
  v7_own(v7, &datav);

  v7_array_set(v7, cb_args, 0, cb);
  v7_array_set(v7, cb_args, 1, datav);
  v7_disown(v7, &datav);

  if (v7_exec_with(v7, &res, "this[0](this[1])", cb_args) != V7_OK) {
    v7_fprintln(stderr, v7, res);
  }

  v7_disown(v7, &cb_args);
  v7_disown(v7, &cb);
}
コード例 #2
0
ファイル: sj_v7_ext.c プロジェクト: ufosaga/smart.js
/*
 * 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
 */
static v7_val_t GC_stat(struct v7 *v7, v7_val_t this_obj, v7_val_t args) {
  /* 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);
  v7_val_t f = v7_create_undefined();
  v7_own(v7, &f);
  f = v7_create_object(v7);

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

  v7_disown(v7, &f);
  return f;
}
コード例 #3
0
ファイル: js_oop.c プロジェクト: DeJQit/v7
int main(void) {
  struct v7 *v7 = v7_create();
  v7_val_t ctor_func, proto, eval_result;

  proto = v7_create_object(v7);
  ctor_func = v7_create_constructor(v7, proto, MyThing_ctor);
  v7_set(v7, ctor_func, "MY_CONST", ~0,
         V7_PROPERTY_READ_ONLY | V7_PROPERTY_DONT_DELETE,
         v7_create_number(123));
  v7_set_method(v7, proto, "myMethod", &MyThing_myMethod);
  v7_set(v7, v7_get_global(v7), "MyThing", ~0, 0, ctor_func);

  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);
  v7_destroy(v7);
  return 0;
}
コード例 #4
0
ファイル: v7_esp.c プロジェクト: ifzz/smart.js
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;
}
コード例 #5
0
ファイル: json.c プロジェクト: CoryXie/v7
V7_PRIVATE void init_json(struct v7 *v7) {
  val_t o = v7_create_object(v7);
  set_cfunc_obj_prop_n(v7, o, "stringify", Json_stringify, 1);
  set_cfunc_obj_prop_n(v7, o, "parse", Std_eval, 1);
  v7_set_property(v7, v7->global_object, "JSON", 4, V7_PROPERTY_DONT_ENUM, o);
}
コード例 #6
0
void init_data_gen_server(struct v7 *v7) {
  v7_val_t tcp = v7_create_object(v7);
  v7_set(v7, v7_get_global_object(v7), "Tcp", 3, 0, tcp);
  v7_set_method(v7, tcp, "gen", Tcp_gen);
}
コード例 #7
0
ファイル: esp_sj_wifi.c プロジェクト: habashynn/smart.js
void wifi_changed_cb(System_Event_t *evt) {
  enum sj_wifi_status sj_ev = -1;

  /* TODO(rojer): Share this logic between platforms. */
  if (wifi_setting_up &&
#ifndef RTOS_SDK
      evt->event == EVENT_STAMODE_GOT_IP
#else
      evt->event_id == EVENT_STAMODE_GOT_IP
#endif
      ) {
    struct station_config config;
    v7_val_t res;
    v7_val_t conf = v7_get(v7, v7_get_global_object(v7), "conf", ~0);
    v7_val_t known, wifi;

    if (v7_is_undefined(conf)) {
      fprintf(stderr, "cannot save conf, no conf object\n");
      return;
    }
    wifi = v7_get(v7, conf, "wifi", ~0);

    if (v7_is_undefined(wifi)) {
      wifi = v7_create_object(v7);
      v7_set(v7, conf, "wifi", ~0, 0, wifi);
    }
    known = v7_get(v7, conf, "known", ~0);
    if (v7_is_undefined(known)) {
      known = v7_create_object(v7);
      v7_set(v7, wifi, "known", ~0, 0, known);
    }

    wifi_station_get_config(&config);

    v7_set(v7, known, (const char *) config.ssid, ~0, 0,
           v7_create_string(v7, (const char *) config.password,
                            strlen((const char *) config.password), 1));

    v7_exec(v7, "conf.save()", &res);
    wifi_setting_up = 0;
  }

#ifndef RTOS_SDK
  switch (evt->event) {
#else
  switch (evt->event_id) {
#endif
    case EVENT_STAMODE_DISCONNECTED:
      sj_ev = SJ_WIFI_DISCONNECTED;
      break;
    case EVENT_STAMODE_CONNECTED:
      sj_ev = SJ_WIFI_CONNECTED;
      break;
    case EVENT_STAMODE_GOT_IP:
      sj_ev = SJ_WIFI_IP_ACQUIRED;
      break;
  }

  if (sj_ev >= 0) sj_wifi_on_change_callback(sj_ev);
}

char *sj_wifi_get_connected_ssid() {
  struct station_config conf;
  if (!wifi_station_get_config(&conf)) return NULL;
  return strdup((const char *) conf.ssid);
}

char *sj_wifi_get_sta_ip() {
  struct ip_info info;
  char *ip;
  if (!wifi_get_ip_info(0, &info) || info.ip.addr == 0) return NULL;
  if (asprintf(&ip, IPSTR, IP2STR(&info.ip)) < 0) {
    return NULL;
  }
  return ip;
}

void wifi_scan_done(void *arg, STATUS status) {
  if (status == OK) {
    STAILQ_HEAD(, bss_info) *info = arg;
    struct bss_info *p;
    const char **ssids;
    int n = 0;
    STAILQ_FOREACH(p, info, next) n++;
    ssids = calloc(n + 1, sizeof(*ssids));
    n = 0;
    STAILQ_FOREACH(p, info, next) {
      ssids[n++] = (const char *) p->ssid;
    }
    wifi_scan_cb(ssids);
    free(ssids);
  } else {