Skip to content

Commit b341f98

Browse files
committed
update: test && README
1 parent d024508 commit b341f98

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# QuickJS Python 扩展
22

33
quickjs 已经出来一段时间了,因其主要目标是linux,在windows上开发编译不是很友好.
4-
54
恰好最近有空研究了下,如果只运行js代码,只需要一部分代码就能编译出来.
6-
75
因为有在python里运行js的需求,所以顺手写了个扩展,经过测试,能正常运行.
6+
由于技术能力和精力有限,细节处理还不是很好,后面慢慢完善.
87

9-
由于技术能力和精力有限,细节处理还不是很好,后面慢慢完善,也希望有大佬指点.
10-
8+
这是我第一个开源项目,希望能帮助到大家,如果对你有帮助,麻烦给个star.
9+
同时,也希望各位能对项目存在的问题 pull request, 感谢您的使用 !!!
1110

1211
## quickjs 说明
1312

@@ -37,11 +36,38 @@ examples_use_module.exe
3736
3837
```
3938

39+
## Python 扩展编译及使用
40+
41+
扩展源码位于pyext目录,目前只进行了简单封装,支持一个函数 `eval_js(code)`
42+
43+
`MinGW Command Prompt` 里运行`python setup.py build -f -c mingw32`即可编译(python3.6版本已经编译好)
44+
45+
编译后的pyd文件在 `build\lib.win-amdxx-3.x` 将其拷贝到pyaxt目录 同时把libqjs.dll toolm.dll 文件也拷贝到该目录.
4046

47+
执行 `python test.py` 即可.
4148

4249

50+
### 注意事项
4351

52+
因为quickjs 执行有两种模式 `JS_EVAL_TYPE_MODULE``JS_EVAL_TYPE_GLOBAL`
53+
54+
global能直接获取返回值, 如果文件有导入语句,会以module模式运行,无法获取返回值,所以要获取返回值需要按以下格式书写:
55+
56+
```
57+
import xxx
58+
59+
((G)=>{
60+
...... // your code
61+
const result = XXX;
62+
G._r = result;
63+
})(globalThis);
64+
65+
```
66+
该方式将返回值放到全局变量 `_r` 执行完后自动将该值传回py
4467

68+
## 存在问题
4569

70+
1. python扩展加载js module(dll) 还需要依赖libqjs
71+
2. ...
4672

4773

pyext/fib_module.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* fib module */
2+
export function fib(n)
3+
{
4+
if (n <= 0)
5+
return 0;
6+
else if (n == 1)
7+
return 1;
8+
else
9+
return fib(n - 1) + fib(n - 2);
10+
}

pyext/test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
code1 = '(()=>{return Date.now();})();'
1616

1717
mod_test = '''import * as m from '../lib/toolm.dll'
18+
import { fib } from "./fib_module.js";
1819
1920
console.log("Hello World 你好");
2021
2122
console.log(m.echo("Hello , 哈哈"));
2223
23-
console.log(m.md5('123456'));'''
24+
console.log(m.md5('123456'));
25+
console.log("fib(10)=", fib(10));'''
26+
27+
# ==================================
2428

2529
print("work dir:", os.getcwd())
2630
print(qjs.eval_js(code))

0 commit comments

Comments
 (0)