Skip to content

Commit 7ecdbda

Browse files
committed
add: libqjs and module
1 parent d5d4ce7 commit 7ecdbda

File tree

10 files changed

+548
-1
lines changed

10 files changed

+548
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
obj/
22

3+
*.dll
4+
*.exe
35

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
11
# QuickJS Python 扩展
22

3-
...
3+
quickjs 已经出来一段时间了,因其主要目标是linux,在windows上开发编译不是很友好.
4+
5+
恰好最近有空研究了下,如果只运行js代码,只需要一部分代码就能编译出来.
6+
7+
因为有在python里运行js的需求,所以顺手写了个扩展,经过测试,能正常运行.
8+
9+
由于技术能力和精力有限,细节处理还不是很好,后面慢慢完善,也希望有大佬指点.
10+
11+
12+
## quickjs 说明
13+
14+
本项目依赖于 [QuickJS Javascript Engine](https://bellard.org/quickjs/)
15+
16+
在Windows平台上完成编译, 目前版本为 2020-01-19.
17+
18+
使用到的源码文件(暂不考虑大数运算): `quickjs.c libunicode.c quickjs-libc.c libregexp.c cutils.c`
19+
20+
21+
## 编译 quickjs 动态链接库
22+
23+
win10系统 TDM-GCC-64编译器
24+
25+
*libqjs.c 文件 实现了 `eval_buf``eval_file` 函数*
26+
27+
`MinGW Command Prompt` 里依次运行 `libqjs_compile.bat` `module_ex_compile.bat` 即可完成编译
28+
29+
生成的dll在lib文件夹里, 例程在bin文件夹下
30+
31+
在bin文件夹下打开cmd, 执行下面命令即可看到结果(中文乱码是终端问题,git-shell显示正常).
32+
33+
```
34+
examples_libqjs_eval_file.exe ..\examples\hello.js
35+
36+
examples_use_module.exe
37+
38+
```
39+
40+
41+
42+
43+
444

545

646

examples/hello.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as m from './toolm.dll'
2+
3+
console.log("Hello World 你好");
4+
5+
console.log(m.echo("Hello , 哈哈"));
6+
7+
console.log(m.md5('123456'));

examples/libqjs_eval_file.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
4+
#include "libqjs.h"
5+
6+
7+
int main(int argc, char **argv){
8+
if (argc < 2){
9+
printf("no input file.\n");
10+
exit(1);
11+
}
12+
JSRuntime *rt;
13+
JSContext *ctx;
14+
15+
rt = JS_NewRuntime();
16+
ctx = JS_NewContext(rt);
17+
18+
/* loader for ES6 modules */
19+
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
20+
21+
js_std_add_helpers(ctx, 0, NULL); // int argc, char **argv
22+
23+
/* system modules */
24+
js_init_module_std(ctx, "std");
25+
js_init_module_os(ctx, "os");
26+
27+
eval_file(ctx, argv[1], -1);
28+
29+
js_std_free_handlers(rt);
30+
JS_FreeContext(ctx);
31+
JS_FreeRuntime(rt);
32+
33+
}

examples/use-module.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <string.h>
2+
3+
#include "libqjs.h"
4+
5+
JSModuleDef *js_init_module_toolm(JSContext *ctx, const char *module_name);
6+
7+
int main(){
8+
JSRuntime *rt;
9+
JSContext *ctx;
10+
11+
char s1[255] = "console.log('Hello World');\n"
12+
"console.log(std.getenv('windir'));"
13+
"let a=2;a=m.add(a,3);\n"
14+
"console.log('result:',a); console.log(m.echo('vcmnbxvnmbc..'));";
15+
16+
const char *str = "import * as std from 'std';\n"
17+
"import * as os from 'os';\n"
18+
"import * as m from 'm';\n"
19+
"globalThis.std = std;\n"
20+
"globalThis.os = os;\n"
21+
"globalThis.m = m;\n";
22+
23+
rt = JS_NewRuntime();
24+
ctx = JS_NewContext(rt);
25+
26+
/* loader for ES6 modules */
27+
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
28+
29+
js_std_add_helpers(ctx, 0, NULL); // int argc, char **argv
30+
31+
/* system modules */
32+
js_init_module_std(ctx, "std");
33+
js_init_module_os(ctx, "os");
34+
35+
36+
// lib
37+
js_init_module_toolm(ctx, "m");
38+
39+
eval_buf(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE);
40+
41+
eval_buf(ctx, s1, strlen(s1), "<input>", JS_EVAL_TYPE_GLOBAL);
42+
43+
js_std_free_handlers(rt);
44+
JS_FreeContext(ctx);
45+
JS_FreeRuntime(rt);
46+
47+
}

lib/libqjs.lib

557 KB
Binary file not shown.

0 commit comments

Comments
 (0)