Package org.webkitgtk.jsc
Class Context
java.lang.Object
org.javagi.base.ProxyInstance
org.gnome.gobject.TypeInstance
org.gnome.gobject.GObject
org.webkitgtk.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
Nested ClassesModifier and TypeClassDescriptionstatic classContext.Builder<B extends Context.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static classNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionContext()Creates a new Context.Context(MemorySegment address) Create a Context proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected ContextasParent()Returns this instance as if it were its parent type.static Context.Builder<? extends Context.Builder> builder()AContext.Builderobject constructs aContextwith the specified properties.checkSyntax(String code, long length, CheckSyntaxMode mode, String uri, int lineNumber, @Nullable Out<Exception> exception) Check the givencodein this Context for syntax errors.voidClear the uncaught exception in this Context if any.Evaluatecodeincontext.evaluateInObject(String code, long length, @Nullable MemorySegment objectInstance, @Nullable Class objectClass, String uri, int lineNumber, Out<Value> object) Evaluatecodeand create an new object where symbols defined incodewill be added as properties, instead of being added to this Context global object.evaluateWithSourceUri(String code, long length, String uri, int lineNumber) Evaluatecodein this Context usingurias the source URI.static @Nullable ContextGet theJSCContextthat is currently executing a function.@Nullable ExceptionGet the last unhandled exception thrown in this Context by API functions calls.Get aJSCValuereferencing the this Context global objectstatic @Nullable TypegetType()Get the GType of the Context classGet a property of this Context global object withname.Get theJSCVirtualMachinewhere this Context was created.voidRemove the lastJSCExceptionHandlerpreviously pushed to this Context with jsc_context_push_exception_handler().voidpushExceptionHandler(@Nullable ExceptionHandler handler) Push an exception handler incontext.Whenever a JavaScript exception happens in theJSCContext, the givenhandlerwill be called.registerClass(String name, @Nullable Class parentClass, @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,otherwisenullshould be used.voidSet a property of this Context global object withnameandvalue.voidThrow an exception to this Context using the given error message.voidthrowException(Exception exception) Throwexceptiontocontext.voidthrowPrintf(String format, Object... varargs) Throw an exception to this Context using the given formatted string as error message.voidthrowWithName(String errorName, String errorMessage) Throw an exception to this Context using the given error name and message.voidthrowWithNamePrintf(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 ContextCreate a newJSCContextinvirtualMachine.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, 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, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.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
public Context()Creates a new Context.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, 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 newJSCContextinvirtualMachine.- Parameters:
vm- aJSCVirtualMachine- Returns:
- the newly created
JSCContext.
-
getCurrent
Get theJSCContextthat is currently executing a function. This should only be called within a function or method callback, otherwisenullwill be returned.- Returns:
- the
JSCContextthat is currently executing.
-
checkSyntax
public CheckSyntaxResult checkSyntax(String code, long length, CheckSyntaxMode mode, String uri, int lineNumber, @Nullable Out<Exception> exception) Check the givencodein this Context for syntax errors. ThelineNumberis the starting line number inuri;the value is one-based so the first line is 1.uriandlineNumberare only used to fill theexception.In case of errorsexceptionwill be set to a newJSCExceptionwith the details. You can passnulltoexceptionto ignore the error details.- Parameters:
code- a JavaScript script to checklength- length ofcode,or -1 ifcodeis a nul-terminated stringmode- aJSCCheckSyntaxModeuri- the source URIlineNumber- the starting line numberexception- return location for aJSCException, ornullto 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 MemorySegment objectInstance, @Nullable Class objectClass, String uri, int lineNumber, Out<Value> object) Evaluatecodeand create an new object where symbols defined incodewill be added as properties, instead of being added to this Context global object. The new object is returned asobjectparameter. Similar to how jsc_value_new_object() works, ifobjectInstanceis notnullobjectClassmust be provided too. ThelineNumberis the starting line number inuri;the value is one-based so the first line is 1.uriandlineNumberwill 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 ifcodeis a nul-terminated stringobjectInstance- an object instanceobjectClass- aJSCClassornullto use the defaulturi- the source URIlineNumber- the starting line numberobject- return location for aJSCValue.- Returns:
- a
JSCValuerepresenting the last value generated by the script.
-
evaluateWithSourceUri
Evaluatecodein this Context usingurias the source URI. ThelineNumberis the starting line number inuri;the value is one-based so the first line is 1.uriandlineNumberwill 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 ifcodeis a nul-terminated stringuri- the source URIlineNumber- the starting line number- Returns:
- a
JSCValuerepresenting the last value generated by the script.
-
getException
Get the last unhandled exception thrown in this Context by API functions calls.- Returns:
- a
JSCExceptionornullif there isn't any unhandled exception in theJSCContext.
-
getGlobalObject
Get aJSCValuereferencing the this Context global object- Returns:
- a
JSCValue
-
getValue
-
getVirtualMachine
Get theJSCVirtualMachinewhere this Context was created.- Returns:
- the
JSCVirtualMachinewhere theJSCContextwas created.
-
popExceptionHandler
public void popExceptionHandler()Remove the lastJSCExceptionHandlerpreviously pushed to this Context with jsc_context_push_exception_handler(). -
pushExceptionHandler
Push an exception handler incontext.Whenever a JavaScript exception happens in theJSCContext, the givenhandlerwill be called. The defaultJSCExceptionHandlersimply 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() inhandlerlike 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. Whenhandleris removed from the context,destroyNotifyi called withuserDataas parameter.- Parameters:
handler- aJSCExceptionHandler
-
registerClass
Register a custom class in this Context using the givenname.If the new class inherits from anotherJSCClass, the parent should be passed asparentClass,otherwisenullshould 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 theJSCClassis cleared in the context,destroyNotifyis called with the instance as parameter.- Parameters:
name- the class nameparentClass- aJSCClassornullvtable- an optionalJSCClassVTableornull- Returns:
- a
JSCClass
-
setValue
-
throw_
Throw an exception to this Context using the given error message. The createdJSCExceptioncan be retrieved with jsc_context_get_exception().- Parameters:
errorMessage- an error message
-
throwException
Throwexceptiontocontext.- Parameters:
exception- aJSCException
-
throwPrintf
Throw an exception to this Context using the given formatted string as error message. The createdJSCExceptioncan 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 createdJSCExceptioncan 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.Builderobject constructs aContextwith the specified properties. Use the variousset...()methods to set properties, and finish construction withContext.Builder.build().- Returns:
- the builder object
-