Class Context

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public final class Context extends GObject

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 jsc_context_evaluate() or jsc_context_evaluate_with_source_uri(). It's also possible to register custom objects in the context with jsc_context_register_class().

  • Constructor Details

    • Context

      public Context(MemorySegment address)
      Create a Context instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Context

      public Context()
      Create a new Context.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Context class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Context asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • withVirtualMachine

      public static Context withVirtualMachine(VirtualMachine vm)
      Create a new JSCContext in virtualMachine.
      Parameters:
      vm - a JSCVirtualMachine
      Returns:
      the newly created JSCContext.
    • getCurrent

      public static @Nullable Context getCurrent()
      Get the JSCContext that is currently executing a function. This should only be called within a function or method callback, otherwise null will be returned.
      Returns:
      the JSCContext that is currently executing.
    • checkSyntax

      public CheckSyntaxResult checkSyntax(String code, long length, CheckSyntaxMode mode, String uri, int lineNumber, @Nullable Out<Exception> exception)
      Check the given code in this Context for syntax errors. The lineNumber is the starting line number in uri; the value is one-based so the first line is 1. uri and lineNumber are only used to fill the exception. In case of errors exception will be set to a new JSCException with the details. You can pass null to exception to ignore the error details.
      Parameters:
      code - a JavaScript script to check
      length - length of code, or -1 if code is a nul-terminated string
      mode - a JSCCheckSyntaxMode
      uri - the source URI
      lineNumber - the starting line number
      exception - return location for a JSCException, or null to ignore
      Returns:
      a JSCCheckSyntaxResult
    • clearException

      public void clearException()
      Clear the uncaught exception in this Context if any.
    • evaluate

      public Value evaluate(String code, long length)
      Evaluate code in context.
      Parameters:
      code - a JavaScript script to evaluate
      length - length of code, or -1 if code is a nul-terminated string
      Returns:
      a JSCValue representing the last value generated by the script.
    • evaluateInObject

      public Value evaluateInObject(String code, long length, @Nullable MemorySegment objectInstance, @Nullable Class objectClass, String uri, int lineNumber, Out<Value> object)
      Evaluate code and create an new object where symbols defined in code will be added as properties, instead of being added to this Context global object. The new object is returned as object parameter. Similar to how jsc_value_new_object() works, if objectInstance is not null objectClass must be provided too. The lineNumber is the starting line number in uri; the value is one-based so the first line is 1. uri and lineNumber 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 if code is a nul-terminated string
      objectInstance - an object instance
      objectClass - a JSCClass or null to use the default
      uri - the source URI
      lineNumber - the starting line number
      object - return location for a JSCValue.
      Returns:
      a JSCValue representing the last value generated by the script.
    • evaluateWithSourceUri

      public Value evaluateWithSourceUri(String code, long length, String uri, int lineNumber)
      Evaluate code in this Context using uri as the source URI. The lineNumber is the starting line number in uri; the value is one-based so the first line is 1. uri and lineNumber 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 if code is a nul-terminated string
      uri - the source URI
      lineNumber - the starting line number
      Returns:
      a JSCValue representing the last value generated by the script.
    • getException

      public @Nullable Exception getException()
      Get the last unhandled exception thrown in this Context by API functions calls.
      Returns:
      a JSCException or null if there isn't any unhandled exception in the JSCContext.
    • getGlobalObject

      public Value getGlobalObject()
      Get a JSCValue referencing the this Context global object
      Returns:
      a JSCValue
    • getValue

      public Value getValue(String name)
      Get a property of this Context global object with name.
      Parameters:
      name - the value name
      Returns:
      a JSCValue
    • getVirtualMachine

      public VirtualMachine getVirtualMachine()
      Get the JSCVirtualMachine where this Context was created.
      Returns:
      the JSCVirtualMachine where the JSCContext was created.
    • popExceptionHandler

      public void popExceptionHandler()
      Remove the last JSCExceptionHandler previously pushed to this Context with jsc_context_push_exception_handler().
    • pushExceptionHandler

      public void pushExceptionHandler(@Nullable ExceptionHandler handler)
      Push an exception handler in context. Whenever a JavaScript exception happens in the JSCContext, the given handler will be called. The default JSCExceptionHandler simply calls jsc_context_throw_exception() to throw the exception to the JSCContext. If you don't want to catch the exception, but only get notified about it, call jsc_context_throw_exception() in handler like the default one does. The last exception handler pushed is the only one used by the JSCContext, use jsc_context_pop_exception_handler() to remove it and set the previous one. When handler is removed from the context, destroyNotify is called with userData as parameter.
      Parameters:
      handler - a JSCExceptionHandler
    • registerClass

      public Class registerClass(String name, @Nullable Class parentClass, @Nullable ClassVTable vtable)
      Register a custom class in this Context using the given name. If the new class inherits from another JSCClass, the parent should be passed as parentClass, otherwise null should be used. The optional vtable 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 the JSCClass is cleared in the context, destroyNotify is called with the instance as parameter.
      Parameters:
      name - the class name
      parentClass - a JSCClass or null
      vtable - an optional JSCClassVTable or null
      Returns:
      a JSCClass
    • setValue

      public void setValue(String name, Value value)
      Set a property of this Context global object with name and value.
      Parameters:
      name - the value name
      value - a JSCValue
    • throw_

      public void throw_(String errorMessage)
      Throw an exception to this Context using the given error message. The created JSCException can be retrieved with jsc_context_get_exception().
      Parameters:
      errorMessage - an error message
    • throwException

      public void throwException(Exception exception)
      Throw exception to context.
      Parameters:
      exception - a JSCException
    • throwPrintf

      public void throwPrintf(String format, Object... varargs)
      Throw an exception to this Context using the given formatted string as error message. The created JSCException can be retrieved with jsc_context_get_exception().
      Parameters:
      format - the string format
      varargs - the parameters to insert into the format string
    • throwWithName

      public void throwWithName(String errorName, String errorMessage)
      Throw an exception to this Context using the given error name and message. The created JSCException can be retrieved with jsc_context_get_exception().
      Parameters:
      errorName - the error name
      errorMessage - an error message
    • throwWithNamePrintf

      public void throwWithNamePrintf(String errorName, String format, Object... varargs)
      Throw an exception to this Context using the given error name and the formatted string as error message. The created JSCException can be retrieved with jsc_context_get_exception().
      Parameters:
      errorName - the error name
      format - the string format
      varargs - the parameters to insert into the format string
    • builder

      public static Context.Builder<? extends Context.Builder> builder()
      A Context.Builder object constructs a Context with the specified properties. Use the various set...() methods to set properties, and finish construction with Context.Builder.build().
      Returns:
      the builder object