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

New Post: Script timeout

$
0
0
Is allow to call Interrupt function from the timer thread or I did not understand correctly?

New Post: Calling VBScript function by dispatchId

$
0
0
Ok, I will to wait your implementation. I tried to implement that as in our legacy application, but that does not work as inspected.

New Post: Script timeout

$
0
0
Yes, ScriptEngine.Interrupt() can be called safely from any thread.

New Post: calling native dll

$
0
0
hi, how can i use native functions from user32.dll or something else in clearscript?

New Post: Handling CLR exceptions in JS

$
0
0
Hi,

I'm using the V8ScriptEngine and am wondering how to handle CLR exceptions.

Right now, the object I use with AddHostObject may throw some exceptions but I haven't found a way to properly handle them. Wrapping the call (in JS) with a try/catch block didn't really help as the CLR exception remains unhandled, and what I seem to catch in JS is a TargetInvocationException, not the exception actually thrown by the CLR object (which, I guess, is down the InnerException stack).

Is there some best practice here?
Thanks in advance,
Thomas

New Post: calling native dll

$
0
0
Hi furesoft,

You can expose a .NET type with a P/Invoke method and invoke it from script. For example:
publicstaticclass Native
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    publicstaticexternint MessageBox(IntPtr hWnd, string text, string caption, int options);
}
and then
engine.AddHostType("Native", typeof(Native));
engine.AddHostType("IntPtr", typeof(IntPtr));
engine.Execute("Native.MessageBox(IntPtr.Zero, 'Hello!', 'Greetings', 0)");
Good luck!

New Post: Handling CLR exceptions in JS

$
0
0
Hi Thomas,

ClearScript doesn't expose .NET exceptions through the script language's native exception handling mechanism. V8 actually might make this possible, but JScript and VBScript definitely do not.

Instead, we recommend that you expose a function that runs script code inside a .NET try/catch block and returns any caught exception to the script. For example:
engine.Script.tryCatch = new Func<dynamic, object>(func => {
    try {
        returnnew { returnValue = func() };
    }
    catch (Exception exception) {
        returnnew { exception = exception.GetBaseException() };
    }
});
Here's an example of how you'd use such a function:
engine.AddHostType("File", typeof(File));
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    result = tryCatch(function () {
        return File.ReadAllText('SomeFileName.txt');
    });
    if (result.exception)
        Console.WriteLine(result.exception);
    else
        Console.WriteLine(result.returnValue);
");
Hopefully this solution, or something similar, is practical in your scenario.

Cheers!

New Post: Calling VBScript function by dispatchId

$
0
0
Small question about debugging.
I created my lite variation of the script engine based on your code :
ActiveXDebugging.cs
ActiveXScripting.cs
ActiveXWrappers.cs
WindowsScriptEngine.Debug.cs
WindowsScriptEngine.Site.cs
WindowsScriptEngine.cs
It's work fine, but script debugging is not worked as expected, the host application is locked by debugger
Image
https://www.dropbox.com/s/7svagob2w71s6ga/script.jpg
Which code responsibility for breaking in debugger?

New Post: How to call function by name with arguments as array

$
0
0
How to call function by name with arguments as array.
Something like this _engine.Execute("sum", new []{ 2, 4});

New Post: How to Access a Web Service?

$
0
0
I'm trying to access a web service from my JavaScript code. I tried to use the AJAX capability of jQuery but ClearScript was not able to load the jQuery library. I got the error "defaultView is not a property of undefined". It seems that jQuery is expecting to be hosted in a web page. I'm using it to allow customization of a desktop application. Is there another way to access a web service that is compatible with ClearScript?

Thanks,
Neil

New Post: casting

$
0
0
hi, how can i cast types?
like these in c#: (string)helloworld;
or these: Convert.ChangeType(helloworld, typeof(string));

New Post: Calling VBScript function by dispatchId

$
0
0
Hi ifle,

From the screenshot it looks like you've successfully attached the script debugger to your application and can view the script documents. You should now be able to use the Visual Studio UI to set breakpoints. If you're using JavaScript, you can also use the debugger statement to break into the debugger.

Cheers!

New Post: How to call function by name with arguments as array

$
0
0
Hello ifle,

Currently ClearScript doesn't have an API method like that. We'll add one in the next release. In the meantime, you can add an extension method that does what you want:
publicstaticclass ScriptEngineExtensions
{
    publicstaticobject InvokeFunction(this ScriptEngine engine, string name, object[] args)
    {
        var host = new HostFunctions();
        ((IScriptableObject)host).OnExposedToScriptCode(engine);
        var del = (Delegate)host.func<object>(args.Length, engine.Script[name]);
        return del.DynamicInvoke(args);
    }
}
With this class in place, you can do this:
engine.InvokeFunction("foo", newobject[] { 1, 2, 3 });
Cheers!

New Post: Calling VBScript function by dispatchId

$
0
0
Script that I run (2+3/0) from my test console application throw the divide by zero exception, but VS did not catch it.

New Post: How to call function by name with arguments as array


New Post: How to Access a Web Service?

$
0
0
Hi Neil,

Since you have .NET at your disposal, why not use something like System.Net.WebClient?
engine.AddHostType("WebClient", typeof(WebClient));
engine.Execute(@"
    var client = new WebClient();
    var html = client.DownloadString('http://cnn.com');
");
Cheers!

New Post: Handling CLR exceptions in JS

$
0
0
Hi,

Thanks once again for your quick and thorough reply. I guess this method should be usable in my context.

I'd like to take this opportunity to thank you for the outstanding support you're giving to the community, it's rather rare to find this level of support in open-source projects!

Cheers
Thomas

New Post: Accessing SqlDataReader from ClearScript

$
0
0
Hi,

This does not work:
Imports System.Data.SqlClient
Imports Microsoft.ClearScript

Public Class WebForm1
    Inherits System.Web.UI.Page

    Public engine As Windows.JScriptEngine = New Windows.JScriptEngine

    Public conn As SqlConnection = New SqlConnection("connection string")
    Public cmd As SqlCommand = New SqlCommand("select * from contacts", conn)
    Public rdr As SqlDataReader

    Public Sub Initialize()
        engine.AddRestrictedHostObject("me", HostItemFlags.GlobalMembers, Me)

        Try
            engine.Execute("conn.Open(); rdr = cmd.ExecuteReader();")

        Catch ex As Exception
            Response.Write(ex)
        Finally
            engine.Dispose()
            rdr.Close()
            rdr.Dispose()
            cmd.Dispose()
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
End Class
I get this error:

Microsoft.ClearScript.ScriptEngineException: Ambiguous match found. ---> System.Reflection.AmbiguousMatchException: Ambiguous match found. at System.DefaultBinder.SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers) at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) at System.Type.GetProperty(String name, BindingFlags bindingAttr, Type returnType) at System.Attribute.GetParentDefinition(PropertyInfo property) at System.Attribute.InternalGetCustomAttributes(PropertyInfo element, Type type, Boolean inherit) at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit) at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit) at Microsoft.ClearScript.Util.MemberHelpers.GetAttribute[T](MemberInfo member, Boolean inherit) in ClearScript\Util\MemberHelpers.cs:line 136 at Microsoft.ClearScript.Util.MemberHelpers.GetScriptAccess(MemberInfo member) in ClearScript\Util\MemberHelpers.cs:line 124 at Microsoft.ClearScript.Util.MemberHelpers.IsBlockedFromScript(MemberInfo member) in ClearScript\Util\MemberHelpers.cs:line 97 at Microsoft.ClearScript.Util.MemberHelpers.IsScriptable(PropertyInfo property) in ClearScript\Util\MemberHelpers.cs:line 86 at Microsoft.ClearScript.Util.TypeHelpers.b__1a(PropertyInfo property) in ClearScript\Util\TypeHelpers.cs:line 300 at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at Microsoft.ClearScript.HostItem.GetLocalPropertyNames() in ClearScript\HostItem.cs:line 451 at Microsoft.ClearScript.HostItem.GetAllPropertyNames() in ClearScript\HostItem.cs:line 497 at Microsoft.ClearScript.HostItem.UpdatePropertyNames(Boolean& updated) in ClearScript\HostItem.cs:line 557 at Microsoft.ClearScript.HostItem.b__39() in ClearScript\HostItem.cs:line 1236 at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func1 func) in ClearScript\ScriptEngine.cs:line 784 at Microsoft.ClearScript.Windows.WindowsScriptEngine.HostInvoke[T](Func1 func) in ClearScript\Windows\WindowsScriptEngine.cs:line 573 --- End of inner exception stack trace --- at Microsoft.ClearScript.ScriptEngine.ThrowScriptError(IScriptEngineException scriptError) in ClearScript\ScriptEngine.cs:line 840 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ThrowScriptError(Exception exception) in ClearScript\Windows\WindowsScriptEngine.cs:line 642 at Microsoft.ClearScript.Windows.WindowsScriptEngine.<>c__DisplayClass14.b__13() in ClearScript\Windows\WindowsScriptEngine.cs:line 611 at Microsoft.ClearScript.ScriptEngine.ScriptInvoke(Action action) in ClearScript\ScriptEngine.cs:line 800 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ScriptInvoke(Action action) in ClearScript\Windows\WindowsScriptEngine.cs:line 603 at Microsoft.ClearScript.Windows.WindowsScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard) in ClearScript\Windows\WindowsScriptEngine.cs:line 523 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code) in ClearScript\ScriptEngine.cs:line 494 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code) in ClearScript\ScriptEngine.cs:line 473 at Microsoft.ClearScript.ScriptEngine.Execute(String code) in ClearScript\ScriptEngine.cs:line 454 at WebApplication1.WebForm1.Initialize() in WebApplication1\WebForm1.aspx.vb:line 17

New Post: Calling VBScript function by dispatchId

$
0
0
Is it possible that the error-generating script ran before you attached the debugger?

New Post: casting

$
0
0
Hi furesoft,

You can expose System.Convert to the script engine, or use HostFunctions.cast(). Keep in mind however that numbers are converted to native JavaScript or VBScript values when brought into the script engine. If you need to invoke a host method that requires a numeric argument of a specific .NET type, you may have to use a function such as HostFunctions.toInt16() to ensure proper method binding.

Good luck!
Viewing all 2297 articles
Browse latest View live