Skip to content

Commit 9398f40

Browse files
committed
refact
1 parent b341f98 commit 9398f40

File tree

9 files changed

+114
-44
lines changed

9 files changed

+114
-44
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
obj/
2+
build/
3+
qjs.egg-info/
4+
dist/
25

3-
pyext/build/
46
*.dll
57
*.exe
68

File renamed without changes.

README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,55 @@ quickjs 已经出来一段时间了,因其主要目标是linux,在windows上开
1010

1111
## quickjs 说明
1212

13-
本项目依赖于 [QuickJS Javascript Engine](https://bellard.org/quickjs/)
14-
15-
在Windows平台上完成编译, 目前版本为 2020-01-19.
13+
本项目依赖于 [QuickJS Javascript Engine](https://bellard.org/quickjs/) 目前版本为 2020-01-19.
1614

1715
使用到的源码文件(暂不考虑大数运算): `quickjs.c libunicode.c quickjs-libc.c libregexp.c cutils.c`
1816

17+
如只想用c库,请参考 [编译指导](compile_lib.md)
1918

20-
## 编译 quickjs 动态链接库
19+
## 安装 && 使用
2120

22-
win10系统 TDM-GCC-64编译器
21+
### Windows
2322

24-
*libqjs.c 文件 实现了 `eval_buf``eval_file` 函数*
23+
目前预编译了3.6 3.7, 其他版本自行编译参考 [编译指导](compile_lib.md)
2524

26-
`MinGW Command Prompt` 里依次运行 `libqjs_compile.bat` `module_ex_compile.bat` 即可完成编译
25+
下载 qjs-1.0-cpXX-cpXXm-win_amd64.whl
2726

28-
生成的dll在lib文件夹里, 例程在bin文件夹下
27+
执行 `pip install qjs-xxx.whl` 完成安装
2928

30-
在bin文件夹下打开cmd, 执行下面命令即可看到结果(中文乱码是终端问题,git-shell显示正常).
29+
### Linux
3130

3231
```
33-
examples_libqjs_eval_file.exe ..\examples\hello.js
32+
git clone https://github.com/lcexc/py-quickjs.git
3433
35-
examples_use_module.exe
34+
cd py-quickjs
3635
37-
```
36+
sudo python setup.py install
3837
39-
## Python 扩展编译及使用
4038
41-
扩展源码位于pyext目录,目前只进行了简单封装,支持一个函数 `eval_js(code)`
39+
```
40+
### 使用
4241

43-
`MinGW Command Prompt` 里运行`python setup.py build -f -c mingw32`即可编译(python3.6版本已经编译好)
42+
目前只进行了简单封装,支持一个函数 `eval_js(code)`
4443

45-
编译后的pyd文件在 `build\lib.win-amdxx-3.x` 将其拷贝到pyaxt目录 同时把libqjs.dll toolm.dll 文件也拷贝到该目录.
44+
```
45+
import qjs
4646
47-
执行 `python test.py` 即可.
47+
js_code = '''
48+
(()=>{
49+
console.log("Hello World");
50+
return "Current TimeStamp: " + Date.now()
51+
})();
52+
'''
4853
54+
rt = qjs.eval_js(js_code)
55+
print(rt)
56+
57+
```
4958

5059
### 注意事项
5160

5261
因为quickjs 执行有两种模式 `JS_EVAL_TYPE_MODULE``JS_EVAL_TYPE_GLOBAL`
53-
5462
global能直接获取返回值, 如果文件有导入语句,会以module模式运行,无法获取返回值,所以要获取返回值需要按以下格式书写:
5563

5664
```
@@ -65,6 +73,10 @@ import xxx
6573
```
6674
该方式将返回值放到全局变量 `_r` 执行完后自动将该值传回py
6775

76+
**test.py 里有几个例子,如果使用第三方js库(dll) 需要把lib下的libqjs.dll放到test.py同目录**
77+
78+
出现类似问题 `could not load module filename 'xxx.dll' as shared library, ERR code: 126` 是dll导入失败,请仔细检查路径
79+
6880
## 存在问题
6981

7082
1. python扩展加载js module(dll) 还需要依赖libqjs

compile_lib.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 编译指导
2+
3+
## 编译 quickjs 动态链接库(windows)
4+
5+
环境: win10系统 TDM-GCC-64编译器
6+
7+
*libqjs.c 文件 实现了 `eval_buf``eval_file` 函数*
8+
9+
`MinGW Command Prompt` 里依次运行 `libqjs_compile.bat` `module_ex_compile.bat` 即可完成编译
10+
11+
生成的dll在lib文件夹里, 例程在bin文件夹下
12+
13+
在bin文件夹下打开cmd, 执行下面命令即可看到结果(中文乱码是终端问题,git-shell显示正常).
14+
15+
```
16+
examples_libqjs_eval_file.exe ..\examples\hello.js
17+
18+
examples_use_module.exe
19+
20+
```
21+
22+
## Python 扩展编译
23+
24+
目前只测试了3.6+版本(3.8 lib链接失败)
25+
26+
### Windows
27+
`MinGW Command Prompt` 里运行`python setup.py build -f -c mingw32`即可编译
28+
29+
编译后的pyd文件在 `build\lib.win-amdxx-3.x`.
30+
31+
执行 `python setup.py install` 即可完成安装
32+
33+
### Linux
34+
35+
```
36+
git clone https://github.com/lcexc/py-quickjs.git
37+
38+
cd py-quickjs
39+
40+
sudo python setup.py install
41+
42+
43+
```
44+
45+

pyext/qjs.cp36-win_amd64.pyd

-683 KB
Binary file not shown.

pyext/setup.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
from setuptools import setup, Extension
3+
4+
srcs = ['QJS.c', "src/cutils.c","src/libregexp.c","src/libunicode.c","src/quickjs.c","src/quickjs-libc.c"]
5+
extra_compile_args = ['-flto','-Wno-array-bounds', '-Wno-format-truncation','-w' ]
6+
extra_link_args = ["-Wl,-Bstatic"]
7+
8+
# python setup.py build -f -c mingw32
9+
# python setup.py bdist_wheel
10+
11+
if sys.platform == 'win32':
12+
import distutils.cygwinccompiler
13+
distutils.cygwinccompiler.get_msvcr = lambda: []
14+
15+
CONFIG_VERSION='\\"2020-01-19\\"'
16+
17+
else:
18+
CONFIG_VERSION='\"2020-01-19\"'
19+
extra_link_args = []
20+
21+
22+
mod_qjs = Extension('qjs', srcs ,
23+
define_macros=[('CONFIG_VERSION', CONFIG_VERSION), ('_GNU_SOURCE','')],
24+
include_dirs = ['include'],
25+
extra_compile_args=extra_compile_args,
26+
extra_link_args = extra_link_args )
27+
28+
setup(name='qjs',
29+
version='1.0',
30+
ext_modules=[mod_qjs, ])
31+
32+

pyext/test.py renamed to test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
code = '''import * as std from 'std';
55
6-
76
((G)=>{
87
const sys_path = std.getenv('PATH');
98
console.log("run .....");
@@ -14,8 +13,8 @@
1413

1514
code1 = '(()=>{return Date.now();})();'
1615

17-
mod_test = '''import * as m from '../lib/toolm.dll'
18-
import { fib } from "./fib_module.js";
16+
mod_test = '''import * as m from './lib/toolm.dll'
17+
import { fib } from "./examples/fib_module.js";
1918
2019
console.log("Hello World 你好");
2120
@@ -30,7 +29,8 @@
3029
print(qjs.eval_js(code))
3130
print(qjs.eval_js(code1))
3231

33-
print(qjs.eval_js(mod_test))
32+
# need libqjs.dll
33+
# print(qjs.eval_js(mod_test))
3434

3535

3636

0 commit comments

Comments
 (0)