File tree Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Original file line number Diff line number Diff line change 11import json
2+ import threading
23
34import _quickjs
45
@@ -14,19 +15,24 @@ def test():
1415
1516class Function :
1617 def __init__ (self , name : str , code : str ) -> None :
17- self .context = Context ()
18- self .context .eval (code )
19- self .f = self .context .get (name )
18+ self ._context = Context ()
19+ self ._context .eval (code )
20+ self ._f = self ._context .get (name )
21+ self ._lock = threading .Lock ()
2022
2123 def __call__ (self , * args ):
24+ with self ._lock :
25+ return self ._call (* args )
26+
27+ def _call (self , * args ):
2228 def convert_arg (arg ):
2329 if isinstance (arg , (str , int )):
2430 return arg
2531 else :
2632 # More complex objects are passed through JSON.
27- return self .context .eval ("(" + json .dumps (arg ) + ")" )
33+ return self ._context .eval ("(" + json .dumps (arg ) + ")" )
2834
29- result = self .f (* [convert_arg (a ) for a in args ])
35+ result = self ._f (* [convert_arg (a ) for a in args ])
3036 if isinstance (result , Object ):
3137 result = json .loads (result .json ())
32- return result
38+ return result
Original file line number Diff line number Diff line change @@ -167,6 +167,10 @@ def test_adder(self):
167167 self .assertEqual (f (100 , 200 ), 300 )
168168 self .assertEqual (f ("a" , "b" ), "ab" )
169169
170+ def test_empty (self ):
171+ f = quickjs .Function ("f" , "function f() { }" )
172+ self .assertEqual (f (), None )
173+
170174 def test_lists (self ):
171175 f = quickjs .Function (
172176 "f" , """
You can’t perform that action at this time.
0 commit comments