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 | .... | 96 | 97 | (Page 98) | 99 | 100 | .... | 115 | newer

    0 0
  • 03/30/16--13:38: New Post: GeckoFx and WebAPI
  • Hi,

    I am using GeckoFx (core of Mozilla Firefox Browser, wrapped for C#), and I was wondering, if it's possible to provide to Clearscript engine the "document" (DOM) and "window" objects from GeckoFx ?

    The goal should be that clearscript scriptengine could interact with the web page loaded in the browser.

    Someone has already used ClearScript with geckofx and done this sort of thing ?

    2nd question : Do you know, which "Web API" or object should be implemented to load JQuery into clearscriptengine ?

    Thanks for advance for your answers.

    Sybaris

    0 0
  • 03/30/16--14:23: New Post: Func and delegate
  • Hello Sybaris,

    If you expose an open generic type, script code can invoke the exposed object to yield a closed generic type:
    engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core"));
    engine.AddHostType("SomeFunc", typeof(Func<,>));
    engine.Execute(@"
        var System = DotNet.System;
        var IntToStringFunc = SomeFunc(System.Int32, System.String);
        var f = new IntToStringFunc(function(n) {
            return 'The value is ' + n + '.';
        });
    ");
    Also, a single JavaScript new expression can close a generic type and pass arguments to its constructor:
    engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core"));
    engine.AddHostType("SomeFunc", typeof(Func<,>));
    engine.Execute(@"
        var System = DotNet.System;
        var f = new SomeFunc(System.Int32, System.String, function(n) {
            return 'The value is ' + n + '.';
        });
    ");
    This syntax seems to be what you're looking for in your first question.

    As for your second question, host type collections merge overloaded types when they differ only in their generic parameter count, so you can already do things like this:
    engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core"));
    engine.Execute(@"
        var System = DotNet.System;
        var f1 = new System.Func(System.Int32, System.String, function(n) {
            return 'The value is ' + n + '.';
        });
        var f2 = new System.Func(System.Double, System.Double, System.Double, function(x, y) {
            return x * y;
        });
    ");
    Please let us know if you have more questions.

    Thanks!

    0 0
  • 03/30/16--22:55: New Post: Func and delegate
  • Hello,

    Wondeful, exactly the answer I was looking for :-)
    ClearScript is a very great projet !!!

    Thanks.
    Sybaris

    0 0
  • 03/31/16--05:42: New Post: Func and delegate
  • Thank you very much!

    0 0
  • 03/31/16--06:33: New Post: GeckoFx and WebAPI
  • Hi Sybaris,

    We haven't tested with GeckoFX, but ClearScript aims to make it easy to use any managed API from script code. If you decide to give it a try, please let us know if you encounter something that doesn't work or requires special workarounds.

    Thank you!

    0 0

    I've asked this over on StackOverflow but have included the full question here to save your mouse.

    I define JSengine as
        static JScriptEngine JSengine = null;
    instantiate it in Main as
        JSengine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableJITDebugging);
    
    and then hook various C# objects into it, e.g.
        static void JSSetup(JScriptEngine jse)
        {
            jse.AddHostType("CSString", typeof(String));
            jse.AddHostType("CSConsole", typeof(Console));
            jse.AddHostType("CSFile", typeof(File));
            jse.AddHostType("CSFileInfo", typeof(FileInfo));
            jse.AddHostType("CSDirectory", typeof(Directory));
            jse.AddHostType("CSPath", typeof(Path));
        ....
    What I want to know is this: Is there a means whereby I may Add an HostType by specifying it as a string so that I can do something like
        jse.AddHostType("CSPath",fergyTurtle("System.IO.Path"));
    The reason for this madness is a desire to be able specify
        CSPath=System.IO.Path
    in a configuration file and therefore be able to control what symbols get made available to the called script.

    0 0

    Hello,

    Yes, ClearScript provides AddHostType overloads that let you specify the type by name. For example:
    jse.AddHostType("Random", "System.Random");
    However, there are some considerations. First, the above code works only if the type resides in mscorlib or in the currently executing assembly. For other types, you can specify the assembly:
    engine.AddHostType("Enumerable", "System.Linq.Enumerable", "System.Core");
    However, you might want to avoid doing so. Identifying an assembly based on a short name like "System.Core" requires an expensive brute-force search that incurs a large one-time performance hit (the results of the search are saved in a file for subsequent reuse).

    Our recommended approach is to load the type manually via Type.GetType. For non-mscorlib types this does require an assembly-qualified name:
    engine.AddHostType("Enumerable", Type.GetType("System.Linq.Enumerable, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
    Good luck!

    0 0

    It looks like calling a JavaScript function from c# with an implicit this object will be executed twice if an exception ist thrown during execution.

    It is possible to work around this issue via explicit this.

    Consider the following test code:

    ```
    var engine = new V8ScriptEngine( "Test", V8ScriptEngineFlags.DisableGlobalMembers );
    dynamic funcObj = engine.Evaluate( "({ func: function(counter) { counter.val = counter.val + 1; throw new Error('Counter was ' + counter.val); } })" );
    dynamic counter1 = engine.Evaluate( "({val: 0})" );
    dynamic counter2 = engine.Evaluate( "({val: 0})" );
    try
    {
    funcObj.func( counter1 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 1: " + counter1.val );
    // Counter 1: 2
    }

    try
    {
    var func = funcObj.func;
    func.call( funcObj, counter2 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 2: " + counter2.val );
    // Counter 2: 1
    }
    ```

    Am I doing something wrong here?

    0 0

    Hello,

    I'd like to use the new API for converting between byte[] and Uint8Array but I can't find any documentation. What do I need to do?

    0 0

    It looks like calling a JavaScript function from c# with an implicit this object will be executed twice if an exception ist thrown during execution.

    It is possible to work around this issue via explicit this.

    Consider the following test code:

    ```
    var engine = new V8ScriptEngine( "Test", V8ScriptEngineFlags.DisableGlobalMembers );
    dynamic funcObj = engine.Evaluate( "({ func: function(counter) { counter.val = counter.val + 1; throw new Error('Counter was ' + counter.val); } })" );
    dynamic counter1 = engine.Evaluate( "({val: 0})" );
    dynamic counter2 = engine.Evaluate( "({val: 0})" );
    try
    {
    funcObj.func( counter1 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 1: " + counter1.val );
    // Counter 1: 2
    }

    try
    {
    var func = funcObj.func;
    func.call( funcObj, counter2 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 2: " + counter2.val );
    // Counter 2: 1
    }
    ```

    Am I doing something wrong here?

    0 0

    It looks like calling a JavaScript function from c# with an implicit this object will be executed twice if an exception ist thrown during execution.

    It is possible to work around this issue via explicit this.

    Consider the following test code:

    ```
    var engine = new V8ScriptEngine( "Test", V8ScriptEngineFlags.DisableGlobalMembers );
    dynamic funcObj = engine.Evaluate( "({ func: function(counter) { counter.val = counter.val + 1; throw new Error('Counter was ' + counter.val); } })" );
    dynamic counter1 = engine.Evaluate( "({val: 0})" );
    dynamic counter2 = engine.Evaluate( "({val: 0})" );
    try
    {
    funcObj.func( counter1 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 1: " + counter1.val );
    // Counter 1: 2
    }

    try
    {
    var func = funcObj.func;
    func.call( funcObj, counter2 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 2: " + counter2.val );
    // Counter 2: 1
    }
    ```

    Am I doing something wrong here?
    Comments: Hi ZoneRunner, You aren't doing anything wrong. This issue is the result of an unexpected interaction between ClearScript and the C# dynamic runtime. Specifically, the C# syntax `funcObj.func(counter1)` can have two meanings: "invoke the `func` method" or "retrieve the `func` property and invoke the result (e.g., if it's a delegate)". So the runtime tries it both ways, oblivious to the fact that in JavaScript they amount to the same thing. Thanks for reporting this issue!

    0 0

    Hi ZoneRunner,

    Here's a sample:
    // using Microsoft.ClearScript.JavaScript;
    engine.Execute("values = new Uint8Array([1, 2, 3, 4, 5])");
    var values = (ITypedArray<byte>)engine.Script.values;
    Console.WriteLine(string.Join(", ", values.ToArray()));
    The ITypedArray interface also has methods for transferring array elements to and from existing .NET arrays.

    Good luck!

    0 0

    It looks like calling a JavaScript function from c# with an implicit this object will be executed twice if an exception ist thrown during execution.

    It is possible to work around this issue via explicit this.

    Consider the following test code:

    ```
    var engine = new V8ScriptEngine( "Test", V8ScriptEngineFlags.DisableGlobalMembers );
    dynamic funcObj = engine.Evaluate( "({ func: function(counter) { counter.val = counter.val + 1; throw new Error('Counter was ' + counter.val); } })" );
    dynamic counter1 = engine.Evaluate( "({val: 0})" );
    dynamic counter2 = engine.Evaluate( "({val: 0})" );
    try
    {
    funcObj.func( counter1 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 1: " + counter1.val );
    // Counter 1: 2
    }

    try
    {
    var func = funcObj.func;
    func.call( funcObj, counter2 );
    }
    catch( Exception ex )
    {
    System.Console.WriteLine( "Counter 2: " + counter2.val );
    // Counter 2: 1
    }
    ```

    Am I doing something wrong here?

    0 0

    Thanks :)

    0 0

    When allocating a V8ScriptEngine objects within a loop causes memory to remain allocated even if you force all references to the object to null and eventually crashes the application with a System.AccessViolationException. The details of the crash is below.

    _Currently coding against version 5.4.5.0 of ClearScript._

    __This Code Snippet Fails (Normally at iteration 134 the failure occurs)__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    }

    __This Code Snippet Works:__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    //Invoking Garbage Collection Every 50 V8ScriptEngine object allocated
    //Fixes the issue
    if(i%50 == 0)
    GC.Collect();
    }

    __Here are the details of the crash:__
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: ClearScriptDemo.exe
    Problem Signature 02: 1.0.0.0
    Problem Signature 03: 572a5c94
    Problem Signature 04: ClearScriptV8-32
    Problem Signature 05: 5.4.5.0
    Problem Signature 06: 56e184c1
    Problem Signature 07: e6
    Problem Signature 08: 78
    Problem Signature 09: System.AccessViolationException
    OS Version: 6.1.7601.2.1.0.256.4
    Locale ID: 1033
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789


    0 0

    When allocating a V8ScriptEngine objects within a loop causes memory to remain allocated even if you force all references to the object to null and eventually crashes the application with a System.AccessViolationException. The details of the crash is below.

    _Currently coding against version 5.4.5.0 of ClearScript._

    __This Code Snippet Fails (Normally at iteration 134 the failure occurs)__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    }

    __This Code Snippet Works:__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    //Invoking Garbage Collection Every 50 V8ScriptEngine object allocated
    //Fixes the issue
    if(i%50 == 0)
    GC.Collect();
    }

    __Here are the details of the crash:__
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: ClearScriptDemo.exe
    Problem Signature 02: 1.0.0.0
    Problem Signature 03: 572a5c94
    Problem Signature 04: ClearScriptV8-32
    Problem Signature 05: 5.4.5.0
    Problem Signature 06: 56e184c1
    Problem Signature 07: e6
    Problem Signature 08: 78
    Problem Signature 09: System.AccessViolationException
    OS Version: 6.1.7601.2.1.0.256.4
    Locale ID: 1033
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

    Comments: Hello! This is not ClearScript issue. Each invocation of the `V8ScriptEngine` constructor creates a V8 runtime, which reserves a block of address space between 16MB and 32MB in size, yielding a theoretical maximum of 128 instances in a 32-bit process (assuming 2GB of available address space), and a practical maximum significantly lower than that. When V8 is no longer able to reserve memory at startup, it crashes its host process. You can a relevant discussion [here](https://groups.google.com/forum/#!topic/v8-users/VCy-_Ao4GIE). Some suggestions: * Be sure to [dispose](https://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=vs.110).aspx) your script engines when they're no longer in use. * Consider using a fixed-size of pool of script engines to run your script code. We recommend an instance count no greater than the number of CPU cores in your system. * It's possible for multiple script engines to share a V8 runtime (see ClearScript's `V8Runtime` class). Consider taking advantage of this capability if it makes sense for your application. * Consider switching to a 64-bit process. Good luck!

    0 0

    When allocating a V8ScriptEngine objects within a loop causes memory to remain allocated even if you force all references to the object to null and eventually crashes the application with a System.AccessViolationException. The details of the crash is below.

    _Currently coding against version 5.4.5.0 of ClearScript._

    __This Code Snippet Fails (Normally at iteration 134 the failure occurs)__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    }

    __This Code Snippet Works:__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    //Invoking Garbage Collection Every 50 V8ScriptEngine object allocated
    //Fixes the issue
    if(i%50 == 0)
    GC.Collect();
    }

    __Here are the details of the crash:__
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: ClearScriptDemo.exe
    Problem Signature 02: 1.0.0.0
    Problem Signature 03: 572a5c94
    Problem Signature 04: ClearScriptV8-32
    Problem Signature 05: 5.4.5.0
    Problem Signature 06: 56e184c1
    Problem Signature 07: e6
    Problem Signature 08: 78
    Problem Signature 09: System.AccessViolationException
    OS Version: 6.1.7601.2.1.0.256.4
    Locale ID: 1033
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789


    0 0

    When allocating a V8ScriptEngine objects within a loop causes memory to remain allocated even if you force all references to the object to null and eventually crashes the application with a System.AccessViolationException. The details of the crash is below.

    _Currently coding against version 5.4.5.0 of ClearScript._

    __This Code Snippet Fails (Normally at iteration 134 the failure occurs)__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    }

    __This Code Snippet Works:__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    //Invoking Garbage Collection Every 50 V8ScriptEngine object allocated
    //Fixes the issue
    if(i%50 == 0)
    GC.Collect();
    }

    __Here are the details of the crash:__
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: ClearScriptDemo.exe
    Problem Signature 02: 1.0.0.0
    Problem Signature 03: 572a5c94
    Problem Signature 04: ClearScriptV8-32
    Problem Signature 05: 5.4.5.0
    Problem Signature 06: 56e184c1
    Problem Signature 07: e6
    Problem Signature 08: 78
    Problem Signature 09: System.AccessViolationException
    OS Version: 6.1.7601.2.1.0.256.4
    Locale ID: 1033
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789


    0 0

    When allocating a V8ScriptEngine objects within a loop causes memory to remain allocated even if you force all references to the object to null and eventually crashes the application with a System.AccessViolationException. The details of the crash is below.

    _Currently coding against version 5.4.5.0 of ClearScript._

    __This Code Snippet Fails (Normally at iteration 134 the failure occurs)__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    }

    __This Code Snippet Works:__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    //Invoking Garbage Collection Every 50 V8ScriptEngine object allocated
    //Fixes the issue
    if(i%50 == 0)
    GC.Collect();
    }

    __Here are the details of the crash:__
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: ClearScriptDemo.exe
    Problem Signature 02: 1.0.0.0
    Problem Signature 03: 572a5c94
    Problem Signature 04: ClearScriptV8-32
    Problem Signature 05: 5.4.5.0
    Problem Signature 06: 56e184c1
    Problem Signature 07: e6
    Problem Signature 08: 78
    Problem Signature 09: System.AccessViolationException
    OS Version: 6.1.7601.2.1.0.256.4
    Locale ID: 1033
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

    Comments: Hello! This is not a ClearScript issue. Each invocation of the `V8ScriptEngine` constructor creates a V8 runtime, which reserves a block of address space between 16MB and 32MB in size, yielding a theoretical maximum of 128 instances in a 32-bit process (assuming 2GB of available address space), and a practical maximum significantly lower than that. When V8 is no longer able to reserve memory at startup, it crashes its host process. You can find a relevant discussion [here](https://groups.google.com/forum/#!topic/v8-users/VCy-_Ao4GIE). Some suggestions: * Be sure to [dispose](https://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=vs.110).aspx) your script engines when they're no longer in use. * Consider using a fixed-size of pool of script engines to run your script code. We recommend an instance count no greater than the number of CPU cores in your system. * It's possible for multiple script engines to share a V8 runtime (see ClearScript's `V8Runtime` class). Consider taking advantage of this capability if it makes sense for your application. * Consider switching to a 64-bit process. Good luck!

    0 0

    When allocating a V8ScriptEngine objects within a loop causes memory to remain allocated even if you force all references to the object to null and eventually crashes the application with a System.AccessViolationException. The details of the crash is below.

    _Currently coding against version 5.4.5.0 of ClearScript._

    __This Code Snippet Fails (Normally at iteration 134 the failure occurs)__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    }

    __This Code Snippet Works:__
    for (int i = 1; i <= 1000; i++)
    {
    var v8Engine = new Microsoft.ClearScript.V8ScriptEngine();
    v8Engine = null;
    //Invoking Garbage Collection Every 50 V8ScriptEngine object allocated
    //Fixes the issue
    if(i%50 == 0)
    GC.Collect();
    }

    __Here are the details of the crash:__
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: ClearScriptDemo.exe
    Problem Signature 02: 1.0.0.0
    Problem Signature 03: 572a5c94
    Problem Signature 04: ClearScriptV8-32
    Problem Signature 05: 5.4.5.0
    Problem Signature 06: 56e184c1
    Problem Signature 07: e6
    Problem Signature 08: 78
    Problem Signature 09: System.AccessViolationException
    OS Version: 6.1.7601.2.1.0.256.4
    Locale ID: 1033
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

    Comments: Thank you for the feedback. I made the changes based on the suggestion of using the V8Runtime.CreateScriptEngine() function. However now I am seeing something that I did not expect. According the ClearScript Library documentation: "Call Dispose() when you are finished using the script engine. Dispose() leaves the script engine in an unusable state. After calling Dispose(), you must release all references to the script engine so the garbage collector can reclaim the memory that the script engine was occupying. " However it appears that only when I dispose of the V8Runtime object does the Garbage Collection actually reclaim the memory for the unreferrenced V8ScriptEngine objects created using the V8Runtime.CreateScriptEngine() function. Please see the two code snippets below. __//This code does not cause Garbage Collection to clean up unreferenced V8ScriptEngine objects__ V8Runtime _v8Runtime = new V8Runtime(); V8ScriptEngine v8Engine = null; for (int i = 1; i <= 1000; i++) { v8Engine = _v8Runtime.CreateScriptEngine(); v8Engine.Dispose(); //Dispose call does not free memory. } __//This code cause Garbage Collection to clean up unreferenced V8ScriptEngine objects__ V8Runtime _v8Runtime = new V8Runtime(); V8ScriptEngine v8Engine = null; for (int i = 1; i <= 1000; i++) { var v8Engine = _v8Runtime.CreateScriptEngine(); v8Engine.Dispose(); //Calling Dispose on V8Runtime frees memory for V8ScriptEngines if (i%50 == 0) { _v8Runtime.Dispose(); _v8Runtime = new V8Runtime(V8RuntimeFlags.EnableDebugging); } }

older | 1 | .... | 96 | 97 | (Page 98) | 99 | 100 | .... | 115 | newer