コード例 #1
0
ファイル: unit_test.c プロジェクト: dev-gitter/v7
static const char *test_v7_destroy(void) {
  struct v7 *v7 = v7_create();
  ASSERT(v7 != NULL);
  v7_destroy(&v7);
  ASSERT(v7 == NULL);
  v7_destroy(NULL);
  return NULL;
}
コード例 #2
0
ファイル: js_oop.c プロジェクト: 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;
}
コード例 #3
0
ファイル: main.c プロジェクト: mwanna/v7
int main(int argc, char *argv[]) {
  struct v7 *v7 = v7_create();
  int i, error_code;

  // Execute inline code
  for (i = 1; i < argc && argv[i][0] == '-'; i++) {
    if (strcmp(argv[i], "-e") == 0 && i + 1 < argc) {
      if ((error_code = v7_exec(v7, argv[i + 1])) != V7_OK) {
        fprintf(stderr, "Error executing [%s]: %s\n", argv[i + 1],
                v7_strerror(error_code));
      }
      i++;
    }
  }

  // Execute files
  for (; i < argc; i++) {
    if ((error_code = v7_exec_file(v7, argv[i])) != V7_OK) {
      fprintf(stderr, "%s line %d: %s\n", argv[i], v7->pstate.line_no,
              v7_strerror(error_code));
    }
  }

  v7_destroy(&v7);
  return EXIT_SUCCESS;
}
コード例 #4
0
ファイル: unit_test.c プロジェクト: dev-gitter/v7
static const char *test_is_true(void) {
  struct v7 *v7 = v7_create();

  ASSERT(test_if_expr(v7, "true", 1));
  ASSERT(test_if_expr(v7, "false", 0));
  ASSERT(test_if_expr(v7, "1", 1));
  ASSERT(test_if_expr(v7, "123.24876", 1));
  ASSERT(test_if_expr(v7, "0", 0));
  ASSERT(test_if_expr(v7, "-1", 1));
  ASSERT(test_if_expr(v7, "'true'", 1));
  ASSERT(test_if_expr(v7, "'false'", 1));
  ASSERT(test_if_expr(v7, "'hi'", 1));
  ASSERT(test_if_expr(v7, "'1'", 1));
  ASSERT(test_if_expr(v7, "'0'", 1));
  ASSERT(test_if_expr(v7, "'-1'", 1));
  ASSERT(test_if_expr(v7, "''", 0));
  ASSERT(test_if_expr(v7, "null", 0));
  ASSERT(test_if_expr(v7, "undefined", 0));
#ifndef _WIN32
  ASSERT(test_if_expr(v7, "Infinity", 1));
  ASSERT(test_if_expr(v7, "-Infinity", 1));
#endif
  ASSERT(test_if_expr(v7, "[]", 1));
  ASSERT(test_if_expr(v7, "{}", 1));
  ASSERT(test_if_expr(v7, "[[]]", 1));
  ASSERT(test_if_expr(v7, "[0]", 1));
  ASSERT(test_if_expr(v7, "[1]", 1));
  ASSERT(test_if_expr(v7, "NaN", 0));

  v7_destroy(&v7);
  return NULL;
}
コード例 #5
0
int main(void) {
  struct v7 *v7 = v7_create();
  v7_val_t result;
  v7_set_method(v7, v7_get_global_object(v7), "sum", &js_sum);
  v7_exec(v7, &result, "print(sum(1.2, 3.4))");
  v7_destroy(v7);
  return 0;
}
コード例 #6
0
ファイル: call_js_from_c.c プロジェクト: neolp/v7
int main(void) {
  v7_val_t result;
  struct v7 *v7 = v7_create();
  v7_exec(v7, "var sum = function(a, b) { return a + b; };", &result);
  call_sum(v7);
  v7_destroy(v7);
  return 0;
}
コード例 #7
0
ファイル: unit_test.c プロジェクト: dev-gitter/v7
static const char *test_native_functions(void) {
  struct v7_val *v;
  struct v7 *v7 = v7_create();

  ASSERT(v7_set(v7, v7_global(v7), "adder", v7_push_func(v7, adder)) == V7_OK);
  ASSERT((v = v7_exec(v7, "adder(1, 2, 3 + 4);")) != NULL);
  ASSERT(check_num(v7, v, 10.0));
  v7_destroy(&v7);

  return NULL;
}
コード例 #8
0
ファイル: Bot.cpp プロジェクト: slashbmp/bmpbot-v7
Bot::~Bot()
{
	long i, l;
	TIMER* p;
	l = _timers.GetLength();
	for (i = 0; i < l; i++) {
		p = (TIMER*)_timers.GetAt(i);
		v7_disown(v7, (v7_val_t*)p->pToFunc);
		delete (v7_val_t*)p->pToFunc;
		delete p;
	}

	l = _onTexts.GetLength();
	for (i = 0; i < l; i++) {
		v7_disown(v7, (v7_val_t*)_onTexts.GetAt(i));
		delete (v7_val_t*)_onTexts.GetAt(i);
	}

	l = _onJoin.GetLength();
	for (i = 0; i < l; i++) {
		v7_disown(v7, (v7_val_t*)_onJoin.GetAt(i));
		delete (v7_val_t*)_onJoin.GetAt(i);
	}

	l = _onLeft.GetLength();
	for (i = 0; i < l; i++) {
		v7_disown(v7, (v7_val_t*)_onLeft.GetAt(i));
		delete (v7_val_t*)_onLeft.GetAt(i);
	}

	l = _onTalk.GetLength();
	for (i = 0; i < l; i++) {
		v7_disown(v7, (v7_val_t*)_onTalk.GetAt(i));
		delete (v7_val_t*)_onTalk.GetAt(i);
	}

	l = _onMotd.GetLength();
	for (i = 0; i < l; i++) {
		v7_disown(v7, (v7_val_t*)_onMotd.GetAt(i));
		delete (v7_val_t*)_onMotd.GetAt(i);
	}
	
	l = _onIm.GetLength();
	for (i = 0; i < l; i++) {
		v7_disown(v7, (v7_val_t*)_onIm.GetAt(i));
		delete (v7_val_t*)_onIm.GetAt(i);
	}

	v7_disown(v7, botObj);
	v7_disown(v7, v7Obj);

	v7_destroy(v7);
}
コード例 #9
0
ファイル: unit_test.c プロジェクト: Speedy37/v7
static const char *test_native_functions(void) {
  static struct v7_val adder_func_obj;
  struct v7 *v7 = v7_create();

  v7_init_func(&adder_func_obj, &adder);
  v7_setv(v7, v7_rootns(v7), V7_TYPE_STR, V7_TYPE_OBJ,
          "adder", 5, 0, &adder_func_obj);
  ASSERT(v7_exec(v7, "adder(1, 2, 3 + 4);") == V7_OK);
  ASSERT(check_num(v7, 10.0));
  v7_destroy(&v7);

  return NULL;
}
コード例 #10
0
ファイル: call_c_from_js.c プロジェクト: EyMenZ/NetSurf-OS3
int main(void) {
  enum v7_err rcode = V7_OK;
  struct v7 *v7 = v7_create();
  v7_val_t result;
  v7_set_method(v7, v7_get_global(v7), "sum", &js_sum);

  rcode = v7_exec(v7, "print(sum(1.2, 3.4))", &result);
  if (rcode != V7_OK) {
    v7_print_error(stderr, v7, "Evaluation error", result);
  }

  v7_destroy(v7);
  return (int) rcode;
}
コード例 #11
0
ファイル: unit_test.c プロジェクト: dev-gitter/v7
static const char *test_closure(void) {
  struct v7_val *v;
  struct v7 *v7 = v7_create();

  ASSERT((v = v7_exec(v7, "function a(x){return function(y){return x*y}}")) != NULL);
  ASSERT((v = v7_exec(v7, "var f1 = a(5);")) != NULL);
  ASSERT((v = v7_exec(v7, "var f2 = a(7);")) != NULL);
  ASSERT((v = v7_exec(v7, "f1(3);")) != NULL);
  ASSERT(check_num(v7, v, 15.0));
  ASSERT((v = v7_exec(v7, "f2(3);")) != NULL);
  ASSERT(check_num(v7, v, 21.0));
  v7_destroy(&v7);
  return NULL;
}
コード例 #12
0
ファイル: call_js_from_c.c プロジェクト: wespan/v7
int main(void) {
  enum v7_err rcode = V7_OK;
  v7_val_t result;
  struct v7 *v7 = v7_create();
  rcode = v7_exec(v7, "var sum = function(a, b) { return a + b; };", &result);
  if (rcode != V7_OK) {
    fprintf(stderr, "exec error: %d\n", (int)rcode);
    goto clean;
  }

  call_sum(v7);

clean:
  v7_destroy(v7);
  return (int)rcode;
}
コード例 #13
0
ファイル: v7.c プロジェクト: di3online/v7
int main(int argc, char *argv[]) {
  struct v7 *v7 = v7_create();
  int i, error_code;

  v7_init_stdlib(v7);

  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-e") == 0 && i + 1 < argc) {
      if ((error_code = v7_exec(v7, argv[i + 1])) != V7_OK) {
        fprintf(stderr, "Error executing [%s]: %d\n", argv[i + 1], error_code);
      }
    }
  }

  v7_destroy(&v7);
  return 0;
}
コード例 #14
0
ファイル: main.c プロジェクト: arnognulf/v7
int main(int argc, char *argv[]) {
  struct v7 *v7;
  int i, show_ast = 0;

  v7 = v7_create();

  /* Execute inline code */
  for (i = 1; i < argc && argv[i][0] == '-'; i++) {
    if (strcmp(argv[i], "-e") == 0 && i + 1 < argc) {
      if (show_ast) {
        dump_ast(argv[i + 1]);
      } else if (!v7_exec(v7, argv[i + 1])) {
        fprintf(stderr, "Error executing [%s]: %s\n", argv[i + 1],
                v7_get_error_string(v7));
      }
      i++;
    } else if (strcmp(argv[i], static_show_ast_flag) == 0) {
      show_ast = 1;
    } else if (strcmp(argv[i], "-h") == 0) {
      show_usage(argv);
    }
  }

  /* Execute files */
  for (; i < argc; i++) {
    if (show_ast) {
      size_t size;
      char *source_code;
      if ((source_code = read_file(argv[i], &size)) == NULL) {
        fprintf(stderr, "Cannot read [%s]\n", argv[i]);
      } else {
        dump_ast(source_code);
        free(source_code);
      }
    } else if (!v7_exec_file(v7, argv[i])) {
      fprintf(stderr, "%s\n", v7_get_error_string(v7));
    }
  }

  v7_destroy(&v7);
  return EXIT_SUCCESS;
}
コード例 #15
0
ファイル: js_oop.c プロジェクト: cgbystrom/scriptorium
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, 1);
  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_object(v7), "MyThing", ~0, 0, ctor_func);

  v7_exec(v7, &eval_result, "\
      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());");
  v7_destroy(v7);
  return 0;
}
コード例 #16
0
ファイル: nsv7.c プロジェクト: datetree/net_skeleton
int main(int argc, char *argv[]) {
  const char *script = "nsv7.js", *port = "4000";
  struct ns_server server;
  struct v7 *v7;
  int i;
  
  // Parse command line options
  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-f") == 0 && i + 1 < argc) {
      script = argv[++i];
    } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
      port = argv[++i];
    } else {
      elog(1, "Usage: %s [-f FILE] [-p PORT]", argv[0]);
    }
  }

  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);

  // Initialize scripting engine
  v7 = v7_create();
  v7_init_stdlib(v7);
  if (v7_exec_file(v7, script) != V7_OK) {
    elog(1, "Error executing %s", script);
  }

  // Initialize server
  ns_server_init(&server, v7, ev_handler);
  ns_bind(&server, port);
  while (s_received_signal == 0) {
    ns_server_poll(&server, 1000);
  }
  printf("Existing on signal %d\n", s_received_signal);
  v7_destroy(&v7);
  ns_server_free(&server);

  return EXIT_SUCCESS;
}
コード例 #17
0
ファイル: load_json_config.c プロジェクト: Jalloc/v7
int v7_example(void) {
  size_t n;
  const char *domain_str;
  struct v7 *v7 = v7_create();
  v7_val_t domain, port0, config;

  /* Load JSON configuration */
  if (v7_parse_json_file(v7, "config.json", &config) != V7_OK) {
    printf("%s\n", "Cannot load JSON config");
    return 1;
  }

  /* Lookup values in JSON configuration object */
  domain = v7_get(v7, config, "domain", 6);
  port0 = v7_array_get(v7, v7_get(v7, config, "ports", 5), 0);
  domain_str = v7_get_string(v7, &domain, &n);

  printf("Domain: [%.*s], port 0: [%d]\n",
         (int) n, domain_str, (int) v7_get_double(port0));

  v7_destroy(v7);
  return 0;
}
コード例 #18
0
ファイル: engine.c プロジェクト: di3online/websocket.js
int main(int argc, char *argv[]) {
  int i, error_code;

  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);

  s_v7 = v7_create();
  v7_init_stdlib(s_v7);
  v7_set_func(s_v7, v7_get_root_namespace(s_v7), "WebsocketServer", js_ws);
  v7_set_func(s_v7, v7_get_root_namespace(s_v7), "RunTcpServer", js_tcp);

  for (i = 1; i < argc; i++) {
    if ((error_code = v7_exec_file(s_v7, argv[i])) != V7_OK) {
      fprintf(stderr, "Error executing %s line %d: %s\n", argv[i],
                       s_v7->line_no, v7_err_to_str(error_code));
      exit(EXIT_FAILURE);
    }
  }

  fprintf(stderr, "Existing on signal %d\n", s_received_signal);
  v7_destroy(&s_v7);
  
  return EXIT_SUCCESS;
}
コード例 #19
0
ファイル: unit_test.c プロジェクト: Speedy37/v7
static const char *test_v7_exec(void) {
  struct v7 *v7 = v7_create();

  ASSERT(v7_exec(v7, "") == V7_OK);
  ASSERT(v7_exec(v7, "-2;") == V7_OK);
  ASSERT(check_num(v7, -2.0));
  ASSERT(v7_exec(v7, "3 + 4") == V7_OK);
  ASSERT(check_num(v7, 7.0));
  ASSERT(v7_exec(v7, "123.456") == V7_OK);
  ASSERT(check_num(v7, 123.456));
  ASSERT(v7_exec(v7, "NaN") == V7_OK);
  ASSERT(check_num(v7, NAN));

  // TODO: fix infinity handling under MSVC6
#ifndef _WIN32
  ASSERT(v7_exec(v7, "Infinity") == V7_OK);
  ASSERT(check_num(v7, INFINITY));
  ASSERT(v7_exec(v7, "-Infinity") == V7_OK);
  ASSERT(check_num(v7, -INFINITY));
#endif

  ASSERT(v7_exec(v7, "2()") == V7_CALLED_NON_FUNCTION);
  ASSERT(v7_exec(v7, " 15 +	2 \r\n * 2  / 1 - 3 * 4 ; ") == V7_OK);

  ASSERT(v7_exec(v7, "( (5  ) );") == V7_OK);
  ASSERT(check_num(v7, 5.0));

  ASSERT(v7_exec(v7, "(2 + (12 / 4));") == V7_OK);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_NUM);
  ASSERT(v7_top(v7)[-1]->v.num == 5.0);

  ASSERT(v7_exec(v7, "1;2 7") == V7_OK);
  ASSERT(check_num(v7, 7.0));
  ASSERT(v7_exec(v7, "a + 5") == V7_TYPE_ERROR);

  ASSERT(v7_exec(v7, "print();") == V7_OK);
  ASSERT(v7_exec(v7, "print(this);") == V7_OK);

  ASSERT(v7_exec(v7, "a = 7;") == V7_OK);
  ASSERT(check_num(v7, 7.0));
  ASSERT(v7_exec(v7, "print(this);") == V7_OK);

  ASSERT(v7_exec(v7, "b = a + 3;") == V7_OK);
  ASSERT(check_num(v7, 10.0));
  ASSERT(v7_exec(v7, "print(this);") == V7_OK);

  ASSERT(v7_exec(v7, "c = b * (a + 3) / 2;") == V7_OK);
  ASSERT(check_num(v7, 50.0));
  ASSERT(v7_exec(v7, "print(this);") == V7_OK);

  ASSERT(v7_exec(v7, "var x = 12 + 2 - a + b+ 3 / 4 * a;") == V7_OK);
  ASSERT(v7_exec(v7, "b + 2; x + 3 + 1 z = x -2;") == V7_OK);
  ASSERT(v7_exec(v7, "x; var y, z;") == V7_OK);
  ASSERT(v7_exec(v7, "1 2 3") == V7_OK);

  ASSERT(v7_exec(v7, "var k = true;") == V7_OK);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_BOOL);
  ASSERT(v7_top(v7)[-1]->v.num != 0.0);

  ASSERT(v7_exec(v7, "var blah = 'kuku';") == V7_OK);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_STR);

  // Test that k.y does exist
  ASSERT(v7_exec(v7, "k = { y: 17 };") == V7_OK);
  ASSERT(v7_exec(v7, "k.y") == V7_OK);
  ASSERT(check_num(v7, 17.0));
  v7_exec(v7, "print(this);");

  // Delete k.y and make sure it's gone
  ASSERT(v7_exec(v7, "delete k.y;") == V7_OK);
  ASSERT(v7_exec(v7, "k.y;") == V7_OK);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_UNDEF);
  ASSERT(v7_sp(v7) == 1);
  ASSERT(v7_exec(v7, "delete b; b;") == V7_OK);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_UNDEF);
  ASSERT(v7_sp(v7) == 1);

  ASSERT(v7_exec(v7, "k = { key1: {x:3}, key2: ':-)', y: 5 };") == V7_OK);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_OBJ);
  ASSERT(v7_sp(v7) == 1);

  ASSERT(v7_exec(v7, "k.x = 47;") == V7_OK);
  ASSERT(v7_exec(v7, "k.qwe = { foo: 5 };") == V7_OK);
  v7_exec(v7, "print(k);");
  ASSERT(v7_exec(v7, "k.qwe.foo = 15;") == V7_OK);
  v7_exec(v7, "print(k);");

  ASSERT(v7_exec(v7, "k.key1.x + 4") == V7_OK);
  ASSERT(check_num(v7, 7.0));

  ASSERT(v7_exec(v7, "k.foo") == V7_OK);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_UNDEF);

  ASSERT(v7_exec(v7, "var z = 'key1'; k[z]['x']") == V7_OK);
  ASSERT(check_num(v7, 3.0));

  ASSERT(v7_exec(v7, "var stk = 1;") == V7_OK);
  ASSERT(check_num(v7, 1.0));

  ASSERT(v7_exec(v7, "var f1 = function(x, y) { } ; ") == V7_OK);
  ASSERT(v7_sp(v7) > 0);
  ASSERT(v7_top(v7)[-1]->type == V7_TYPE_OBJ);
  ASSERT(memcmp(v7_top(v7)[-1]->v.func.source_code, "(x, y) { } ", 10) == 0);

  ASSERT(v7_exec(v7, "var f1 = function(x, y) { return x * y; };") == V7_OK);
  ASSERT(v7_is_class(v7_top(v7)[-1], V7_CLASS_FUNCTION));
  ASSERT(v7_exec(v7, "f1(2, 3)") == V7_OK);
  ASSERT(check_num(v7, 6.0));
  ASSERT(v7_exec(v7, "f1(12, 4) + 1;") == V7_OK);
  ASSERT(check_num(v7, 49.0));
  ASSERT(v7_exec(v7, "f1(12, 4) * 2;") == V7_OK);
  ASSERT(check_num(v7, 96.0));

  ASSERT(v7_exec(v7, "f = function(x,y,z) {print(this);};") == V7_OK);
  ASSERT(v7_sp(v7) == 1);
  ASSERT(v7_exec(v7, "f();") == V7_OK);
  ASSERT(v7_exec(v7, "f({});") == V7_OK);
  ASSERT(v7_exec(v7, "f(1, 2);") == V7_OK);
  ASSERT(v7_exec(v7, "f(123, {});") == V7_OK);

  ASSERT(v7_exec(v7, "if (0) f1 = 2; ") == V7_OK);
  ASSERT(v7_exec(v7, "if (5) { f1 = 3; f2 = function(){}; } ") == V7_OK);

  ASSERT(v7_exec(v7, "0 ? 1 : 2;") == V7_OK);
  ASSERT(check_num(v7, 2.0));

  ASSERT(v7_exec(v7, "k = true ? 1 : 2;") == V7_OK);
  ASSERT(check_num(v7, 1.0));

  ASSERT(v7_exec(v7, "var f = function(){var x=12; return x + 1;};") == V7_OK);
  ASSERT(v7_sp(v7) > 0);

  ASSERT(v7_exec(v7, "k = f(1,2,3);") == V7_OK);
  ASSERT(check_num(v7, 13.0));

  ASSERT(v7_exec(v7, "(function() { return f() + 7; })()") == V7_OK);
  ASSERT(check_num(v7, 20.0));

  ASSERT(v7_exec(v7, "var a = 1; if (a == 1) { a = 2; }; a;") == V7_OK);
  ASSERT(check_num(v7, 2.0));

  ASSERT(v7_exec(v7, "var a = 'foo'; a == 'foo';") == V7_OK);
  ASSERT(check_bool(v7, 1.0));

  ASSERT(v7_exec(v7, "a = { x: function(p) { print(this); } }") == V7_OK);
  ASSERT(v7_exec(v7, "a.x(2);") == V7_OK);

  ASSERT(v7_exec(v7, "74.toString()") == V7_OK);
  ASSERT(check_str(v7, "74"));
  ASSERT(v7_exec(v7, "'hello'.length") == V7_OK);
  ASSERT(check_num(v7, 5.0));

  ASSERT(v7_exec(v7, "k = { x : function() { if (1) 2; } }") == V7_OK);

  ASSERT(v7_exec(v7, "'foo' + 'bar'") == V7_OK);
  ASSERT(check_str(v7, "foobar"));

  ASSERT(v7_exec(v7, "var x = [1, 'foo', true, 7];") == V7_OK);
  ASSERT(v7_exec(v7, "x.length") == V7_OK);
  ASSERT(check_num(v7, 4.0));
  ASSERT(v7_exec(v7, "x[1]") == V7_OK);
  ASSERT(check_str(v7, "foo"));

  ASSERT(v7_exec(v7, "var f1 = function() { 1; };") == V7_OK);
  ASSERT(v7_exec(v7, "var f2 = function(x) { if (x) return x; };") == V7_OK);
  ASSERT(v7_exec(v7, "f1()") == V7_OK);
  ASSERT(v7->sp == 1 && v7->stack[0]->type == V7_TYPE_UNDEF);
  ASSERT(v7_exec(v7, "f2(false)") == V7_OK);
  ASSERT(v7->sp == 1 && v7->stack[0]->type == V7_TYPE_UNDEF);
  ASSERT(v7_exec(v7, "f2(17)") == V7_OK);
  ASSERT(check_num(v7, 17.0));
  ASSERT(v7_exec(v7, "f2(true)") == V7_OK);
  ASSERT(check_bool(v7, 1.0));

  ASSERT(v7_exec(v7, "1 <= 2 ? 7 : 8") == V7_OK);
  ASSERT(check_num(v7, 7.0));

  ASSERT(v7_exec(v7, "function a (t) { return t * t }; ") == V7_OK);
  ASSERT(v7_exec(v7, "a(2)") == V7_OK);
  ASSERT(check_num(v7, 4.0));
  ASSERT(v7_exec(v7, "a(0)") == V7_OK);
  ASSERT(check_num(v7, 0.0));

  ASSERT(v7_exec(v7, "function fac(x) { "
         "return x <= 1 ? 1 : x * fac(x - 1); }") == V7_OK);
  ASSERT(v7_exec(v7, "fac(1)") == V7_OK);
  ASSERT(check_num(v7, 1.0));
  ASSERT(v7_exec(v7, "fac(5)") == V7_OK);
  ASSERT(check_num(v7, 120.0));
  ASSERT(v7_exec(v7, "fac(20)") == V7_OK);

  ASSERT(v7_exec(v7, "function qq(a,b) { return a + b; }") == V7_OK);
  ASSERT(v7_exec(v7, "qq(1,2)") == V7_OK);
  ASSERT(check_num(v7, 3.0));

  ASSERT(v7_exec(v7, "1 < 2 == 2 < 3") == V7_OK);
  ASSERT(check_bool(v7, 1.0));

  ASSERT(v7_exec(v7, "5 % 3 * 3") == V7_OK);
  ASSERT(check_num(v7, 6.0));
  ASSERT(v7_exec(v7, "76 & 13") == V7_OK);
  ASSERT(check_num(v7, 12.0));
  ASSERT(v7_exec(v7, "34325 ^ 190992 & 74832") == V7_OK);
  ASSERT(check_num(v7, 42501.0));

  ASSERT(v7_exec(v7, "a = 12;") == V7_OK);
  ASSERT(v7_exec(v7, "a += 44; a;") == V7_OK);
  ASSERT(check_num(v7, 56.0));
  ASSERT(v7_exec(v7, "a -= a / 2; a;") == V7_OK);
  ASSERT(check_num(v7, 28.0));
  ASSERT(v7_exec(v7, "a *= 0.5; a;") == V7_OK);
  ASSERT(check_num(v7, 14.0));
#ifndef _WIN32
  ASSERT(v7_exec(v7, "a /= 0; a;") == V7_OK);
  ASSERT(check_num(v7, INFINITY));
#endif

  ASSERT(v7_exec(v7, "!5") == V7_OK);
  ASSERT(check_bool(v7, 0.0));
  ASSERT(v7_exec(v7, "!''") == V7_OK);
  ASSERT(check_bool(v7, 1.0));
  ASSERT(v7_exec(v7, "1 != 2") == V7_OK);
  ASSERT(check_bool(v7, 1.0));
  ASSERT(v7_exec(v7, "7 >= 0") == V7_OK);
  ASSERT(check_bool(v7, 1.0));

  ASSERT(v7_exec(v7, "if (false) 3; ") == V7_OK);
  ASSERT(v7_exec(v7, "if (true) { if (1) {2;} 5; } ") == V7_OK);
  ASSERT(check_num(v7, 5.0));
  ASSERT(v7_exec(v7, "if ('') 3; ") == V7_OK);
  ASSERT(v7_exec(v7, "if ('0') 9; ") == V7_OK);
  ASSERT(check_num(v7, 9.0));
  ASSERT(v7_exec(v7, "if (false) 1; else 3;") == V7_OK);
  ASSERT(check_num(v7, 3.0));
  ASSERT(v7_exec(v7, "if (false) 1; else if (0) { 3 } else { 2 }") == V7_OK);
  ASSERT(check_num(v7, 2.0));
  ASSERT(v7_exec(v7, "if (false) 1; else if (1) { 3 } else { 2 }") == V7_OK);
  ASSERT(check_num(v7, 3.0));

  ASSERT(v7_exec(v7, "a = 32; 2 + a++;") == V7_OK);
  ASSERT(check_num(v7, 35.0));

  ASSERT(v7_exec(v7, "print(['hi', 1, true, null, /\\s+/])") == V7_OK);

  ASSERT(v7_exec(v7, "a = {};") == V7_OK);
  ASSERT(v7_exec(v7, "a.foo = function(x) { var y = "
                 "x.substr(1).split() }") == V7_OK);

  ASSERT(v7_exec(v7, "typeof 2") == V7_OK);
  ASSERT(check_str(v7, "number"));

  v7_destroy(&v7);
  return NULL;
}
コード例 #20
0
ファイル: unit_test.c プロジェクト: Speedy37/v7
static const char *test_stdlib(void) {
  struct v7 *v7 = v7_create();

  // Number
#ifndef _WIN32
  ASSERT(v7_exec(v7, "Math.PI") == V7_OK);
  ASSERT(check_num(v7, M_PI));
  ASSERT(v7_exec(v7, "Number.NaN") == V7_OK);
  ASSERT(check_num(v7, NAN));
#endif
  ASSERT(v7_exec(v7, "1 == 2") == V7_OK);
  ASSERT(check_bool(v7, 0));
  ASSERT(v7_exec(v7, "1 + 2 * 7 === 15") == V7_OK);
  ASSERT(check_bool(v7, 1));
  ASSERT(v7_exec(v7, "Number(1.23) === 1.23") == V7_OK);
  ASSERT(check_bool(v7, 1));
  ASSERT(v7_exec(v7, "Number(1.23)") == V7_OK);
  ASSERT(check_num(v7, 1.23));
  ASSERT(v7_exec(v7, "new Number(21.23)") == V7_OK);

  // String
  ASSERT(v7_exec(v7, "'hello'.charCodeAt(1)") == V7_OK);
  ASSERT(check_num(v7, 'e'));
  ASSERT(v7_exec(v7, "'hello'.charCodeAt(4)") == V7_OK);
  ASSERT(check_num(v7, 'o'));
  ASSERT(v7_exec(v7, "'hello'.charCodeAt(5) == Number.NaN") == V7_OK);
  ASSERT(check_bool(v7, 1.0));
  ASSERT(v7_exec(v7, "'hello'.indexOf()") == V7_OK);
  ASSERT(check_num(v7, -1.0));
  ASSERT(v7_exec(v7, "'HTTP/1.0\\r\\n'.indexOf('\\r\\n')") == V7_OK);
  ASSERT(check_num(v7, 8.0));
  ASSERT(v7_exec(v7, "'hi there'.indexOf('e')") == V7_OK);
  ASSERT(check_num(v7, 5.0));
  ASSERT(v7_exec(v7, "'hi there'.indexOf('e', 6)") == V7_OK);
  ASSERT(check_num(v7, 7.0));
  ASSERT(v7_exec(v7, "'hi there'.substr(3, 2)") == V7_OK);
  ASSERT(check_str(v7, "th"));
  ASSERT(v7_exec(v7, "'hi there'.substr(3)") == V7_OK);
  ASSERT(check_str(v7, "there"));
  ASSERT(v7_exec(v7, "'hi there'.substr(-2)") == V7_OK);
  ASSERT(check_str(v7, "re"));
  ASSERT(v7_exec(v7, "'hi there'.substr(-20)") == V7_OK);
  ASSERT(check_str(v7, ""));
  ASSERT(v7_exec(v7, "'hi there'.substr(0, 300)") == V7_OK);
  ASSERT(check_str(v7, ""));
  ASSERT(v7_exec(v7, "'dew dee'.match(/\\d+/)") == V7_OK);
  ASSERT(v7->sp == 1 && v7->stack[0]->type == V7_TYPE_NULL);
  ASSERT(v7_exec(v7, "m = 'foo 1234 bar'.match(/\\S+ (\\d+)/)") == V7_OK);
  ASSERT(v7_exec(v7, "m.length") == V7_OK);
  ASSERT(check_num(v7, 2.0));
  ASSERT(v7_exec(v7, "m[0]") == V7_OK);
  ASSERT(check_str(v7, "foo 1234"));
  ASSERT(v7_exec(v7, "m[1]") == V7_OK);
  ASSERT(check_str(v7, "1234"));
  ASSERT(v7_exec(v7, "m[2]") == V7_OK);
  ASSERT(v7->sp == 1 && v7->stack[0]->type == V7_TYPE_UNDEF);
  ASSERT(v7_exec(v7, "m = 'aa bb cc'.split(); m.length") == V7_OK);
  ASSERT(check_num(v7, 1.0));
  ASSERT(v7_exec(v7, "m = 'aa bb cc'.split(''); m.length") == V7_OK);
  ASSERT(check_num(v7, 8.0));
  ASSERT(v7_exec(v7, "m = 'aa bb cc'.split(' '); m.length") == V7_OK);
  ASSERT(check_num(v7, 3.0));
  ASSERT(v7_exec(v7, "m = 'aa bb cc'.split(' ', 2); m.length") == V7_OK);
  ASSERT(check_num(v7, 2.0));
  ASSERT(v7_exec(v7, "m = 'aa bb cc'.split(/ /, 2); m.length") == V7_OK);
  ASSERT(check_num(v7, 2.0));
  ASSERT(v7_exec(v7, "'aa bb cc'.substr(0, 4).split(' ').length") == V7_OK);
  ASSERT(check_num(v7, 2.0));
  ASSERT(v7_exec(v7, "'aa bb cc'.substr(0, 4).split(' ')[1]") == V7_OK);
  ASSERT(check_str(v7, "b"));
  ASSERT(v7_exec(v7, "{z: '123456'}.z.substr(0, 3).split('').length") == V7_OK);
  ASSERT(check_num(v7, 3.0));
  ASSERT(v7_exec(v7, "String('hi')") == V7_OK);
  ASSERT(check_str(v7, "hi"));
  ASSERT(v7_exec(v7, "new String('blah')") == V7_OK);

  // Math
  ASSERT(v7_exec(v7, "Math.sqrt(144)") == V7_OK);
  ASSERT(check_num(v7, 12.0));

  // Regexp
  ASSERT(v7_exec(v7, "re = /GET (\\S+) HTTP/") == V7_OK);

  v7_destroy(&v7);
  return NULL;
}
コード例 #21
0
ファイル: unit_test.c プロジェクト: dev-gitter/v7
static const char *test_v7_exec(void) {
  struct v7_val *v;
  struct v7 *v7 = v7_create();

  ASSERT((v = v7_exec(v7, "")) == NULL);
  ASSERT((v = v7_exec(v7, "-2;")) != NULL);
  ASSERT(check_num(v7, v, -2.0));
  ASSERT((v = v7_exec(v7, "3 + 4")) != NULL);
  ASSERT(check_num(v7, v, 7.0));
  ASSERT((v = v7_exec(v7, "123.456")) != NULL);
  ASSERT(check_num(v7, v, 123.456));
  ASSERT((v = v7_exec(v7, "NaN")) != NULL);
  ASSERT(check_num(v7, v, NAN));

  // TODO: fix infinity handling under MSVC6
#ifndef _WIN32
  ASSERT((v = v7_exec(v7, "Infinity")) != NULL);
  ASSERT(check_num(v7, v, INFINITY));
  ASSERT((v = v7_exec(v7, "-Infinity")) != NULL);
  ASSERT(check_num(v7, v, -INFINITY));
#endif

  ASSERT((v = v7_exec(v7, "2()")) == NULL);
  ASSERT((v = v7_exec(v7, " 15 +	2 \r\n * 2  / 1 - 3 * 4 ; ")) != NULL);

  ASSERT((v = v7_exec(v7, "( (5  ) );")) != NULL);
  ASSERT(check_num(v7, v, 5.0));

  ASSERT((v = v7_exec(v7, "(2 + (12 / 4));")) != NULL);
  ASSERT(check_num(v7, v, 5.0));

  ASSERT((v = v7_exec(v7, "1;2 7")) != NULL);
  ASSERT(check_num(v7, v, 7.0));

  v = v7_exec(v7, "a=undefined; a + 5");
  ASSERT(check_num(v7, v, NAN));

  /* ReferenceErrors are not thrown yet, treat as undefined */
  v = v7_exec(v7, "a + 5");
  ASSERT(check_num(v7, v, NAN));

  ASSERT((v = v7_exec(v7, "print();")) != NULL);
  ASSERT((v = v7_exec(v7, "print(this);")) != NULL);

  ASSERT((v = v7_exec(v7, "a = 7;")) != NULL);
  ASSERT(check_num(v7, v, 7.0));
  ASSERT((v = v7_exec(v7, "print(this);")) != NULL);

  ASSERT((v = v7_exec(v7, "b = a + 3;")) != NULL);
  ASSERT(check_num(v7, v, 10.0));
  ASSERT((v = v7_exec(v7, "c = b * (a + 3) / 2;")) != NULL);
  ASSERT(check_num(v7, v, 50.0));
  ASSERT((v = v7_exec(v7, "print(this);")) != NULL);

  ASSERT((v = v7_exec(v7, "var x = 1.23; x")) != NULL);
  ASSERT(check_num(v7, v, 1.23));

  ASSERT((v = v7_exec(v7, "b = 7; a = b *= 4;")) != NULL);

  ASSERT((v = v7_exec(v7, "var x = 12 + 2 - a + b+ 3 / 4 * a; x;")) != NULL);
  ASSERT((v = v7_exec(v7, "x + 1")) != NULL);
  ASSERT((v = v7_exec(v7, "b + 2; x + 3 + 1 z = x -2;")) != NULL);
  ASSERT((v = v7_exec(v7, "x; var y, z;")) != NULL);
  ASSERT((v = v7_exec(v7, "1 2 3")) != NULL);

  ASSERT((v = v7_exec(v7, "var k = true; k ")) != NULL);
  ASSERT(check_bool(v7, v, 1));

  ASSERT((v = v7_exec(v7, "var blah = 'kuku'; blah")) != NULL);
  ASSERT(check_str(v7, v, "kuku"));

  // Test that k.y does exist
  ASSERT((v = v7_exec(v7, "k = { y: 17 };")) != NULL);
  ASSERT((v = v7_exec(v7, "k.y")) != NULL);
  ASSERT(check_num(v7, v, 17.0));
  v7_exec(v7, "print(this);");

  // Delete k.y and make sure it's gone
  ASSERT((v = v7_exec(v7, "delete k.y;")) != NULL);
  ASSERT((v = v7_exec(v7, "k.y;")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_UNDEF);
  ASSERT((v = v7_exec(v7, "delete b; b;")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_UNDEF);

  ASSERT((v = v7_exec(v7, "k = { key1: {x:3}, key2: ':-)', y: 5 };")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_OBJ);

  ASSERT((v = v7_exec(v7, "k.x = 47;")) != NULL);
  ASSERT((v = v7_exec(v7, "k.qwe = { foo: 5 };")) != NULL);
  ASSERT((v = v7_exec(v7, "k.qwe.foo = 15;")) != NULL);

  ASSERT((v = v7_exec(v7, "k.key1.x + 4")) != NULL);
  ASSERT(check_num(v7, v, 7.0));

  ASSERT((v = v7_exec(v7, "k.foo")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_UNDEF);

  ASSERT((v = v7_exec(v7, "var z = 'key1'; k[z]['x']")) != NULL);
  ASSERT(check_num(v7, v, 3.0));

  ASSERT((v = v7_exec(v7, "var stk = 1; stk")) != NULL);
  ASSERT(check_num(v7, v, 1.0));

  ASSERT((v = v7_exec(v7, "var f1 = function(x, y) { }; typeof f1 ")) != NULL);
  ASSERT(check_str(v7, v, "function"));

  ASSERT((v = v7_exec(v7, "var f1 = function(x, y) { return x * y }")) != NULL);
  //ASSERT(v7_is_class(v7_top(v7)[-1], V7_CLASS_FUNCTION));
  ASSERT((v = v7_exec(v7, "f1(2, 3)")) != NULL);
  ASSERT(check_num(v7, v, 6.0));
  ASSERT((v = v7_exec(v7, "f1(12, 4) + 1;")) != NULL);
  ASSERT(check_num(v7, v, 49.0));
  ASSERT((v = v7_exec(v7, "f1(12, 4) * 2;")) != NULL);
  ASSERT(check_num(v7, v, 96.0));

  ASSERT((v = v7_exec(v7, "f = function(x,y,z) {print(this);};")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_OBJ);
  //ASSERT(v7_is_class(v7->stack[0], V7_CLASS_FUNCTION));
  ASSERT((v = v7_exec(v7, "f();")) != NULL);
  ASSERT((v = v7_exec(v7, "f({});")) != NULL);
  ASSERT((v = v7_exec(v7, "f(1, 2);")) != NULL);
  ASSERT((v = v7_exec(v7, "f(123, {});")) != NULL);

  ASSERT((v = v7_exec(v7, "if (0) f1 = 2; ")) == NULL);
  ASSERT((v = v7_exec(v7, "if (5) { f1 = 3; f2 = function(){}; } ")) != NULL);

  ASSERT((v = v7_exec(v7, "0 ? 1 : 2;")) != NULL);
  ASSERT(check_num(v7, v, 2.0));

  ASSERT((v = v7_exec(v7, "k = true ? 1 : 2;")) != NULL);
  ASSERT(check_num(v7, v, 1.0));

  ASSERT((v = v7_exec(v7, "var f = function(){var x=12; return x + 1;};")) != NULL);

  ASSERT((v = v7_exec(v7, "k = f(1,2,3);")) != NULL);
  ASSERT(check_num(v7, v, 13.0));

  ASSERT((v = v7_exec(v7, "(function() { return f() + 7; })()")) != NULL);
  ASSERT(check_num(v7, v, 20.0));

  ASSERT((v = v7_exec(v7, "var a = 1; if (a == 1) { a = 2; }; a;")) != NULL);
  ASSERT(check_num(v7, v, 2.0));

  ASSERT((v = v7_exec(v7, "var a = 'foo'; a == 'foo';")) != NULL);
  ASSERT(check_bool(v7, v, 1));

  ASSERT((v = v7_exec(v7, "a = { x: function(p) { print(this); } }")) != NULL);
  ASSERT((v = v7_exec(v7, "a.x(2);")) != NULL);

  ASSERT((v = v7_exec(v7, "(74).toString()")) != NULL);
  ASSERT(check_str(v7, v, "74"));
  ASSERT((v = v7_exec(v7, "'hello'.length")) != NULL);
  ASSERT(check_num(v7, v, 5.0));

  ASSERT((v = v7_exec(v7, "k = { x : function() { if (1) 2; } }")) != NULL);

  ASSERT((v = v7_exec(v7, "'foo' + 'bar'")) != NULL);
  ASSERT(check_str(v7, v, "foobar"));

  ASSERT((v = v7_exec(v7, "var x = [1, 'foo', true, 7];")) != NULL);
  ASSERT((v = v7_exec(v7, "x.length")) != NULL);
  ASSERT(check_num(v7, v, 4.0));
  ASSERT((v = v7_exec(v7, "x[1]")) != NULL);
  ASSERT(check_str(v7, v, "foo"));

  ASSERT((v = v7_exec(v7, "var f1 = function() { 1; };")) != NULL);
  ASSERT((v = v7_exec(v7, "var f2 = function(x) { if (x) return x; };")) != NULL);
  ASSERT((v = v7_exec(v7, "f1()")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_UNDEF);
  ASSERT((v = v7_exec(v7, "f2(false)")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_UNDEF);
  ASSERT((v = v7_exec(v7, "f2(17)")) != NULL);
  ASSERT(check_num(v7, v, 17.0));
  ASSERT((v = v7_exec(v7, "f2(true)")) != NULL);
  ASSERT(check_bool(v7, v, 1.0));

  ASSERT((v = v7_exec(v7, "1 <= 2 ? 7 : 8")) != NULL);
  ASSERT(check_num(v7, v, 7.0));

  ASSERT((v = v7_exec(v7, "function a (t) { return t * t }; ")) != NULL);
  ASSERT((v = v7_exec(v7, "a(2)")) != NULL);
  ASSERT(check_num(v7, v, 4.0));
  ASSERT((v = v7_exec(v7, "a(0)")) != NULL);
  ASSERT(check_num(v7, v, 0.0));

  ASSERT((v = v7_exec(v7, "function fac(x) { "
         "return x <= 1 ? 1 : x * fac(x - 1); }")) != NULL);
  ASSERT((v = v7_exec(v7, "fac(1)")) != NULL);
  ASSERT(check_num(v7, v, 1.0));
  ASSERT((v = v7_exec(v7, "fac(5)")) != NULL);
  ASSERT(check_num(v7, v, 120.0));
  ASSERT((v = v7_exec(v7, "fac(20)")) != NULL);

  ASSERT((v = v7_exec(v7, "function qq(a,b) { return a + b; }")) != NULL);
  ASSERT((v = v7_exec(v7, "qq(1,2)")) != NULL);
  ASSERT(check_num(v7, v, 3.0));

  ASSERT((v = v7_exec(v7, "1 < 2 == 2 < 3")) != NULL);
  ASSERT(check_bool(v7, v, 1.0));

  ASSERT((v = v7_exec(v7, "5 % 3 * 3")) != NULL);
  ASSERT(check_num(v7, v, 6.0));
  ASSERT((v = v7_exec(v7, "76 & 13")) != NULL);
  ASSERT(check_num(v7, v, 12.0));
  ASSERT((v = v7_exec(v7, "34325 ^ 190992 & 74832")) != NULL);
  ASSERT(check_num(v7, v, 42501.0));

  ASSERT((v = v7_exec(v7, "a = 12;")) != NULL);
  ASSERT((v = v7_exec(v7, "a += 44; a;")) != NULL);
  ASSERT(check_num(v7, v, 56.0));
  ASSERT((v = v7_exec(v7, "a -= a / 2; a;")) != NULL);
  ASSERT(check_num(v7, v, 28.0));
  ASSERT((v = v7_exec(v7, "a *= 0.5; a;")) != NULL);
  ASSERT(check_num(v7, v, 14.0));
#ifndef _WIN32
  ASSERT((v = v7_exec(v7, "a /= 0; a;")) != NULL);
  ASSERT(check_num(v7, v, INFINITY));
#endif

  ASSERT((v = v7_exec(v7, "!5")) != NULL);
  ASSERT(check_bool(v7, v, 0.0));
  ASSERT((v = v7_exec(v7, "!''")) != NULL);
  ASSERT(check_bool(v7, v, 1.0));
  ASSERT((v = v7_exec(v7, "1 != 2")) != NULL);
  ASSERT(check_bool(v7, v, 1.0));
  ASSERT((v = v7_exec(v7, "7 >= 0")) != NULL);
  ASSERT(check_bool(v7, v, 1.0));

  ASSERT((v = v7_exec(v7, "if (false) 3; ")) == NULL);
  ASSERT((v = v7_exec(v7, "if (true) { if (1) {2;} 5; } ")) != NULL);
  ASSERT(check_num(v7, v, 5.0));
  ASSERT((v = v7_exec(v7, "if ('') 3; ")) == NULL);
  ASSERT((v = v7_exec(v7, "if ('0') 9; ")) != NULL);
  ASSERT(check_num(v7, v, 9.0));
  ASSERT((v = v7_exec(v7, "if (false) 1; else 3;")) != NULL);
  ASSERT(check_num(v7, v, 3.0));
  ASSERT((v = v7_exec(v7, "if (false) 1; else if (0) { 3 } else { 2 }")) != NULL);
  ASSERT(check_num(v7, v, 2.0));
  ASSERT((v = v7_exec(v7, "if (false) 1; else if (1) { 3 } else { 2 }")) != NULL);
  ASSERT(check_num(v7, v, 3.0));

  ASSERT((v = v7_exec(v7, "a = 32; 2 + a++;")) != NULL);
  ASSERT(check_num(v7, v, 34.0));

  ASSERT((v = v7_exec(v7, "print()")) != NULL);
  ASSERT((v = v7_exec(v7, "print(['hi', 1, true, null, /\\s+/])")) != NULL);

  ASSERT((v = v7_exec(v7, "a = {};")) != NULL);
  ASSERT((v = v7_exec(v7, "a.foo = function(x) { var y = "
                 "x.substr(1).split() }")) != NULL);

  ASSERT((v = v7_exec(v7, "typeof 2")) != NULL);
  ASSERT(check_str(v7, v, "number"));

  ASSERT((v = v7_exec(v7, "a = { b: { k: 44 } };")) != NULL);
  STOP = 1;
  ASSERT((v = v7_exec(v7, "a.b['x'] = 79;")) != NULL);
  ASSERT((v = v7_exec(v7, "a.b.x")) != NULL);
  ASSERT(check_num(v7, v, 79.0));

  v7_destroy(&v7);
  return NULL;
}
コード例 #22
0
ファイル: unit_test.c プロジェクト: dev-gitter/v7
static const char *test_stdlib(void) {
  struct v7_val *v;
  struct v7 *v7 = v7_create();

  // Number
#ifndef _WIN32
  ASSERT((v = v7_exec(v7, "Math.PI")) != NULL);
  ASSERT(check_num(v7, v, M_PI));
  ASSERT((v = v7_exec(v7, "Number.NaN")) != NULL);
  ASSERT(check_num(v7, v, NAN));
#endif
  ASSERT((v = v7_exec(v7, "1 == 2")) != NULL);
  ASSERT(check_bool(v7, v, 0));
  ASSERT((v = v7_exec(v7, "1 + 2 * 7 === 15")) != NULL);
  ASSERT(check_bool(v7, v, 1));
  ASSERT((v = v7_exec(v7, "Number(1.23) === 1.23")) != NULL);
  ASSERT(check_bool(v7, v, 1));
  ASSERT((v = v7_exec(v7, "Number(1.23)")) != NULL);
  ASSERT(check_num(v7, v, 1.23));
#ifdef TODO /* New operator: Assertion failed: (v7->root_scope.proto == &s_global), function do_exec, file src/util.c, line 557. */
  ASSERT((v = v7_exec(v7, "new Number(21.23)")) != NULL);
#endif

  // String
  ASSERT((v = v7_exec(v7, "'hello'.charCodeAt(1)")) != NULL);
  ASSERT(check_num(v7, v, 'e'));
  ASSERT((v = v7_exec(v7, "'hello'.charCodeAt(4)")) != NULL);
  ASSERT(check_num(v7, v, 'o'));
  ASSERT((v = v7_exec(v7, "'hello'.charCodeAt(5) == Number.NaN")) != NULL);
  ASSERT(check_bool(v7, v, 1.0));
  ASSERT((v = v7_exec(v7, "'hello'.indexOf()")) != NULL);
  ASSERT(check_num(v7, v, -1.0));
  ASSERT((v = v7_exec(v7, "'HTTP/1.0\\r\\n'.indexOf('\\r\\n')")) != NULL);
  ASSERT(check_num(v7, v, 8.0));
  ASSERT((v = v7_exec(v7, "'hi there'.indexOf('e')")) != NULL);
  ASSERT(check_num(v7, v, 5.0));
  ASSERT((v = v7_exec(v7, "'hi there'.indexOf('e', 6)")) != NULL);
  ASSERT(check_num(v7, v, 7.0));
  ASSERT((v = v7_exec(v7, "'hi there'.substr(3, 2)")) != NULL);
  ASSERT(check_str(v7, v, "th"));
  ASSERT((v = v7_exec(v7, "'hi there'.substring(3, 5)")) != NULL);
  ASSERT(check_str(v7, v, "th"));
  ASSERT((v = v7_exec(v7, "'hi there'.substr(3)")) != NULL);
  ASSERT(check_str(v7, v, "there"));
  ASSERT((v = v7_exec(v7, "'hi there'.substr(-2)")) != NULL);
  ASSERT(check_str(v7, v, "hi there"));
  ASSERT((v = v7_exec(v7, "'hi there'.substr(NaN)")) != NULL);
  ASSERT(check_str(v7, v, "hi there"));
  ASSERT((v = v7_exec(v7, "'hi there'.substr(0, 300)")) != NULL);
  ASSERT(check_str(v7, v, "hi there"));
  ASSERT((v = v7_exec(v7, "'dew dee'.match(/\\d+/)")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_NULL);
  ASSERT((v = v7_exec(v7, "m = 'foo 1234 bar'.match(/\\S+ (\\d+)/)")) != NULL);
  ASSERT((v = v7_exec(v7, "m.length")) != NULL);
#ifdef TODO /* got: [foo 1234] i.e. 1 length array with input string !! */
  ASSERT(check_num(v7, v, 2.0));
  ASSERT((v = v7_exec(v7, "m[0]")) != NULL);
  ASSERT(check_str(v7, v, "foo 1234"));
  ASSERT((v = v7_exec(v7, "m[1]")) != NULL);
  ASSERT(check_str(v7, v, "1234"));
  ASSERT((v = v7_exec(v7, "m[2]")) != NULL);
  ASSERT(v7_type(v) == V7_TYPE_UNDEF);
#endif
  ASSERT((v = v7_exec(v7, "m = 'aa bb cc'.split(); m.length")) != NULL);
  ASSERT(check_num(v7, v, 1.0));
#ifdef TODO  /* doesn't split at every char */
  ASSERT((v = v7_exec(v7, "m = 'aa bb cc'.split(''); m.length")) != NULL);
  ASSERT(check_num(v7, v, 8.0));
#endif
  ASSERT((v = v7_exec(v7, "m = 'aa bb cc'.split(' '); m.length")) != NULL);
  ASSERT(check_num(v7, v, 3.0));
  ASSERT((v = v7_exec(v7, "m = 'aa bb cc'.split(' ', 2); m.length")) != NULL);
  ASSERT(check_num(v7, v, 2.0));
  ASSERT((v = v7_exec(v7, "m = 'aa bb cc'.split(/ /, 2); m.length")) != NULL);
  ASSERT(check_num(v7, v, 2.0));
  ASSERT((v = v7_exec(v7, "'aa bb cc'.substr(0, 4).split(' ').length")) != NULL);
  ASSERT(check_num(v7, v, 2.0));
  ASSERT((v = v7_exec(v7, "'aa bb cc'.substr(0, 4).split(' ')[1]")) != NULL);
  ASSERT(check_str(v7, v, "b"));
#ifdef TODO  /* doesn't split at every char */
  ASSERT((v = v7_exec(v7, "{z: '123456'}.z.substr(0, 3).split('').length")) != NULL);
  ASSERT(check_num(v7, v, 3.0));
#endif
  ASSERT((v = v7_exec(v7, "String('hi')")) != NULL);
  ASSERT(check_str(v7, v, "hi"));
#ifdef TODO /* New operator: Assertion failed: (v7->root_scope.proto == &s_global), function do_exec, file src/util.c, line 557. */
  ASSERT((v = v7_exec(v7, "new String('blah')")) != NULL);
#endif

  // Math
  ASSERT((v = v7_exec(v7, "Math.sqrt(144)")) != NULL);
  ASSERT(check_num(v7, v, 12.0));

  // Regexp
  ASSERT((v = v7_exec(v7, "re = /GET (\\S+) HTTP/; re")) != NULL);
  ASSERT((v = v7_exec(v7, "re = /GET (\\S+) HTTP/;")) != NULL);
  ASSERT((v = v7_exec(v7, "re = /GET (\\S+) HTTP/ ")) != NULL);
  ASSERT((v = v7_exec(v7, "re = /GET (\\S+) HTTP/\n")) != NULL);
  ASSERT((v = v7_exec(v7, "re = /GET (\\S+) HTTP/")) != NULL);

  v7_destroy(&v7);
  return NULL;
}