Skip to content

Commit

Permalink
fix: some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
JamzumSum committed Nov 12, 2023
1 parent 858c226 commit bcf1a55
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
40 changes: 20 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pychaosvm"
version = "0.2.8"
version = "0.3.0"
description = "A Python envirionment for Tencent ChaosVM."
authors = ["aioqzone <[email protected]>"]
license = "AGPL-3.0-or-later"
Expand Down
5 changes: 3 additions & 2 deletions src/chaosvm/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import defaultdict
from hashlib import md5
from typing import Any, Callable, Dict, Iterable, List, Union
from urllib.parse import unquote

import pyjsparser as jsparser

Expand Down Expand Up @@ -122,5 +123,5 @@ def parse_opcodes(b64: str, arr: List[int]) -> List[int]:
win.add_mouse_track([(50, 42), (50, 55)])
with open("js/vm.js.bak", encoding="utf8") as f:
parse_vm(f.read(), win)(win)
print(win.TDC.getInfo().__dict__)
print(win.TDC.getData(None, True))
print(win.TDC.getInfo(None).__dict__)
print(unquote(win.TDC.getData(None, True)))
16 changes: 9 additions & 7 deletions src/chaosvm/proxy/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ def stringify(cls, o):


class Symbol(Proxy):
tag: str
register: ClassVar[Dict[str, Self]] = {}
iterator: ClassVar[Self]

def __init__(self, tag: Optional[str] = None) -> None:
super().__init__()
self.tag = tag
super().__init__(tag=tag)

def __getattribute__(self, __name):
if __name == "for":
Expand Down Expand Up @@ -215,8 +215,8 @@ def __init__(self, func: Callable) -> None:
super().__init__()
self.__f__ = func

def __call__(self, *args):
return self.__f__(*args)
def __call__(self, this: Optional[Proxy], *args):
return self.__f__(this, *args)

def call(self, this, *args):
return self.__f__(*args)
Expand Down Expand Up @@ -273,7 +273,7 @@ def __init__(self, *args):
def __len__(self):
return self.length

def forEach(self, pred: Callable):
def forEach(self, pred: Function):
for i in range(self.length):
pred(None, self[i])

Expand Down Expand Up @@ -374,7 +374,9 @@ def replace(self, reg: Union[str, Self, "RegExp"], newstr: Union[str, Function])
pattern = reg.pattern if isinstance(reg, RegExp) else re.compile(reg)
if isinstance(newstr, str):
return pattern.sub(newstr, self._s)
return pattern.sub(lambda m: newstr(None, m.group()), self._s)
return pattern.sub(
lambda m: str(newstr(None, m.group(), *m.groups(), m.start(), self)), self._s
)

def slice(self, start: int, stop: Optional[int] = None):
return self._s[slice(start, stop)]
Expand Down Expand Up @@ -433,7 +435,7 @@ def __init__(self, pattern: str, modifiers: str = "") -> None:

self.pattern = re.compile(pattern, flags)

def exec(self, s: str) -> Union[Array, object]:
def exec(self, s: str) -> Union[Array, NULL]:
if m := self.pattern.search(s):
return Array(m.group(0), *m.groups())
return NULL()
Expand Down
6 changes: 3 additions & 3 deletions src/chaosvm/proxy/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def setItem(self, name: str, v):


class TDC(Proxy):
getInfo: Callable[[], Proxy]
getInfo: Callable[[Optional["Window"]], Proxy]
getData: Callable[[Optional["Window"], bool], str]
setData: Callable[[Proxy], None]
clearTc: Callable[[], None]
Expand Down Expand Up @@ -266,8 +266,8 @@ def location(self):
def btoa(self, s: str):
return b64encode(s.encode()).decode()

def setTimeout(self, cb: Callable[[], Any], ms: float):
cb()
def setTimeout(self, cb: Function, ms: float):
cb(None)

def setInterval(self, func: Function, delay=0, *args):
func(None, *args)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_execute(vmjs: str):

tdc = prepare(vmjs, "", mouse_track=[(50, 42), (50, 55)])

info = tdc.getInfo()
info = tdc.getInfo(None)
assert info.__dict__
assert str(info["info"])

Expand Down

0 comments on commit bcf1a55

Please sign in to comment.