public final class

JavaScriptIsolate

extends java.lang.Object

implements java.lang.AutoCloseable

 java.lang.Object

↳androidx.javascriptengine.JavaScriptIsolate

Gradle dependencies

compile group: 'androidx.javascriptengine', name: 'javascriptengine', version: '1.0.0-beta01'

  • groupId: androidx.javascriptengine
  • artifactId: javascriptengine
  • version: 1.0.0-beta01

Artifact androidx.javascriptengine:javascriptengine:1.0.0-beta01 it located at Google repository (https://maven.google.com/)

Overview

Environment within a JavaScriptSandbox where JavaScript is executed.

A single JavaScriptSandbox process can contain any number of JavaScriptIsolate instances where JS can be evaluated independently and in parallel.

Each isolate has its own state and JS global object, and cannot interact with any other isolate through JS APIs. There is only a moderate security boundary between isolates in a single JavaScriptSandbox. If the code in one JavaScriptIsolate is able to compromise the security of the JS engine then it may be able to observe or manipulate other isolates, since they run in the same process. For strong isolation multiple JavaScriptSandbox processes should be used, but it is not supported at the moment. Please find the feature request here.

This class is thread-safe.

Summary

Methods
public voidaddOnTerminatedCallback(Consumer<TerminationInfo> callback)

Add a callback to listen for isolate crashes.

public voidaddOnTerminatedCallback(java.util.concurrent.Executor executor, Consumer<TerminationInfo> callback)

Add a callback to listen for isolate crashes.

public voidclearConsoleCallback()

Clear any JavaScriptConsoleCallback set via JavaScriptIsolate.setConsoleCallback(Executor, JavaScriptConsoleCallback).

public voidclose()

Closes the JavaScriptIsolate object and renders it unusable.

public <any>evaluateJavaScriptAsync(AssetFileDescriptor afd)

Reads and evaluates the JavaScript code in the file described by the given AssetFileDescriptor.

public <any>evaluateJavaScriptAsync(ParcelFileDescriptor pfd)

Reads and evaluates the JavaScript code in the file described by the given ParcelFileDescriptor.

public <any>evaluateJavaScriptAsync(java.lang.String code)

Evaluates the given JavaScript code and returns the result.

protected voidfinalize()

public voidprovideNamedData(java.lang.String name, byte[] inputBytes[])

Provides a byte array for consumption from the JavaScript environment.

public voidremoveOnTerminatedCallback(Consumer<TerminationInfo> callback)

Remove a callback previously registered with addOnTerminatedCallback.

public voidsetConsoleCallback(java.util.concurrent.Executor executor, JavaScriptConsoleCallback callback)

Set a JavaScriptConsoleCallback to process console messages from the isolate.

public voidsetConsoleCallback(JavaScriptConsoleCallback callback)

Set a JavaScriptConsoleCallback to process console messages from the isolate.

from java.lang.Objectclone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public <any> evaluateJavaScriptAsync(java.lang.String code)

Evaluates the given JavaScript code and returns the result.

There are 3 possible behaviors based on the output of the expression:

  • If the JS expression evaluates to a JS String, then the Java Future resolves to a Java String.
  • If the JS expression evaluates to a JS Promise, and if JavaScriptSandbox.isFeatureSupported(String) for JavaScriptSandbox.JS_FEATURE_PROMISE_RETURN returns true, the Java Future resolves to a Java String once the promise resolves. If it returns false, then the Future resolves to an empty Java string.
  • If the JS expression evaluates to another data type, then the Java Future resolves to an empty Java String.
The environment uses a single JS global object for all the calls to evaluateJavaScriptAsync(String) and JavaScriptIsolate.provideNamedData(String, byte[]) methods. These calls are queued up and are run one at a time in sequence, using the single JS environment for the isolate. The global variables set by one evaluation are visible for later evaluations. This is similar to adding multiple