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

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: We're not seeing this, and as far as we can tell the host has no control over this behavior. Note however that VBScript appears to reset the global `On Error` state for each invocation of the engine, so if you're invoking the engine multiple times, you probably want to insert "On Error Resume Next" at the beginning of each script. Can you confirm that doing so resolves your issue?

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: This is a script containing a function that has an On Error Resume Next and calls into an office application. When an error occurs in this call, it crashes the application. An example is: On Error Resume Next Set xlSheet = ExcelApplication.Workbooks(strWorkbookName).Sheets(1) If the workbook doesn't exist, it crashes the application.

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: How are you exposing `ExcelApplication` for scripting?

New Post: invoking js callback with params argument

$
0
0
hi,

i want to invoke an js callback from c# with unknown arguments count

example:
public void Fire(string name, params object[]args)
        {
            if (_listeners.ContainsKey(name))
            {
                foreach (var l in _listeners[name])
                {
                    l(args);
                }
            }
        }
but the expected 2. argument in js is undefined

New Post: invoking js callback with params argument

$
0
0
Hello!

One way to do this is to create a general-purpose dynamic invocation helper:
// using System.Dynamic;// using System.Linq;// using Microsoft.CSharp.RuntimeBinder;publicstaticclass ObjectHelpers {
    privatestaticreadonly CSharpArgumentInfo argInfo = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
    publicstaticobject DynamicInvoke(object target, object[] args) {
        var del = target as Delegate;
        if (del != null)
            return del.DynamicInvoke(args);
        var dynamicObject = target as DynamicObject;
        if (dynamicObject != null) {
            object result;
            var binder = Binder.Invoke(CSharpBinderFlags.None, null, Enumerable.Repeat(argInfo, args.Length));
            if (dynamicObject.TryInvoke((InvokeBinder)binder, args, out result))
                return result;
        }
        thrownew InvalidOperationException("Invocation failed");
    }
}
Then, instead of l(args), use ObjectHelpers.DynamicInvoke(l, args).

Good luck!

New Post: invoking js callback with params argument

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: I am adding a global script object that contains a property reference to Excel: Public Property ExcelApplication() As Microsoft.Office.Interop.Excel.Application Get Return _excelApplication End Get Friend Set(value As Microsoft.Office.Interop.Excel.Application) _excelApplication = value End Set End Property

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: We've tried to replicate your setup but still cannot reproduce the problem. Here's our C# and VBScript test code: ``` C# var xlapp = new Microsoft.Office.Interop.Excel.Application(); engine.AddHostObject("api", HostItemFlags.GlobalMembers, new { ExcelApplication = xlapp }); engine.Execute(@" Function GetSheetName(workbook, sheet) On Error Resume Next GetSheetName = ExcelApplication.Workbooks(workbook).Sheets(sheet).Name End Function "); engine.Execute(@" ExcelApplication.Workbooks.Add(""hello.xlsx"") : Rem valid file path name1 = GetSheetName(1, 1) : Rem succeeds; name1 is 'Sheet1' name2 = GetSheetName(2, 1) : Rem fails but does NOT crash; name2 is empty "); ``` Is there something you're doing differently? What exception are you seeing in your application code?

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: It appears this problem only occurs when running in Debug mode. If I compile the application, it works fine.

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: That's great to hear. Please let us know if you collect any additional information such as the exception you're seeing in your application code.

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: Since we are able to continue past this error when compiled, I need to finish working on the project. Hopefully, by next week, I'll be able to put together a small example that illustrates what happens and post that for you.

New Post: V8 Iterators

$
0
0
It would be great if .NET objects exposed to V8 could provide ECMA Script 6 Iterator functionality.

In order to achieve this, they would have to have a Symbol.iterator property when seen from V8.

I would like to patch ClearScript in order to achieve this. Approach: Introduce some attribute (e.g. V8Iterator) to indicate which method of the .NET class should become the Symbol.iterator property. When the V8 object is built, the property is added.

Now I looked at the ClearScript sources, but I had to realize that I do not understand the mechanism how the V8 object is built.

Where exactly is the reflection on the .NET object done?

New Post: V8 Iterators

$
0
0
Hello JohnGeeB,

Patching ClearScript may not be necessary. Consider this code:
engine.Script.host = new HostFunctions();
engine.AddHostType(typeof(IEnumerable));
engine.Execute(@"
    host.constructor.prototype[Symbol.iterator] = function () {
        var enumerable = host.asType(IEnumerable, this);
        if (host.isNull(enumerable))
            throw new TypeError('the host object is not enumerable');
        var enumerator = enumerable.GetEnumerator();
        return {
            next: function () {
                if (enumerator.MoveNext())
                    return { done: false, value: enumerator.Current };
                return { done: true };
            }
        };
    }
");
With this setup, all exposed .NET objects that implement IEnumerable support the ES6 iteration protocols.

Good luck!

New Post: V8 Iterators

$
0
0
Amazing. Works perfectly. Thank you.

May I ask another -- related -- question? The goal is Linq-style iterating, e.g. implementing a forEach with a callback parameter.

Here http://www.csharpcity.com/2013/from-c-to-javascript-and-back-again-via-clearscript/ it is explained how to pass a JS callback to a .NET function requiring an Action parameter (some code added by myself and tested, it works):
C#:

public class MyNetCollection {
   public void forEach (Action<object> a) { ...}
}
...
engine.AddHostType("Action", typeof(Action<object>));
...

JS:

// we want to sum up all values in myNetCollection
var s = 0;
myNetCollection.forEach ( new Action( function(v) {
   s += v; // &#43; should be a plus
} ));
Now that wrapping of the callback function into the new Action ( ) constructor is inconvenient. Is there some clean way of doing that on the .NET side? I.e. such that forEach accepts a parameter of type object and does the wrapping into the Action itself? I think I could abuse some method of the HostFunctions class and create a delegate, yet the methods of the class do not seem to be intended to be used from outside a script? Is there a cleaner way? Assumption: myNetCollection, of course, knows which script engine it belongs to.

New Post: global object - types

$
0
0
hello,

i know you can add a global object like in browser - window

so is it possible to add types to this object?

so something like -

globals.XmlHttpRequest

New Post: Script editor

$
0
0
you can use in winforms digitalrune texteditor and for wpf avalonedit

these are the best editor controls,

ive started an ide with digitalrune.texteditor

and it will have js support

New Post: XMLHttpRequest, window and other Javascript objects

New Post: V8 Iterators

$
0
0
Hi again,

Amazing. Works perfectly.

Well, not quite :) It doesn't support strongly-typed enumeration via IEnumerable<T>. We'll add more robust support for ES6 iteration in a future update.

Now that wrapping of the callback function into the new Action() constructor is inconvenient. Is there some clean way of doing that on the .NET side? I.e. such that forEach accepts a parameter of type object and does the wrapping into the Action itself?

Sure. Here's a simple extension class that implements a script-friendly forEach method for .NET enumerables:
publicstaticclass ScriptEnumerable {
    publicstaticvoid forEach<T>(this IEnumerable<T> source, object action) {
        source.ToList().ForEach(item => ((dynamic)action)(item));
    }
}
And here's some test code that demonstrates its usage:
engine.Script.testDictionary = new Dictionary<string, object> {
    { "foo", 123 },
    { "bar", 456.789 },
    { "baz", "hello" },
    { "qux", DayOfWeek.Wednesday }
};
engine.AddHostType(typeof(ScriptEnumerable));
engine.AddHostType(typeof(Console));
engine.Execute(@"
    testDictionary.forEach(function(item) {
        Console.WriteLine(item);
    })
");
Cheers!

New Post: global object - types

$
0
0
Hi furesoft,

You can use ClearScript's ExtendedHostFunctions for this purpose. Here's a sample:
publicclass XmlHttpRequest {
    publicstaticvoid SomeMethod() {
        // code goes here
    }
}
publicclass Globals {
    privatereadonly ExtendedHostFunctions _extHost = new ExtendedHostFunctions();
    publicobject XmlHttpRequest {
        get { return _extHost.type(typeof(XmlHttpRequest)); }
    }
}
And then:
engine.Script.globals = new Globals();
engine.Execute("globals.XmlHttpRequest.SomeMethod()");
Good luck!

Commented Unassigned: On Error Resume Next in VBScript [97]

$
0
0
I have previously used the MSScriptControl COM object and have switched to ClearScript. The problem I am seeing, currently, is that whatever implementation ClearScript uses doesn't honor the On Error Resume Next and simply crashes out, throwing an error.

Is there a way to have ClearScript honor this error handling or something I can do to prevent it from crashing?
Comments: Closing this for now. Feel free to post any additional information; we'll be happy to take another look.
Viewing all 2297 articles
Browse latest View live




Latest Images