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 newContextin an existingVirtualMachine.
- classmethod new_with_virtual_machine(vm: VirtualMachine) Context#
Create a new
Contextinvirtual_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
codeincontextfor syntax errors. Theline_numberis the starting line number inuri; the value is one-based so the first line is 1.uriandline_numberare only used to fill theexception. In case of errorsexceptionwill be set to a newExceptionwith the details. You can passNonetoexceptionto ignore the error details.- Parameters:
code – a JavaScript script to check
length – length of
code, or -1 ifcodeis a nul-terminated stringmode – a
CheckSyntaxModeuri – the source URI
line_number – the starting line number
- evaluate(code: str, length: int) Value#
Evaluate
codeincontext.- Parameters:
code – a JavaScript script to evaluate
length – length of
code, or -1 ifcodeis 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
codeand create an new object where symbols defined incodewill be added as properties, instead of being added tocontextglobal object. The new object is returned asobjectparameter. Similar to hownew_object()works, ifobject_instanceis notNoneobject_classmust be provided too. Theline_numberis the starting line number inuri; the value is one-based so the first line is 1.uriandline_numberwill 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 ifcodeis a nul-terminated stringobject_instance – an object instance
object_class – a
ClassorNoneto 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
codeincontextusingurias the source URI. Theline_numberis the starting line number inuri; the value is one-based so the first line is 1.uriandline_numberwill 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 ifcodeis a nul-terminated stringuri – the source URI
line_number – the starting line number
- classmethod get_current() Context | None#
Get the
Contextthat is currently executing a function. This should only be called within a function or method callback, otherwiseNonewill be returned.
- get_exception() Exception | None#
Get the last unhandled exception thrown in
contextby API functions calls.
- get_value(name: str) Value#
Get a property of
contextglobal object withname.- Parameters:
name – the value name
- get_virtual_machine() VirtualMachine#
Get the
VirtualMachinewherecontextwas created.
- pop_exception_handler() None#
Remove the last
ExceptionHandlerpreviously pushed tocontextwithpush_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 givenhandlerwill be called. The defaultExceptionHandlersimply 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()inhandlerlike 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. Whenhandleris removed from the context,destroy_notifyi called withuser_dataas parameter.- Parameters:
handler – a
ExceptionHandleruser_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
contextusing the givenname. If the new class inherits from anotherClass, the parent should be passed asparent_class, otherwiseNoneshould be used. The optionalvtableparameter 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 theClassis cleared in the context,destroy_notifyis called with the instance as parameter.- Parameters:
name – the class name
parent_class – a
ClassorNonevtable – an optional
ClassVTableorNonedestroy_notify – a destroy notifier for class instances
- set_value(name: str, value: Value) None#
Set a property of
contextglobal object withnameandvalue.- Parameters:
name – the value name
value – a
Value
- throw(error_message: str) None#
Throw an exception to
contextusing the given error message. The createdExceptioncan be retrieved withget_exception().- Parameters:
error_message – an error message
Properties#
- class Context
- props.virtual_machine: VirtualMachine#
The type of the None singleton.