From 5c9c4e01a9492270896f52461c868bee9086e419 Mon Sep 17 00:00:00 2001 From: devm18426 <125835885+devm18426@users.noreply.github.com> Date: Tue, 3 Oct 2023 18:11:03 +0300 Subject: [PATCH 1/2] Add typing info --- STPyV8.py | 30 ++++--- STPyV8.pyi | 248 +++++++++++++++++++++++++++++++++++++++++++++++++++++ py.typed | 0 3 files changed, 264 insertions(+), 14 deletions(-) create mode 100644 STPyV8.pyi create mode 100644 py.typed diff --git a/STPyV8.py b/STPyV8.py index cc522962..7d7826ee 100644 --- a/STPyV8.py +++ b/STPyV8.py @@ -6,6 +6,7 @@ import re import collections.abc +from typing import Callable import _STPyV8 @@ -34,8 +35,8 @@ class JSAttribute: - def __init__(self, name): - self.name = name + def __init__(self, name: str): + self.name: str = name def __call__(self, func): setattr(func, f"__{self.name}__", True) @@ -50,9 +51,9 @@ def __call__(self, func): class JSError(Exception): - def __init__(self, impl): + def __init__(self, impl: BaseException): Exception.__init__(self) - self._impl = impl + self._impl: BaseException = impl def __str__(self): return str(self._impl) @@ -70,7 +71,7 @@ def __getattribute__(self, attr): RE_FILE = re.compile(r"\s+at\s(?P[^:]+):?(?P\d+)?:?(?P\d+)?") @staticmethod - def parse_stack(value): + def parse_stack(value: str): stack = [] def int_or_nul(value): @@ -147,10 +148,11 @@ def __bool__(self): class JSClass: - __properties__ = {} + _CALLBACK_TYPE = Callable[[], None] # () -> None + __properties__: dict[str, tuple[_CALLBACK_TYPE | None, _CALLBACK_TYPE | None]] = {} __watchpoints__ = {} - def __getattr__(self, name): + def __getattr__(self, name: str): if name == 'constructor': return JSClassConstructor(self.__class__) @@ -164,7 +166,7 @@ def __getattr__(self, name): raise AttributeError(name) - def __setattr__(self, name, value): + def __setattr__(self, name: str, value): prop = self.__dict__.setdefault('__properties__', {}).get(name, None) if prop and isinstance(prop[1], collections.abc.Callable): @@ -191,7 +193,7 @@ def valueOf(self): """ return self - def hasOwnProperty(self, name): + def hasOwnProperty(self, name: str): """ Return a boolean value indicating whether the object has a property with the specified name @@ -205,27 +207,27 @@ def isPrototypeOf(self, obj): """ raise NotImplementedError() - def __defineGetter__(self, name, getter): + def __defineGetter__(self, name: str, getter: _CALLBACK_TYPE): """ Bind the object property to a function to be called when that property is looked up """ self.__properties__[name] = (getter, self.__lookupSetter__(name)) - def __lookupGetter__(self, name): + def __lookupGetter__(self, name: str): """ Return the function bound as a getter to the specified property """ return self.__properties__.get(name, (None, None))[0] - def __defineSetter__(self, name, setter): + def __defineSetter__(self, name: str, setter: _CALLBACK_TYPE): """ Bind the object property to a function to be called when an attempt is made to set that property """ self.__properties__[name] = (self.__lookupGetter__(name), setter) - def __lookupSetter__(self, name): + def __lookupSetter__(self, name: str): """ Return the function bound as setter to the specified property """ @@ -303,7 +305,7 @@ def __exit__(self, exc_type, exc_value, traceback): class JSContext(_STPyV8.JSContext): - def __init__(self, obj = None, ctxt = None): + def __init__(self, obj = None, ctxt: "JSContext" = None): self.lock = JSLocker() self.lock.enter() diff --git a/STPyV8.pyi b/STPyV8.pyi new file mode 100644 index 00000000..f4741fb0 --- /dev/null +++ b/STPyV8.pyi @@ -0,0 +1,248 @@ +from typing import Iterable, overload, Any, Iterator + + +class JSObject: + def __getattr__(self, item: str): ... + + def __setattr__(self, key: str, value): ... + + def __delattr__(self, item: str): ... + + def __hash__(self) -> int: ... + + def __dir__(self) -> Iterable[str]: ... + + def __getitem__(self, item: str): ... + + def __setitem__(self, key: str, value): ... + + def __delitem__(self, key: str): ... + + def __contains__(self, item: str) -> bool: ... + + def __int__(self) -> int: ... + + def __float__(self) -> float: ... + + def __str__(self) -> str: ... + + def __bool__(self) -> bool: ... + + def __eq__(self, other: JSObject) -> bool: ... + + def __ne__(self, other: JSObject) -> bool: ... + + def clone(self) -> JSObject: ... + + def keys(self) -> list[JSObject]: ... + + @staticmethod + def create(constructor: JSFunction, arguments=(), propertiesObject=dict()): ... + + +class JSNull: + def __bool__(self) -> bool: ... + + def __str__(self) -> str: ... + + +class JSUndefined: + def __bool__(self) -> bool: ... + + def __str__(self) -> str: ... + + +class JSArray: + def __init__(self): ... + + def __len__(self) -> int: ... + + def __getitem__(self, item): ... + + def __setitem__(self, key, value): ... + + def __delitem__(self, key): ... + + def __dir__(self) -> Iterable[str]: ... + + def __contains__(self, item) -> bool: ... + + +class JSFunction: + def __call__(self, *args, **kwargs): ... + + def apply(self, args: list, kwds: dict): ... + + def setName(self, name: str): ... + + @property + def name(self) -> str: ... + + @property + def owner(self) -> JSFunction: ... + + @property + def linenum(self) -> int: ... + + @property + def colnum(self) -> int: ... + + @property + def resname(self) -> str: ... + + @property + def inferredname(self) -> str: ... + + +class JSPlatform: + def __init__(self, argv=""): ... + + +class JSIsolate: + def __init__(self, owner: bool = False): ... + + @staticmethod + @property + def current() -> JSIsolate: ... + + @property + def locked(self) -> bool: ... + + def GetCurrentStackTrace(self, frame_limit: int, options: JSObject): ... + + def enter(self): ... + + def leave(self): ... + + +class JSContext: + @overload + def __init__(self, context: JSContext): ... + + @overload + def __init__(self, global_): ... + + @property + def securityToken(self) -> str: ... + + @securityToken.setter + def securitySetter(self, token: str): ... + + @property + def locals(self): ... + + @staticmethod + @property + def entered() -> JSContext: ... + + @staticmethod + @property + def current() -> JSContext: ... + + @staticmethod + @property + def calling() -> JSContext: ... + + @staticmethod + @property + def inContext() -> bool: ... + + def eval(self, source, name="", line=-1, col=-1): ... + + def enter(self): ... + + def leave(self): ... + + def __bool__(self) -> bool: ... + + +class JSEngine: + def __init__(self): ... + + @staticmethod + @property + def version() -> str: ... + + @staticmethod + @property + def dead() -> bool: ... + + @staticmethod + def setFlags(flags: str): ... + + @staticmethod + def terminateAllThreads(): ... + + @staticmethod + def dispose(): ... + + @staticmethod + def lowMemory(): ... + + @staticmethod + def setStackLimit(stack_limit_size=0): ... + + def compile(self, source: str, name="", line=-1, col=-1) -> JSScript: ... + + +class JSScript: + @property + def source(self) -> str: ... + + def run(self) -> Any: ... + + +class JSStackTrace: + def __len__(self) -> int: ... + + def __getitem__(self, item) -> JSStackFrame: ... + + def __iter__(self) -> Iterator[JSStackFrame]: ... + + def __str__(self) -> str: ... + + +class JSStackTraceOptions: + LineNumber: int + ColumnOffset: int + ScriptName: int + FunctionName: int + IsEval: int + IsConstructor: int + Overview: int + Detailed: int + + +class JSStackFrame: + lineNum: int + column: int + scriptName: str + funcName: str + isEval: bool + isConstructor: bool + + +class JSLocker: + @overload + def __init__(self): ... + + @overload + def __init__(self, isolate: JSIsolate): ... + + @staticmethod + @property + def locked() -> bool: ... + + def entered(self) -> bool: ... + + def enter(self): ... + + def leave(self): ... + + +class JSUnlocker: + def entered(self) -> bool: ... + + def enter(self): ... + + def leave(self): ... diff --git a/py.typed b/py.typed new file mode 100644 index 00000000..e69de29b From fad8adeba9fab708f967df3f4e880251a37125de Mon Sep 17 00:00:00 2001 From: devm18426 <125835885+devm18426@users.noreply.github.com> Date: Tue, 3 Oct 2023 18:14:08 +0300 Subject: [PATCH 2/2] Remove unneeded future imports --- STPyV8.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/STPyV8.py b/STPyV8.py index 7d7826ee..92b4ab5c 100644 --- a/STPyV8.py +++ b/STPyV8.py @@ -1,9 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import with_statement -from __future__ import print_function - import re import collections.abc from typing import Callable