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

Source code checked in, #b761f46f3c7fe9c00a646000237e49399ec1b22f

$
0
0
Updates for V8 API changes. V8Update now fetches tested revision by default.

Released: ClearScript 5.3 (May 21, 2013)

$
0
0
5.3.2
  • Updates for breaking V8 API changes.
  • V8Update now fetches a tested revision by default.

5.3.1
  • Fixed JScriptEngine dynamic binding bug (Issue #9).

5.3.0
  • Enhancements for V8 users:
    • V8ScriptEngine now supports script compilation and re-execution.
    • New V8Runtime class allows engines to share resources and compiled scripts.
    • New experimental APIs for restricting and querying V8 memory usage.
    • Improved V8 loading algorithm for simplified deployment.
    • V8Update can now fetch a V8 version that is known to work with ClearScript.
  • New method: ScriptEngine.CollectGarbage().
  • Host item caching improves performance and reduces memory usage.
  • Several new tests.

Updated Release: ClearScript 5.3 (May 21, 2013)

$
0
0
5.3.2
  • Updates for breaking V8 API changes.
  • V8Update now fetches a tested revision by default.

5.3.1
  • Fixed JScriptEngine dynamic binding bug (Issue #9).

5.3.0
  • Enhancements for V8 users:
    • V8ScriptEngine now supports script compilation and re-execution.
    • New V8Runtime class allows engines to share resources and compiled scripts.
    • New experimental APIs for restricting and querying V8 memory usage.
    • Improved V8 loading algorithm for simplified deployment.
    • V8Update can now fetch a V8 version that is known to work with ClearScript.
  • New method: ScriptEngine.CollectGarbage().
  • Host item caching improves performance and reduces memory usage.
  • Several new tests.

Updated Wiki: Announcements

$
0
0

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 we are aware that 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

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.jpg6/6/2013: Version 5.3.2 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));
    ");

    // 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);
}

New Post: Array as return type

$
0
0
Ive been trying to return an array (or basicly any collection) with strings from C# to V8 and it doesnt work. Is there any way to return a collection to V8 so that the return type becomes a javascript string array?

My current workaround is to JSON it in c# before I return it and parse the json before using it in javascript. It works, but feels really clumsy. If this is the best way, cant clearscript do it for me? At least for base types.

New Post: Array as return type

$
0
0
Hi Klator, and thanks for your question!

There are several ways to do what you need, and JSON serialization should not be necessary. One thing you could do is access a .NET array directly from JavaScript:
public class Foo {
    public string[] GetNames() {
        return new[] { "Adolph", "Blaine", "Charles", "David" };
    }
}
and then:
engine.AddHostObject("foo", new Foo());
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    names = foo.GetNames();
    for (var i = 0; i < names.Length; i++) {
        Console.WriteLine(names[i]);
    }
");
On the other hand, if it's important that the method's return value be a JavaScript array, ClearScript lets you do that too:
public class Foo {
    private readonly ScriptEngine engine;
    public Foo(ScriptEngine engine) {
        this.engine = engine;
    }
    public object GetNames() {
        dynamic jsArray = engine.Evaluate("[]");
        jsArray.push("Adolph");
        jsArray.push("Blaine");
        jsArray.push("Charles");
        jsArray.push("David");
        return jsArray;
    }
}
and then:
engine.AddHostObject("foo", new Foo(engine));
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    names = foo.GetNames();
    for (var i in names) {
        Console.WriteLine(names[i]);
    }
");
Please let us know if these suggestions don't work for you! Good luck!

New Post: Array as return type

$
0
0
Yeah, option 1 is exactly what Ive done. (Or so I think.)

This is what I do:
    public class ElementCollection : List<Element>
    {
        [ScriptMember(Name = "text")]
        public string[] Text()
        {
            // Desired output.
            var s = this.Where(e => e.Text() != "").Select(e => e.Text()).ToArray();

            // Test to compare types
            var a = new[] { "Adolph", "Blaine", "Charles", "David" };

            return s;
        }
   }
In javascript I then populate a part of an object with this.

I actually first encountered this problem when I tried to JSON.stringify the populated object. (Circular reference error)

Anyway, in JS I do:
function parseArticles(page) {
    var c = page.element.findAll("div.article_content").text();

    var a = { "header": h, "content": c };

    // Type test
    var myArray = ["1", "2", "3"];

    // myArray.getName() returns Array
    $eng.debug("TYPENAME Array:" + myArray.getName()); 
    
    // c.getName() returns HostObject
    $eng.debug("TYPENAME:"+c.getName());

    $eng.out(a);
}
$eng.out() then tries to make JSON out of it and it crashes. But thats beside the point, myArray and a (in the second example) has different types, which is my problem. (I think)

I never explicitly expose the ElementCollection class to the engine, its just returned from a different host object.

The getName() function is ripped from Stack Overflow, its this:
Object.prototype.getName = function () {
    var funcNameRegex = /function (.{1,})\(/;
    var results = (funcNameRegex).exec((this).constructor.toString());
    return (results && results.length > 1) ? results[1] : "";
};

New Post: Array as return type

$
0
0
Hi Klator,

It looks like JSON.stringify() can't work with .NET arrays. Each .NET array has a property named SyncRoot that simply returns the array instance. This makes the array a "circular" object and therefore incompatible with JSON.

In general, .NET objects often have lots of methods and other non-data members that make them JSON-unfriendly. Therefore we recommend in this case that you use script arrays instead. You can use something like this to make it dead simple:
public static class ScriptHelpers {
    public static object ToScriptArray<T>(this IEnumerable<T> source, ScriptEngine engine) {
        dynamic array = engine.Evaluate("[]");
        foreach (var element in source) {
            array.push(element);
        }
        return array;
    }
}
and then, in your .NET method:
var s = this.Where(e => e.Text() != "").Select(e => e.Text()).ToScriptArray(engine);
You could also make ToScriptArray() more sophisticated - e.g., it could handle arrays within arrays, it could convert elements to script objects, etc.

Cheers!

New Post: Array as return type

$
0
0
Aha, I see.

I thought Clearscript would handle the conversion from .NET array to javascript array.
Yeah, that will work. And, yeah, its dead simple. Thanks.

Then again, your solution is dependent on me having access to the engine object at all times.
Guess Ill have to do some refactoring.

Ill try it right away.

K.

New Post: Array as return type

$
0
0
Yes, for better and worse, ClearScript is all about seamless access rather than conversion :)

The idea is that with seamless access, conversion (should you need it) is easy and can be tailored to your requirements. We agree however that the need to pass the engine around might be painful.

What if ClearScript allowed something like this:
[ScriptMember(Name = "text", Flags = ScriptMemberFlags.ConvertArray)]
public string[] Text()
{
    // ...
}
The conversion from (one-dimensional) .NET arrays to script arrays is unambiguous and probably useful in many scenarios. Hmm... :)

New Post: Array as return type

$
0
0
Ok. Makes sense.

Yeah, that would do the trick. Looks great.

Passing engine around is quite painful, then again I have some other objects (logging, statistics) that I also need to pass around, so its probably better if I just accept it. :) I would be great if you could make local-global (global static for a certain scope) variables in .NET. But thats not your problem.

Anyway, I like Clearscript, this is the second prototype im using it for, and it works really good.

Released: ClearScript 5.3 (May 21, 2013)

$
0
0
5.3.3
  • Improved V8 error handling (Issue #12).
  • Lowered .NET Framework target to v4.0.
  • Added several tests.
  • Tested with V8 3.19.18.

5.3.2
  • Updates for breaking V8 API changes.
  • V8Update now fetches a tested revision by default.
  • Tested with V8 3.19.9.

5.3.1
  • Fixed JScriptEngine dynamic binding bug (Issue #9).

5.3.0
  • Enhancements for V8 users:
    • V8ScriptEngine now supports script compilation and re-execution.
    • New V8Runtime class allows engines to share resources and compiled scripts.
    • New experimental APIs for restricting and querying V8 memory usage.
    • Improved V8 loading algorithm for simplified deployment.
    • V8Update can now fetch a V8 version that is known to work with ClearScript.
  • New method: ScriptEngine.CollectGarbage().
  • Host item caching improves performance and reduces memory usage.
  • Several new tests.

Updated Release: ClearScript 5.3 (May 21, 2013)

$
0
0
5.3.3
  • Improved V8 error handling (Issue #12).
  • Lowered .NET Framework target to v4.0.
  • Added several tests.
  • Tested with V8 3.19.18.

5.3.2
  • Updates for breaking V8 API changes.
  • V8Update now fetches a tested revision by default.
  • Tested with V8 3.19.9.

5.3.1
  • Fixed JScriptEngine dynamic binding bug (Issue #9).

5.3.0
  • Enhancements for V8 users:
    • V8ScriptEngine now supports script compilation and re-execution.
    • New V8Runtime class allows engines to share resources and compiled scripts.
    • New experimental APIs for restricting and querying V8 memory usage.
    • Improved V8 loading algorithm for simplified deployment.
    • V8Update can now fetch a V8 version that is known to work with ClearScript.
  • New method: ScriptEngine.CollectGarbage().
  • Host item caching improves performance and reduces memory usage.
  • Several new tests.

Updated Wiki: Announcements

$
0
0

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.jpg7/2/2013: Version 5.3.3 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));
    ");

    // 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 Issue: V8ScriptEngine does not always collect error details [12]

$
0
0
We have reproduced the following:
1. V8ScriptEngine collects error details only when the thrown object is an Error instance.
2. V8ScriptEngine does not collect error details for syntax errors.

Commented Issue: V8ScriptEngine does not always collect error details [12]

$
0
0
We have reproduced the following:
1. V8ScriptEngine collects error details only when the thrown object is an Error instance.
2. V8ScriptEngine does not collect error details for syntax errors.
Comments: Fixed in [Version 5.3.3](https://clearscript.codeplex.com/SourceControl/changeset/caa2c86e14b1c84e38fa3aedd34e0420aef45209).

New Post: Getting V8 compilation error details, and uncaught exceptions stack traces

Viewing all 2297 articles
Browse latest View live




Latest Images