Context#
Superclasses: Object
JSCContext represents a JavaScript execution context, where all operations take place and where the values will be associated.
When a new context is created, a global object is allocated and the built-in JavaScript
objects (Object, Function, String, Array) are populated. You can execute JavaScript in
the context by using evaluate()
or evaluate_with_source_uri()
.
It’s also possible to register custom objects in the context with register_class()
.
Constructors#
- class Context
- classmethod new() Context #
Create a new
Context
. The context is created in a newVirtualMachine
. Usenew_with_virtual_machine()
to create a newContext
in an existingVirtualMachine
.
- classmethod new_with_virtual_machine(vm: VirtualMachine) Context #
Create a new
Context
invirtual_machine
.- Parameters:
vm – a
VirtualMachine
Methods#
- class Context
- check_syntax(code: str, length: int, mode: CheckSyntaxMode, uri: str, line_number: int) Tuple[CheckSyntaxResult, Exception] #
Check the given
code
incontext
for syntax errors. Theline_number
is the starting line number inuri
; the value is one-based so the first line is 1.uri
andline_number
are only used to fill theexception
. In case of errorsexception
will be set to a newException
with the details. You can passNone
toexception
to ignore the error details.- Parameters:
code – a JavaScript script to check
length – length of
code
, or -1 ifcode
is a nul-terminated stringmode – a
CheckSyntaxMode
uri – the source URI
line_number – the starting line number
- evaluate(code: str, length: int) Value #
Evaluate
code
incontext
.- Parameters:
code – a JavaScript script to evaluate
length – length of
code
, or -1 ifcode
is a nul-terminated string
- evaluate_in_object(code: str, length: int, object_instance: Any, object_class: Class | None, uri: str, line_number: int) Tuple[Value, Value] #
Evaluate
code
and create an new object where symbols defined incode
will be added as properties, instead of being added tocontext
global object. The new object is returned asobject
parameter. Similar to hownew_object()
works, ifobject_instance
is notNone
object_class
must be provided too. Theline_number
is the starting line number inuri
; the value is one-based so the first line is 1.uri
andline_number
will be shown in exceptions and they don’t affect the behavior of the script.- Parameters:
code – a JavaScript script to evaluate
length – length of
code
, or -1 ifcode
is a nul-terminated stringobject_instance – an object instance
object_class – a
Class
orNone
to use the defaulturi – the source URI
line_number – the starting line number
- evaluate_with_source_uri(code: str, length: int, uri: str, line_number: int) Value #
Evaluate
code
incontext
usinguri
as the source URI. Theline_number
is the starting line number inuri
; the value is one-based so the first line is 1.uri
andline_number
will be shown in exceptions and they don’t affect the behavior of the script.- Parameters:
code – a JavaScript script to evaluate
length – length of
code
, or -1 ifcode
is a nul-terminated stringuri – the source URI
line_number – the starting line number
- classmethod get_current() Context | None #
Get the
Context
that is currently executing a function. This should only be called within a function or method callback, otherwiseNone
will be returned.
- get_exception() Exception | None #
Get the last unhandled exception thrown in
context
by API functions calls.
- get_value(name: str) Value #
Get a property of
context
global object withname
.- Parameters:
name – the value name
- get_virtual_machine() VirtualMachine #
Get the
VirtualMachine
wherecontext
was created.
- pop_exception_handler() None #
Remove the last
ExceptionHandler
previously pushed tocontext
withpush_exception_handler()
.
- push_exception_handler(handler: Callable[[Context, Exception, Any], None], user_data: Any = None) None #
Push an exception handler in
context
. Whenever a JavaScript exception happens in theContext
, the givenhandler
will be called. The defaultExceptionHandler
simply callsthrow_exception()
to throw the exception to theContext
. If you don’t want to catch the exception, but only get notified about it, callthrow_exception()
inhandler
like the default one does. The last exception handler pushed is the only one used by theContext
, usepop_exception_handler()
to remove it and set the previous one. Whenhandler
is removed from the context,destroy_notify
i called withuser_data
as parameter.- Parameters:
handler – a
ExceptionHandler
user_data – user data to pass to
handler
- register_class(name: str, parent_class: Class | None = None, vtable: ClassVTable | None = None, destroy_notify: Callable[[Any], None] | None = None) Class #
Register a custom class in
context
using the givenname
. If the new class inherits from anotherClass
, the parent should be passed asparent_class
, otherwiseNone
should be used. The optionalvtable
parameter allows to provide a custom implementation for handling the class, for example, to handle external properties not added to the prototype. When an instance of theClass
is cleared in the context,destroy_notify
is called with the instance as parameter.- Parameters:
name – the class name
parent_class – a
Class
orNone
vtable – an optional
ClassVTable
orNone
destroy_notify – a destroy notifier for class instances
- set_value(name: str, value: Value) None #
Set a property of
context
global object withname
andvalue
.- Parameters:
name – the value name
value – a
Value
- throw(error_message: str) None #
Throw an exception to
context
using the given error message. The createdException
can be retrieved withget_exception()
.- Parameters:
error_message – an error message
Properties#
- class Context
- props.virtual_machine: VirtualMachine#
The type of the None singleton.