-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathpythonmonkey.pyi
More file actions
174 lines (120 loc) · 3.55 KB
/
pythonmonkey.pyi
File metadata and controls
174 lines (120 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
stub file for type hints & documentations for the native module
@see https://typing.readthedocs.io/en/latest/source/stubs.html
"""
import typing as _typing
class EvalOptions(_typing.TypedDict, total=False):
filename: str
lineno: int
column: int
mutedErrors: bool
noScriptRval: bool
selfHosting: bool
strict: bool
module: bool
fromPythonFrame: bool
# pylint: disable=redefined-builtin
def eval(code: str, evalOpts: EvalOptions = {}, /) -> _typing.Any:
"""
JavaScript evaluator in Python
"""
def require(moduleIdentifier: str, /) -> JSObjectProxy:
"""
Return the exports of a CommonJS module identified by `moduleIdentifier`, using standard CommonJS semantics
"""
def new(ctor: JSFunctionProxy) -> _typing.Callable[..., _typing.Any]:
"""
Wrap the JS new operator, emitting a lambda which constructs a new
JS object upon invocation
"""
def typeof(jsval: _typing.Any, /):
"""
This is the JS `typeof` operator, wrapped in a function so that it can be used easily from Python.
"""
def wait() -> _typing.Awaitable[None]:
"""
Block until all asynchronous jobs (Promise/setTimeout/etc.) finish.
```py
await pm.wait()
```
This is the event-loop shield that protects the loop from being prematurely terminated.
"""
def stop() -> None:
"""
Stop all pending asynchronous jobs, and unblock `await pm.wait()`
"""
def runProgramModule(filename: str, argv: _typing.List[str], extraPaths: _typing.List[str] = []) -> None:
"""
Load and evaluate a program (main) module. Program modules must be written in JavaScript.
"""
def isCompilableUnit(code: str) -> bool:
"""
Hint if a string might be compilable Javascript without actual evaluation
"""
def collect() -> None:
"""
Calls the spidermonkey garbage collector
"""
def internalBinding(namespace: str) -> JSObjectProxy:
"""
INTERNAL USE ONLY
See function declarations in ./builtin_modules/internal-binding.d.ts
"""
class JSFunctionProxy():
"""
JavaScript Function proxy
"""
class JSMethodProxy(JSFunctionProxy, object):
"""
JavaScript Method proxy
This constructs a callable object based on the first argument, bound to the second argument
Useful when you wish to implement a method on a class object with JavaScript
Example:
import pythonmonkey as pm
jsFunc = pm.eval("(function(value) { this.value = value})")
class Class:
def __init__(self):
self.value = 0
self.setValue = pm.JSMethodProxy(jsFunc, self) #setValue will be bound to self, so `this` will always be `self`
myObject = Class()
print(myObject.value) # 0
myObject.setValue(42)
print(myObject.value) # 42.0
"""
def __init__(self) -> None: "deleted"
class JSObjectProxy(dict):
"""
JavaScript Object proxy dict
"""
def __init__(self) -> None: "deleted"
class JSArrayProxy(list):
"""
JavaScript Array proxy
"""
def __init__(self) -> None: "deleted"
class JSArrayIterProxy(_typing.Iterator):
"""
JavaScript Array Iterator proxy
"""
def __init__(self) -> None: "deleted"
class JSStringProxy(str):
"""
JavaScript String proxy
"""
def __init__(self) -> None: "deleted"
class bigint(int):
"""
Representing JavaScript BigInt in Python
"""
class SpiderMonkeyError(Exception):
"""
Representing a corresponding JS Error in Python
"""
null = _typing.Annotated[
_typing.NewType("pythonmonkey.null", object),
"Representing the JS null type in Python using a singleton object",
]
globalThis = _typing.Annotated[
JSObjectProxy,
"A Python Dict which is equivalent to the globalThis object in JavaScript",
]