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 | .... | 100 | 101 | (Page 102) | 103 | 104 | .... | 115 | newer

    0 0

    Repeated calls to `V8Runtime.CreateScriptEngine` make the runtime's heap grow even if the engine instances are disposed immediately. This is because the `V8ContextImpl` destructor neglects to to dispose the termination exception.

    0 0
  • 06/07/16--07:16: Updated Wiki: Home
  • 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.

    Features

    • Simple usage; create a script engine, add your objects and/or types, run scripts
    • Support for several script engines: Google's V8, Microsoft's JScript and VBScript
    • Exposed resources require no modification, decoration, or special coding of any kind
    • Scripts get simple access to most of the features of exposed objects and types:
      • Methods, properties, fields, events
      • (Objects) Indexers, extension methods, conversion operators, explicitly implemented interfaces
      • (Types) Constructors, nested types
    • Full support for generic types and methods, including C#-like type inference and explicit type arguments
    • Scripts can invoke methods with output parameters, optional parameters, and parameter arrays
    • Script delegates enable callbacks into script code
    • Support for exposing all the types defined in one or more assemblies in one step
    • Optional support for importing types and assemblies from script code
    • The host can invoke script functions and access script objects directly
    • Full support for script debugging

    Examples

    using System;
    using Microsoft.ClearScript;
    using Microsoft.ClearScript.JavaScript;
    using Microsoft.ClearScript.V8;
    
    // create a script engineusing (var engine = new V8ScriptEngine())
    {
        // expose a host type
        engine.AddHostType("Console", typeof(Console));
        engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");
    
        // expose a host object
        engine.AddHostObject("random", new Random());
        engine.Execute("Console.WriteLine(random.NextDouble())");
    
        // expose entire assemblies
        engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
        engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");
    
        // create a host object from script
        engine.Execute(@"
            birthday = new lib.System.DateTime(2007, 5, 22);
            Console.WriteLine(birthday.ToLongDateString());
        ");
    
        // use a generic class from script
        engine.Execute(@"
            Dictionary = lib.System.Collections.Generic.Dictionary;
            dict = new Dictionary(lib.System.String, lib.System.Int32);
            dict.Add('foo', 123);
        ");
    
        // call a host method with an output parameter
        engine.AddHostObject("host", new HostFunctions());
        engine.Execute(@"
            intVar = host.newVar(lib.System.Int32);
            found = dict.TryGetValue('foo', intVar.out);
            Console.WriteLine('{0} {1}', found, intVar);
        ");
    
        // create and populate a host array
        engine.Execute(@"
            numbers = host.newArr(lib.System.Int32, 20);
            for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; }
            Console.WriteLine(lib.System.String.Join(', ', numbers));
        ");
    
        // create a script delegate
        engine.Execute(@"
            Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);
            oddFilter = new Filter(function(value) {
                return (value & 1) ? true : false;
            });
        ");
    
        // use LINQ from script
        engine.Execute(@"
            oddNumbers = numbers.Where(oddFilter);
            Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
        ");
    
        // use a dynamic host object
        engine.Execute(@"
            expando = new lib.System.Dynamic.ExpandoObject();
            expando.foo = 123;
            expando.bar = 'qux';
            delete expando.foo;
        ");
    
        // call a script function
        engine.Execute("function print(x) { Console.WriteLine(x); }");
        engine.Script.print(DateTime.Now.DayOfWeek);
    
        // examine a script object
        engine.Execute("person = { name: 'Fred', age: 5 }");
        Console.WriteLine(engine.Script.person.name);
    
        // read a JavaScript typed array
        engine.Execute("values = new Int32Array([1, 2, 3, 4, 5])");
        var values = (ITypedArray<int>)engine.Script.values;
        Console.WriteLine(string.Join(", ", values.ToArray()));
    }

    0 0

    I have a weird issue, ClearScript sometimes crashes the application pool when
    used intensively in my web application.

    I got a dump file and opened it in Visual Studio but I'm missing the symbols file
    ClearScriptV8-32.pdb to see a proper StackTrace and report something useful.

    I'm currently using the nuget package, I'd like to avoid setup up the build on my computer to generate a pdb (I never built C++ projects on this computer, will probably need some installs...), could someone with the build already setup send this pdb to me (for the v5.04.6.0) ?

    Thanks

    0 0

    Greetings!

    The symbol file may not be useful unless it matches the assembly precisely. It would have to have been generated from the same source code and with the same compiler version, identical options, etc. Therefore it would make sense to contact the NuGet package owners for a copy of the file.

    V8 issues can also be difficult to diagnose without a debug build of V8, so setting up a ClearScript/V8 build environment might be worthwhile.

    In the meantime, can you somehow share your dump file?

    Thanks!

    0 0

    Hi,

    Thank you for your answer.
    I was wondering the same thing about the pdb but unfortunately I failed to build v8 (the build script reports a failed step but there's no other information).
    I'll try to look more into it monday.
    The dump file is pretty large (800mb): you can grab it here:
    https://dl.dropboxusercontent.com/u/177896/dev/iisexpress__PID__13340__Date__06_10_2016__Time_12_46_50PM__896__Second_Chance_Exception_C0000409.dmp

    Thank you in advance

    0 0

    HI guillaume,

    this might be the same issue that we are getting - but only with versions of ClearScript after 5.4.3. Could you try and see if you get the same issue after downgrading?

    0 0

    Hi,

    I'm trying the downgrade, thanks for the tip.

    0 0

    HI again Andrea,

    It looks like it works, thanks a lot.
    I'll not be 100% sure before letting my app run for few days but my attempts to manually trigger the issue do not work on that version, seems good.

    I hope this information will also help the devs to find the origin of the issue.

    0 0

    Hi guillaume86,

    Thanks for sharing your crash dump! Although we couldn't symbolize most of the crash stack without matching PDBs, a VCRT symbol at the very top provided a strong clue. We'll post a potential fix within a few days. Unfortunately, since we can't reproduce your crash, we won't be able to verify the fix.

    Thanks again!

    0 0

    Thanks,

    I'll run your new version locally to confirm (at 99%) that the problem is gone, and if not I'll try again to build it myself to provide the call stack.
    Unfortunately I was unable to isolate the issue in a small project to share the crashing code easily (still have no idea what is triggering it).

    0 0

    Hi guillaume86,

    A potential fix has been posted here. Please give it a try if you get a chance.

    Cheers!

    0 0

    Hardened native timers and V8 background tasks, potentially fixing reported intermittent V8 crashes. Tested with V8 5.1.281.65.

    0 0
  • 06/24/16--01:52: New Post: Waiting for input
  • Hey everyone.

    I'm developing a game server for a game client and I needed a scripting interface to use for NPCs, so I tried using ClearScript, however I came into an issue.

    First, here's an example of a NPC script:
    character.SendNext("Welcome, press next to proceed.");
    character.SendNext("Good! Now, press next again.");
    bool yes = character.SendYesNo("Do you like it?");
    
    if (yes) {
          character.SendOk("Good!");
    } else {
         character.SendOk("Okay!");
    }
    When you click on a NPC in the game, the client sends a packet with the NPC id, and then the server knows which script to execute. Once it executes the script, I want the script to be able to wait for the user input. So, after it executes "character.SendNext", I want to script to halt and wait until an input has been given.

    Once the user clicks the next button, the client sends another packet. Once that packet is received, I want to resume the script depending on the packet's value.

    How can that be achieved?

    0 0

    Hey everyone.

    I'm working on a game server for a game client and I'm using ClearScript as my scripting interface.

    I have an abstract class called 'ScriptBase' and classes that inherit from it: 'NpcScript', 'PortalScript' and 'ReactorScript'.

    'ScriptBase' contains all the necessary fields and methods like the script's engine, the 'Run' method and everything else.

    'NpcScript', 'PortalScript' and 'ReactorScript' are basically the implementation of each game object's script. So, when I interact with a NPC for example, I run a new instance of NpcScript like so:
    new NpcScript(104000).Run();
    The 'ScriptBase' class does all the work - it looks for the appropriate file and executes it. However, I want to be able to include methods to expose to the engine from within each class. In 'NpcScript' class, for example, I have a few methods: 'SendNext(string text)', 'SendYesNo(string text)' - how would I expose them to the engine if 'ScriptBase' is abstract so I can use them like so (in a script file):
    SendYesNo("hello");

    0 0

    Hello, Fraysa!

    There are many ways to do this. Here's one possibility:
    publicabstractclass ScriptBase {
        protected ScriptEngine _engine { get; privateset; }
        privatestring _code;
        publicvirtualvoid Run() {
            _engine.Execute(_code);
        }
        /* ... */
    }
    publicclass NpcScript : ScriptBase {
        privatevoid SendNext(string text) { /* ... */ }
        privatevoid SendYesNo(string text) { /* ... */ }
        publicoverridevoid Run() {
            _engine.Script.SendNext = new Action<string>(SendNext);
            _engine.Script.SendYesNo = new Action<string>(SendYesNo);
            base.Run();
        }
        /* ... */
    }
    This is just one possibility, but hopefully it gives you an idea of what's possible.

    Good luck!

    0 0
  • 06/24/16--10:55: New Post: Waiting for input
  • Hi Fraysa,

    Like any other code, script code can invoke modal or blocking APIs. Your SendYesNo function could be made to await user input before returning to its (script-based) caller; in fact, its signature implies that it does just that, as it returns the user's selection. You could also expose a function that just waits for input. It all depends on your application's architecture.

    Script code can also use asynchronous programming techniques to avoid tying up threads while waiting for input. This might be a good idea on the server if you expect to have many scripts in flight concurrently, but heavy use of callbacks will make your script code less straightforward.

    Unfortunately there's no way to suspend script execution externally and save its state for subsequent resumption. The operating system can do that at the thread level, but here again script code is no different from other code.

    Cheers!

    0 0

    Hi,

    I have a number of C# host objects added into the engine.
    In case of exception within C#, the exception caught in the javascript has message as "Exception has been thrown by the target of an invocation" and type as object. Here is the sample code:
    _try
    {
    loginResponse = httpclient.Post(authUri,JSON.stringify(loginInfo), '', '', 'application/json');
    }
    catch(err)
    {
    Log.Error('err message...' + err.message);
    var type = typeof err;
    Log.Info('Exception returned type - ' + type);  
    }_
    httpclient and Log are my host objects here.

    I tried adding the expected exception type as hosttype but still its the same.

    Is there any better way to handle this ?
    I need to know the actual type or message. Looks like the whole exception object is getting lost due to a re-throw like scenario?

    0 0

    Hello maicalal,

    Which JavaScript engine are you using (V8 or JScript)?

    Thanks!

    0 0

    V8

    0 0

    Hi again,

    When you use V8, ClearScript marshals host exceptions to script code. In the above, the host exception should be accessible via err.hostException. Note that this is likely to be an instance of TargetInvocationException, so you might want to use err.hostException.GetBaseException() to access the "root" exception (see here).

    Another possibility (and one that works with all script engines) is to use ClearScript's HostFunctions.tryCatch.

    Good luck!

older | 1 | .... | 100 | 101 | (Page 102) | 103 | 104 | .... | 115 | newer