【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】


    很多同学都熟悉javascript语言,却很少有同学感兴趣jvm是怎么实现的。现在正好网上有一个js v7的开源项目,大家可以将代码下载下来,好好编译查看一下。


1、下载代码

https://github.com/cesanta/v7


2、解压文件

一般双击压缩包即可


3、在v7.c的最后添加如下代码

struct mg_str mg_mk_str(const char *s) {
  struct mg_str ret = {s, 0};
  if (s != NULL) ret.len = strlen(s);
  return ret;
}

struct mg_str mg_mk_str_n(const char *s, size_t len) {
  struct mg_str ret = {s, len};
  return ret;
}

const char *mg_strchr(const struct mg_str s, int c) {
  size_t i;
  for (i = 0; i < s.len; i++) {
    if (s.p[i] == c) return &s.p[i];
  }
  return NULL;
}

4、编译,测试

make


5、查看输出,确认编译成功

mac-pro$ make
cc v7.c -o v7 -DV7_EXE -W -Wall -pedantic -Wno-comment -Wno-variadic-macros -Wno-unused-function -g -O3 -lm -DCS_ENABLE_UTF8   -lm
rm -rf */*/Debug */*/Release */*/.launches */*/.settings */*/.xdchelp */*/src
cc call_c_from_js.c main.c ../../v7.c -o call_c_from_js -W -Wall -I../..   -lm
cc call_js_from_c.c main.c ../../v7.c -o call_js_from_c -W -Wall -I../..   -lm
cc -o libcall_js_from_c.so -W -Wall -I../..   -shared -fpic ../call_js_from_c/call_js_from_c.c ../../v7.c
cc ../call_js_from_c/main.c -o main -W -Wall -I../..   -lm -lcall_js_from_c -L.
cc js_oop.c main.c ../../v7.c -o js_oop -W -Wall -I../..   -lm
cc load_json_config.c main.c ../../v7.c -o load_json_config -W -Wall -I../..   -lm
make[2]: Nothing to be done for `all'.
cc tests/unit_test.c tests/common/test_util.c -W -Wall -pedantic -Wno-comment -Wno-variadic-macros -Wno-unused-function -g -O3 -lm -DCS_ENABLE_UTF8   -DV7_EXPOSE_PRIVATE -DV7_UNIT_TEST -DV7_FILENAMES_SUPPRESS_CFUNC_ADDR -lm -o tests/unit_test
cd tests; TZ=UTC ./unit_test 
  [0.000] test_unescape
  [0.002] test_to_json
  [0.001] test_json_parse
  [0.000] test_tokenizer
  [0.000] test_string_encoding
  [0.001] test_is_true
  [0.000] test_closure
  [0.000] test_native_functions
  [0.003] test_stdlib
  [0.001] test_runtime
  [0.001] test_apply
  [0.120] test_parser
  [0.014] test_parser_large_ast
  [0.007] test_interpreter
  [0.001] test_interp_unescape
  [0.001] test_strings
  [0.001] test_gc_ptr_check
  [0.003] test_gc_mark
  [0.001] test_gc_sweep
  [0.001] test_gc_own
  [0.001] test_user_data
foo 
  [0.007] test_exec_generic
ECMA tests coverage: 60.68% (6743 of 11112)
  [35.212] test_ecmac
PASS, run 25807 in 35.377s


6、根据makefile的提示打印信息,进一步学习和整理


Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐