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

New Post: The type initializer for 'Microsoft.ClearScript.Util.AssemblyHelpers' threw an exception

$
0
0
Dear ClearScript.

Two solutions are feasible in my project.

Thank you very much.


ps. I'm using VS2010, I must find the *.dll from the nuget or otherway. would you please maintain a official package?

New Post: Debugging with Eclipse

$
0
0
Hi ClearScripts,

I try to find out how to debugging with Eclipse. The setup works fine and the output window in Eclipse shows me information like this:
> Received from Chrome:
Type: connect
V8-Version: 3.24.17
Protocol-Version: 1
Embedding-Host: Visual Studio JavaScript
Content-Length: 0


> end of message

> Sent to Chrome:

> end of message
Content-Length:46

{"seq":1,"type":"request","command":"version"}
> end of message

> Received from Chrome:
Content-Length: 134

{"seq":0,"request_seq":1,"type":"response","command":"version","success":true,"body":{"V8Version":"3.24.17"},"refs":[],"running":true}
> end of message
and so on. I guess "Chrome" is here my application, I named the V8ScriptEngine instance "Visual Studio JavaScript". But I don't see what to do with that. Is there any chance to write from my script something to the output window? And how may I set a breakpoint?

Best regards,
Torsten

New Post: Debugging with Eclipse

$
0
0
Hi Torsten,

You can use the Project Explorer window in Eclipse to open loaded scripts and set breakpoints. There's no way to write your own text to the Communication console, but you can use the Expressions window to evaluate arbitrary JavaScript expressions and invoke JavaScript functions.

Cheers!

New Post: How do I catch an exception if my script has an error

$
0
0
Hi,

My code:

Imports System.Xml
Imports Microsoft.ClearScript

Public Class WebForm1
Inherits System.Web.UI.Page

Dim xml As XmlDocument = New XmlDocument
Dim engine As Windows.JScriptEngine = New Windows.JScriptEngine

Public Sub Initialize()
    engine.AddHostObject("xml",xml)

    Try
        engine.execute("xml.LoadXml('</>/sales id=""1""/>');")
    Catch e As Exception
        Response.Write(e.Message)
    End Try

    If xml.InnerXml.ToString <> "" Then Response.Write(xml.SelectSingleNode("//sales").Attributes.GetNamedItem("id").InnerText)
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

End Class

I know the loadxml statement is wrong (means I am loading incorrect XML in it). But in this case VS breaks during run and it sends me to some clearscript source code. How can I trap this error. I do the same using Jint and I can trap the error and the project does not stop running. Thanks.

New Post: Does ClearScript use CodeDom

$
0
0
Does ClearScript use CodeDom where assemblies cannot be unloaded or is it a true scripting component like MS Script Control ?

New Post: How do I catch an exception if my script has an error

$
0
0
Greetings!

To prevent Visual Studio from breaking into the debugger in this situation, go to "DEBUG" -> "Exceptions..." and uncheck both boxes to the right of System.Runtime.InteropServices.COMException under "Common Language Runtime Exceptions".

Good luck!

New Post: Does ClearScript use CodeDom

$
0
0
Hi,

ClearScript does not use CodeDom.

Cheers!

New Post: Marshalling/Proxying between V8 and .Net

$
0
0
Hey guys, first off this project is fantastic. I'm really surprised by the ease of use here. My main question is that of performance and best practices.
  • Probably not a one-sentence answer, is there any documentation on how marshalling between the runtimes works so that I don't do something really terrible?
  • Are there any related recommended best practices?
  • What is the relative cost of adding host types?
  • What happens when "Compile" occurs? What costs does this mitigate?
  • Do you see this as something that could be used enterprise-scale or more of a utility?
Thanks!
Eric

New Post: How do I catch an exception if my script has an error

$
0
0
Thanks it worked like a charm! Truly awesome project which was long due from Microsoft for dotNet developers.

New Post: Does ClearScript use CodeDom

$
0
0
Is it a true scripting component like MS Script Control ? Can it be used in a 64-bit ASP.NET app ? I think it uses COM Interop and Reflection, do these cause memory leaks in the long run, etc ?

I am asking this because I have to oversee migration of an old VB 6 ASP.NET application to dotNET 4/4.5 framework. The old app makes extensive use of MS Script Control and has substantial amount of business logic. We cannot afford any mistakes in it and the reason for such a long delay in migration was only because there was no scripting support for dotNet and we did not want to use CodeDom which would cause memory leaks in the long run.

Thanks again for your time.

New Post: Does ClearScript use CodeDom

$
0
0
Hello!

Based on your earlier question in another thread, we'll assume that you're considering using ClearScript with JScript/VBScript rather than V8.

If that's correct, then yes, ClearScript uses the same JScript and VBScript engines as the Windows Script Control. However, as you pointed out, it also uses .NET facilities such as COM Interop and Reflection, and brings together components that were designed for fundamentally different environments.

To answer your specific questions:

Yes, ClearScript can be used in a 64-bit ASP.NET app. However, JScript and VBScript instances have thread affinity and therefore require careful handling in server applications. Also, ClearScript doesn't support certain server-friendly features, such as cloning. Finally, we've observed some JScript and VBScript bugs that are specific to the 64-bit versions of these components.

We're not currently aware of any memory leaks related to the use of COM Interop and Reflection, but of course there may always be bugs in any layer. We try to investigate issues as they're reported, but please keep in mind that this an open-source project with no official support.

Thanks for your questions, and good luck!

New Post: Marshalling/Proxying between V8 and .Net

$
0
0
Hi Eric,

Thanks for your kind words!

Probably not a one-sentence answer, is there any documentation on how marshalling between the runtimes works so that I don't do something really terrible?

Unfortunately there isn't much documentation on ClearScript internals other than the code itself.

Generally speaking, each exposed .NET object or type is wrapped in an instance of the internal class HostItem, which is then referenced by a JavaScript proxy object on the V8 side. Conversely, when a script returns a reference to a native JavaScript object, ClearScript wraps it in an instance of the internal class ScriptItem, which subclasses System.Dynamic.DynamicObject to support .NET's dynamic infrastructure.

These cross-runtime references prevent their targets from being garbage-collected, so care should be taken to avoid cross-runtime circular references that leak memory. You can dispose a script object to release its managed reference, but an exposed .NET reference can only be released when its V8-side proxy is collected - and V8 is extremely leisurely when it comes to garbage collection.

Are there any related recommended best practices?

Here are a few that come to mind:
  • Try not to compile the same script code more than once. Instead, reuse the output of the Compile() method, or hold references to compiled script functions for repeated execution.
  • Minimize your use of cross-runtime calls. Consider whether an expression such as a.b.c.d() can be simplified by caching a direct reference to a nested object. This is relevant in both host-to-script and script-to-host scenarios.
  • Control your use of .NET's dynamic machinery. Instead of declaring a variable as dynamic, consider casting it to dynamic where necessary. Otherwise the compiler may emit expensive dynamic operations in inconspicuous places.
  • Remember that direct access is convenient but relatively expensive. It may be cheaper to transfer a complex object in one step (via JSON or something similar) than to access its properties and nested objects one by one from the other side.
  • Consider sharing V8 runtimes among engine instances to reduce memory usage.
  • Use V8ScriptEngineFlags.DisableGlobalMembers in performance-critical applications.
Note that these are optimization ideas, not rules. ClearScript aims to combine ease of use with reasonable performance in most cases.

What is the relative cost of adding host types?

Exposing a host type is very similar to exposing a host object; it just provides access to a different set of members - constructors, static methods, nested types, etc. If the host type is a class that defines extension methods, then exposing it adds overhead to the method binding process for all exposed objects.

What happens when "Compile" occurs? What costs does this mitigate?

The Compile() method returns an opaque script representation that can be executed multiple times without recompilation. Sorry about the recursive explanation, but the details of the compiled script representation are not well documented. We assume that compilation involves at least parsing the script source code into a syntax tree of some sort.

Do you see this as something that could be used enterprise-scale or more of a utility?

ClearScript started life as a tool for automated testing, initially supporting only JScript and VBScript. In theory, V8 support should make it more suitable for server-side work, but to be honest, enterprise-level scalability has not been a priority for us so far. However, if you choose to evaluate ClearScript in that capacity, we'd love to get your feedback!

Cheers!

New Post: Does ClearScript use CodeDom

$
0
0
Hey guys!

Thanks for the prompt replies and lovely support!

Based on your answer I guess first thing I have to do is not use VBScript and JScript so .NET facilities like COM Interop and Reflection won't be used since these engine were made for fundamentally different enviroments as you yourself said. If COM Interop and Reflection are not used theres no question of memory leaks too. Theres no problem of server cloning feature missing or thread affinity or bugs in 64-bit applications.

I would prefer to use V8. Converting all old scripts (they are VBScript) to V8 is a one time thing so that's no big deal.

My final question is if I use V8 it supports 64-bit apps ? I hope it does not uses COM Interop / Reflection and does not have thread affinity problem or bugs in 64-bit apps and can work with server cloning features ?

New Post: Dynamic properties added to DynamicObject are undefined

$
0
0
Hi,

First, thanks again for an amazing project.

It seems that dynamic properties added to a DynamicObject are not handled correctly.
    class MyDynamicObject : DynamicObject
    {
        private Dictionary<string, object> _dict;
        public MyDynamicObject()
            : base()
        {
            this._dict = new Dictionary<string, object>();
        }

        public string RealProp { get; set; }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return this._dict.TryGetValue(binder.Name, out result);
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            this._dict[binder.Name] = value;
            return true;
        }
    }


    class DynamicTest
    {
        public void Run()
        {
            using (var engine = new V8ScriptEngine())
            {
                dynamic myDynamicObject = new MyDynamicObject();

                string val = "Value1";
                myDynamicObject.DynProp = val;
                myDynamicObject.RealProp = val;
                
                engine.AddHostObject("myDynamicObject", myDynamicObject);
                engine.AddHostType("Console", typeof(Console));

                Console.WriteLine("myDynamicObject.DynProp = " + myDynamicObject.DynProp);

                Console.WriteLine("myDynamicObject.RealProp = " + myDynamicObject.RealProp);

                // The following print 'undefined'
                engine.Execute("Console.WriteLine('In script: myDynamicObject.DynProp = ' + myDynamicObject.DynProp)");

                engine.Execute("Console.WriteLine('In script: myDynamicObject.RealProp = ' + myDynamicObject.RealProp)");

                // Dynamic properties added to ExpandoObject are OK
                dynamic myExpandoObject = new ExpandoObject();
                myExpandoObject.DynProp = val;
                engine.AddHostObject("myExpandoObject", myExpandoObject);
                engine.Execute("Console.WriteLine('In script: myExpandoObject.DynProp = ' + myExpandoObject.DynProp)");
            }
        }
    }
The output of this test is:
myDynamicObject.DynProp = Value1
myDynamicObject.RealProp = Value1
In script: myDynamicObject.DynProp = undefined
In script: myDynamicObject.RealProp = Value1
In script: myExpandoObject.DynProp = Value1
Note that the same test for ExpandoObject works OK.
It seems to me that ClearScript overrides TryGetMember() without calling base.TryGetMember().

Thanks again,
Ron

New Post: Dynamic properties added to DynamicObject are undefined

$
0
0
Hi Ron,

To provide scriptable access to the members of a dynamic host object, ClearScript requires that the object provide a correct implementation of GetDynamicMemberNames. In your example, it could look something like this:
publicoverride IEnumerable<string> GetDynamicMemberNames()
{
    returnthis._dict.Keys;
}
Thanks for your question, and good luck!

New Post: Performance Improvements

$
0
0
Hi,

performance is really important in our application. That's why we switched from Jurassic to ClearScript. Now we have much better results, but I still found some places in ClearScript code, that could be improved. I made these improvements and the execution of one of our scripts is now more then two times faster then without these improvements. I have quite special case - milions of JavaScript objects mapped to .Net objects. But I still think, these improvements may help also to some other people. May I create fork in your repository and upload the changes? Maybe you will like them and include them to the main branch.

Anyway, great work on ClearScript. It's well created and really easy to use.

Thanks, Libor.

New Post: Performance Improvements

$
0
0
Hi Libor,

That sounds great! Please feel free to fork the code and add your changes. And thanks for your positive feedback!

Cheers!

New Post: Performance Improvements

$
0
0
Hi,

I've commited updates in the new created fork. I hope, it will be usefull ;-)

Libor.

New Post: Marshalling/Proxying between V8 and .Net

New Post: Does ClearScript use CodeDom

$
0
0
Hello again shripaldalal!

Thanks for the detailed description of your scenario. It sounds like JScript/VBScript should work just fine, and yes, both engines support 64-bit processes. Thread affinity should not be a problem as long as your application processes requests synchronously.

V8 should work as well, and is your only choice if your application uses asynchronous operations to process requests. V8 is generally much faster than JScript, but deploying ClearScript with V8 is a bit more difficult because it involves multiple assemblies.

Good luck!
Viewing all 2297 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>