Skip to content

Commit 089843e

Browse files
committed
ctrl-c in repl
1 parent 3ae50bf commit 089843e

2 files changed

Lines changed: 29 additions & 56 deletions

File tree

src/quickjs_runtime/__init__.py

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,47 @@
1-
from _quickjs import Runtime as _Runtime
21
import sys
2+
from _quickjs import Runtime as _Runtime
33

4-
"""
5-
{"set_runtime_info", (PyCFunction)Runtime_SetRuntimeInfo, METH_O, "Set runtime info"},
6-
{"set_memory_limit", (PyCFunction)Runtime_SetMemoryLimit, METH_O, "Set memory limit"},
7-
{"set_gc_threshold", (PyCFunction)Runtime_SetGCThreshold, METH_O, "Set GC threshold"},
8-
{"set_max_stack_size", (PyCFunction)Runtime_SetMaxStackSize, METH_O, "Set max stack size"},
9-
{"update_stack_top", (PyCFunction)Runtime_UpdateStackTop, METH_NOARGS, "Update stack top"},
10-
{"run_gc", (PyCFunction)Runtime_RunGC, METH_NOARGS, "Run garbage collector"},
11-
{"new_context", (PyCFunction)Runtime_NewContext, METH_NOARGS, "Create a new QuickJS Context"},
12-
13-
"""
144
from abc import ABC, abstractmethod
155
from typing import override
166

7+
178
class IRuntime(ABC):
189

1910
@abstractmethod
20-
def set_runtime_info(self, info: str) -> None:
21-
...
22-
11+
def set_runtime_info(self, info: str) -> None: ...
12+
2313
@abstractmethod
24-
def set_memory_limit(self, limit: int) -> None:
25-
...
26-
14+
def set_memory_limit(self, limit: int) -> None: ...
15+
2716
@abstractmethod
28-
def set_gc_threshold(self, threshold: int) -> None:
29-
...
30-
17+
def set_gc_threshold(self, threshold: int) -> None: ...
18+
3119
@abstractmethod
32-
def set_max_stack_size(self, size: int) -> None:
33-
...
34-
20+
def set_max_stack_size(self, size: int) -> None: ...
21+
3522
@abstractmethod
36-
def update_stack_top(self) -> None:
37-
...
38-
23+
def update_stack_top(self) -> None: ...
24+
3925
@abstractmethod
40-
def run_gc(self) -> None:
41-
...
26+
def run_gc(self) -> None: ...
4227

4328
@abstractmethod
44-
def new_context(self) -> "IContext":
45-
...
29+
def new_context(self) -> "IContext": ...
30+
4631

4732
class Context(ABC):
4833

4934
@abstractmethod
50-
def eval(self, code: str, filename: str = "input.js") -> any:
51-
...
35+
def eval(self, code: str, filename: str = "input.js") -> any: ...
5236

5337
@abstractmethod
54-
def eval_sync(self, code: str, filename: str = "input.js") -> any:
55-
...
38+
def eval_sync(self, code: str, filename: str = "input.js") -> any: ...
5639

5740
@abstractmethod
58-
def set(self, name: str, value: any) -> None:
59-
...
41+
def set(self, name: str, value: any) -> None: ...
6042

6143
@abstractmethod
62-
def get_runtime(self) -> IRuntime:
63-
...
44+
def get_runtime(self) -> IRuntime: ...
6445

6546

6647
class Runtime(IRuntime, _Runtime):
@@ -71,31 +52,30 @@ def __init__(self) -> None:
7152
@override
7253
def set_runtime_info(self, info: str) -> None:
7354
return _Runtime.set_runtime_info(self, info)
74-
55+
7556
@override
7657
def set_memory_limit(self, limit: int) -> None:
7758
return _Runtime.set_memory_limit(self, limit)
78-
59+
7960
@override
8061
def set_gc_threshold(self, threshold: int) -> None:
8162
return _Runtime.set_gc_threshold(self, threshold)
82-
63+
8364
@override
8465
def set_max_stack_size(self, size: int) -> None:
8566
return _Runtime.set_max_stack_size(self, size)
86-
67+
8768
@override
8869
def update_stack_top(self) -> None:
8970
return _Runtime.update_stack_top(self)
90-
71+
9172
@override
9273
def run_gc(self) -> None:
9374
return _Runtime.run_gc(self)
94-
75+
9576
@override
9677
def new_context(self) -> Context:
9778
ctx = _Runtime.new_context(self)
98-
9979
# Setup console object
10080
console = {
10181
"log": lambda *args: print(*args),
@@ -104,8 +84,7 @@ def new_context(self) -> Context:
10484
"error": lambda *args: print(*args, file=sys.stderr),
10585
}
10686
ctx.set("console", console)
107-
10887
return ctx
10988

11089

111-
__all__ = ["Runtime", "Context"]
90+
__all__ = ["Runtime", "Context"]

src/quickjs_runtime/repl.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ def main():
1010
print("QuickJS REPL")
1111
eof_key = "Ctrl+Z" if sys.platform == "win32" else "Ctrl+D"
1212
print(f"Type 'exit()' or {eof_key} to exit")
13-
14-
console = {
15-
"log": print,
16-
}
17-
ctx.set("console", console)
18-
ctx.eval("""globalThis.console = console;""")
19-
13+
2014
while True:
2115
try:
2216
line = input("> ")
@@ -32,8 +26,8 @@ def main():
3226
print()
3327
break
3428
except KeyboardInterrupt:
35-
print("\nKeyboardInterrupt")
36-
continue
29+
print()
30+
break
3731

3832
if __name__ == "__main__":
3933
main()

0 commit comments

Comments
 (0)