Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions STPyV8.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/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

import _STPyV8

Expand Down Expand Up @@ -34,8 +32,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)
Expand All @@ -50,9 +48,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)
Expand All @@ -70,7 +68,7 @@ def __getattribute__(self, attr):
RE_FILE = re.compile(r"\s+at\s(?P<file>[^:]+):?(?P<row>\d+)?:?(?P<col>\d+)?")

@staticmethod
def parse_stack(value):
def parse_stack(value: str):
stack = []

def int_or_nul(value):
Expand Down Expand Up @@ -147,10 +145,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__)

Expand All @@ -164,7 +163,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):
Expand All @@ -191,7 +190,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
Expand All @@ -205,27 +204,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
"""
Expand Down Expand Up @@ -303,7 +302,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()

Expand Down
248 changes: 248 additions & 0 deletions STPyV8.pyi
Original file line number Diff line number Diff line change
@@ -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): ...
Empty file added py.typed
Empty file.