V8JavaScriptEngine
Namespace: Wisej.Ext.ClearScript
Assembly: Wisej.Ext.ClearScript (4.1.0.0)
Represents an instance of the V8 JavaScript engine.
- C#
- VB.NET
public class V8JavaScriptEngine : V8ScriptEngine
Public Class V8JavaScriptEngine
Inherits V8ScriptEngine
Unlike WindowsScriptEngine instances, V8ScriptEngine instances do not have thread affinity. The underlying script engine is not thread-safe, however, so this class uses internal locks to automatically serialize all script code execution for a given instance. Script delegates and event handlers are invoked on the calling thread without marshaling.
Because there is no dedicated thread to release, a V8 engine doesn't need the dispatcher plumbing used by JScriptEngine and VBScriptEngine. It does hold unmanaged memory, so it should still be disposed when it's no longer in use.
The native V8 libraries are embedded in this assembly and extracted on demand the first time an engine is created, therefore instantiating this class through Create with V8 is the recommended way to create it.
Example:
This example creates a V8 engine, exposes a server object and a .NET type to the script, evaluates an expression and invokes a script function.
using (var engine = Wisej.Ext.ClearScript.ClearScript.Create(
Wisej.Ext.ClearScript.EngineType.V8, "calculator"))
{
// expose a host object and a host type to the script code.
engine.AddHostObject("customer", this.customer);
engine.AddHostType("Math", typeof(System.Math));
engine.Execute(@"
function discount(total) {
return customer.IsPreferred ? Math.Round(total * 0.9) : total;
}
");
// evaluate an expression, or invoke a script function.
var net = Convert.ToDouble(engine.Evaluate("discount(1000)"));
var other = engine.Script.discount(250);
}
See also: ClearScript, JScriptEngine, VBScriptEngine
Constructors
V8JavaScriptEngine(name, constraints, flags)
Initializes a new instance of the V8JavaScriptEngine class with the specified name, resource constraints and options.
| Name | Type | Description |
|---|---|---|
| name | String | A user defined name for the engine instance. It is used only for identification purposes, e.g. in debugger user interfaces. May be null. |
| constraints | V8RuntimeConstraints | Resource constraints for the V8 runtime that backs this engine, or null to use the V8 defaults. |
| flags | V8ScriptEngineFlags | A bitwise combination of V8ScriptEngineFlags values that configure the engine. Pass None for the defaults. |
Unlike the Windows script engines, this engine can be created and used on any thread. The first instantiation in the process extracts and loads the embedded native V8 libraries.
Example:
Recommended: let ClearScript create the engine.
var engine = (Wisej.Ext.ClearScript.V8JavaScriptEngine)
Wisej.Ext.ClearScript.ClearScript.Create(
Wisej.Ext.ClearScript.EngineType.V8,
"rules",
v8flags: Microsoft.ClearScript.V8.V8ScriptEngineFlags.EnableDebugging);
engine.Execute("var version = 1;");
Direct instantiation, limiting the memory the script code may use.
var constraints = new Microsoft.ClearScript.V8.V8RuntimeConstraints
{
MaxOldSpaceSize = 64, // MB
MaxNewSpaceSize = 8 // MB
};
using (var engine = new Wisej.Ext.ClearScript.V8JavaScriptEngine(
"rules", constraints, Microsoft.ClearScript.V8.V8ScriptEngineFlags.None))
{
var total = engine.Evaluate("1 + 1");
}