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

Reviewed: ClearScript 5.3 (Mar 07, 2014)

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

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

$
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

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

$
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!

New Post: Delegate

$
0
0
Hi,
How can i use a Delegate to bind a function with a event?

New Post: Parent-child script relation

$
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();
}

New Post: Delegate

$
0
0
Hi furesoft,

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

Good luck!

New Post: Parent-child script relation

$
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!

New Post: Parent-child script relation

$
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

New Post: Parent-child script relation

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

New Post: Parent-child script relation

$
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?

New Post: execute external javascript

$
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

New Post: Getting JS exception

$
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!

New Post: execute external javascript

$
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!

New Post: Getting JS exception

$
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!

New Post: Getting JS exception

$
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

New Post: Parent-child script relation

New Post: Decimal values problems

$
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

New Post: Problem with nullable property of object

$
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

New Post: Decimal values problems

$
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!

New Post: Decimal values problems

Viewing all 2297 articles
Browse latest View live




Latest Images