quick.py 2.9 KB
Newer Older
1 2
from quickjs import Function,Context
import requests
H
hjdhnx 已提交
3 4
from time import time
import js2py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
import json
ctx = Context()
ctx.add_callable("print", print)
def print2(text):
    print('print2:',text)
# ctx.set('print2',print2)
ctx.add_callable("print2", print2)
# ctx.add_callable("ua", 'mobile')
gg="""
    function adder(a, b) {
        c=[1,2].filter(it=>it>1);
        print(c);
        print(ua);
        print(c.join('$'))
        print(typeof c)
        print(Array.isArray(c))
        return a + b+`你这个a是${a},c是${c}`;
    }
    
    function bd(){
    let html = request('https://www.baidu.com/')
    }
    
    
    function f(a, b) {
        let e = print2;
        e(2222);
        return adder(a, b);
    }
    var d = 123;
    print2('我是注入的');
    print2(typeof(ccc));
    var gs = {a:1};
    print2(gs)
    // print2(ccc(gs))
    var qw = [1,2,3]
    print2(mmp)
    """


# f = Function("f",gg)
#print(f(1,3))
#assert f(1, 2) == 3
# ctx.add_callable("f", f)
# ctx.add_callable("f", f)
ctx.set('ua','mobile')
cc = {
    # "json":json
    "json":'22323'
    # json:json
}
def ccc(aa):
    return json.dumps(aa)
# ctx.add_callable('json',json)
# ctx.set('cc',cc) # 报错
ctx.add_callable('ccc',ccc)
# ctx.eval(gg)
ctx.set('mmp','我的mm')
#ctx.eval(f(1,3))
ctx.eval(gg+'let lg = print;lg(111);lg(f(1,3))')
ctx.set("v", 10**25)
print('v',type(ctx.eval("v")),ctx.eval("v"))
print(ctx.get('d'))
gs = ctx.get('gs')
qw = ctx.get('qw')
print('qw',qw)
print(qw.json())
print('gs',gs)

print(gs.json())
print(json.loads(gs.json()))
print(ctx.get('e'))
print(ctx.get('print2'))
print(11,ctx.parse_json('{"a":1}'))

def request(url):
    r = requests.get(url)
    r.encoding = r.apparent_encoding
    html = r.text
    print(html)
    return html

ctx.add_callable('request',request)
ctx.eval('bd()')

H
hjdhnx 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
t1 = time()
with open('../../js/兔小贝.js',encoding='utf-8') as f1:
    jscode = f1.read()
    print(jscode)

ctx1 = Context()
ctx1.eval(jscode)
ret = ctx1.get('rule')
print(type(ret.json()),ret.json())
print(json.loads(ret.json()))
t2 = time()
print(f'quickjs耗时:{round((t2-t1)*1000,2)}毫秒')
tt1 = time()
ctx1.eval(jscode)
ret = ctx1.get('rule')
print(type(ret.json()),ret.json())
print(json.loads(ret.json()))
tt2 = time()
print(f'quickjs第2次耗时:{round((tt2-tt1)*1000,2)}毫秒')

t3 = time()
with open('../../js/兔小贝.js',encoding='utf-8') as f1:
    jscode = f1.read()
    print(jscode)
ctx2 = js2py.EvalJs({},enable_require=False) # enable_require启用require关键字,会自动获取系统nodejs环境
ctx2.execute(jscode)
ret = ctx2.rule.to_dict()
print(type(ret),ret)
t4 = time()
print(f'js2py耗时:{round((t4-t3)*1000,2)}毫秒')
t5 = time()
ctx2.execute(jscode)
ret = ctx2.rule.to_dict()
print(type(ret),ret)
t6 = time()
print(f'js2py第2次耗时:{round((t6-t5)*1000,2)}毫秒')
126
# 报错提示:(没法把python对象给qkjs,基础数据类型字典也不行,json等包更不行了)  Unsupported type when converting a Python object to quickjs: dict.