Quantcast
Channel: ClearScript
Viewing all 2297 articles
Browse latest View live

Closed Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?

Edited Issue: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?

New Post: V8 Iterators

$
0
0
Thank you so much. Works like a charm. And seems to improve performance over the Action approach I presented above. (Did not measure performance, though.)

Remaining question: Do I have to ((IDisposable)action).Dispose (); the action object (i.e. the ScriptItem) at the end of forEach?

The ClearScript code seems to do that sometimes with objects returned from Evaluate, and it is not entirely clear to me when that is necessary.

New Post: V8 Iterators

$
0
0
Hi JohnGeeB,

In general, if you're using a recent ClearScript version, you don't have to dispose V8 script objects. However, in some scenarios doing so may improve the application's memory efficiency.

The script engine and the CLR use separate garbage-collected heaps. When you pass an object from one to the other, the destination environment gets a proxy that by necessity prevents its target from being collected. Only when the proxy itself is collected does the target become available to its garbage collector.

Disposing the proxy simply speeds up that process by releasing the target immediately. This may be advisable if, for example, the target is known to be a very large object that is no longer in use.

Good luck!

Updated Wiki: Announcements

$
0
0

12/8/2015: Version 5.4.4 released.

View the release notes and download the source code here.! 8/17/2015: Version 5.4.3 released.
View the release notes and download the source code here. Note: This release adds support for Visual Studio 2015 and updates the procedure for downloading and building V8. See the ClearScript ReadMe for the latest information.

6/30/2015: Version 5.4.2.1 released.

View the release notes and download the source code here. This is a bug fix release only.

5/10/2015: Version 5.4.2 released.

View the release notes and download the source code here.

2/8/2015: Version 5.4.1 released.

View the release notes and download the source code here. Note: This release removes support for Visual Studio 2012 and updates the procedure for downloading and building V8. See the ClearScript ReadMe for the latest information.

10/22/2014: ClearScript 5.4 released.

View the release notes and download the source code here.

1/16/2014: Version 5.3.11 released.

View the release notes and download the source code here.

11/28/2013: Version 5.3.10 released.

View the release notes and download the source code here.

10/29/2013: Version 5.3.9 released.

View the release notes and download the source code here.

10/14/2013: Version 5.3.8 released.

View the release notes and download the source code here.

8/22/2013: Version 5.3.7 released.

View the release notes and download the source code here.

8/14/2013: Version 5.3.6 released.

View the release notes and download the source code here.

8/2/2013: Version 5.3.5 released.

View the release notes and download the source code here.

7/10/2013: Version 5.3.4 released.

View the release notes and download the source code here.

7/2/2013: Version 5.3.3 released.

View the release notes and download the source code here.

6/6/2013: Version 5.3.2 released.

View the release notes and download the source code here. Due to issues with several recent V8 trunk releases (build failures, breaking API changes, etc.), and because more breaking changes are coming soon, this release modifies V8Update so that it fetches a tested, known-good V8 revision by default. See the ClearScript ReadMe for more information.

5/31/2013: Version 5.3.1 released.

View the release notes and download the source code here.

5/21/2013: ClearScript 5.3 released.

View the release notes and download the source code here.

5/15/2013: Version 5.2.2 released.

View the release notes and download the source code here. This release addresses the build issue reported yesterday and can be used with the latest V8 version. Here's how to update your copy of V8:
C:\ClearScript> set V8REV=
C:\ClearScript> V8Update

5/14/2013: ClearScript build failure due to API deprecation in V8 3.19.1.

We are aware of a ClearScript build issue with the latest version of V8. Until this issue is resolved we recommend that you explicitly use V8 3.19.0:
C:\ClearScript> set V8REV=14604
C:\ClearScript> V8Update

5/2/2013: Version 5.2.1 released.

View the release notes and download the source code here.

4/18/2013: V8Update failure resolved by V8 3.18.1.

The V8Update issue reported yesterday has been fixed by a new version of V8 released today. Here's how to update to the latest V8 version:
C:\ClearScript> set V8REV=
C:\ClearScript> V8Update

4/17/2013: V8Update failure with V8 3.18.0.

We are aware of a V8Update issue with the latest version of V8. Until this issue is resolved we recommend that you explicitly use V8 3.17.16:
C:\ClearScript> set V8REV=14138
C:\ClearScript> V8Update

3/27/2013: ClearScript 5.2 released.

View the release notes and download the source code here.

3/8/2013: Version 5.1.3 released.

View the release notes and download the source code here.

3/4/2013: V8Update failure resolved by V8 3.17.7.

The V8Update issue reported earlier has been fixed by a new version of V8 released today. Here's how to update to the latest V8 version:
C:\ClearScript> set V8REV=
C:\ClearScript> V8Update

3/4/2013: V8Update failure with V8 3.17.6.

We are aware of a V8Update issue with the latest version of V8. The issue affects the "Building 64-bit V8" step. The V8 team appears to have already submitted a fix, but the fix has not yet been merged into the trunk. In the meantime, we recommend that you explicitly use V8 3.17.5:
C:\ClearScript> set V8REV=13745
C:\ClearScript> V8Update

Updated Wiki: Announcements

$
0
0

12/8/2015: Version 5.4.4 released.

View the release notes and download the source code here.

8/17/2015: Version 5.4.3 released.

View the release notes and download the source code here. Note: This release adds support for Visual Studio 2015 and updates the procedure for downloading and building V8. See the ClearScript ReadMe for the latest information.

6/30/2015: Version 5.4.2.1 released.

View the release notes and download the source code here. This is a bug fix release only.

5/10/2015: Version 5.4.2 released.

View the release notes and download the source code here.

2/8/2015: Version 5.4.1 released.

View the release notes and download the source code here. Note: This release removes support for Visual Studio 2012 and updates the procedure for downloading and building V8. See the ClearScript ReadMe for the latest information.

10/22/2014: ClearScript 5.4 released.

View the release notes and download the source code here.

1/16/2014: Version 5.3.11 released.

View the release notes and download the source code here.

11/28/2013: Version 5.3.10 released.

View the release notes and download the source code here.

10/29/2013: Version 5.3.9 released.

View the release notes and download the source code here.

10/14/2013: Version 5.3.8 released.

View the release notes and download the source code here.

8/22/2013: Version 5.3.7 released.

View the release notes and download the source code here.

8/14/2013: Version 5.3.6 released.

View the release notes and download the source code here.

8/2/2013: Version 5.3.5 released.

View the release notes and download the source code here.

7/10/2013: Version 5.3.4 released.

View the release notes and download the source code here.

7/2/2013: Version 5.3.3 released.

View the release notes and download the source code here.

6/6/2013: Version 5.3.2 released.

View the release notes and download the source code here. Due to issues with several recent V8 trunk releases (build failures, breaking API changes, etc.), and because more breaking changes are coming soon, this release modifies V8Update so that it fetches a tested, known-good V8 revision by default. See the ClearScript ReadMe for more information.

5/31/2013: Version 5.3.1 released.

View the release notes and download the source code here.

5/21/2013: ClearScript 5.3 released.

View the release notes and download the source code here.

5/15/2013: Version 5.2.2 released.

View the release notes and download the source code here. This release addresses the build issue reported yesterday and can be used with the latest V8 version. Here's how to update your copy of V8:
C:\ClearScript> set V8REV=
C:\ClearScript> V8Update

5/14/2013: ClearScript build failure due to API deprecation in V8 3.19.1.

We are aware of a ClearScript build issue with the latest version of V8. Until this issue is resolved we recommend that you explicitly use V8 3.19.0:
C:\ClearScript> set V8REV=14604
C:\ClearScript> V8Update

5/2/2013: Version 5.2.1 released.

View the release notes and download the source code here.

4/18/2013: V8Update failure resolved by V8 3.18.1.

The V8Update issue reported yesterday has been fixed by a new version of V8 released today. Here's how to update to the latest V8 version:
C:\ClearScript> set V8REV=
C:\ClearScript> V8Update

4/17/2013: V8Update failure with V8 3.18.0.

We are aware of a V8Update issue with the latest version of V8. Until this issue is resolved we recommend that you explicitly use V8 3.17.16:
C:\ClearScript> set V8REV=14138
C:\ClearScript> V8Update

3/27/2013: ClearScript 5.2 released.

View the release notes and download the source code here.

3/8/2013: Version 5.1.3 released.

View the release notes and download the source code here.

3/4/2013: V8Update failure resolved by V8 3.17.7.

The V8Update issue reported earlier has been fixed by a new version of V8 released today. Here's how to update to the latest V8 version:
C:\ClearScript> set V8REV=
C:\ClearScript> V8Update

3/4/2013: V8Update failure with V8 3.17.6.

We are aware of a V8Update issue with the latest version of V8. The issue affects the "Building 64-bit V8" step. The V8 team appears to have already submitted a fix, but the fix has not yet been merged into the trunk. In the meantime, we recommend that you explicitly use V8 3.17.5:
C:\ClearScript> set V8REV=13745
C:\ClearScript> V8Update

Updated Wiki: Home

$
0
0
InfoIcon.jpgVersion 5.4.4 released. More...

Description

ClearScript is a library that makes it easy to add scripting to your .NET applications. It currently supports JavaScript (via V8 and JScript) and VBScript.

Features

  • Simple usage; create a script engine, add your objects and/or types, run scripts
  • Support for several script engines: Google's V8, Microsoft's JScript and VBScript
  • Exposed resources require no modification, decoration, or special coding of any kind
  • Scripts get simple access to most of the features of exposed objects and types:
    • Methods, properties, fields, events
    • (Objects) Indexers, extension methods, conversion operators, explicitly implemented interfaces
    • (Types) Constructors, nested types
  • Full support for generic types and methods, including C#-like type inference and explicit type arguments
  • Scripts can invoke methods with output parameters, optional parameters, and parameter arrays
  • Script delegates enable callbacks into script code
  • Support for exposing all the types defined in one or more assemblies in one step
  • Optional support for importing types and assemblies from script code
  • The host can invoke script functions and access script objects directly
  • Full support for script debugging

Examples

using System;
using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;

// create a script engineusing (var engine = new V8ScriptEngine())
{
    // expose a host type
    engine.AddHostType("Console", typeof(Console));
    engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");

    // expose a host object
    engine.AddHostObject("random", new Random());
    engine.Execute("Console.WriteLine(random.NextDouble())");

    // expose entire assemblies
    engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
    engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");

    // create a host object from script
    engine.Execute(@"
        birthday = new lib.System.DateTime(2007, 5, 22);
        Console.WriteLine(birthday.ToLongDateString());
    ");

    // use a generic class from script
    engine.Execute(@"
        Dictionary = lib.System.Collections.Generic.Dictionary;
        dict = new Dictionary(lib.System.String, lib.System.Int32);
        dict.Add('foo', 123);
    ");

    // call a host method with an output parameter
    engine.AddHostObject("host", new HostFunctions());
    engine.Execute(@"
        intVar = host.newVar(lib.System.Int32);
        found = dict.TryGetValue('foo', intVar.out);
        Console.WriteLine('{0} {1}', found, intVar);
    ");

    // create and populate a host array
    engine.Execute(@"
        numbers = host.newArr(lib.System.Int32, 20);
        for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; }
        Console.WriteLine(lib.System.String.Join(', ', numbers));
    ");

    // create a script delegate
    engine.Execute(@"
        Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);
        oddFilter = new Filter(function(value) {
            return (value & 1) ? true : false;
        });
    ");

    // use LINQ from script
    engine.Execute(@"
        oddNumbers = numbers.Where(oddFilter);
        Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
    ");

    // use a dynamic host object
    engine.Execute(@"
        expando = new lib.System.Dynamic.ExpandoObject();
        expando.foo = 123;
        expando.bar = 'qux';
        delete expando.foo;
    ");

    // call a script function
    engine.Execute("function print(x) { Console.WriteLine(x); }");
    engine.Script.print(DateTime.Now.DayOfWeek);

    // examine a script object
    engine.Execute("person = { name: 'Fred', age: 5 }");
    Console.WriteLine(engine.Script.person.name);
}

Edited Feature: [FEATURE] C# direct access to TypedArray data [83]

$
0
0
I have a need to pass large amounts of data back and forth between JavaScript and C#. Ideally I would like to create a Float32Array in JavaScript and then access it as a native array or byte array in C#. The JavaScript code would calculate values while the C# code would consume them. I can currently brute force this by accessing elements one at a time, but it is way too slow for arrays of 100k or 1M elements.

V8 has a facility for this via ArrayBuffer::Externalize(). Are there any plans to support this capability in ClearScript? What would be awesome is if I could do something like the following:

V8ScriptEngine engine = new V8ScriptEngine();
engine.Execute("var a = new Float32Array(100000);");

... do something in JavaScript to set array values

// access the array and compute the sum of the values
dynamic a = engine.Script.a;

// this is how I have to get values today and it is too slow for large arrays
double total = 0;
for (int i = 0; i < 100000; i++)
{
total += a[i]; // each call dives into V8 to get an indexed value - slow
}

// this is what I want to do
float[] array = (float[]) engine.Script.a.externalize(); // one call to V8
for (int i = 0; i < 100000; i++)
{
total += array[i]; // fast!
}

This is also basically what browsers do to process data in WebGL. The JavaScript code calls bufferData() with a Typed Array. See https://msdn.microsoft.com/en-us/library/dn302373(v=vs.85).aspx


Commented Feature: [FEATURE] C# direct access to TypedArray data [83]

$
0
0
I have a need to pass large amounts of data back and forth between JavaScript and C#. Ideally I would like to create a Float32Array in JavaScript and then access it as a native array or byte array in C#. The JavaScript code would calculate values while the C# code would consume them. I can currently brute force this by accessing elements one at a time, but it is way too slow for arrays of 100k or 1M elements.

V8 has a facility for this via ArrayBuffer::Externalize(). Are there any plans to support this capability in ClearScript? What would be awesome is if I could do something like the following:

V8ScriptEngine engine = new V8ScriptEngine();
engine.Execute("var a = new Float32Array(100000);");

... do something in JavaScript to set array values

// access the array and compute the sum of the values
dynamic a = engine.Script.a;

// this is how I have to get values today and it is too slow for large arrays
double total = 0;
for (int i = 0; i < 100000; i++)
{
total += a[i]; // each call dives into V8 to get an indexed value - slow
}

// this is what I want to do
float[] array = (float[]) engine.Script.a.externalize(); // one call to V8
for (int i = 0; i < 100000; i++)
{
total += array[i]; // fast!
}

This is also basically what browsers do to process data in WebGL. The JavaScript code calls bufferData() with a Typed Array. See https://msdn.microsoft.com/en-us/library/dn302373(v=vs.85).aspx

Comments: Support for typed arrays and other `ArrayBuffer` views added in [Version 5.4.4](https://clearscript.codeplex.com/SourceControl/changeset/4d6048f4be2d433dbfb7de01d05eb5cb33b42007).

Edited Issue: [BUG] Exposed IDispatchEx instances don't support dynamic properties [96]

$
0
0
Exposed COM objects that implement `IDispatchEx` should provide full dynamic access (a-la `IPropertyBag`).

Commented Issue: [BUG] Exposed IDispatchEx instances don't support dynamic properties [96]

$
0
0
Exposed COM objects that implement `IDispatchEx` should provide full dynamic access (a-la `IPropertyBag`).
Comments: Fixed in [Version 5.4.4](https://clearscript.codeplex.com/SourceControl/changeset/4d6048f4be2d433dbfb7de01d05eb5cb33b42007).

Edited Issue: [BUG] JScript for..in loop fails in Standards Mode [94]

$
0
0
It appears that the for/in loop is not working when JScript engine is initialized with WindowsScriptEngineFlags.EnableStandardsMode flag.

I tried adding PropertyBag and ExpandoObject as host objects.

I noticed that there is a ScriptEngineException when executing for/in loop on either of the two objects which has HResult of 0x800a0005 (Invalid procedure call or argument).

Without WindowsScriptEngineFlags.EnableStandardsMode flag everything works without exceptions.

I am developing on Windows 10 with ClearScript 5.4.3

Here is the sample program:

```
static void Main(string[] args)
{
var engine = new JScriptEngine(WindowsScriptEngineFlags.EnableStandardsMode);
engine.AddHostType("Console", typeof(Console));

dynamic expandoObj = new ExpandoObject();
expandoObj.testProp = "expando testProp Text";
engine.AddHostObject("expandoObj", expandoObj);

var propertyBag = new PropertyBag();
propertyBag["testProp"] = "property bag testProp Text";
engine.AddHostObject("propertyBagObj", propertyBag);

engine.Execute(@"Console.WriteLine('expandoObj.testProp=' + expandoObj.testProp);");
engine.Execute(@"Console.WriteLine('propertyBagObj.testProp=' + propertyBagObj.testProp);");
engine.Execute(@"Console.WriteLine('');");
engine.Execute(@"for (var item in propertyBagObj) Console.WriteLine('propertyBagObj[' + item + ']=' + propertyBagObj[item]);");
engine.Execute(@"Console.WriteLine('');");
engine.Execute(@"for (var item in expandoObj) Console.WriteLine('expandoObj[' + item + ']=' + expandoObj[item]);");

}

```

Commented Issue: [BUG] JScript for..in loop fails in Standards Mode [94]

$
0
0
It appears that the for/in loop is not working when JScript engine is initialized with WindowsScriptEngineFlags.EnableStandardsMode flag.

I tried adding PropertyBag and ExpandoObject as host objects.

I noticed that there is a ScriptEngineException when executing for/in loop on either of the two objects which has HResult of 0x800a0005 (Invalid procedure call or argument).

Without WindowsScriptEngineFlags.EnableStandardsMode flag everything works without exceptions.

I am developing on Windows 10 with ClearScript 5.4.3

Here is the sample program:

```
static void Main(string[] args)
{
var engine = new JScriptEngine(WindowsScriptEngineFlags.EnableStandardsMode);
engine.AddHostType("Console", typeof(Console));

dynamic expandoObj = new ExpandoObject();
expandoObj.testProp = "expando testProp Text";
engine.AddHostObject("expandoObj", expandoObj);

var propertyBag = new PropertyBag();
propertyBag["testProp"] = "property bag testProp Text";
engine.AddHostObject("propertyBagObj", propertyBag);

engine.Execute(@"Console.WriteLine('expandoObj.testProp=' + expandoObj.testProp);");
engine.Execute(@"Console.WriteLine('propertyBagObj.testProp=' + propertyBagObj.testProp);");
engine.Execute(@"Console.WriteLine('');");
engine.Execute(@"for (var item in propertyBagObj) Console.WriteLine('propertyBagObj[' + item + ']=' + propertyBagObj[item]);");
engine.Execute(@"Console.WriteLine('');");
engine.Execute(@"for (var item in expandoObj) Console.WriteLine('expandoObj[' + item + ']=' + expandoObj[item]);");

}

```

Comments: Fixed in [Version 5.4.4](https://clearscript.codeplex.com/SourceControl/changeset/4d6048f4be2d433dbfb7de01d05eb5cb33b42007).

Edited Issue: [BUG] Casting is always required for [S]Byte and [U]Int16 parameters [95]

$
0
0
A numeric argument, if it can be converted to an integer without data loss, should match `System.[S]Byte` and `System.[U]Int16` parameters implicitly if the narrowing conversion doesn't result in overflow.

Commented Issue: [BUG] Casting is always required for [S]Byte and [U]Int16 parameters [95]

$
0
0
A numeric argument, if it can be converted to an integer without data loss, should match `System.[S]Byte` and `System.[U]Int16` parameters implicitly if the narrowing conversion doesn't result in overflow.
Comments: Fixed in [Version 5.4.4](https://clearscript.codeplex.com/SourceControl/changeset/4d6048f4be2d433dbfb7de01d05eb5cb33b42007).

Updated Wiki: Home

$
0
0
InfoIcon.jpgVersion 5.4.4 released. More...

Description

ClearScript is a library that makes it easy to add scripting to your .NET applications. It currently supports JavaScript (via V8 and JScript) and VBScript.

Features

  • Simple usage; create a script engine, add your objects and/or types, run scripts
  • Support for several script engines: Google's V8, Microsoft's JScript and VBScript
  • Exposed resources require no modification, decoration, or special coding of any kind
  • Scripts get simple access to most of the features of exposed objects and types:
    • Methods, properties, fields, events
    • (Objects) Indexers, extension methods, conversion operators, explicitly implemented interfaces
    • (Types) Constructors, nested types
  • Full support for generic types and methods, including C#-like type inference and explicit type arguments
  • Scripts can invoke methods with output parameters, optional parameters, and parameter arrays
  • Script delegates enable callbacks into script code
  • Support for exposing all the types defined in one or more assemblies in one step
  • Optional support for importing types and assemblies from script code
  • The host can invoke script functions and access script objects directly
  • Full support for script debugging

Examples

using System;
using Microsoft.ClearScript;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;

// create a script engineusing (var engine = new V8ScriptEngine())
{
    // expose a host type
    engine.AddHostType("Console", typeof(Console));
    engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");

    // expose a host object
    engine.AddHostObject("random", new Random());
    engine.Execute("Console.WriteLine(random.NextDouble())");

    // expose entire assemblies
    engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
    engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");

    // create a host object from script
    engine.Execute(@"
        birthday = new lib.System.DateTime(2007, 5, 22);
        Console.WriteLine(birthday.ToLongDateString());
    ");

    // use a generic class from script
    engine.Execute(@"
        Dictionary = lib.System.Collections.Generic.Dictionary;
        dict = new Dictionary(lib.System.String, lib.System.Int32);
        dict.Add('foo', 123);
    ");

    // call a host method with an output parameter
    engine.AddHostObject("host", new HostFunctions());
    engine.Execute(@"
        intVar = host.newVar(lib.System.Int32);
        found = dict.TryGetValue('foo', intVar.out);
        Console.WriteLine('{0} {1}', found, intVar);
    ");

    // create and populate a host array
    engine.Execute(@"
        numbers = host.newArr(lib.System.Int32, 20);
        for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; }
        Console.WriteLine(lib.System.String.Join(', ', numbers));
    ");

    // create a script delegate
    engine.Execute(@"
        Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);
        oddFilter = new Filter(function(value) {
            return (value & 1) ? true : false;
        });
    ");

    // use LINQ from script
    engine.Execute(@"
        oddNumbers = numbers.Where(oddFilter);
        Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
    ");

    // use a dynamic host object
    engine.Execute(@"
        expando = new lib.System.Dynamic.ExpandoObject();
        expando.foo = 123;
        expando.bar = 'qux';
        delete expando.foo;
    ");

    // call a script function
    engine.Execute("function print(x) { Console.WriteLine(x); }");
    engine.Script.print(DateTime.Now.DayOfWeek);

    // examine a script object
    engine.Execute("person = { name: 'Fred', age: 5 }");
    Console.WriteLine(engine.Script.person.name);

    // read a JavaScript typed array
    engine.Evaluate(@"
        floats = new Float32Array(new ArrayBuffer(32));
        for (var i = 0; i < floats.length; ++i) {
            floats[i] = i * Math.PI;
        }
    ");
    var floats = (ITypedArray<float>)engine.Script.floats;
    Console.WriteLine(string.Join(", ", floats.ToArray()));
}

Updated Wiki: Home

$
0
0
InfoIcon.jpgVersion 5.4.4 released. More...

Description

ClearScript is a library that makes it easy to add scripting to your .NET applications. It currently supports JavaScript (via V8 and JScript) and VBScript.

Features

  • Simple usage; create a script engine, add your objects and/or types, run scripts
  • Support for several script engines: Google's V8, Microsoft's JScript and VBScript
  • Exposed resources require no modification, decoration, or special coding of any kind
  • Scripts get simple access to most of the features of exposed objects and types:
    • Methods, properties, fields, events
    • (Objects) Indexers, extension methods, conversion operators, explicitly implemented interfaces
    • (Types) Constructors, nested types
  • Full support for generic types and methods, including C#-like type inference and explicit type arguments
  • Scripts can invoke methods with output parameters, optional parameters, and parameter arrays
  • Script delegates enable callbacks into script code
  • Support for exposing all the types defined in one or more assemblies in one step
  • Optional support for importing types and assemblies from script code
  • The host can invoke script functions and access script objects directly
  • Full support for script debugging

Examples

using System;
using Microsoft.ClearScript;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;

// create a script engineusing (var engine = new V8ScriptEngine())
{
    // expose a host type
    engine.AddHostType("Console", typeof(Console));
    engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");

    // expose a host object
    engine.AddHostObject("random", new Random());
    engine.Execute("Console.WriteLine(random.NextDouble())");

    // expose entire assemblies
    engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
    engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");

    // create a host object from script
    engine.Execute(@"
        birthday = new lib.System.DateTime(2007, 5, 22);
        Console.WriteLine(birthday.ToLongDateString());
    ");

    // use a generic class from script
    engine.Execute(@"
        Dictionary = lib.System.Collections.Generic.Dictionary;
        dict = new Dictionary(lib.System.String, lib.System.Int32);
        dict.Add('foo', 123);
    ");

    // call a host method with an output parameter
    engine.AddHostObject("host", new HostFunctions());
    engine.Execute(@"
        intVar = host.newVar(lib.System.Int32);
        found = dict.TryGetValue('foo', intVar.out);
        Console.WriteLine('{0} {1}', found, intVar);
    ");

    // create and populate a host array
    engine.Execute(@"
        numbers = host.newArr(lib.System.Int32, 20);
        for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; }
        Console.WriteLine(lib.System.String.Join(', ', numbers));
    ");

    // create a script delegate
    engine.Execute(@"
        Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);
        oddFilter = new Filter(function(value) {
            return (value & 1) ? true : false;
        });
    ");

    // use LINQ from script
    engine.Execute(@"
        oddNumbers = numbers.Where(oddFilter);
        Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
    ");

    // use a dynamic host object
    engine.Execute(@"
        expando = new lib.System.Dynamic.ExpandoObject();
        expando.foo = 123;
        expando.bar = 'qux';
        delete expando.foo;
    ");

    // call a script function
    engine.Execute("function print(x) { Console.WriteLine(x); }");
    engine.Script.print(DateTime.Now.DayOfWeek);

    // examine a script object
    engine.Execute("person = { name: 'Fred', age: 5 }");
    Console.WriteLine(engine.Script.person.name);

    // read a JavaScript typed array
    engine.Execute("values = new Int32Array([1, 2, 3, 4, 5])");
    var values = (ITypedArray<int>)engine.Script.values;
    Console.WriteLine(string.Join(", ", values.ToArray()));
}

New Post: Callbacks for some host objects are not executing

$
0
0
Hi,

It appears that ClearScript 5.4.4 broke callbacks for some host objects. In ClearScript 5.4.3 the callbacks work.

I am exposing IE HTML window object as a global host object to the script engine. My scripts use addEventListener, setTimeout. The callbacks set by the window host object are never executed.

The callbacks on my private host objects work.

I tried setting WindowsScriptEngineFlags.DoNotEnableVTablePatching just to see if that helps but it did not.

New Post: Callbacks for some host objects are not executing

$
0
0
Hi frolovm,

Hmm, this is probably an unintended consequence of this fix. We'll take a look ASAP.

Thanks for reporting it!

New Post: Callbacks for some host objects are not executing

$
0
0
Hi again,

Some questions:
  1. How are you acquiring and exposing the window object?
  2. What is your script code passing into setTimeout()?
Sample code would be best, but a detailed description should work as well.

Cheers!
Viewing all 2297 articles
Browse latest View live




Latest Images