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

New Post: Handling CLR exceptions in JS

$
0
0
Thomas,

Your positive feedback is greatly appreciated!

By the way, we're adding a more general version of the tryCatch method above to the ClearScript API.

Thanks for your input, and please let us know if you run into any other issues.

New Post: Calling VBScript function by dispatchId

$
0
0
No, debugger attached before error-generating script.

New Post: Accessing SqlDataReader from ClearScript

$
0
0
Greetings!

Unfortunately it looks like SqlDataReader currently cannot be used directly from script code. The root cause is this .NET bug.

We'll provide a workaround in the next ClearScript release. In the meantime, all we can recommend is that you avoid dealing with SqlDataReader in script code. Consider exposing an alternate API that encapsulates SqlDataReader within another .NET class.

Thanks for reporting this issue!

New Post: Accessing SqlDataReader from ClearScript

$
0
0
Hey,

Well I had to take the longer route of using a SqlDataAdapter and DataSet and it worked but would have loved to use SqlDataReader... Hope your team can fix this soon for us... Thanks for the support!

New Post: method 'is inaccessible due to its protection level'

$
0
0
Hi,

In the following short test when the script tries to access the method I get a ScriptEngineException:
"'ClearScriptAccessTest.TestClass.TestMethod()' is inaccessible due to its protection level"
However, accessing the property succeeds.
    class TestClass
    {
        public int TestProperty { get; set; }

        public void TestMethod()
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var engine = new V8ScriptEngine())
            {
                try
                {
                    TestClass testClass = new TestClass();
                    engine.AddHostObject("testClass", testClass);
                    engine.Execute(
                        @"
                    testClass.TestProperty = 17;
                    testClass.TestMethod();
"
                        );

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: '{0}'", ex.Message);
                }
            }

        }
    }
The obvious workaround is to change
class TestClass
to
public class TestClass
but in real life this requires changing many other classes.

I also tried adding
[assembly: InternalsVisibleTo("ClearScript")]
[assembly: InternalsVisibleTo("ClearScriptV8-64")]
[assembly: InternalsVisibleTo("ClearScriptV8-32")]
but that also didn't work.

Is there a reason why the property is accessible and the method is not?


Thanks a lot,
Ron

New Post: method 'is inaccessible due to its protection level'

$
0
0
Hi Ron,

Is there a reason why the property is accessible and the method is not?

Yes, it has to do with the way ClearScript binds to .NET type members. For most things it uses reflection, but for methods it uses the C# runtime binder. These facilities have different rules for accessing non-public resources.

Try using ScriptEngine.AccessContext. For example:
engine.AccessContext = typeof(Program);
From the ClearScript Library Reference: "By setting this property to a type you declare that script code running in the current script engine is to be treated as if it were part of that type's implementation. Doing so does not expose any host resources to script code, but it affects which host resources are importable and which members of exposed resources are accessible."

Hopefully this solution is practical in your actual scenario.

Good luck!

New Post: object does not contains method or property

$
0
0
hi,
i have this js code:
function WebClient() {

    DownloadString: function (path, callback){
        var wc = new System.Net.WebClient();
            
        var result = wc.DownloadString(path);
        if (typeof(callback) != "undefined")
        {
                callback(result);
        }
        return result;
    };

}
and when i use this class:
var w = new WebClient();
w.DownloadString("http://www.google.de/");
come error: object does not contains method or property
but all is correct?

New Post: object does not contains method or property

$
0
0
Hi furesoft,

You probably intended to define your function like this:
function WebClient() {
    this.DownloadString = function (path, callback) {
        var wc = new System.Net.WebClient();
        var result = wc.DownloadString(path);
        if (typeof(callback) != 'undefined') {
            callback(result);
        }
        return result;
    };
}
Cheers!

New Post: method 'is inaccessible due to its protection level'

$
0
0
Thanks for an immediate perfect answer, as always!

Ron

New Post: Some things don't work in JScript Engine

$
0
0
Hi,

I don't know if I am doing something wrong or its a bug but just thought I will post if for your information. Following works in VBScript engine but not in JScript engine.
Imports System.Xml
Imports Microsoft.ClearScript

Public Class WebForm1
    Inherits System.Web.UI.Page

    Public vbs As Windows.VBScriptEngine = New Windows.VBScriptEngine
    Public js As Windows.JScriptEngine = New Windows.JScriptEngine

    Public xml As XmlDocument = New XmlDocument
    Public search As XmlNodeList, this As XmlNode

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

        xml.LoadXml("<sales><row><product>watch</product><rate>100</rate></row><row><product>laptop</product><rate>600</rate></row></sales>")

        Try
            search = xml.SelectNodes("//product")
            For Each Me.this In search
                js.Execute("Response.Write(this.InnerText);")
                Response.Write("<BR>")
                vbs.Execute("response.write this.innertext")
                Response.Write("<BR>")
            Next

        Catch ex As Exception
            Response.Write(ex)
        End Try

    End Sub

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

    End Sub
End Class
The output is as:
[undefined]
watch
[undefined]
thermos

New Post: for each item in collection loop does not work for datatable

$
0
0
Hi.

The code:
Imports System.Data.SqlClient
Imports Microsoft.ClearScript

Public Class WebForm1
    Inherits System.Web.UI.Page

    Public vbs As Windows.VBScriptEngine = New Windows.VBScriptEngine

    Public conn As SqlConnection = New SqlConnection("connection string")
    Public da As SqlDataAdapter = New SqlDataAdapter("select top 10 * from table", conn), dt As DataTable = New DataTable

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

        Try
            conn.Open()

            vbs.Execute("da.Fill dt")
            vbs.Execute("for i = 0 to dt.Rows.Count - 1:for j = 0 to dt.rows.item(i).table.columns.count - 1:response.write dt.rows.item(i).item(j) & "" - "":next:response.write ""<br>"":next")

            ' ------------------------------------------------------------
        ' THIS DOES NOT WORK
        vbs.Execute("for each row in dt.rows: for each col in row.table.columns: response.write row(col.columnname).tostring: next: next")
            ' ------------------------------------------------------------

        Catch ex As Exception
            Response.Write(ex)
        Finally
            da.Dispose()
            dt.Clear()
            dt.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
When I use for i = 0 to object.count - 1 it works, but the second line where I use for each loop it gives me this error:

Microsoft.ClearScript.ScriptEngineException: Object not a collection at Microsoft.ClearScript.ScriptEngine.ThrowScriptError(IScriptEngineException scriptError) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 840 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ThrowScriptError(Exception exception) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 642 at Microsoft.ClearScript.Windows.WindowsScriptEngine.<>c__DisplayClass14.b__13() in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 611 at Microsoft.ClearScript.ScriptEngine.ScriptInvoke(Action action) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 800 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ScriptInvoke(Action action) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 603 at Microsoft.ClearScript.Windows.WindowsScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 523 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 494 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 473 at Microsoft.ClearScript.ScriptEngine.Execute(String code) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 454 at WebApplication1.WebForm1.Initialize() in C:\Users\Shripal\Documents\Visual Studio 2012\Projects\WebApplication1\WebApplication1\WebForm1.aspx.vb:line 37

New Post: Some things don't work in JScript Engine

$
0
0
Hello shripaldalal,

Could you try renaming "this"? It's a reserved word in JavaScript.

Thanks!

New Post: Some things don't work in JScript Engine

$
0
0
Worked. Sorry for disturbing you over something so trivial! Thanks for your help!

New Post: Calling VBScript function by dispatchId

$
0
0
The fact that the debugger attaches successfully and displays your script documents indicates that you have things mostly working. However, because you're using a modified version of ClearScript, we have no way to reproduce the issue and can't be of much help.

New Post: Calling VBScript function by dispatchId

$
0
0
I understand. Thanks .
Is there any reason always to create a debug document in WindowsScriptEngine::Parse function? It should not be dependent to EnableDebugging flag?

New Post: for each item in collection loop does not work for datatable

$
0
0
Hi shripaldalal,

VBScript's for each statement only works with native VBScript arrays and specially crafted COM objects. ClearScript exposes .NET objects by encasing them in COM-aware wrappers, but it doesn't emulate VBScript collections. That would be an interesting feature to add!

Thanks!

New Post: for each item in collection loop does not work for datatable

$
0
0
Hi,

Can this be done in JScript or V8 ???

Also I declared a DataRow object and DataColumn object and called it through VBScript still I get an error.

Please advise.

New Post: for each item in collection loop does not work for datatable

$
0
0
I believe Microsoft should make a real scripting solution without COM Interop and Reflection or CodeDom it would be a sell out.

New Post: Calling VBScript function by dispatchId

$
0
0
It isn't required, but having the host manage these documents gives it a bit more control over how they're presented in the debugger.

New Post: for each item in collection loop does not work for datatable

$
0
0
Hi shripaldalal,

Ok I tried it in JScript and it does not work there too.

Sorry, what doesn't work? You said above that you could iterate through the data by using the Count and Item properties. The same technique should work with JScript and V8. The only thing that doesn't work is VBScript's for each statement, and that's because VBScript doesn't recognize those objects as collections.

What are you trying to do that yields "Invalid field assignment"?

The MS Script Control was so powerful we could do so many things with it.

Other than the bug you found that affects SqlDataReader (thanks again for that, BTW!), what are you having trouble with?
Viewing all 2297 articles
Browse latest View live




Latest Images