API Reference¶
JSArray
¶
Bases: MutableSequence[PythonJSConvertedTypes]
, JSObjectImpl
JavaScript array.
Has Pythonic MutableSequence methods (e.g., insert()
, __getitem__()
, ...).
Source code in src/py_mini_racer/_objects.py
JSArrayIndexError
¶
Bases: IndexError
, MiniRacerBaseException
Invalid index into a JSArray.
Source code in src/py_mini_racer/_objects.py
JSEvalException
¶
JSFunction
¶
Bases: JSMappedObject
JavaScript function.
You can call this object from Python, passing in positional args to match what the
JavaScript function expects, along with a keyword argument, timeout_sec
.
Source code in src/py_mini_racer/_objects.py
JSKeyError
¶
Bases: JSEvalException
, KeyError
No such key found.
JSOOMException
¶
Bases: JSEvalException
JavaScript execution ran out of memory.
JSParseException
¶
Bases: JSEvalException
JavaScript could not be parsed.
JSPromise
¶
Bases: JSObjectImpl
JavaScript Promise.
To get a value, call promise.get()
to block, or await promise
from within an
async
coroutine. Either will raise a Python exception if the JavaScript Promise
is rejected.
Source code in src/py_mini_racer/_objects.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
get(*, timeout=None)
¶
Get the value, or raise an exception. This call blocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout |
Numeric | None
|
number of milliseconds after which the execution is interrupted. This is deprecated; use timeout_sec instead. |
None
|
Source code in src/py_mini_racer/_objects.py
JSPromiseError
¶
Bases: MiniRacerBaseException
JavaScript rejected a promise.
Source code in src/py_mini_racer/_objects.py
JSSymbol
¶
JSTimeoutException
¶
JSUndefinedType
¶
The JavaScript undefined type.
Where JavaScript null is represented as None, undefined is represented as this type.
Source code in src/py_mini_racer/_types.py
JSValueError
¶
Bases: JSEvalException
, ValueError
Bad value passed to JavaScript engine.
LibAlreadyInitializedError
¶
Bases: MiniRacerBaseException
MiniRacer-wrapped V8 build not found.
Source code in src/py_mini_racer/_dll.py
LibNotFoundError
¶
Bases: MiniRacerBaseException
MiniRacer-wrapped V8 build not found.
Source code in src/py_mini_racer/_dll.py
MiniRacer
¶
MiniRacer evaluates JavaScript code using a V8 isolate.
A MiniRacer instance can be explicitly closed using the close() method, or by using the MiniRacer as a context manager, i.e,:
with MiniRacer() as mr: ...
The MiniRacer instance will otherwise clean up the underlying V8 resource upon garbage collection.
Attributes:
Name | Type | Description |
---|---|---|
json_impl |
Any
|
JSON module used by helper methods default is json |
Source code in src/py_mini_racer/_mini_racer.py
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
v8_version: str
property
¶
Return the V8 version string.
call(expr, *args, encoder=None, timeout=None, timeout_sec=None, max_memory=None)
¶
Helper to call a JavaScript function and return compositve types.
The expr
argument refers to a JavaScript function in the current V8
isolate context. Further positional arguments are serialized using the JSON
implementation json_impl
and passed to the JavaScript function as arguments.
Returned value is serialized to JSON inside the V8 isolate and deserialized
using json_impl
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
str
|
JavaScript expression referring to a function |
required |
encoder |
JSONEncoder | None
|
Custom JSON encoder |
None
|
timeout |
Numeric | None
|
number of milliseconds after which the execution is interrupted. |
None
|
timeout_sec |
Numeric | None
|
number of seconds after which the execution is interrupted |
None
|
max_memory |
int | None
|
hard memory limit, in bytes, after which the execution is interrupted |
None
|
Source code in src/py_mini_racer/_mini_racer.py
close()
¶
Close this MiniRacer instance.
It is an error to use this MiniRacer instance or any JS objects returned by it after calling this method.
eval(code, timeout=None, timeout_sec=None, max_memory=None)
¶
Evaluate JavaScript code in the V8 isolate.
Side effects from the JavaScript evaluation is persisted inside a context (meaning variables set are kept for the next evaluation).
The JavaScript value returned by the last expression in code
is converted to
a Python value and returned by this method. Only primitive types are supported
(numbers, strings, buffers...). Use the
py_mini_racer.MiniRacer.execute method to return more complex
types such as arrays or objects.
The evaluation can be interrupted by an exception for several reasons: a limit was reached, the code could not be parsed, a returned value could not be converted to a Python value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code |
str
|
JavaScript code |
required |
timeout |
Numeric | None
|
number of milliseconds after which the execution is interrupted. This is deprecated; use timeout_sec instead. |
None
|
timeout_sec |
Numeric | None
|
number of seconds after which the execution is interrupted |
None
|
max_memory |
int | None
|
hard memory limit, in bytes, after which the execution is interrupted. |
None
|
Source code in src/py_mini_racer/_mini_racer.py
execute(expr, timeout=None, timeout_sec=None, max_memory=None)
¶
Helper to evaluate a JavaScript expression and return composite types.
Returned value is serialized to JSON inside the V8 isolate and deserialized
using json_impl
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
str
|
JavaScript expression |
required |
timeout |
Numeric | None
|
number of milliseconds after which the execution is interrupted. This is deprecated; use timeout_sec instead. |
None
|
timeout_sec |
Numeric | None
|
number of seconds after which the execution is interrupted |
None
|
max_memory |
int | None
|
hard memory limit, in bytes, after which the execution is interrupted. |
None
|
Source code in src/py_mini_racer/_mini_racer.py
heap_stats()
¶
low_memory_notification()
¶
set_hard_memory_limit(limit)
¶
Set a hard memory limit on this V8 isolate.
JavaScript execution will be terminated when this limit is reached.
:param int limit: memory limit in bytes or 0 to reset the limit
Source code in src/py_mini_racer/_mini_racer.py
set_soft_memory_limit(limit)
¶
Set a soft memory limit on this V8 isolate.
The Garbage Collection will use a more aggressive strategy when the soft limit is reached but the execution will not be stopped.
:param int limit: memory limit in bytes or 0 to reset the limit
Source code in src/py_mini_racer/_mini_racer.py
was_hard_memory_limit_reached()
¶
was_soft_memory_limit_reached()
¶
wrap_py_function(func)
¶
Wrap a Python function such that it can be called from JS.
To be wrapped and exposed in JavaScript, a Python function should:
- Be async,
- Accept variable positional arguments each of type PythonJSConvertedTypes, and
- Return one value of type PythonJSConvertedTypes (a type union which includes None).
The function is rendered on the JavaScript side as an async function (i.e., a function which returns a Promise).
Returns:
Type | Description |
---|---|
AbstractAsyncContextManager[JSFunction]
|
An async context manager which, when entered, yields a JS Function which |
AbstractAsyncContextManager[JSFunction]
|
can be passed into MiniRacer and called by JS code. |
Source code in src/py_mini_racer/_mini_racer.py
init_mini_racer(*, flags=DEFAULT_V8_FLAGS, ignore_duplicate_init=False)
¶
Initialize py_mini_racer (and V8).
This function can optionally be used to set V8 flags. This function can be called at most once, before any instances of MiniRacer are initialized. Instances of MiniRacer will automatically call this function to initialize MiniRacer and V8.