Package org.gnome.webkit.jsc
Class Context
java.lang.Object
io.github.jwharm.javagi.base.ProxyInstance
org.gnome.gobject.TypeInstance
org.gnome.gobject.GObject
org.gnome.webkit.jsc.Context
- All Implemented Interfaces:
Proxy
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().
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Context.Builder<B extends Context.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionContext()
Create a newJSCContext
.Context
(MemorySegment address) Create a Context proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected Context
asParent()
Returns this instance as if it were its parent type.static Context.Builder
<? extends Context.Builder> builder()
AContext.Builder
object constructs aContext
with the specified properties.checkSyntax
(String code, long length, CheckSyntaxMode mode, String uri, int lineNumber, @Nullable Out<Exception> exception) Check the givencode
in this Context for syntax errors.void
Clear the uncaught exception in this Context if any.Evaluatecode
in this Context.evaluateInObject
(String code, long length, @Nullable MemorySegment objectInstance, @Nullable Class objectClass, String uri, int lineNumber, Out<Value> object) Evaluatecode
and create an new object where symbols defined incode
will be added as properties, instead of being added to this Context global object.evaluateWithSourceUri
(String code, long length, String uri, int lineNumber) Evaluatecode
in this Context usinguri
as the source URI.static Context
Get theJSCContext
that is currently executing a function.Get the last unhandled exception thrown in this Context by API functions calls.Get aJSCValue
referencing the this Context global objectstatic Type
getType()
Get the GType of the Context classGet a property of this Context global object withname
.Get theJSCVirtualMachine
where this Context was created.void
Remove the lastJSCExceptionHandler
previously pushed to this Context with jsc_context_push_exception_handler().void
pushExceptionHandler
(ExceptionHandler handler) Push an exception handler in this Context.registerClass
(String name, @Nullable Class parentClass, @Nullable ClassVTable vtable) Register a custom class in this Context using the givenname
.void
Set a property of this Context global object withname
andvalue
.void
Throw an exception to this Context using the given error message.void
throwException
(Exception exception) Throwexception
to this Context.void
throwPrintf
(String format, Object... varargs) Throw an exception to this Context using the given formatted string as error message.void
throwWithName
(String errorName, String errorMessage) Throw an exception to this Context using the given error name and message.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.static Context
Create a newJSCContext
invirtualMachine
.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Context
Create a Context proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Context
Create a newJSCContext
. The context is created in a newJSCVirtualMachine
. Use jsc_context_new_with_virtual_machine() to create a newJSCContext
in an existingJSCVirtualMachine
.- Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
withVirtualMachine
Create a newJSCContext
invirtualMachine
.- Parameters:
vm
- aJSCVirtualMachine
- Returns:
- the newly created
JSCContext
. - Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
getCurrent
Get theJSCContext
that is currently executing a function. This should only be called within a function or method callback, otherwisenull
will be returned.- Returns:
- the
JSCContext
that is currently executing. - Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
checkSyntax
public CheckSyntaxResult checkSyntax(String code, long length, CheckSyntaxMode mode, String uri, int lineNumber, @Nullable @Nullable Out<Exception> exception) Check the givencode
in this Context for syntax errors. ThelineNumber
is the starting line number inuri
; the value is one-based so the first line is 1.uri
andlineNumber
are only used to fill theexception
. In case of errorsexception
will be set to a newJSCException
with the details. You can passnull
toexception
to ignore the error details.- Parameters:
code
- a JavaScript script to checklength
- length ofcode
, or -1 ifcode
is a nul-terminated stringmode
- aJSCCheckSyntaxMode
uri
- the source URIlineNumber
- the starting line numberexception
- return location for aJSCException
, ornull
to ignore- Returns:
- a
JSCCheckSyntaxResult
-
clearException
public void clearException()Clear the uncaught exception in this Context if any. -
evaluate
-
evaluateInObject
public Value evaluateInObject(String code, long length, @Nullable @Nullable MemorySegment objectInstance, @Nullable @Nullable Class objectClass, String uri, int lineNumber, Out<Value> object) Evaluatecode
and create an new object where symbols defined incode
will be added as properties, instead of being added to this Context global object. The new object is returned asobject
parameter. Similar to how jsc_value_new_object() works, ifobjectInstance
is notnull
objectClass
must be provided too. ThelineNumber
is the starting line number inuri
; the value is one-based so the first line is 1.uri
andlineNumber
will be shown in exceptions and they don't affect the behavior of the script.- Parameters:
code
- a JavaScript script to evaluatelength
- length ofcode
, or -1 ifcode
is a nul-terminated stringobjectInstance
- an object instanceobjectClass
- aJSCClass
ornull
to use the defaulturi
- the source URIlineNumber
- the starting line numberobject
- return location for aJSCValue
.- Returns:
- a
JSCValue
representing the last value generated by the script.
-
evaluateWithSourceUri
Evaluatecode
in this Context usinguri
as the source URI. ThelineNumber
is the starting line number inuri
; the value is one-based so the first line is 1.uri
andlineNumber
will be shown in exceptions and they don't affect the behavior of the script.- Parameters:
code
- a JavaScript script to evaluatelength
- length ofcode
, or -1 ifcode
is a nul-terminated stringuri
- the source URIlineNumber
- the starting line number- Returns:
- a
JSCValue
representing the last value generated by the script.
-
getException
Get the last unhandled exception thrown in this Context by API functions calls.- Returns:
- a
JSCException
ornull
if there isn't any unhandled exception in theJSCContext
.
-
getGlobalObject
Get aJSCValue
referencing the this Context global object- Returns:
- a
JSCValue
-
getValue
-
getVirtualMachine
Get theJSCVirtualMachine
where this Context was created.- Returns:
- the
JSCVirtualMachine
where theJSCContext
was created.
-
popExceptionHandler
public void popExceptionHandler()Remove the lastJSCExceptionHandler
previously pushed to this Context with jsc_context_push_exception_handler(). -
pushExceptionHandler
Push an exception handler in this Context. Whenever a JavaScript exception happens in theJSCContext
, the givenhandler
will be called. The defaultJSCExceptionHandler
simply calls jsc_context_throw_exception() to throw the exception to theJSCContext
. If you don't want to catch the exception, but only get notified about it, call jsc_context_throw_exception() inhandler
like the default one does. The last exception handler pushed is the only one used by theJSCContext
, use jsc_context_pop_exception_handler() to remove it and set the previous one. Whenhandler
is removed from the context,destroyNotify
i called withuserData
as parameter.- Parameters:
handler
- aJSCExceptionHandler
-
registerClass
public Class registerClass(String name, @Nullable @Nullable Class parentClass, @Nullable @Nullable ClassVTable vtable) Register a custom class in this Context using the givenname
. If the new class inherits from anotherJSCClass
, the parent should be passed asparentClass
, otherwisenull
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 theJSCClass
is cleared in the context,destroyNotify
is called with the instance as parameter.- Parameters:
name
- the class nameparentClass
- aJSCClass
ornull
vtable
- an optionalJSCClassVTable
ornull
- Returns:
- a
JSCClass
-
setValue
-
throw_
Throw an exception to this Context using the given error message. The createdJSCException
can be retrieved with jsc_context_get_exception().- Parameters:
errorMessage
- an error message
-
throwException
Throwexception
to this Context.- Parameters:
exception
- aJSCException
-
throwPrintf
Throw an exception to this Context using the given formatted string as error message. The createdJSCException
can be retrieved with jsc_context_get_exception().- Parameters:
format
- the string formatvarargs
- the parameters to insert into the format string
-
throwWithName
-
throwWithNamePrintf
Throw an exception to this Context using the given error name and the formatted string as error message. The createdJSCException
can be retrieved with jsc_context_get_exception().- Parameters:
errorName
- the error nameformat
- the string formatvarargs
- the parameters to insert into the format string
-
builder
AContext.Builder
object constructs aContext
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withContext.Builder.build()
.
-