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 | .... | 42 | 43 | (Page 44) | 45 | 46 | .... | 115 | newer

    0 0

    Hi JC,

    ClearScript converts strings automatically, so if the script function returns a string, the host should see it as a .NET string, and casting via CStr() or CType() should work. It appears instead that your return value is a script object. Can you check (or share) the getChunkJSON() source code?

    Regarding, your second question, please see here. Edit: In VB.NET, the extension class might look something like this:
    Imports System.Dynamic
    Imports Microsoft.ClearScript
    Module ScriptEngineExtensions
        <System.Runtime.CompilerServices.Extension>
        PublicFunction InvokeFunction(engine As ScriptEngine, name AsString, args() AsObject)
            Dim host AsNew HostFunctions
            CType(host, IScriptableObject).OnExposedToScriptCode(engine)
            Dim func = host.getProperty(CType(engine.Script, IDynamicMetaObjectProvider), name)
            Dim del = CType(host.func(Of Object)(args.Length, func), System.Delegate)
            InvokeFunction = del.DynamicInvoke(args)
        EndFunctionEndModule
    Cheers!

    0 0

    Hello again!

    Here is what my getChunkJSON function looks like...
    function getChunkJSON(num) {
        return JSON.stringify(Chunks[num]);
    }
    That code simply returns a string value. That is not my exact code, but that has the same result. If I try to change my VB code to something a little simpler like,
    s1 = LUA.JSEngine.Script.getChunkJSON(chunkNum)

    Where s1 is defined as a string, I get a "Conversion from type 'V8ScriptItem' to type 'String' is not valid." cast exception error.


    As for the second question, that block of code looks like it will work great, thanks so much!

    -JC

    0 0

    Hi JC,

    That code simply returns a string value. That is not my exact code, but that has the same result.

    Hmm, is it possible that getChunkJSON() actually returns a string object rather than string - that is, does it do something like this:
    function getChunkJSON(num) {
        returnnew String(JSON.stringify(Chunks[num]));
    }
    Thanks!

    0 0

    Yeah, that is more than likely because the final step is a string compressor with a ton of String class functions. Is there an easy way to read the string object as a string without using the .toString() function in JS itself?

    0 0

    my code:
    engine.AddHostObject("$", new Action<dynamic>(o => new Window(f).AddEventHandler("Load", o)));
                engine.AddHostObject("$", new Action<string, dynamic>((n, o) => new Window(f).AddEventHandler(n, o)));

    0 0

    Hi JC,

    Is there an easy way to read the string object as a string without using the .toString() function in JS itself?

    Sure, you can call toString() or valueOf() from the host:
    s = engine.getChunkJSON(num).valueOf()
    Of course, this will break if getChunkJSON() changes and starts returning normal strings.

    In general we don't recommend explicit usage of JavaScript's String, Number, or Boolean objects, as their behavior can be unexpected:
    if (new String('foo') == new String('foo')) {
        // code here WILL NOT be executed
    }
    if (new Boolean(false)) {
        // code here WILL be executed
    }
    Good luck!

    0 0

    Hi furesoft,

    Can you give us more info? Are you seeing a compilation error? Runtime exception? Is there a stack, line number, etc.?

    Thanks!

    0 0

    Alright thanks, and hopefully my last question... I am using the Function you provided above for calling methods by name and arguments..

    Every time it tries to actually invoke the delegate it errors on the function below.
    public TResult InvokeTarget()
                {
                    return Invoke(() => (TResult)target());
                }
    With a cannot invoke a non-delegate type error.

    I know my function exists and is returning a ScriptItem but I have no idea what is going on in the interim.

    THanks again for all of your help,
    -JC

    0 0
  • 08/05/14--09:40: New Post: Performance...
  • Hi,

    I have also seen that the JScript version of ClearScript works quite well in my environment as well.
    Better for micro benchmarks over v8 and jint.

    I am considering moving to this over my old engine.
    Is this version of JScripting fully managed? And what ECMA level does it adhere to?

    Thanks,

    Jules

    0 0

    my js:
    $(function(sender, e) {
                alert("hello to my EFML-Application :D");
            });
            $("FormClosing", function(sender, e) {
                alert("Application is closing");
            });
    no line number, no compilation error. COMException is throwed

    0 0

    Hi furesoft,

    JavaScript doesn't support property overloading. Your host code sets script property "$" to a delegate that takes one argument, then overwrites it with a delegate that takes two arguments. Then your script code invokes the property and passes a single argument, and that fails because two are required.

    Cheers!

    0 0

    i dont understand what you mean, give me a code example, please

    0 0

    1. Your first call to AddHostObject() creates a property named "$" whose value is a delegate with one parameter.
    2. Your second call to AddHostObject() overwrites the property created in step 1. The new value is a delegate with two parameters.
    3. The first line of your script invokes the delegate assigned in step 2, but passes only one argument.

    0 0

    when i change the order of addhostobject i get this exception: One can not be called non-delegate type.

    0 0

    Hi JC,

    It sounds like the named property you're invoking isn't a function :)

    The following test program demonstrates the extension module above:
    Module TestModule
        Sub Main()
            Using engine AsNew V8ScriptEngine
                engine.AddHostType("Console", GetType(System.Console))
                engine.Execute("function foo(arg) { Console.WriteLine('Foo: {0}', arg); }")
                engine.InvokeFunction("foo", {123.456})
            EndUsingEndSubEndModule
    Is there anything you're doing differently?

    Thanks!

    0 0

    That makes sense. With the order reversed, the "$" property ends up being set to a one-parameter delegate. The second call in your script invokes it with two arguments, which is OK, but the first argument is a string that's assumed to be a script function. When the event is raised, an attempt is made to invoke the string, which understandably fails.

    The bottom line is that a JavaScript property can't have two values. However, you can create a single delegate that supports both calling patterns:
    publicdelegatevoid DollarSignFunc(object arg1, object arg2 = null);
    ...
    engine.AddHostObject("$", new DollarSignFunc((arg1, arg2) => {
        var name = arg1 asstring;
        if (name == null)
            new Window(f).AddEventHandler("Load", arg1);
        elsenew Window(f).AddEventHandler(name, arg2);
    }));
    Good luck!

    0 0

    No. Here is my exact code with your test above. (Little messier for testing purposes.)
    Public Function InvokeJSFunction(name As String, args() As Object)
            Dim host As New HostFunctions
            JSEngine.AddHostType("Console", GetType(System.Console))
            JSEngine.Execute("function foo(arg) { Console.WriteLine('Foo: {0}', arg); }")
            name = "foo"
            args = {123.456}
            CType(host, IScriptableObject).OnExposedToScriptCode(JSEngine)
            Dim func = host.getProperty(CType(JSEngine.Script, IDynamicMetaObjectProvider), name)
            Dim del = CType(host.func(Of Object)(args.Length, func), System.Delegate)
            InvokeJSFunction = del.DynamicInvoke(args)
        End Function
    It fails at the same part, granted I am sending arguments this time.
    public TResult InvokeTarget(T1 a1)
                {
                    return Invoke(() => (TResult)target(a1));
                }
    Any ideas?

    -JC

    0 0

    Are the exceptions you're seeing unhandled? The C# dynamic infrastructure throws and handles exceptions internally. Please test outside the debugger, or just continue when you hit first-chance exceptions of type RuntimeBinderException.

    0 0
  • 08/05/14--22:44: New Post: Performance...
  • Jules, the JScript is "unmanaged" in terms of that it is part of the "Active Scripting" technologies of Microsoft which includes VBScript and JScript.

    See this Wikipedia article for the ECMA version compatibility of the different JScript versions.

    0 0

    how can i use a third version?

    $("elementname")

older | 1 | .... | 42 | 43 | (Page 44) | 45 | 46 | .... | 115 | newer