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 | .... | 33 | 34 | (Page 35) | 36 | 37 | .... | 115 | newer

    0 0

    Hi everybody,

    I just noticed a behavior that differs between ClearScript v 5.3.9 and 5.3.10, and it's about limiting memory for a script execution. Consider following faulty script (it emulates a memory leak) being executed inside the 64 bit console application:
    ```
    var sample_arr = [-1, 5, 7, 4, 0, 1, -5]
    function My_Partition(container, first_index, last_index) {
    var x = container[last_index];
    var i = first_index - 1;

    for (var elem = 0; elem < container.length-1; elem++) {
    if (container[elem] <= x) {
    i += 1;
    var temp_1 = container[i];
    container[i] = container[elem];
    container[elem] = temp_1;
    }
    }
    var temp_2 = container[i+1];
    container[i+1] = container[last_index];
    container[last_index] = temp_2;

    return i+1;
    }
    function My_Quick_Sort(container, first_index, last_index) {
    if (first_index < last_index) {
    var mid = My_Partition(container, first_index, last_index);
    My_Quick_Sort(container, first_index, mid-1);
    My_Quick_Sort(container, mid+1, last_index);
    }
    }
    My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
    console.WriteLine("Sorted Array:", sample_arr);
    ```
    as well as following ClearScript's V8 engine configuration:
    ```
    Using engine As New V8ScriptEngine("V8Engine", New V8RuntimeConstraints() With {.MaxOldSpaceSize = 209715200}, V8ScriptEngineFlags.EnableDebugging, 9222)
    ```
    You'll notice that after running above code in 5.3.9 it gracefully ends up with an exception that indicates a memory limit being exceeded (see attachment). However, in 5.3.10 it doesn't result in exception and rather hard-crashes with a message in the console (see attachment). Could you test it on your end and provide some follow-up?

    System used for testing: Window 7, 64 bit.

    Thanks for your work,
    Max


    0 0

    Hi everybody,

    I just noticed a behavior that differs between ClearScript v 5.3.9 and 5.3.10, and it's about limiting memory for a script execution. Consider following faulty script (it emulates a memory leak) being executed inside the 64 bit console application:
    ```
    var sample_arr = [-1, 5, 7, 4, 0, 1, -5]
    function My_Partition(container, first_index, last_index) {
    var x = container[last_index];
    var i = first_index - 1;

    for (var elem = 0; elem < container.length-1; elem++) {
    if (container[elem] <= x) {
    i += 1;
    var temp_1 = container[i];
    container[i] = container[elem];
    container[elem] = temp_1;
    }
    }
    var temp_2 = container[i+1];
    container[i+1] = container[last_index];
    container[last_index] = temp_2;

    return i+1;
    }
    function My_Quick_Sort(container, first_index, last_index) {
    if (first_index < last_index) {
    var mid = My_Partition(container, first_index, last_index);
    My_Quick_Sort(container, first_index, mid-1);
    My_Quick_Sort(container, mid+1, last_index);
    }
    }
    My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
    console.WriteLine("Sorted Array:", sample_arr);
    ```
    as well as following ClearScript's V8 engine configuration:
    ```
    Using engine As New V8ScriptEngine("V8Engine", New V8RuntimeConstraints() With {.MaxOldSpaceSize = 209715200}, V8ScriptEngineFlags.EnableDebugging, 9222)
    ```
    You'll notice that after running above code in 5.3.9 it gracefully ends up with an exception that indicates a memory limit being exceeded (see attachment). However, in 5.3.10 it doesn't result in exception and rather hard-crashes with a message in the console (see attachment). Could you test it on your end and provide some follow-up?

    System used for testing: Window 7, 64 bit.

    Thanks for your work,
    Max


    0 0

    Hi everybody,

    I just noticed a behavior that differs between ClearScript v 5.3.9 and 5.3.10, and it's about limiting memory for a script execution. Consider following faulty script (it emulates a memory leak) being executed inside the 64 bit console application:
    ```
    var sample_arr = [-1, 5, 7, 4, 0, 1, -5]
    function My_Partition(container, first_index, last_index) {
    var x = container[last_index];
    var i = first_index - 1;

    for (var elem = 0; elem < container.length-1; elem++) {
    if (container[elem] <= x) {
    i += 1;
    var temp_1 = container[i];
    container[i] = container[elem];
    container[elem] = temp_1;
    }
    }
    var temp_2 = container[i+1];
    container[i+1] = container[last_index];
    container[last_index] = temp_2;

    return i+1;
    }
    function My_Quick_Sort(container, first_index, last_index) {
    if (first_index < last_index) {
    var mid = My_Partition(container, first_index, last_index);
    My_Quick_Sort(container, first_index, mid-1);
    My_Quick_Sort(container, mid+1, last_index);
    }
    }
    My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
    console.WriteLine("Sorted Array:", sample_arr);
    ```
    as well as following ClearScript's V8 engine configuration:
    ```
    Using engine As New V8ScriptEngine("V8Engine", New V8RuntimeConstraints() With {.MaxOldSpaceSize = 209715200}, V8ScriptEngineFlags.EnableDebugging, 9222)
    ```
    You'll notice that after running above code in 5.3.9 it gracefully ends up with an exception that indicates a memory limit being exceeded (see attachment). However, in 5.3.10 it doesn't result in exception and rather hard-crashes with a message in the console (see attachment). Could you test it on your end and provide some follow-up?

    System used for testing: Window 7, 64 bit.

    Thanks for your work,
    Max

    Comments: Update: The V8 developers have doubled down on their position that even artificial out-of-memory and stack overflow conditions warrant process termination, and that the sandboxing APIs they published earlier were a mistake. Their rationale is that clean recovery from these conditions is impossible without costly checks, and since Chrome's multi-process architecture withstands tab crashes, they're not willing to pay that price. Our bug report triggered a clarification of the team's policy: "I can understand that there are scenarios outside of Chrome where a slower execution handling OOM more gracefully would be preferable, but this is outside v8's scope." Over the last several months we've been trying to identify a safe, reliable, and durable workaround. A big problem is that, in addition to tangible resource constraints, V8 raises fatal errors when it exceeds various internal limits that the host cannot control or monitor. Therefore we've decided to abandon the search for a way to block V8 crashes. ClearScript will retain APIs such as `V8RuntimeConstraints` and `MaxRuntimeStackUsage`, but they will now function like the corresponding V8 APIs do; that is, they will not prevent process termination. We apologize for any inconvenience this causes ClearScript users.

    0 0

    Hi everybody,

    I just noticed a behavior that differs between ClearScript v 5.3.9 and 5.3.10, and it's about limiting memory for a script execution. Consider following faulty script (it emulates a memory leak) being executed inside the 64 bit console application:
    ```
    var sample_arr = [-1, 5, 7, 4, 0, 1, -5]
    function My_Partition(container, first_index, last_index) {
    var x = container[last_index];
    var i = first_index - 1;

    for (var elem = 0; elem < container.length-1; elem++) {
    if (container[elem] <= x) {
    i += 1;
    var temp_1 = container[i];
    container[i] = container[elem];
    container[elem] = temp_1;
    }
    }
    var temp_2 = container[i+1];
    container[i+1] = container[last_index];
    container[last_index] = temp_2;

    return i+1;
    }
    function My_Quick_Sort(container, first_index, last_index) {
    if (first_index < last_index) {
    var mid = My_Partition(container, first_index, last_index);
    My_Quick_Sort(container, first_index, mid-1);
    My_Quick_Sort(container, mid+1, last_index);
    }
    }
    My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
    console.WriteLine("Sorted Array:", sample_arr);
    ```
    as well as following ClearScript's V8 engine configuration:
    ```
    Using engine As New V8ScriptEngine("V8Engine", New V8RuntimeConstraints() With {.MaxOldSpaceSize = 209715200}, V8ScriptEngineFlags.EnableDebugging, 9222)
    ```
    You'll notice that after running above code in 5.3.9 it gracefully ends up with an exception that indicates a memory limit being exceeded (see attachment). However, in 5.3.10 it doesn't result in exception and rather hard-crashes with a message in the console (see attachment). Could you test it on your end and provide some follow-up?

    System used for testing: Window 7, 64 bit.

    Thanks for your work,
    Max

    Comments: Update: The V8 developers have doubled down on their position that even artificial out-of-memory and stack overflow conditions warrant process termination, and that the sandboxing APIs they published earlier were a mistake. Their rationale is that clean recovery from these conditions is impossible without costly checks, and since Chrome's multi-process architecture withstands tab crashes, they're not willing to pay that price. Our [bug report](https://code.google.com/p/v8/issues/detail?id=3060) triggered a clarification of the team's policy: "I can understand that there are scenarios outside of Chrome where a slower execution handling OOM more gracefully would be preferable, but this is outside v8's scope." Over the last several months we've been trying to identify a safe, reliable, and durable workaround. A big problem is that, in addition to tangible resource constraints, V8 raises fatal errors when it exceeds various internal limits that the host cannot control or monitor. Therefore we've decided to abandon the search for a way to block V8 crashes. ClearScript will retain APIs such as `V8RuntimeConstraints` and `MaxRuntimeStackUsage`, but they will now function like the corresponding V8 APIs do; that is, they will not prevent process termination. We apologize for any inconvenience this causes ClearScript users.

    0 0

    Hi everybody,

    I just noticed a behavior that differs between ClearScript v 5.3.9 and 5.3.10, and it's about limiting memory for a script execution. Consider following faulty script (it emulates a memory leak) being executed inside the 64 bit console application:
    ```
    var sample_arr = [-1, 5, 7, 4, 0, 1, -5]
    function My_Partition(container, first_index, last_index) {
    var x = container[last_index];
    var i = first_index - 1;

    for (var elem = 0; elem < container.length-1; elem++) {
    if (container[elem] <= x) {
    i += 1;
    var temp_1 = container[i];
    container[i] = container[elem];
    container[elem] = temp_1;
    }
    }
    var temp_2 = container[i+1];
    container[i+1] = container[last_index];
    container[last_index] = temp_2;

    return i+1;
    }
    function My_Quick_Sort(container, first_index, last_index) {
    if (first_index < last_index) {
    var mid = My_Partition(container, first_index, last_index);
    My_Quick_Sort(container, first_index, mid-1);
    My_Quick_Sort(container, mid+1, last_index);
    }
    }
    My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
    console.WriteLine("Sorted Array:", sample_arr);
    ```
    as well as following ClearScript's V8 engine configuration:
    ```
    Using engine As New V8ScriptEngine("V8Engine", New V8RuntimeConstraints() With {.MaxOldSpaceSize = 209715200}, V8ScriptEngineFlags.EnableDebugging, 9222)
    ```
    You'll notice that after running above code in 5.3.9 it gracefully ends up with an exception that indicates a memory limit being exceeded (see attachment). However, in 5.3.10 it doesn't result in exception and rather hard-crashes with a message in the console (see attachment). Could you test it on your end and provide some follow-up?

    System used for testing: Window 7, 64 bit.

    Thanks for your work,
    Max


    0 0

    Hi everybody,

    I just noticed a behavior that differs between ClearScript v 5.3.9 and 5.3.10, and it's about limiting memory for a script execution. Consider following faulty script (it emulates a memory leak) being executed inside the 64 bit console application:
    ```
    var sample_arr = [-1, 5, 7, 4, 0, 1, -5]
    function My_Partition(container, first_index, last_index) {
    var x = container[last_index];
    var i = first_index - 1;

    for (var elem = 0; elem < container.length-1; elem++) {
    if (container[elem] <= x) {
    i += 1;
    var temp_1 = container[i];
    container[i] = container[elem];
    container[elem] = temp_1;
    }
    }
    var temp_2 = container[i+1];
    container[i+1] = container[last_index];
    container[last_index] = temp_2;

    return i+1;
    }
    function My_Quick_Sort(container, first_index, last_index) {
    if (first_index < last_index) {
    var mid = My_Partition(container, first_index, last_index);
    My_Quick_Sort(container, first_index, mid-1);
    My_Quick_Sort(container, mid+1, last_index);
    }
    }
    My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
    console.WriteLine("Sorted Array:", sample_arr);
    ```
    as well as following ClearScript's V8 engine configuration:
    ```
    Using engine As New V8ScriptEngine("V8Engine", New V8RuntimeConstraints() With {.MaxOldSpaceSize = 209715200}, V8ScriptEngineFlags.EnableDebugging, 9222)
    ```
    You'll notice that after running above code in 5.3.9 it gracefully ends up with an exception that indicates a memory limit being exceeded (see attachment). However, in 5.3.10 it doesn't result in exception and rather hard-crashes with a message in the console (see attachment). Could you test it on your end and provide some follow-up?

    System used for testing: Window 7, 64 bit.

    Thanks for your work,
    Max


    0 0

    This is what i come out with in the end
    public static void forEach(this IEnumerable collection, dynamic action)
            {
                int i = 0;
                foreach (var item in collection)
                {
                    action(item, i, collection);
                    i++;
                }
            }

    0 0

    Hi people, I'm using ClearScript for some JavaScript integration recently, and noticed that calling a V8 function is exceptionally expensive if the calling method is being invoked for the first time, creating high latency. To demonstrate the problem I created this:
        class Program
        {
            static V8ScriptEngine engine;
    
            static void Main(string[] args)
            {
                engine = new V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers);
                dynamic func = engine.Evaluate("(function(){})");
    
                Console.WriteLine("3 times per call (1st)");
                CallMultipleTimes(func);
                Console.WriteLine();
    
                Console.WriteLine("3 times per call (2nd)");
                CallMultipleTimes(func);
                Console.WriteLine();
    
                Console.WriteLine("once per call");
                CallOnce(func);
                CallOnce(func);
            }
    
            static void CallMultipleTimes(dynamic func)
            {
                var sw1 = new Stopwatch();
                var sw2 = new Stopwatch();
                var sw3 = new Stopwatch();
    
                sw1.Start();
                func();
                sw1.Stop();
    
                sw2.Start();
                func();
                sw2.Stop();
    
                sw3.Start();
                func();
                sw3.Stop();
    
                Console.WriteLine("1st: " + sw1.ElapsedTicks.ToString());
                Console.WriteLine("2nd: " + sw2.ElapsedTicks.ToString());
                Console.WriteLine("3rd: " + sw3.ElapsedTicks.ToString());
            }
    
            static void CallOnce(dynamic func)
            {
                var sw = new Stopwatch();
                sw.Start();
                func();
                sw.Stop();
                Console.WriteLine(sw.ElapsedTicks.ToString());
            }
        }
    Which outputs something like:
    3 times per call (1st)
    1st: 123257
    2nd: 38059
    3rd: 37722
    
    3 times per call (2nd)
    1st: 114
    2nd: 93
    3rd: 76
    
    once per call
    38188
    112
    I tried profiling method calls, but the results aren't very assuming (70% in V8ScriptEngine.ctor, some 20% in Action.Invoke). I wonder if there is any way to mitigate this, beside creating a method just for calling functions? If it's not a ClearScript problem could ClearScript be modified to workaround, or it has to be left up to the application?

    Thanks!

    0 0

    Hi tadokoro,

    That's just the way .NET's dynamic infrastructure works. The initial pass through a dynamic call site is expensive, but inline caching greatly speeds up subsequent passes.

    One possibility, as you suggested, would be to replace dynamic calls with normal calls to a common method that holds a single dynamic call site. You'd still take the hit for the very first call though.

    Another possibility would be to eliminate the use of the dynamic infrastructure. Unfortunately ClearScript currently doesn't support any alternate API for interacting with script objects.

    Thanks!

    0 0

    For one of the projects, I use some pre-existing java script files and expose them via C# to use in some C# libraries.

    I am encountering errors while trying to execute java scripts files which uses async calls via setTimeout/setInterval.

    Specific error is "ReferenceError: setTimeout is not defined".

    Any suggestions or workaround on how I can execute such java script files?

    Thanks!!!

    0 0

    Greetings!

    ClearScript provides a bare JavaScript environment, whereas the functions you're looking for are part of the Web API, which is typically provided by web browsers.

    The functions are relatively easy to implement, but because they provide asynchronous operations, the implementation may depend on your application's threading model.

    Here's an example that uses .NET timers under the covers and should work in typical multithreaded .NET applications. First, a simple .NET class that provides timer access and can route callbacks to script code:
    publicstaticclass ScriptTimer {
        publicstaticobject Create(dynamic func, double delay, double period, object args) {
            var callback = new TimerCallback(state => func.apply(null, args));
            returnnew Timer(callback, null, Convert.ToInt64(delay), Convert.ToInt64(period));
        }
    }
    Next, script functions that use the class to provide the required behavior:
    engine.AddHostType("ScriptTimer", typeof(ScriptTimer));
    engine.Execute(@"
        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);
        setInterval = createTimer.bind(null, true);
        clearTimeout = function(id) { id.Dispose(); };
        clearInterval = clearTimeout;
    ");
    Again, this implementation may not be right for your application, but hopefully it gives you an idea of how .NET and script code can work together to provide whatever functionality you need.

    Good luck!

    0 0

    Hello xenadu,

    I have similar scenario like you except the scripts are not 'safe' in my case. Performance is also important in my case so I did some profiling and I created some performance improvements of the ClearScript engine. These improvements have very good performance impact on my application and I think, they can help also in your case. I've uploaded them to the 'PerformanceImprovements' fork on this server. I don't know if some of the improvements are merged or rewritten to the main trunk (they were commited on the 24th of Feb.).
    It will be very helpfull, if you have some time and you will execute your tests also on the improved version. Would be nice to have results from independent source.

    0 0

    hi, i have this source:
    http://pastebin.de/124823

    i add this class to the engine:
    se.AddHostType("NativeLibraryImporter", typeof(DynamicDllImport));
    Usage:
    dynamic c =  js.Evaluate(@"var o = new NativeLibraryImporter(""user32.dll"");");
    but c is undefined, and when i use MessageBox function with o.MessageBox then i get error: method.
    it work with c#, what is wrong?

    thank you

    0 0

    Hi furesoft,

    First of all, wow, that is some interesting code! A dynamic class for invoking native methods? Very clever.

    Unfortunately, there are a few issues:
    • In the above, the variable c is undefined because JavaScript statements (var in this case) don't have results. If you remove "var", you'll have an assignment expression, which has a result and is probably what you want.
    • At line 54 your class uses reflection to retrieve a private field from the binder. ClearScript's binder doesn't have a field with that name. As written, the code probably works only with a specific binder implementation.
    • DynamicDllImportMetaObject needs to provide an appropriate override for DynamicMetaObject.GetDynamicMemberNames(). JavaScript-style method invocation cannot work without it. Unfortunately there's no .NET API for enumerating native DLL exports.
    • Finally, there's a subtle bug in ClearScript that would prevent this from working even if all the other issues were resolved. We'll fix that ASAP.
    Good luck!

    0 0

    how can i get the method names from native dll?

    idk how, its not my wrapper

    0 0
  • 05/09/14--21:07: New Post: extension methods
  • hello,
    when i write a extension method to object in c#, can i use it in js?

    0 0

    hi,
    how can i create a function with a dynamic parameter length?

    0 0

    hi i have an Invalid constructor invocation but all is correct
        var xhr = new XmlHttpRequest();
        
        xhr.OnLoadFinish = function() {
            Console.Info(xhr.ResponseText);
        }; 
    
        xhr.Open("GET", "http://www.google.de/");
        xhr.Send(null);
    what is wrong?

    ps. here is the xmlhttprequest class:
    public class XmlHttpRequest
        {
    
            public XmlHttpRequest()
            {
                
            }
    
            public dynamic OnLoadFinish;
            public dynamic OnSendFinish;
    
            public string MimeType { get { return _webrequest.MediaType; } set { _webrequest.MediaType = value; } }
            public object ResponseText { get; set; }
    
            private HttpWebRequest _webrequest;
    
            public void Open(string method, string url)
            {
                _webrequest = (HttpWebRequest) WebRequest.Create(url);
                _webrequest.Method = method;
            }
    
            public void Send(object data)
            {
                if(data == null)
                {
                    var resp = (HttpWebResponse)_webrequest.GetResponse();
                    using (var s = resp.GetResponseStream())
                    {
                        ResponseText = new StreamReader(s).ReadToEnd();
                        if (OnLoadFinish != null) OnLoadFinish();  
                    }
                }
                else
                {
                    using (var s = _webrequest.GetRequestStream())
                    {
                        var sw = new StreamWriter(s);
                        sw.Write(data);
                        sw.Flush();
                        if(OnSendFinish != null) OnSendFinish();
                    }
                }
            }
    
        }

    0 0

    Unfortunately it isn't very simple. Have a look here.

    Alternatively, you can just fake it. Consider changing your class so that the client code must specify what native methods it intends to call:
    user32 = new NativeLibraryImporter('user32.dll', 'MessageBox', 'MessageBeep'/* etc */);
    The class would just store those names and return them from GetDynamicMemberNames().

    0 0
  • 05/10/14--07:51: New Post: extension methods
  • Yes. To make an extension method available for scripting, expose its class via AddHostType().

older | 1 | .... | 33 | 34 | (Page 35) | 36 | 37 | .... | 115 | newer