TangledHub QuickJs 包装库
项目描述
thquickjs
概述
TangledHub 库,用于为 JavaScript 和 Python 代码创建上下文。QuickJS 是一个小型且可嵌入的 Javascript 引擎。安全地评估不受信任的 Javascript。在 QuickJS 运行时中创建和操作值。向 QuickJS 运行时公开宿主函数。
许可
thquickjs 在 BSD 许可下获得许可。检查许可证以获取详细信息
安装
pip install thquickjs
测试
docker-compose build thquickjs-test ; docker-compose run --rm thquickjs-test
建造
docker-compose build thquickjs-build ; docker-compose run --rm thquickjs-build
发布
docker-compose build thquickjs-publish ; docker-compose run --rm -e PYPI_USERNAME=__token__ -e PYPI_PASSWORD=__SECRET__ thquickjs-publish
用法
评估 JavaScript 代码
创建 QuickJS 的实例。使用该实例来评估 JavaScript 代码。JavaScript 代码可以在字符串或文件中。可以从 quickjs 上下文访问 JavaScript 代码(变量、函数)。
'''
setup and create instance of QuickJS object
- eval code function evaluate JS code
- get function obtain a function from quickjs context
- js function call in python code
params:
eval:
code: string(js code)
get:
function_name: str
returns:
get:
function: js function
'''
from thquickjs import QuickJS
# create QuckJS object
qjs: QuickJS = QuickJS()
# example js code
code = '''
f = function(x) {
return 40 + x;
}
f1 = function(x, y) {
return x + y;
}
'''
# evaluate JS code
qjs.eval(code).unwrap()
# obtain a function from quickjs context
func = qjs.get('f1').unwrap() # js function in python
# invoke function from JS code in Python code
result = func(2, 3) # returns 5
JavaScript 代码可以在单独的文件中。请注意,文件扩展名不必是.js。
'''
setup and create instance of QuickJS object
- eval code function evaluate JS code from file
- get function obtain a function from quickjs context
- js function call in python code
params:
eval:
code: string(js code)
get:
function_name: str
returns:
get:
function: js function
'''
from thquickjs import QuickJS
# create QuckJS object
qjs = QuickJS()
# value of code is content of file
code = '''
f = function(x) {
return 40 + x;
}
f1 = function(x, y) {
return x + y;
}
'''
file_name: str = 'abc.txt'
with open(file_name, 'r') as reader:
content = reader.read()
# evaluate JS code
qjs.eval(content).unwrap()
# obtain a function from quickjs context
func = qjs.get('f1').unwrap() # js function in python
# invoke function from JS code in Python code
result = func(2, 3) # returns 5
尝试从上下文中获取不存在的变量将返回 None。
from thquickjs import QuickJS
# create QuckJS object
qjs = QuickJS()
# example js code
code = '''
f = function(x) {
return 40 + x;
}
f1 = function(x, y) {
return x + y;
}
'''
# evaluate JS code
qjs.eval(code).unwrap()
# try to get non-existing variable from context will return None
func = qjs.get('a').unwrap() # None
解析js模块
# create QuckJS object
qjs = QuickJS()
# import lodash
path = os.path.join('/deps', 'thquickjs', 'vendor', 'lodash.js')
# import module by specified path
qjs.import_js_module(path).unwrap()
# use lodash
code = '''
var a = _.range(10);
a;
'''
# evaluate JS code
res = qjs.eval(code, as_json=True).unwrap()
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
将值添加到 thquickjs 上下文
可以在 thquickjs 上下文中添加变量。
from thquickjs import QuickJS
# create QucikJS object
qjs: QuickJS = QuickJS()
# set variable and value to context
qjs.set('x', 8).unwrap()
# get value by given variable name
v = qjs.get('x').unwrap() # v is 8
# change value of variable x in context
qjs.set('x', 12).unwrap()
# get value by given variable name
v = qjs.get('x').unwrap() # v is 12
在 thquickjs 上下文中添加 Python 可调用对象
from thquickjs import QuickJS
# create QucikJS object
qjs = QuickJS()
py_name = 'pylam'
py_func = lambda x: x * 10
# adding Python callable in context
qjs.add_callable(py_name, py_func).unwrap()
# get function by given variable name
f = qjs.get(py_name).unwrap() # v is 8
# result
result = f(5) # returns value of 50
处理错误
在前面的示例中,使用 unwrap() 将返回适当的值或引发异常。使用unwrap_or(v: Any)或unwrap_value()将防止出现异常并终止程序。
解析 JavaScript 代码时处理错误
from _quickjs import Object, JSException
from thquickjs import QuickJS
# create QuckJS object
qjs = QuickJS()
# example of unparsable js code
code = '''unparsable js code'''
# standard way to handle exception using try and except blocks
try:
# evaluate unparsable JS code
qjs_func: Object = qjs.eval(code).unwrap()
except JSException as e:
pass
# evaluation of js code and unwraps_value - returns exception as string
qjs_func = qjs.eval(code).unwrap_value()
# unwrap_or method sets default value in case of exception
# qjs_func in case of exception will have default value
qjs_func = qjs.eval(code).unwrap_or('default value in case of exception')
添加 Python 可调用对象时处理错误
from thquickjs import QuickJS
## handling errors
# create QucikJS object
qjs: QuickJS = QuickJS()
# handling error with try/except
def add_ten(n: int) -> int:
return n + 10
py_name = 'pylam'
try:
# add callable to context
f = qjs.add_callable(py_name, 'unparsable').unwrap()
except TypeError as e:
f = add_ten
# handling error with unwrap_value
def add_two(n: int) -> int:
return n + 2
py_name = 'pylam'
# in case of Err, variable f will contain error message
f = qjs.add_callable(py_name, 'unparsable').unwrap_value()
# in case of Err, add_two function will be assigned to variable f
f = qjs.add_callable(py_name, 'unparsable').unwrap_or(add_two)
f(3) # result 5
在没有异常的情况下使用 unwrap_value() 和 unwrap_or(v: Any) 对返回值没有影响。
from thquickjs import QuickJS
# create QucikJS object
qjs = QuickJS()
# set variable and value to context
qjs.set('x', 8).unwrap()
# get value by given variable name
v = qjs.get('x').unwrap() # v is 8
# change value of variable x in context
qjs.set('x', 12).unwrap()
# get value by given variable name
v = qjs.get('x').unwrap() # v is 12
# if unwrap doesn't rise Exception, unwrap_value will return expected value
v1 = qjs.get('x').unwrap_value() # v1 is 12
# if unwrap doesn't rise Exception, unwrap_or will have no effects on return value
v2 = qjs.get('x').unwrap_or(11) # v2 is 12
设置内存限制
使用set_memory_limit方法为 thquickjs 设置可用内存。要获取有关内存的信息,请使用内存方法。
qjs: QuickJS = QuickJS()
qjs.set_memory_limit(memory_limit = 1024000)
# get informations about memory - used, available etc.
res: dict = qjs.memory()
设定时间限制
要在上下文中设置时间限制,请使用set_time_limit方法。
qjs = QuickJS()
qjs.set_time_limit(time_limit = 600)
测试
在控制台中运行:
docker-compose build thquickjs-test ; docker-compose run --rm thquickjs-test
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
thquickjs-0.9.2.tar.gz
(6.1 kB
查看哈希)
内置分布
thquickjs-0.9.2-py3-none-any.whl
(5.4 kB
查看哈希)