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

    0 0

    Rated 5 Stars (out of 5) - Impressive project and very responsive contributors. Actively developed, easy to use and it works well.

    0 0

    I tried this in JScript and it does not work (same as VBScript):
    js.Execute("for(row in dt.Rows) { for(col in row.Table.Columns) { Response.Write row(col.ColumnName).ToString(); } }")
    Can this work in V8 ? I don't mind using Count and Item properties for iteration but I would prefer this more.

    If I declare row and col (the variables I use in the engine) initially as then I get error invalid assignment:
    Public row as DataRow
    Public col as DataColumn 
    
    Public Sub Initialize()
    
    js.AddRestrictedHostObject("me", HostItemFlags.GlobalMembers, Me)
    js.Execute("for(row in dt.Rows) { for(col in row.Table.Columns) { Response.Write row(col.ColumnName).ToString(); } }")
    
    End Sub
    If I dont declare the variables it gives error as: Object not a collection at Microsoft.ClearScript.ScriptEngine

    0 0

    Hello shripaldalal,

    Can this work in V8 ? I don't mind using Count and Item properties for iteration but I would prefer this more.

    JavaScript's for/in statement does work, but it doesn't do what you think it does. It iterates through the names of an object's properties. For a .NET DataRowCollection, it returns "Add", "Clear", "Contains", etc. It returns strings, and since you can't assign a string to a .NET property of type DataRow, you get "Invalid field assignment".

    In general, script constructs such as JavaScript's for/in and VBScript's for each were designed for script object models that are very different from .NET's. ClearScript aims to provide full access to .NET objects and types; it doesn't (and often can't) automatically create ideal APIs for specific script languages.

    That said, it might be possible to expose .NET collections to VBScript's for each. We're looking into it.

    If I dont declare the variables it gives error as: Object not a collection at Microsoft.ClearScript.ScriptEngine

    "Object not a collection" is a VBScript-specific error message. Are you sure you're seeing it when using JScript?

    Thanks!

    0 0
  • 03/09/14--10:56: New Post: Delegate
  • Hi,
    How can i use a Delegate to bind a function with a event?

    0 0

    Hi,

    Following code does not work. Expected result is 20.
    I have one global script and one child script that use it.
    numValue variable from the global script is not available from in child script.
    using(var globalScript = new VBScriptEngine())
    using(var childScript = new VBScriptEngine())
    {
        Console.WriteLine("Start VBScriptEngine");
        globalScript.Script.numValue = 4;
        childScript.Script.GlobalScript = globalScript.Script;
        Console.WriteLine(childScript.Evaluate("5 * numValue"));
        Console.WriteLine("End script");
        Console.ReadKey();
    }

    0 0
  • 03/10/14--04:23: New Post: Delegate
  • Hi furesoft,

    Please see this thread for an example of how to handle .NET events in script code.

    Good luck!

    0 0

    Hello ifle,

    Since you're exposing the parent's global object in the child under the name "GlobalScript", you probably meant to write this:
    Console.WriteLine(childScript.Evaluate("5 * GlobalScript.numValue"));
    By the way, we don't recommend exposing script items to other script engines. It may work, but it isn't well tested and there are scenarios that are known to be problematic, especially when different script engine classes are involved.

    Cheers!

    0 0

    This way that works our legacy application.
    My example is very simple, but our global script contains hundreds functions that can be called from a customer child script functions

    0 0

    Sorry. This my mistake, our application used with AddNamedItem with globalvariable flag.

    0 0

    Hi ifle,

    This seems to work:
    using (var globalScript = new VBScriptEngine())
    using (var childScript = new VBScriptEngine())
    {
        Console.WriteLine("Start VBScriptEngine");
        globalScript.Script.numValue = 4;
        childScript.AddHostObject("GlobalScript", HostItemFlags.GlobalMembers, globalScript.Script);
        Console.WriteLine(childScript.Evaluate("5 * numValue"));
        Console.WriteLine("End script");
        Console.ReadKey();
    } 
    However, again, it isn't recommended. AddHostObject() is intended to be used with, well, host objects :)

    Instead of exposing script items across engines, would it not be possible to load everything you need into the child engine?

    0 0

    Hi ClearScript team,

    I have javascript loaded in my website and it creates div and append that to body. I want to know by using clearscripts, can I execute that javascript file and get that output to a string variable (I have to manipulate that html code in server side and save that to db without using any browser activities). Please give me your suggestions about this matter?

    I really appreciate your work and thanks for sharing this.

    Regards
    chap

    0 0

    Hi there,

    I'm back with more exception-related questions! :)
    Now I'm trying to handle (or at least, correctly log) an unhandled exception thrown from JS.

    Something like this:
    engine.Execute("throw 'hello'");
    will throw a ScriptEngineException with its Message set to 'hello'.

    But when the JS throws an object, like:
    engine.Execute("throw { field: 'hello' }");
    it looks like there is no way to retrieve the object from the ScriptEngineException, whose Message only says "[object Object]".

    I get pretty much the same results when throwing a CLR object from JS, with the exception's Message getting "[object HostObject]".

    So is there any other way to catch in .NET objects thrown from JS?

    Thanks!

    0 0

    Greetings, chap!

    ClearScript provides only a "bare" JavaScript execution environment to which you can add .NET objects and types. It does not implement the HTML DOM. If your application can do its job using only a combination of JavaScript built-ins and server-friendly .NET APIs, ClearScript could be an option for you.

    Good luck!

    0 0

    Hello!

    This question is similar to your earlier one about catching .NET exceptions in JavaScript, and our answer is similar as well - consider catching the exception in JavaScript and allowing .NET code to inspect it. For example:
    engine.Execute(@"
        function tryCatch(func) {
            try {
                return { isSuccess: true, returnValue: func() };
            }
            catch (exception) {
                return { isSuccess: false, exception: exception };
            }
        }
    ");
    and then:
    dynamic result = engine.Evaluate(@"tryCatch(function() {
        throw { foo: 123, bar: 456 };
    })");
    
    if (!result.isSuccess)
    {
        Console.WriteLine(result.exception.foo);
        Console.WriteLine(result.exception.bar);
    }
    else
    {
        Console.WriteLine(result.returnValue);
    }
    As you can see, the host and the script engine can work together!

    Good luck!

    0 0

    Hi,

    Well, by reading your answer, the workaround looks obvious! :)
    Thanks for the help.

    Is there any plan to have more seamless exception handling between .NET and JS, even for V8 only? That would be definitely nice!

    Cheers

    0 0

    I understand, thanks

    0 0

    Hi,

    There is a problem with marshaling of decimal values. Following code does not work. Type mismatch error
    using(var scripter = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging))
    {
    
        decimal num1 = 3;
        decimal num2 = 2;
        scripter.Script.num1 = num1;
        scripter.Script.num2 = num2;
        Console.WriteLine(scripter.Evaluate("num1+num2"));
    
    }
    Regards,

    Igor

    0 0

    Hi,

    There is problem with is Nothing keyword in VBScript when object property is null. In my case
    MyClass.Class2 property is null
    public class MyClass
    {
        public MyClass2 Class2 { get; set; } 
    }
    
    public class MyClass2
    {
         
    }
    
    using(var scripter = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging))
    {
        scripter.Script.Class1 = new MyClass();
        scripter.Execute(@"
                function Test
                if Class1.Class2 is Nothing then 
                    Test = true
                else 
                    Test = false
                end if
                end function ");
        Console.WriteLine(scripter.Script.Test());  
    }
    Regards,

    Igor

    0 0

    Hello Igor,

    VBScript doesn't support System.Decimal, but because it's a valid COM automation type, it stores it correctly and can return it to the host without data loss.

    This is by design. If you'd like to perform operations with decimal values, you'll have to call a host method.

    Thanks!

    0 0

    Can you give me example?

older | 1 | .... | 25 | 26 | (Page 27) | 28 | 29 | .... | 115 | newer