示例#1
0
static void process_js(char *cmd) {
  s_sjp.char_processor = interrupt_char_processor;
  char result_str[10];
  v7_val_t v;
  int res = v7_exec(s_sjp.v7, &v, cmd);

  if (res == V7_SYNTAX_ERROR) {
    printf("Syntax error: %s\n", v7_get_parser_error(s_sjp.v7));
  } else if (res == V7_STACK_OVERFLOW) {
    printf("Stack overflow: %s\n", v7_get_parser_error(s_sjp.v7));
  } else {
    char *p = v7_to_json(s_sjp.v7, v, result_str, sizeof(result_str));

    if (res == V7_EXEC_EXCEPTION) {
      printf("Exec error:");
    }

    printf("%s\n", p);

    if (p != result_str) {
      free(p);
    }
  }

  v7_gc(s_sjp.v7, 0 /* full */);
}
示例#2
0
文件: json.c 项目: CoryXie/v7
static val_t Json_stringify(struct v7 *v7, val_t this_obj, val_t args) {
  val_t arg0 = v7_array_get(v7, args, 0);
  char buf[100], *p = v7_to_json(v7, arg0, buf, sizeof(buf));
  val_t res = v7_create_string(v7, p, strlen(p), 1);
  (void) this_obj;
  if (p != buf) free(p);
  return res;
}
示例#3
0
static void server_eval(struct espconn *c, void *body, unsigned short size) {
  char *buf, *resp, *j;
  enum v7_err err;
  v7_val_t v;
  const char *code;

  buf = malloc(size + 1);
  snprintf(buf, size + 1, "%.*s", (int) size, body);
  err = v7_exec(v7, &v, buf);

  switch (err) {
    case V7_SYNTAX_ERROR:
    case V7_EXEC_EXCEPTION:
      code = "500 Eval error";
      break;
    case V7_OK:
      code = "200 OK";
      break;
  }

  switch (err) {
    case V7_SYNTAX_ERROR:
      j = malloc(512);
      strncpy(j, v7_get_parser_error(v7), 512);
      break;
    case V7_EXEC_EXCEPTION:
    case V7_OK:
      j = v7_to_json(v7, v, buf, sizeof(buf));
      break;
  }

  int resp_len = strlen(code) + 13 + strlen(j) + 1;
  resp = malloc(resp_len);
  snprintf(resp, resp_len, "HTTP/1.1 %s\r\n\r\n%s", code, j);

  if (j != buf) free(j);
  free(buf);

  espconn_sent(c, resp, strlen(resp));
  free(resp);
}
示例#4
0
文件: v7_esp.c 项目: fast01/smart.js
/*
 * 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();
}