JavaScriptScriptEngine Class

A ScriptEngine implemented with Meziantou.Framework.JavaScriptEngine: set it as the BrowsingContextOptions.ScriptEngine of a browsing context to run the scripts of its documents. The windows of the browsing contexts share a JsEngine (the similar-origin window agent), and each window is the global object of its own realm, created on first use. The DOM is exposed with its WebIDL bindings.

public class JavaScriptScriptEngine : ScriptEngine, IDisposable

Remarks

The engine is not thread-safe: use it from the thread of the DomEventLoop of the browsing contexts. See JavaScriptScriptEngineOptions for the limits of the resources the scripts can use.

Inheritance
ScriptEngine JavaScriptScriptEngine
Implements

Constructors

NameDescription
JavaScriptScriptEngine(JavaScriptScriptEngineOptions?)Creates a script engine.
JavaScriptScriptEngine(JavaScriptScriptEngineOptions?, CancellationToken)Creates the script engine of a worker (see JavaScriptScriptEngine.CreateWorkerScriptEngine).

JavaScriptScriptEngine overloads

JavaScriptScriptEngine(JavaScriptScriptEngineOptions?)

JavaScriptScriptEngine.cs

Creates a script engine.

public JavaScriptScriptEngine(JavaScriptScriptEngineOptions? options = null)
Parameters
options JavaScriptScriptEngineOptions?
The options, or null for the default options.

JavaScriptScriptEngine(JavaScriptScriptEngineOptions?, CancellationToken)

JavaScriptScriptEngine.cs

Creates the script engine of a worker (see JavaScriptScriptEngine.CreateWorkerScriptEngine).

protected JavaScriptScriptEngine(JavaScriptScriptEngineOptions? options, CancellationToken terminationToken)
Parameters
options JavaScriptScriptEngineOptions?
The options, or null for the default options.
terminationToken CancellationToken
Canceled when the worker is terminated: the running script is aborted.

Properties

NameDescription
EngineGets the JavaScript engine (the agent of the windows).
IsScriptRunningWhether JavaScript code is running: the .NET code that calls into the engine uses JavaScriptScriptEngine.EnterJavaScript.
OptionsGets the options of the engine, also used by the engines of the workers.
SupportsModuleScriptsGets whether the engine supports the module scripts (<script type="module">, import()). When it is false (the default), the module scripts are not fetched nor run.

Gets the JavaScript engine (the agent of the windows).

public JsEngine Engine { get; }

Property value

JsEngine

Whether JavaScript code is running: the .NET code that calls into the engine uses JavaScriptScriptEngine.EnterJavaScript.

protected internal override bool IsScriptRunning { get; }

Property value

bool

Gets the options of the engine, also used by the engines of the workers.

protected JavaScriptScriptEngineOptions Options { get; }

SupportsModuleScripts

JavaScriptModules.cs

Gets whether the engine supports the module scripts (<script type="module">, import()). When it is false (the default), the module scripts are not fetched nor run.

protected internal override bool SupportsModuleScripts { get; }

Property value

bool

Methods

NameDescription
Call(Window, JsValue, JsValue, ReadOnlySpan<JsValue>)Calls a function in the realm of a window, as a callback of the host (e.g. a function returned by JavaScriptScriptEngine.Evaluate). The microtasks are not run: call DomEventLoop.PerformMicrotaskCheckpoint afterwards. The strings the function compiles while it runs (eval(), Function()) are not restricted by the content security policy of the document.
CompileEventHandler(EventHandlerSource)https://html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler (step 1.9)
CreateRealm(GlobalScope)The realms are created on first use: most windows (e.g. the initial about:blank documents) never run scripts.
CreateWorkerScriptEngine(CancellationToken)Creates the engine of a worker: a new engine with the same options (a subclass should override it to create an instance of its own type).
Dispose()Releases the engine.
Dispose(bool)Releases the engine.
EnterNestedMicrotaskQueue()Enters a separate microtask queue: until the returned scope is disposed, the jobs of the engine and the microtasks queued with ScriptEngine.QueueMicrotask go to a new queue, which ScriptEngine.PerformMicrotaskCheckpoint runs; the microtasks that were pending are kept, and are not run in the scope. The scopes nest. A script is considered running in the scope (ScriptEngine.IsScriptRunning): the callbacks invoked in it perform no microtask checkpoint.
Evaluate(Window, JsPreparedScript)Runs a script in the realm of a window: a script parsed once with JsPreparedScript.Parse runs in several realms without being parsed again (e.g. a large library injected in each document). The microtasks are not run: call DomEventLoop.PerformMicrotaskCheckpoint afterwards.
Evaluate(Window, string)Evaluates code in the realm of a window, as an indirect eval. The microtasks are not run: call DomEventLoop.PerformMicrotaskCheckpoint afterwards. As the code evaluated by the developer tools of a browser, the code and the strings it compiles while it runs (eval(), Function()) are not restricted by the content security policy of the document.
ExecuteClassicScript(ClassicScript)Runs a classic script (https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script) in the realm of ClassicScript.Global. The library owns the ordering of the scripts and the "clean up after running script" steps: the engine only evaluates the code.
ExecuteModuleScript(ModuleScript)Runs a module script (https://html.spec.whatwg.org/multipage/webappapis.html#run-a-module-script): links and evaluates the module graph, and reports the error to rethrow or the evaluation errors (including the errors of a top-level await) to the global object.
ExtractErrorInformation(object?)Extracts the information of an exception (https://html.spec.whatwg.org/multipage/webappapis.html#extract-error): its message, the location of the script, and the value of the error attribute of the ErrorEvent.
FetchModuleScriptGraph(GlobalScope, string, ModuleFetchOptions, Element?, Action<ModuleScript?>)Fetches the graph of an external module script ("fetch a module script graph", https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-module-script-tree): the root is fetched ("fetch a single module script"), then its descendants are loaded. onComplete is called with the module script, or with null when a module of the graph cannot be fetched. A parse error is stored in ModuleScript.ErrorToRethrow.
FromJsValue(Window, JsValue)Converts a value of the realm of a window to a .NET value, as the IDL type any: the wrapper object of a platform object is the .NET object (e.g. a Node), a string, a number and a boolean are the .NET values, undefined and null are null, and the other values (e.g. a plain object) are returned as they are.
GetActiveScriptBaseUrl()Returns the base URL of the active script (https://html.spec.whatwg.org/multipage/webappapis.html#active-script), or null when no script is running: the base URL of the strings compiled by setTimeout() and setInterval().
GetActiveScriptFetchOptions()Returns the fetch options of the active script (https://html.spec.whatwg.org/multipage/webappapis.html#active-script), or null when no script is running: the options of the modules imported by import() in a classic script.
GetCurrentGlobal()The incumbent global object while a script runs (https://html.spec.whatwg.org/multipage/webappapis.html#incumbent): the global object of the realm of the code that calls the platform (e.g. the source of the messages of postMessage()), the code of the bindings runs in the realm of its caller.
GetCurrentScriptLocation()Returns the location of the code that is running (the source file, line and column of the innermost running script), or null when no script is running or when the engine cannot extract it: the source location of the violations of the content security policies (https://w3c.github.io/webappsec-csp/#create-violation-for-global, step 2).
IsPromiseHandled(object)Gets whether a promise has a handler (its [[PromiseIsHandled]] internal slot). The promises rejected without handler are given to GlobalScope.TrackPromiseRejection by the engine.
LoadModuleScriptGraph(ModuleScript, Action<bool>)Parses a module script and loads its descendants ("fetch the descendants of and link a module script", https://html.spec.whatwg.org/multipage/webappapis.html#fetch-the-descendants-of-and-link-a-module-script): the engine resolves the module specifiers and fetches the modules with the library. onComplete is called once the graph is loaded, with true, or with false when a module of the graph cannot be fetched (the script element then fires an error event). A parse error is stored in ModuleScript.ErrorToRethrow.
OnRealmCreated(GlobalScope, JsObject)Called when the realm of a window is created, before a script runs in it: the host can add its own functions to the global object.
ParseClassicScript(ClassicScript)Parses a classic script (https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script) without running it: returns the error to rethrow of a script that cannot be parsed (e.g. the SyntaxError of a worker script, which fires an error event at the Worker object instead of being reported), or null.
PerformMicrotaskCheckpoint()Runs the jobs of the engine and the microtasks queued with ScriptEngine.QueueMicrotask until the queue is empty (https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint). The exceptions of the jobs are reported by the engine.
PreloadModuleScript(GlobalScope, string, ModuleType, RequestDestination, ModuleFetchOptions, Element?, Action<bool>)Fetches a module script without loading its descendants nor running it ("fetch a modulepreload module script graph", https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-modulepreload-module-script-graph): the module of a <link rel=modulepreload> element. onComplete is called with true when the module is fetched (even when it has a parse error), or with false when it cannot be fetched.
PrepareToRunScript(GlobalScope)https://html.spec.whatwg.org/multipage/webappapis.html#prepare-to-run-script: the JavaScript execution context stack is not empty until the returned scope is disposed, which cleans up after running script (a microtask checkpoint when the stack becomes empty). The callbacks invoked in the scope do not perform a microtask checkpoint.
QueueMicrotask(Action)Queues a microtask of the library (e.g. a mutation observer notification) in the job queue of the engine, so that the microtasks of the library and the jobs of the engine (promise reactions) run in order (https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-microtask).
StructuredDeserialize(GlobalScope, object?)Deserializes a value serialized by ScriptEngine.StructuredSerialize in the realm of global (https://html.spec.whatwg.org/multipage/structured-data.html#structureddeserialize): the result is a new value of the realm (e.g. the MessageEvent.Data of the message events).
StructuredSerialize(GlobalScope, object?)Serializes a value (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserialize), e.g. the message of BroadcastChannel.PostMessage. The result is given to ScriptEngine.StructuredDeserialize, possibly several times and for other realms.
StructuredSerialize(GlobalScope, object?, IReadOnlyList<object>)Serializes a value and transfers the objects of transferList (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializewithtransfer), e.g. for GlobalScope.StructuredClone: the transferred objects are detached.
StructuredSerializeForAgent(GlobalScope, object?, IReadOnlyList<object>)Serializes a value for another agent (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializewithtransfer), e.g. a message posted to a worker or to a MessagePort: the result is given to ScriptEngine.StructuredDeserialize of another engine instance, on another thread, so it must not reference the values of this engine. The objects of transferList are transferred: the ArrayBuffer objects of the engine, and the MessagePort objects.
StructuredSerializeForStorage(GlobalScope, object?)Serializes a value for storage (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeforstorage), e.g. the state of History.PushState: a SharedArrayBuffer throws a DataCloneError.
ToJsValue(Window, object?)Converts a .NET value to a value of the realm of a window, as the IDL type any: a platform object (e.g. a Node, an Event) is its wrapper object, as the scripts of the window see it; a string, a number and a boolean are the primitive values; null is null.
WaitForAll(GlobalScope, IReadOnlyList<object?>, Action, Action<object?>)Runs the success steps once all the values are fulfilled promises, or the failure steps with the reason of the first rejected one (https://webidl.spec.whatwg.org/#wait-for-all). The values are values of the engine, resolved as promises of the realm of global, or RejectedPromise for a promise rejected with an exception.

Call(Window, JsValue, JsValue, ReadOnlySpan<JsValue>)

JavaScriptScriptEngine.cs

Calls a function in the realm of a window, as a callback of the host (e.g. a function returned by JavaScriptScriptEngine.Evaluate). The microtasks are not run: call DomEventLoop.PerformMicrotaskCheckpoint afterwards. The strings the function compiles while it runs (eval(), Function()) are not restricted by the content security policy of the document.

public JsValue Call(Window window, JsValue function, JsValue thisValue, params scoped ReadOnlySpan<JsValue> arguments)

Parameters

window Window
The window.
function JsValue
The function.
thisValue JsValue
The this value of the call.
arguments ReadOnlySpan<JsValue>
The arguments.

Returns

JsValue The value returned by the function.

Exceptions

JsException
The function threw an exception.

CompileEventHandler(EventHandlerSource)

JavaScriptScriptEngine.cs

https://html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler (step 1.9)

protected internal override EventHandlerCallback CompileEventHandler(EventHandlerSource source)

CreateRealm(GlobalScope)

JavaScriptScriptEngine.cs

The realms are created on first use: most windows (e.g. the initial about:blank documents) never run scripts.

protected internal override void CreateRealm(GlobalScope global)

Parameters

global GlobalScope

CreateWorkerScriptEngine(CancellationToken)

JavaScriptScriptEngine.cs

Creates the engine of a worker: a new engine with the same options (a subclass should override it to create an instance of its own type).

protected internal override ScriptEngine? CreateWorkerScriptEngine(CancellationToken terminationToken)

Parameters

terminationToken CancellationToken

Returns

ScriptEngine?

Dispose overloads

Releases the engine.

public void Dispose()

Releases the engine.

protected virtual void Dispose(bool disposing)
Parameters
disposing bool

EnterNestedMicrotaskQueue()

JavaScriptScriptEngine.cs

Enters a separate microtask queue: until the returned scope is disposed, the jobs of the engine and the microtasks queued with ScriptEngine.QueueMicrotask go to a new queue, which ScriptEngine.PerformMicrotaskCheckpoint runs; the microtasks that were pending are kept, and are not run in the scope. The scopes nest. A script is considered running in the scope (ScriptEngine.IsScriptRunning): the callbacks invoked in it perform no microtask checkpoint.

protected internal override IDisposable EnterNestedMicrotaskQueue()

Returns

IDisposable The scope, which restores the previous microtask queue when it is disposed.

Remarks

The callbacks of a worklet are specified to run in another agent, with its own microtask queue: the layout of a display: layout() box runs the microtasks of its layout worklet until the promise of its callback is settled (https://drafts.css-houdini.org/css-layout-api-1/#run-a-work-queue), possibly while a script of the document reads a geometry, and the microtasks of that script must not run. The default implementation returns a scope that does nothing: the microtasks of the document then run with the ones of the worklet.

Evaluate overloads

Evaluate(Window, JsPreparedScript)

JavaScriptScriptEngine.cs

Runs a script in the realm of a window: a script parsed once with JsPreparedScript.Parse runs in several realms without being parsed again (e.g. a large library injected in each document). The microtasks are not run: call DomEventLoop.PerformMicrotaskCheckpoint afterwards.

public JsValue Evaluate(Window window, JsPreparedScript script)
Parameters
window Window
The window.
script JsPreparedScript
The parsed script.
Returns

JsValue The completion value of the script.

Evaluate(Window, string)

JavaScriptScriptEngine.cs

Evaluates code in the realm of a window, as an indirect eval. The microtasks are not run: call DomEventLoop.PerformMicrotaskCheckpoint afterwards. As the code evaluated by the developer tools of a browser, the code and the strings it compiles while it runs (eval(), Function()) are not restricted by the content security policy of the document.

public JsValue Evaluate(Window window, string code)
Parameters
window Window
The window.
code string
The code.
Returns

JsValue The completion value.

ExecuteClassicScript(ClassicScript)

JavaScriptScriptEngine.cs

Runs a classic script (https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script) in the realm of ClassicScript.Global. The library owns the ordering of the scripts and the "clean up after running script" steps: the engine only evaluates the code.

protected internal override object? ExecuteClassicScript(ClassicScript script)

Parameters

script ClassicScript
The script to run.

Returns

object? The completion value of the script: a String when the value is a JavaScript string (the result of a javascript: URL is a string), otherwise any value (the library ignores it).

Exceptions

Exception
Any exception thrown when the script cannot be parsed or when its evaluation throws (an abrupt completion). The library reports it (https://html.spec.whatwg.org/multipage/webappapis.html#report-an-exception), using ScriptEngine.ExtractErrorInformation.

ExecuteModuleScript(ModuleScript)

JavaScriptModules.cs

Runs a module script (https://html.spec.whatwg.org/multipage/webappapis.html#run-a-module-script): links and evaluates the module graph, and reports the error to rethrow or the evaluation errors (including the errors of a top-level await) to the global object.

protected internal override void ExecuteModuleScript(ModuleScript script)

Parameters

script ModuleScript
The module script, whose graph is loaded.

ExtractErrorInformation(object?)

JavaScriptScriptEngine.cs

Extracts the information of an exception (https://html.spec.whatwg.org/multipage/webappapis.html#extract-error): its message, the location of the script, and the value of the error attribute of the ErrorEvent.

protected internal override ScriptError ExtractErrorInformation(object? exception)

Parameters

exception object?
A .NET Exception thrown by the engine or by the library, or a value of the engine (e.g. the reason of a rejected promise, or the argument of reportError()).

Returns

ScriptError The error information. The default implementation uses the message of the .NET exceptions.

FetchModuleScriptGraph(GlobalScope, string, ModuleFetchOptions, Element?, Action<ModuleScript?>)

JavaScriptModules.cs

Fetches the graph of an external module script ("fetch a module script graph", https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-module-script-tree): the root is fetched ("fetch a single module script"), then its descendants are loaded. onComplete is called with the module script, or with null when a module of the graph cannot be fetched. A parse error is stored in ModuleScript.ErrorToRethrow.

protected internal override void FetchModuleScriptGraph(GlobalScope global, string url, ModuleFetchOptions options, Element? element, Action<ModuleScript?> onComplete)

Parameters

global GlobalScope
The global object of the realm in which the script runs.
url string
The URL of the root.
options ModuleFetchOptions
The fetch options of the script.
element Element?
The script element that fetches the script, or null.
onComplete Action<ModuleScript?>
Called when the graph is loaded (possibly synchronously).

Remarks

The default implementation fetches the root with the library, then calls ScriptEngine.LoadModuleScriptGraph. An engine with a module map overrides it to reuse the module of the URL when it is already fetched (the module is evaluated once, and its parse error is remembered).

FromJsValue(Window, JsValue)

JavaScriptScriptEngine.cs

Converts a value of the realm of a window to a .NET value, as the IDL type any: the wrapper object of a platform object is the .NET object (e.g. a Node), a string, a number and a boolean are the .NET values, undefined and null are null, and the other values (e.g. a plain object) are returned as they are.

public object? FromJsValue(Window window, JsValue value)

Parameters

window Window
The window.
value JsValue
The value of the realm.

Returns

object? The .NET value.

GetActiveScriptBaseUrl()

JavaScriptScriptEngine.cs

Returns the base URL of the active script (https://html.spec.whatwg.org/multipage/webappapis.html#active-script), or null when no script is running: the base URL of the strings compiled by setTimeout() and setInterval().

protected internal override string? GetActiveScriptBaseUrl()

Returns

string? The base URL of the innermost running classic or module script, or null.

GetActiveScriptFetchOptions()

JavaScriptScriptEngine.cs

Returns the fetch options of the active script (https://html.spec.whatwg.org/multipage/webappapis.html#active-script), or null when no script is running: the options of the modules imported by import() in a classic script.

protected internal override ModuleFetchOptions? GetActiveScriptFetchOptions()

Returns

ModuleFetchOptions? The fetch options of the innermost running classic or module script, or null.

GetCurrentGlobal()

JavaScriptScriptEngine.cs

The incumbent global object while a script runs (https://html.spec.whatwg.org/multipage/webappapis.html#incumbent): the global object of the realm of the code that calls the platform (e.g. the source of the messages of postMessage()), the code of the bindings runs in the realm of its caller.

protected internal override GlobalScope? GetCurrentGlobal()

GetCurrentScriptLocation()

JavaScriptScriptEngine.cs

Returns the location of the code that is running (the source file, line and column of the innermost running script), or null when no script is running or when the engine cannot extract it: the source location of the violations of the content security policies (https://w3c.github.io/webappsec-csp/#create-violation-for-global, step 2).

protected internal override ScriptLocation? GetCurrentScriptLocation()

Returns

ScriptLocation? The location of the running code, or null.

IsPromiseHandled(object)

JavaScriptScriptEngine.cs

Gets whether a promise has a handler (its [[PromiseIsHandled]] internal slot). The promises rejected without handler are given to GlobalScope.TrackPromiseRejection by the engine.

protected internal override bool IsPromiseHandled(object promise)

Parameters

promise object
A promise given to GlobalScope.TrackPromiseRejection.

Returns

bool Whether the promise is handled. The default implementation returns false.

LoadModuleScriptGraph(ModuleScript, Action<bool>)

JavaScriptModules.cs

Parses a module script and loads its descendants ("fetch the descendants of and link a module script", https://html.spec.whatwg.org/multipage/webappapis.html#fetch-the-descendants-of-and-link-a-module-script): the engine resolves the module specifiers and fetches the modules with the library. onComplete is called once the graph is loaded, with true, or with false when a module of the graph cannot be fetched (the script element then fires an error event). A parse error is stored in ModuleScript.ErrorToRethrow.

protected internal override void LoadModuleScriptGraph(ModuleScript script, Action<bool> onComplete)

Parameters

script ModuleScript
The root of the module graph.
onComplete Action<bool>
Called when the graph is loaded (possibly synchronously).

OnRealmCreated(GlobalScope, JsObject)

JavaScriptScriptEngine.cs

Called when the realm of a window is created, before a script runs in it: the host can add its own functions to the global object.

protected virtual void OnRealmCreated(GlobalScope global, JsObject globalObject)

Parameters

global GlobalScope
The .NET global object: a Window, or the global object of a worker.
globalObject JsObject
The global object of the realm, whose prototype chain implements the interface of global.

ParseClassicScript(ClassicScript)

JavaScriptScriptEngine.cs

Parses a classic script (https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script) without running it: returns the error to rethrow of a script that cannot be parsed (e.g. the SyntaxError of a worker script, which fires an error event at the Worker object instead of being reported), or null.

protected internal override object? ParseClassicScript(ClassicScript script)

Parameters

script ClassicScript
The script, run afterwards by ScriptEngine.ExecuteClassicScript when it can be parsed.

Returns

object? The parse error (a value of the engine), or null. The default implementation returns null: the parse errors are thrown by ScriptEngine.ExecuteClassicScript.

PerformMicrotaskCheckpoint()

JavaScriptScriptEngine.cs

Runs the jobs of the engine and the microtasks queued with ScriptEngine.QueueMicrotask until the queue is empty (https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint). The exceptions of the jobs are reported by the engine.

protected internal override void PerformMicrotaskCheckpoint()

PreloadModuleScript(GlobalScope, string, ModuleType, RequestDestination, ModuleFetchOptions, Element?, Action<bool>)

JavaScriptModules.cs

Fetches a module script without loading its descendants nor running it ("fetch a modulepreload module script graph", https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-modulepreload-module-script-graph): the module of a <link rel=modulepreload> element. onComplete is called with true when the module is fetched (even when it has a parse error), or with false when it cannot be fetched.

protected internal override void PreloadModuleScript(GlobalScope global, string url, ModuleType type, RequestDestination destination, ModuleFetchOptions options, Element? element, Action<bool> onComplete)

Parameters

global GlobalScope
The global object of the realm of the module.
url string
The URL of the module.
type ModuleType
The type of the module, determined by the destination.
destination RequestDestination
The destination of the request of a JavaScript module (a script-like destination).
options ModuleFetchOptions
The fetch options of the module.
element Element?
The link element that fetches the module, or null.
onComplete Action<bool>
Called when the module is fetched (possibly synchronously).

Remarks

The default implementation fetches the module with the library and ignores its response. An engine with a module map overrides it to add the module to the module map, so that the module scripts and the imports do not fetch it again.

PrepareToRunScript(GlobalScope)

JavaScriptScriptEngine.cs

https://html.spec.whatwg.org/multipage/webappapis.html#prepare-to-run-script: the JavaScript execution context stack is not empty until the returned scope is disposed, which cleans up after running script (a microtask checkpoint when the stack becomes empty). The callbacks invoked in the scope do not perform a microtask checkpoint.

protected internal override IDisposable PrepareToRunScript(GlobalScope global)

Parameters

global GlobalScope
The global object of the settings object.

Returns

IDisposable The scope. The default implementation returns a scope that does nothing.

QueueMicrotask(Action)

JavaScriptScriptEngine.cs

Queues a microtask of the library (e.g. a mutation observer notification) in the job queue of the engine, so that the microtasks of the library and the jobs of the engine (promise reactions) run in order (https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-microtask).

protected internal override void QueueMicrotask(Action microtask)

Parameters

microtask Action
The microtask. The engine runs it when it performs a microtask checkpoint.

StructuredDeserialize(GlobalScope, object?)

JavaScriptScriptEngine.cs

Deserializes a value serialized by ScriptEngine.StructuredSerialize in the realm of global (https://html.spec.whatwg.org/multipage/structured-data.html#structureddeserialize): the result is a new value of the realm (e.g. the MessageEvent.Data of the message events).

protected internal override object? StructuredDeserialize(GlobalScope global, object? serialized)

Parameters

global GlobalScope
The global object of the target realm.
serialized object?
The value returned by ScriptEngine.StructuredSerialize.

Returns

object? The deserialized value.

StructuredSerialize overloads

StructuredSerialize(GlobalScope, object?)

JavaScriptScriptEngine.cs

Serializes a value (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserialize), e.g. the message of BroadcastChannel.PostMessage. The result is given to ScriptEngine.StructuredDeserialize, possibly several times and for other realms.

protected internal override object? StructuredSerialize(GlobalScope global, object? value)
Parameters
global GlobalScope
The global object of the current realm.
value object?
The value: a value of the engine, a .NET platform object, or null for undefined (the value of an IDL any argument).
Returns

object? The serialized value.

Exceptions
DomException
A DataCloneError when the value cannot be serialized.
Remarks

The default implementation serializes the .NET primitive values and the serializable platform objects.

StructuredSerialize(GlobalScope, object?, IReadOnlyList<object>)

JavaScriptScriptEngine.cs

Serializes a value and transfers the objects of transferList (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializewithtransfer), e.g. for GlobalScope.StructuredClone: the transferred objects are detached.

protected internal override object? StructuredSerialize(GlobalScope global, object? value, IReadOnlyList<object> transferList)
Parameters
global GlobalScope
The global object of the current realm.
value object?
The value (see ScriptEngine.StructuredSerialize).
transferList IReadOnlyList<object>
The objects to transfer: values of the engine or .NET platform objects.
Returns

object? The serialized value, given to ScriptEngine.StructuredDeserialize.

Exceptions
DomException
A DataCloneError when the value cannot be serialized or an object cannot be transferred.
Remarks

The default implementation does not support transferring objects.

StructuredSerializeForAgent(GlobalScope, object?, IReadOnlyList<object>)

JavaScriptScriptEngine.cs

Serializes a value for another agent (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializewithtransfer), e.g. a message posted to a worker or to a MessagePort: the result is given to ScriptEngine.StructuredDeserialize of another engine instance, on another thread, so it must not reference the values of this engine. The objects of transferList are transferred: the ArrayBuffer objects of the engine, and the MessagePort objects.

protected internal override object? StructuredSerializeForAgent(GlobalScope global, object? value, IReadOnlyList<object> transferList)

Parameters

global GlobalScope
The global object of the current realm.
value object?
The value (see ScriptEngine.StructuredSerialize).
transferList IReadOnlyList<object>
The objects to transfer.

Returns

object? The serialized value.

Exceptions

DomException
A DataCloneError when the value cannot be serialized or an object cannot be transferred.

Remarks

The default implementation calls ScriptEngine.StructuredSerialize, whose default result does not depend on the engine.

StructuredSerializeForStorage(GlobalScope, object?)

JavaScriptScriptEngine.cs

Serializes a value for storage (https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeforstorage), e.g. the state of History.PushState: a SharedArrayBuffer throws a DataCloneError.

protected internal override object? StructuredSerializeForStorage(GlobalScope global, object? value)

Parameters

global GlobalScope
The global object of the current realm.
value object?
The value (see ScriptEngine.StructuredSerialize).

Returns

object? The serialized value, given to ScriptEngine.StructuredDeserialize.

Remarks

The default implementation calls ScriptEngine.StructuredSerialize.

ToJsValue(Window, object?)

JavaScriptScriptEngine.cs

Converts a .NET value to a value of the realm of a window, as the IDL type any: a platform object (e.g. a Node, an Event) is its wrapper object, as the scripts of the window see it; a string, a number and a boolean are the primitive values; null is null.

public JsValue ToJsValue(Window window, object? value)

Parameters

window Window
The window.
value object?
The .NET value.

Returns

JsValue The value of the realm.

WaitForAll(GlobalScope, IReadOnlyList<object?>, Action, Action<object?>)

JavaScriptScriptEngine.cs

Runs the success steps once all the values are fulfilled promises, or the failure steps with the reason of the first rejected one (https://webidl.spec.whatwg.org/#wait-for-all). The values are values of the engine, resolved as promises of the realm of global, or RejectedPromise for a promise rejected with an exception.

protected internal override void WaitForAll(GlobalScope global, IReadOnlyList<object?> values, Action successSteps, Action<object?> failureSteps)

Parameters

global GlobalScope
The global object of the realm of the promises.
values IReadOnlyList<object?>
The values.
successSteps Action
The success steps, run by a promise reaction (or a microtask when there is no value).
failureSteps Action<object?>
The failure steps, given the rejection reason.

Remarks

The default implementation considers that the values are fulfilled promises: it queues a microtask to run the success steps.