Skip to content

Commit 9efa6da

Browse files
committed
Guard function evaluation with a lock.
1 parent 94789ff commit 9efa6da

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

quickjs/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import threading
23

34
import _quickjs
45

@@ -14,19 +15,24 @@ def test():
1415

1516
class 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

test_quickjs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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", """

0 commit comments

Comments
 (0)