Quantcast
Are you the publisher? Claim or contact us about this channel


Embed this content in your HTML

Search

Report adult content:

click to rate:

Account: (login)

More Channels


Showcase


Channel Catalog



Channel 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.

older | 1 | .... | 83 | 84 | (Page 85) | 86 | 87 | .... | 115 | newer

    0 0

    I just upgraded from version 5.4.2 to 5.4.3 and script code that once worked now fails with the error "Object doesn't support this property or method". I am using V8.

    I added a host object that is a Timer
    ```
    Engine.AddHostType("ScriptTimer", GetType(ScriptTimer))
    ```

    My ScriptTimer looks like this (VB.Net)
    ```
    Public Module ScriptTimer
    Public Function Create(func As Object, delay As Double, period As Double, args As Object)
    Try
    Dim callback As System.Threading.TimerCallback = New System.Threading.TimerCallback(
    Sub()
    Try
    func.apply(Nothing, args)
    Catch ex As Exception
    Console.WriteLine(ex.Message)
    End Try
    End Sub)
    Return New System.Threading.Timer(callback, Nothing, Convert.ToInt64(delay), Convert.ToInt64(period))
    Catch ex As Exception
    Console.WriteLine(ex.Message)
    Return ex
    End Try
    End Function
    End Module
    ```

    And this is the script that fails.
    ```
    function createTimer(periodic, func, delay) {
    var period = periodic ? delay : -1;
    var args = Array.prototype.slice.call(arguments, 3);
    return ScriptTimer.Create(func, delay, period, args);
    }

    setTimeout = createTimer.bind(null, false);
    ```
    Comments: Hi again, It's great to hear that your issue is resolved! ​ >BTW: Should this same setTimeout function work in the ClearScript JScript engine? ​ Not quite; there are two problems. First, JScript doesn't appear to support [Function.prototype.bind()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind), so you'd need to provide your own implementation or eliminate its usage. A deeper issue is that Windows script engines have thread affinity, so you'd have to modify your timer implementation to invoke the callback on the correct thread. Good luck!

    0 0

    I just upgraded from version 5.4.2 to 5.4.3 and script code that once worked now fails with the error "Object doesn't support this property or method". I am using V8.

    I added a host object that is a Timer
    ```
    Engine.AddHostType("ScriptTimer", GetType(ScriptTimer))
    ```

    My ScriptTimer looks like this (VB.Net)
    ```
    Public Module ScriptTimer
    Public Function Create(func As Object, delay As Double, period As Double, args As Object)
    Try
    Dim callback As System.Threading.TimerCallback = New System.Threading.TimerCallback(
    Sub()
    Try
    func.apply(Nothing, args)
    Catch ex As Exception
    Console.WriteLine(ex.Message)
    End Try
    End Sub)
    Return New System.Threading.Timer(callback, Nothing, Convert.ToInt64(delay), Convert.ToInt64(period))
    Catch ex As Exception
    Console.WriteLine(ex.Message)
    Return ex
    End Try
    End Function
    End Module
    ```

    And this is the script that fails.
    ```
    function createTimer(periodic, func, delay) {
    var period = periodic ? delay : -1;
    var args = Array.prototype.slice.call(arguments, 3);
    return ScriptTimer.Create(func, delay, period, args);
    }

    setTimeout = createTimer.bind(null, false);
    ```

    0 0

    I just upgraded from version 5.4.2 to 5.4.3 and script code that once worked now fails with the error "Object doesn't support this property or method". I am using V8.

    I added a host object that is a Timer
    ```
    Engine.AddHostType("ScriptTimer", GetType(ScriptTimer))
    ```

    My ScriptTimer looks like this (VB.Net)
    ```
    Public Module ScriptTimer
    Public Function Create(func As Object, delay As Double, period As Double, args As Object)
    Try
    Dim callback As System.Threading.TimerCallback = New System.Threading.TimerCallback(
    Sub()
    Try
    func.apply(Nothing, args)
    Catch ex As Exception
    Console.WriteLine(ex.Message)
    End Try
    End Sub)
    Return New System.Threading.Timer(callback, Nothing, Convert.ToInt64(delay), Convert.ToInt64(period))
    Catch ex As Exception
    Console.WriteLine(ex.Message)
    Return ex
    End Try
    End Function
    End Module
    ```

    And this is the script that fails.
    ```
    function createTimer(periodic, func, delay) {
    var period = periodic ? delay : -1;
    var args = Array.prototype.slice.call(arguments, 3);
    return ScriptTimer.Create(func, delay, period, args);
    }

    setTimeout = createTimer.bind(null, false);
    ```

    0 0

    Hi johnsev,

    Do first compile the script then execute the script?

    Yes, that's correct. Note that compiling a script only makes sense if you need to execute it more than once in a given V8 runtime. If you re-execute a script only when it changes, compiling it provides no benefit.

    Also, consider what the script actually does. If all it does is define a script function and invoke it, there's no need to re-execute it, as you can invoke an existing script function as many times as necessary. The main goal is to avoid redundant use of the script parser/compiler, as it's relatively slow.

    Good luck!

    0 0

    Hi,

    I have the following problem with a default property indexer (from DataRow dotnet Framework class).

    In C# :
    scriptEngine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core", "System.Data"));
    
    In my Javascript :
    var dt = new DotNet.System.Data.DataTable("testDataTable");
    dt.Columns.Add("Col1",host.typeOf(DotNet.System.String));
    var dr = dt.NewRow();
    dr('Col1') = 'Test1'; // Works only with JScriptEngine, not with  V8ScriptEngine :-(
    dr.set('Col1','Test1'); // Does not work :-(
    dt.Rows.Add(dr);
    1°/ Like you said in previous post, JScriptEngine & V8ScriptEngine are different, so dr('Col1') = 'Test1'; run only on JScriptEngine. For now, I was thinking using V8ScriptEngine, because error handling seems to be better.
    2°/ The 2nd code you said in previous code dr.set('Col1','Test1'); does not run. Perharps because it's a default property indexer (this[]) ?

    Do you have a solution or a workaround for this problem please ?

    Thanks for advance.

    Sybaris

    0 0
  • 09/12/15--01:32: New Post: Generic types
  • Hi,

    Is there is a way to use a generic type with a javascript class ?
    For example, with the List<T> dotnet Framework class, If I define a javascript class "MyClass", is it possible to have a List<MyClass> ?

    Here a pseudo code (that does not run) :
    C# :
    engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core", "System.Data"));
    
    Javascript
    engine.Execute(@"
        function MyClass() {
            this.MyValue = 10;
        }
        var list = host.newObj(DotNet.System.Collections.Generic.List(MyClass));
    ");
    Thanks for advance
    Sybaris

    0 0

    Hi Sybaris,

    The actual name of the indexer property in this case (and most cases) is "Item":
    dr.Item.set('Col1', 'Test1'); // should work :-)
    Cheers!

    0 0
  • 09/12/15--09:37: New Post: Generic types
  • Hi Sybaris,

    A JavaScript class (really just a function) cannot be used as a .NET type argument. As far as the managed side is concerned, it's just a script object and has no public runtime type beyond DynamicObject.

    Therefore an instance of a JavaScript class, like any script object, can be stored in a managed collection of type DynamicObject, IDynamicMetaObjectProvider, or simply Object:
    function MyClass() {
        this.MyValue = 10;
    }
    var list = new DotNet.System.Collections.Generic.List(DotNet.System.Object);
    list.Add(new MyClass());
    var test = list[0].MyValue; // test == 10
    Thanks for your question, and good luck!

    0 0

    Once again you are right :-)
    Thanks you very much

    0 0
  • 09/13/15--11:45: New Post: Generic types
  • Hi,

    I understand.
    My goal is to allow script developers to define structs or classes that will be usable by C# classes.
    I want to offer an API that is data independent, and the data structure have to be defined by script developers.

    So another idea I have, is to offer another API to construct dynamicly a C# class.
    Is it possible then to makes an AddHostType of a dynamic C# class ?

    Thanks for advance

    Sybaris

    0 0
  • 09/14/15--06:01: New Post: Generic types
  • Hi again,

    My goal is to allow script developers to define structs or classes that will be usable by C# classes.

    ClearScript allows C# code to access script objects directly via the dynamic keyword. Another possibility might be to use ClearScript's PropertyBag class, which gives you .NET collections that script code can treat much like native script objects.

    So another idea I have, is to offer another API to construct dynamicly a C# class. Is it possible then to makes an AddHostType of a dynamic C# class ?

    That's certainly an interesting way to go, and you should be able to use AddHostType() as long as you have a Type instance. We haven't tested with dynamically built types, but in theory they should work with ClearScript.

    Good luck!

    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]);");
    
            }
    

    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]);");

    }

    ```


    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: Hi frolovm, You're absolutely right. The [for..in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) statement is broken for host objects in Standards Mode. Unfortunately this appears to be a JScript bug and therefore isn't likely to be fixed. Consider however that for..in support is really only useful for `IPropertyBag` instances. All other host objects include instance methods, extension methods, and other undesirable members in the enumeration. This is by design, as some script engines (including JScript) don't have enumeration options for host object members. There are several ways to enumerate host object members that work in Standards Mode and don't have the problem mentioned above. JScript's [`Enumerator`](https://msdn.microsoft.com/en-us/library/6ch9zb09%28v=vs.84%29.aspx) class is one possibility: ``` C# engine.Execute(@" for (var e = new Enumerator(expandoObj); !e.atEnd(); e.moveNext()) { var item = e.item(); Console.WriteLine('{0} = {1}', item.Key, item.Value); } "); ``` Unfortunately this currently doesn't work with `IPropertyBag`; that's a ClearScript bug that we'll fix in the next point release. Another possibility might be to expose a JavaScript-friendly way to use .NET enumeration: ``` C# public static class IEnumerableExtensions { public static void @foreach(this IEnumerable source, dynamic callback) { foreach (var item in source) { callback(item); } } } ``` and then: ``` C# engine.AddHostType(typeof(IEnumerableExtensions)); engine.Execute(@" expandoObj.foreach(function(item) { Console.WriteLine('{0} = {1}', item.Key, item.Value); }); "); ``` Finally, for scriptable name-value collections, you can always use native JavaScript objects instead of host objects: ``` C# dynamic someObj = engine.Evaluate("({})"); someObj.foo = 123; someObj.bar = 456; engine.Script.someObj = someObj; ``` Thanks for reporting this issue, and good luck!

    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]);");

    }

    ```


    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]);");

    }

    ```


    0 0

    Hi frolovm,

    You're absolutely right. The for..in statement is broken for host objects in Standards Mode. Unfortunately this appears to be a JScript bug and therefore isn't likely to be fixed.

    Consider however that for..in support is really only useful for IPropertyBag instances. All other host objects include instance methods, extension methods, and other undesirable members in the enumeration. This is by design, as some script engines (including JScript) don't have enumeration options for host object members.

    There are several ways to enumerate host object members that work in Standards Mode and don't have the problem mentioned above. JScript's Enumerator class is one possibility:
    engine.Execute(@"
        for (var e = new Enumerator(expandoObj); !e.atEnd(); e.moveNext()) {
            var item = e.item();
            Console.WriteLine('{0} = {1}', item.Key, item.Value);
        }
    ");
    Unfortunately this currently doesn't work with IPropertyBag; that's a ClearScript bug that we'll fix in the next point release.

    Another possibility might be to expose a JavaScript-friendly way to use .NET enumeration:
    publicstaticclass IEnumerableExtensions {
        publicstaticvoid @foreach(this IEnumerable source, dynamic callback) {
            foreach (var item in source) {
                callback(item);
            }
        }
    }
    and then:
    engine.AddHostType(typeof(IEnumerableExtensions));
    engine.Execute(@"
        expandoObj.foreach(function(item) {
            Console.WriteLine('{0} = {1}', item.Key, item.Value);
        });
    ");
    Finally, for scriptable name-value collections, you can always use native JavaScript objects instead of host objects:
    dynamic someObj = engine.Evaluate("({})");
    someObj.foo = 123;
    someObj.bar = 456;
    engine.Script.someObj = someObj;
    Thanks for reporting this issue, and good luck!

    0 0

    Thanks, it seems like for my case I would have to use your last suggestion.

    We have a set of Javascripts that work with Google Chrome and Firefox and we would rather not modify them with non-standard code.

    What makes you think this is a problem with JScript? It is able to enumerate JScript objects, I would imagine it is using some kind of IDispatch/Ex capability. Could it be looking for a "hidden" property or method of an object to perform its for...in enumeration? Perhaps this is a matter of supplying JScript with what it is looking for?

    0 0

    Hi,

    I am integrating clearscript (with v8) in a .NET client application.
    Is there a way I can distribute my application without requiring clients installing the isual VC++ Redistributable packages ? Maybe by changing some compilation options and embedding the VC++ dlls in my application ?

    Thanks,
    Claudio.

    0 0

    Hi Claudio,

    ClearScript's V8 interface (ClearScriptV8-xx.dll) is a mixed assembly, and unfortunately mixed assemblies cannot use static linking for the C++ libraries.

    Is it not possible to incorporate the Visual C++ Redistributables into your installation package?

    Cheers!

older | 1 | .... | 83 | 84 | (Page 85) | 86 | 87 | .... | 115 | newer