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 | .... | 27 | 28 | (Page 29) | 30 | 31 | .... | 115 | newer

    0 0

    Hello nbgedo!

    ClearScript does not convert host arrays to VBScript arrays. To retrieve a value from a host array, you have to use a host method. For example:
    engine.Execute(@"
        x = arr.GetValue(0)
        call arr.SetValue(x, 1)
    ");
    Cheers!

    0 0

    If this helps anybody, I encapsulated some of the ideas in this post into a project:
    https://github.com/eswann/ClearScript.Manager

    And a Nuget package:
    https://www.nuget.org/packages/ClearScript.Manager/

    0 0

    If this helps anybody, I encapsulated some of the ideas in this post into a project:
    https://github.com/eswann/ClearScript.Manager

    And a Nuget package:
    https://www.nuget.org/packages/ClearScript.Manager/

    0 0

    I installed the nuget package and then removed it. Seems to have left behind the post build event command lines. Booo :(

    0 0

    I installed the nuget package and then removed it. Seems to have left behind the post build event command lines. Booo :(
    Comments: For NuGet package issues, please contact the package's owners.

    0 0

    I installed the nuget package and then removed it. Seems to have left behind the post build event command lines. Booo :(

    0 0

    Added shared bind cache for improved performance and enhanced binder leak mitigation.

    0 0

    Thanks, Eric! Very nice work!

    0 0

    I have a List<ISomeInterface> in c# that I am trying to work with in vbscript.

    While I can access some methods of the list (Count seems to work), I cannot enumerate it, no matter what I seem to try.

    I have attempted For Each, but the list is not seen as a container so that fails.
    I have attempted indexing like so
      For i = 0 to someList.Count
         myLog.Info "Name: " & someList.ElementAt(i).Name
      Next
    
    which fails, 'ElementAt' is not recognized "Object doesn't support this property or method"

    someList[i] fails with "expected end of statement" (apparently invalid vbscript)
    someList(i) fails with the "Object doesnt support this property or method" (not unexpected)

    I read the discussion on dealing with arrays in vbscript here https://clearscript.codeplex.com/discussions/539210

    and see my comment about ElementAt above. Also, I tried exporting the list as an array instead, and that had simmilar issues.

    Here is how I am calling it... Please note that I must use .Execute and not .Evaluate
             using (ScriptEngine engine = new Microsoft.ClearScript.Windows.VBScriptEngine())
             {
                engine.AddHostObject("myLog", Logger.Instance);
                engine.AddHostObject("someList", someList);
    
                try
                {
                   engine.Execute(scriptText_);
                }
                catch (ScriptEngineException sce)
                {
                   Log.Error("The Script threw an exception.  Details: {0}", sce.Format());
                   return false;
                }
             }
    Any help would be appreciated.

    0 0

    What is the preferred/best way to get error information from the script when using .Execute?

    Catching 'ScriptEngineException' works... sort of, but it gives very unhelpful data. No line number (from the script itself), no error text that might have been raised or thrown by the script, etc. What it does seem to give is whatever error the interpreter might have thrown.

    Here is an example...
    1) Exception Type: Microsoft.ClearScript.ScriptEngineException EngineName: VBScriptEngine ErrorDetails: __Object does not support the requested invocation operation__ IsFatal: False Message: Object does not support the requested invocation operation Data: System.Collections.ListDictionaryInternal TargetSite: Void ThrowScriptError(Microsoft.ClearScript.IScriptEngineException) HelpLink: NULL Source: ClearScript HResult: -2146190593 Trace: at Microsoft.ClearScript.ScriptEngine.ThrowScriptError(IScriptEngineException scriptError) at Microsoft.ClearScript.Windows.WindowsScriptEngine.ThrowScriptError(Exception exception) at Microsoft.ClearScript.Windows.WindowsScriptEngine.<>c__DisplayClass14.<ScriptInvoke>b__13() at Microsoft.ClearScript.ScriptEngine.ScriptInvoke(Action action) at Microsoft.ClearScript.Windows.WindowsScriptEngine.ScriptInvoke(Action action) at Microsoft.ClearScript.Windows.WindowsScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard) at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code) at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code) at Microsoft.ClearScript.ScriptEngine.Execute(String code) at EmbeddedScript.Execute() in []EmbeddedScript.cs:line 101 2) Exception Type: System.NotSupportedException Message: Object does not support the requested invocation operation Data: System.Collections.ListDictionaryInternal TargetSite: System.Object InvokeHostMember(System.String, System.Reflection.BindingFlags, System.Object[], System.Object[], System.Globalization.CultureInfo) HelpLink: NULL Source: ClearScript HResult: -2146233067 Trace: at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture) at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture) at Microsoft.ClearScript.HostItem.<>c__DisplayClass3e.<System.Reflection.IReflect.InvokeMember>b__3d() at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func`1 func) at Microsoft.ClearScript.Windows.WindowsScriptEngine.HostInvoke[T](Func`1 func)
    
    The call stack is useless, because the error was thrown by the script. Column and line number of the script would be excellent.

    Any ideas?

    0 0

    Greetings!

    The List class has an indexed property named "Item". This property is exposed as a C# indexer, but you can use it from script in its true form:
    engine.AddHostObject("list", new List<string> { "abc", "def", "ghi", "jkl" });
    engine.AddHostType("Console", typeof(Console));
    engine.Execute(@"
        for i = 0 to list.Count - 1
            Console.WriteLine list.Item(i)
        next
    ");
    The ElementAt method is a LINQ extension. To use it, you must expose LINQ's Enumerable class:
    engine.AddHostType("Enumerable", typeof(Enumerable));
    engine.Execute(@"
        for i = 0 to list.Count - 1
            Console.WriteLine list.ElementAt(i)
        next
    ");
    Another thing you can do is use a .NET enumerator:
    engine.Execute(@"
        e = list.GetEnumerator()
        while e.MoveNext()
            Console.WriteLine e.Current
        wend
    ");
    Good luck!

    0 0

    Thank you, that has resolved it!

    0 0

    Hi TooSlow,

    The ErrorDetails property of ScriptEngineException should contain the information you're looking for. However, Windows script engines (JScript and VBScript) only provide this information if you enable script debugging. To do so, pass WindowsScriptEngineFlags.EnableDebugging to the script engine's constructor.

    By the way, please see here for more information.

    Cheers!

    0 0

    Well, I suppose that is good and bad news, but it gets me to where I need to be, thanks again!

    0 0

    Add the nuget package for Clearscript V8 to a solution, build solution this completes with no errors.
    Remove folder packages\ClearScript.V8.5.3.10.0, build solution this restores and completes with no errors.

    remove V8 and replace with Clearscript without V8 dependancies.
    Remove the folder packages\ClearScript.5.3.8, now the build solution fails because package restores fails:

    Unable to find version '5.3.8' of package 'ClearScript'

    0 0

    Add the nuget package for Clearscript V8 to a solution, build solution this completes with no errors.
    Remove folder packages\ClearScript.V8.5.3.10.0, build solution this restores and completes with no errors.

    remove V8 and replace with Clearscript without V8 dependancies.
    Remove the folder packages\ClearScript.5.3.8, now the build solution fails because package restores fails:

    Unable to find version '5.3.8' of package 'ClearScript'
    Comments: Please report NuGet package issues to the package owners.

    0 0

    What do you think about this setup for a highly concurrent server scenario.

    To give the discussion some context, let us suppose that this server lets people browse a hierarchical filesystem. Let us also suppose that this server lets administrators setup javascript to calculate properties on-demand, so as users click on files or directories, in addition to the normal properties they also see the output of each configured javascript property.
    1. One V8Runtime instance
    2. Each script is compiled and stored in an immutable hashtable. If the script associated to a property is modified, then a new script is compiled and a new mutated hashtable is installed with Interlocked.CompareExchange. The next request to come in will thus see the new list of script definitions to choose from.
    3. When a request comes in on a worker thread that has no existing V8ScriptEngine instance assigned, a new instance will be created from the V8Runtime.CreateScriptEngine method. The script to be executed is located in the hashtable and executed. The V8ScriptEngine instance will be kept around, associated to that thread.
    a) Are there any issues with this setup? The data being passed in to the individual scripts is always immutable, so there is no problem with shared state there. The goal is to be completely lock-free if possible and to support high concurrency.

    b) The V8Runtime may eventually end up with a lot of junk old scripts presumably, but assuming the scripts don't change very often that shouldn't be significant. Is it possible to eventually allow the compiled script to be collected if nothing has a reference to it anymore?

    c) The scripts themselves are created by trusted administrators, so if they want to junk up the environment by smashing all the prototypes and replacing Math with an object that always vends NaNs then that's their fault :) Presumably if we needed a higher safety mode we could dump the V8ScriptEngine and create a new instance, correct?

    d) A big question is whether the compiled script can be re-used among V8ScriptEngine instances simultaneously, or if there are thread issues or locks involved. Could someone mutate the compiled script and introduce threading issues?

    e) Another question relates to resource usage... are the limits enforced on the entire runtime or on each V8ScriptEngine instance?

    f) Under what scenario would you even want multiple V8Runtime instances?



    All opinions welcome! I think it would be awesome (and cut down on questions) to have some scenarios like this laid out and on the documentation page here on the site; then people could choose a design that matches their goals.

    0 0

    Greetings, xenadu!

    Your application sounds very interesting!

    One V8Runtime instance [...]
    Under what scenario would you even want multiple V8Runtime instances?

    Actually, without multiple V8 runtime instances there can be no concurrency. V8 runtimes are not thread-safe and explicitly block multithreaded access.

    3.When a request comes in on a worker thread that has no existing V8ScriptEngine instance assigned, a new instance will be created from the V8Runtime.CreateScriptEngine method.

    That sounds good, but again, in order to support concurrent script execution, your worker threads must not share V8 runtimes. This also means that they can't share compiled scripts.

    Is it possible to eventually allow the compiled script to be collected if nothing has a reference to it anymore?

    Sure. A V8 compiled script will eventually be garbage-collected if it's no longer referenced. At that time it'll release its unmanaged counterpart, making it available to V8's garbage collector.

    Presumably if we needed a higher safety mode we could dump the V8ScriptEngine and create a new instance, correct?

    Yes, and this is why it sometimes makes sense to instantiate V8Runtime and call V8Runtime.CreateScriptEngine() even if you don't plan to share the runtime. This way, if you need a clean execution environment, you can spin up a new engine instance efficiently and reuse previously compiled scripts.

    A big question is whether the compiled script can be re-used among V8ScriptEngine instances simultaneously, or if there are thread issues or locks involved. Could someone mutate the compiled script and introduce threading issues

    A V8 compiled script is bound to a single V8 runtime instance. It is therefore incapable of concurrent execution. It is also immutable. Its advantage over plain-text JavaScript is that it can be re-executed more efficiently and reused across engines that share the runtime.

    Another question relates to resource usage... are the limits enforced on the entire runtime or on each V8ScriptEngine instance

    V8 resource constraints are applied at the runtime level, but nothing prevents you from creating a separate runtime for a given engine instance. In fact, that's exactly what happens when you invoke a V8ScriptEngine constructor.

    Thanks for your questions, and good luck!

    0 0
  • 03/23/14--03:15: New Post: Memory consumption
  • Hello,

    I'm using clearscripts V8 facilities to process datasets in an ETL application. For each table I create a V8Runtime, for each row a V8Engine. For testing purposes I hand over no HostObjects right now, the Javascript-Code does nothing, and I don't read something back to the .NET side. After each script processing I dispose the V8Engine and trigger a CollectGarbage on the V8Runtime. After that I output the HeapInfo.

    The HeapInfo suggests, that the memory-consumption of the v8 runtime remains more or less constant. But using Sysinternals Process-Explorer reveals, that the process working set as well as private bytes grow constantly to serveral GBs. When I'm short circuiting the part of my code which uses clearscript and therefore don't interact with V8 the memory-consumption of my .NET app is perfectly constant.

    Has anyone experienced something similar and give me a hint to common pitfalls?
    Thank you and andvance and best regards

    Joachim

    0 0
  • 03/23/14--05:24: New Post: Memory consumption
  • Greetings, Joachim!

    We run several tests before every release to check for memory leaks. For example, we run the SunSpider JavaScript benchmark in a loop for several hours. That test exposes some host objects and collects results, so it would seem to be a little more comprehensive than your current usage, yet we haven't encountered any runaway memory consumption.

    Please give us more information about your application. For example, what ClearScript version are you using? What are you passing to the V8Runtime constructor and V8Runtime.CreateScriptEngine()? You mentioned that your script code currently does nothing, but what exactly does it look like? How many runtimes and engines are active at once?

    If you could distill your application to a small code sample that exhibits the problem, that would be very helpful.

    Thank you!

older | 1 | .... | 27 | 28 | (Page 29) | 30 | 31 | .... | 115 | newer