File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ import json
2+
13import _quickjs
24
35
@@ -8,3 +10,16 @@ def test():
810Context = _quickjs .Context
911Object = _quickjs .Object
1012JSException = _quickjs .JSException
13+
14+
15+ class Function :
16+ def __init__ (self , name : str , code : str ) -> None :
17+ self .context = Context ()
18+ self .context .eval (code )
19+ self .f = self .context .get (name )
20+
21+ def __call__ (self , * args ):
22+ result = self .f (* args )
23+ if isinstance (result , Object ):
24+ result = json .loads (result .json ())
25+ return result
Original file line number Diff line number Diff line change @@ -153,3 +153,15 @@ def test_function_call_unsupported_arg(self):
153153 def test_json (self ):
154154 d = self .context .eval ("d = {data: 42};" )
155155 self .assertEqual (json .loads (d .json ()), {"data" : 42 })
156+
157+
158+ class FunctionTest (unittest .TestCase ):
159+ def test_adder (self ):
160+ f = quickjs .Function ("adder" , """
161+ function adder(x, y) {
162+ return x + y;
163+ }
164+ """ )
165+ self .assertEqual (f (1 , 1 ), 2 )
166+ self .assertEqual (f (100 , 200 ), 300 )
167+ self .assertEqual (f ("a" , "b" ), "ab" )
You can’t perform that action at this time.
0 commit comments