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

New Post: Optional arguments

$
0
0
Currently I'm in the process of migrating my own VBScript ScriptingHost integration to ClearScript.

In my implementation the following script-exposed C# code run successfully:
public string Foo(
    string a,
    [Optional]object argument0,
    [Optional]object argument1)
{
    // ...
}
This could be called from VBScript with either one, two or three parameters. The reason is the "Optional" attribute which are attached to the second and third parameter.

When using it with ClearScript and the VBScript engine, the calling of the method results in and 0x80020101 error.

My questions:

Is it possible to use optional parameters with ClearScript? If yes, how would I do it?

New Post: Optional arguments

$
0
0
OMG, this is so simple. Shame on me!

When writing this one:
public string Foo(
    string a,
    object argument0 = null,
    object argument1 = null)
{
    // ...
}
It runs like a charm!

I.e. simply using the standard C# way of declaring optional parameters is totally sufficient.

(Instead of "null" I also could have used "Missing.Value" to keep my inner logic the same as in my VBScript engine)

New Post: Optional arguments

$
0
0
We're glad you found a solution! Thanks for posting it!

New Post: V8Update issue

$
0
0
Hello,
Here is the error message I get when running V8Update.cmd.

Build mode: Release
V8 revision: Tested (r18635, Version 3.24.17)
Creating build directory ...
Downloading V8 ...
Patching V8 ...
svn: invalid option: --ignore-whitespace
Type 'svn help' for usage.
*** THE PREVIOUS STEP FAILED ***

Any idea ?

Thanks,

Olivier

New Post: V8Update issue

$
0
0
Hi Olivier,

Hmm, what version of Subversion are you using? The --ignore-whitespace flag is documented here. You can download an up-to-date Subversion client binary package here.

Good luck!

New Post: XML Doc comments in JS

$
0
0
Hi,

I'm using ClearScript to provide extension points in one of our applications. As part of our build process, we're using Doxygen to generate API documentation for internal use. I'm having some trouble getting links in the documentation for the javascript extensions back to some of our .Net types to reference correctly.

Here's an example:

JS:
function DoSomething(utilities){
    ///<param name="utilities">An instance of foo.JSUtilities</param>
    ///<seealso cref="foo.JSUtilities" />
    // do stuff
}
C#:
namespace foo {
    public class JSUtilities {
    }
}
Is there some trick that I'm not getting to allow the JS xml docs to reference .Net types? Links within JS or within C# functions and types are fine, it only appears to be an issue where I'd like to reference C# types and functions from the JS xml comments.

New Post: XML Doc comments in JS

$
0
0
Greetings!

If we understand correctly, you have a set of JavaScript and C# source files for which you'd like to build a single, merged set of documentation. If that's correct, your question would appear to be about Doxygen rather than ClearScript, unless the latter is somehow involved in the documentation building process.

Please let us know if we misunderstood your question. Thanks!

Created Unassigned: Adding host primitive types [49]

$
0
0
Dear Sir or Madam,
I need to be able to pass in primitive types.

I cannot use AddHostObject(). Calling AddHostObject() for boxed primitive types (double, int, etc.) raises exception "Invalid host item".

I cannot use AddRestrictedHostObject(). Although exception is not raised when using AddRestrictedHostObject(), however, the resulted value cannot be used within the script: any arithmetic operation results in NaN.

A workaround that I currently employed is to wrap the primitive type into a class or Array. I used Array successfully. However this is less convenient as a script writer would have to be aware of this. I wonder if it is possible to pass primitive types directly. Note that I cannot use 'engine.Script.p = 2.1' construct as parameters are added at run time and are not known during build.

I would perhaps have expected something like AddHostValue(), but could find any.

Sample script:
function foo()
{
string s;

try
{
double d = 2.1;
object paramValue = (object)d;
ScriptEngine engine = new JScriptEngine();
// exception is rasied:
engine.AddHostObject("p", paramValue);
// same exception is rasied: engine.AddHostObject("p", d);
// cannot do this: engine.Script.p = 2.1;
}
catch (Exception ex)
{
s = ex.Message;
}
}

Please help,
Yours faithfully

MW

Commented Unassigned: Adding host primitive types [49]

$
0
0
Dear Sir or Madam,
I need to be able to pass in primitive types.

I cannot use AddHostObject(). Calling AddHostObject() for boxed primitive types (double, int, etc.) raises exception "Invalid host item".

I cannot use AddRestrictedHostObject(). Although exception is not raised when using AddRestrictedHostObject(), however, the resulted value cannot be used within the script: any arithmetic operation results in NaN.

A workaround that I currently employed is to wrap the primitive type into a class or Array. I used Array successfully. However this is less convenient as a script writer would have to be aware of this. I wonder if it is possible to pass primitive types directly. Note that I cannot use 'engine.Script.p = 2.1' construct as parameters are added at run time and are not known during build.

I would perhaps have expected something like AddHostValue(), but could find any.

Sample script:
function foo()
{
string s;

try
{
double d = 2.1;
object paramValue = (object)d;
ScriptEngine engine = new JScriptEngine();
// exception is rasied:
engine.AddHostObject("p", paramValue);
// same exception is rasied: engine.AddHostObject("p", d);
// cannot do this: engine.Script.p = 2.1;
}
catch (Exception ex)
{
s = ex.Message;
}
}

Please help,
Yours faithfully

MW
Comments: Hello MW, Could you please try: ``` C# engine.Script["p"] = 2.1; ``` Please let us know if this works for you. Thanks!

New Post: Cannot load V8-ia32.dll on Pentium III machine

$
0
0
Hi,
I am using Clearscript dll in my application and when I try to load it on the Pentium 3 machine, and I get the following error.
Cannot load v8-ia32.dll -> it fails at loadAssembly() method.

I have installed C++ 2010/2012/2013 distributables on my P3 machine but still I get the same error.

Dependency walker was giving error for mpr.dll which I replaced with the latest version and the problem was resolved, but my application crashed at the same point.

Does anyone have any idea on why is it happening?

One more observation: Even Chrome browser is not supported on PIII machine, does that hints something?

My Machine details;
Processor - Intel Pentium 3
RAM - 512MB
OS - Windows XP SP3
HardDisk - 60GB

Thanks & Regards,
Anand Shanbhag

New Post: Cannot load V8-ia32.dll on Pentium III machine

$
0
0
Hi Anand,

ClearScript's C++ project files use default code generation flags, which target CPUs that support SSE2 instructions (Pentium 4 and up). You could try to create a private build that overrides that setting.

Also note that V8 itself has dropped support for pre-SSE2 processors as of Version 3.27.

Good luck!

New Post: Possible to add a global function?

$
0
0
Knowing that I can add global instances of host objects to the script engine like this:
engine.AddHostObject("random", new Random());
engine.Execute("Console.WriteLine(random.NextDouble())");
I wonder whether it would be possible to directly introduce global functions to the script engine so that I could directly execute this function in my script without specifying an object.

E.g. something like:
engine.AddHostFunction("add", delegate(int a, int b) { return a + b });
engine.Execute("Console.WriteLine(add(1,2))");
Does this sound reasonable? Is this possible?

New Post: Possible to add a global function?

$
0
0
Hello UweKeim,

Yes, this is possible. Simply add an object of a delegate type:
engine.AddHostObject("add", new Func<int, int, int>(delegate(int a, int b) { return a + b; }));
You can also use a lambda expression to accomplish the same thing:
engine.AddHostObject("add", new Func<int, int, int>((a, b) => a + b));
Good luck!

New Post: Possible to add a global function?

$
0
0
Awesome! You seem to have thought right about everything.

New Post: Weird Parameter Passing Behavior

$
0
0
Hello!

I am just getting started with ClearScript but I am finding some weird behavior when it comes to passing .Net objects (strings, integers) into scripts and then using them to return new values.

To be more clear when I load the engine my script is executed and it runs a few functions and fills up an array I am calling Chunks[].

Later on, I want to access the Chunks[] array in a few function, here is a slimmed down, modified version of it.
function getChunk(num) {
    return Chunk[num];
}
That section of code returns fine if I call it like this:
 j = engine.Evaluate("getChunk(" & chunkNum & ")")
Please note that chunkNum is a long value which is equal to 1 in my tests.

If I call it like this, however, it returns as undefined.
j = engine.Script.getChunk(chunkNum)
To conclude, the above code works if I alter the javascript function to assign num to another value (n for example) and then return the chunk based on the index of n.
function getChunk(num) {
        n = num;
    return Chunk[n];
}
This makes absolutely no sense to me. It "works" but some explanation of how the different ways to pass parameters work would be awesome.

Thanks for all the work on this library!
JC

New Post: Weird Parameter Passing Behavior

$
0
0
Hi JC,

We've replicated the first issue, and it appears to be a bug in ClearScript. To be honest, we haven't done much testing with VB.NET hosts. In most cases the host language is irrelevant, but dynamic binding through the Script property involves quite a bit of language-specific machinery. We're investigating.

As for your second question, can you provide a code sample that demonstrates the issue?

Thanks!

New Post: Weird Parameter Passing Behavior

$
0
0
Thanks so much for looking into this.

Here is a short code sample to elaborate on my second question.
Microsoft.ClearScript.V8.V8ScriptEngine se = new Microsoft.ClearScript.V8.V8ScriptEngine();
            //This line fails.
            var j = se.Evaluate("var d = 'this is d'; return d;");
            
            //Along with these.
            //se.Execute("var d = 'this is d';");
            //var j2 = se.Evaluate("return d;");

            //But these two work....
            //se.Execute("var d = 'this is d';");
            //var j1 = se.Script.d;
I understand the complications that VB introduces, especially with dynamic objects. Translating my project to C# is always in option - this library is worth the work - it would take a few days, though, so it is something I would like to refrain from if possible. Please let me know how it all works out, if there are any other code samples I can provide, or anything I can do to help!

-JC

New Post: Weird Parameter Passing Behavior

$
0
0
Hi again,

It looks like the failures in the code samples above are caused by the use of JavaScript return statements outside of a function:
engine.Evaluate("return d;"); // error
engine.Evaluate("d"); // OK
Also, JavaScript statements such as var don't have values, but expressions do, so if you'd like your script to return a value, make sure it ends with an expression:
engine.Evaluate("var d = 123"); // returns no value (undefined)
engine.Evaluate("var d = 123; d"); // returns 123
By the way, we'll post a fix for the VB.NET issue very soon.

Cheers!

New Post: Performance...

$
0
0
Hi - First off: great job on this ClearScript!

I have a question about performance when invoking many small script blocks in a web application - short explanation:
  • I have a proprietary language that utilizes JavaScript as it's scripting language. It is an interpreted language written in .net (so its an interpreted, interpreted language!).
  • It is a web based application and a 'scripting' context is created per request.
  • At run-time, all conditions/logic etc are all evaluated with calls to the scripting engine.
  • My current scripting engine is an older Rhino port (https://code.google.com/p/ecmascript-net/) that is all managed (good performance) but not sure how well maintained/documented.
I was reading that with ClearScriptV8, as it is high performing with no thread affinity - this would be a great replacement!
I have implemented a ClearScriptV8 version and tested with performance about 10x slower than what I have now.
The implementation is very simple - essentially a new ClearScriptV8Scripting instance is created per request, and during that call many requests to evaluateScript are called.
    public class ClearScriptV8Scripting : IScriptEngine
    {
        private V8ScriptEngine engine;

        public ClearScriptV8Scripting()
        {
            engine = new V8ScriptEngine();
        }

        public String evaluateScript(String script)
        {
            Object result = engine.Evaluate(script);
            return (result == null || result is Microsoft.ClearScript.Undefined) ? string.Empty : result.ToString();
        }

        public String callFunction(String function, String[] parameters)
        {
            Object result = engine.InvokeFunction(function, parameters);
            return result == null ? string.Empty : result.ToString();
        }

        public String[] convertToArray(String strArr) 
        {
            dynamic arr = engine.Evaluate(strArr);
            String[] result = new String[arr.length];

            for (int i = 0; i < arr.length; i++)
            {
                result[i] = arr[i].ToString();
            }
            return result;
        }
    }

//extension for calling a function directly from c#
 public static class ClearScriptEngineExtensions
    {
        public static object InvokeFunction(this Microsoft.ClearScript.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);
        }
    }
I'm just curious if anyone has any ideas on how I can improve performance my performance ?

Thanks,

Jules

New Post: Performance...

$
0
0
Hi Jules,

Thanks for giving ClearScript a try. A few questions:

  1. How are you measuring performance? For example, are you looking at the average time to process a request, average time within evaluateScript(), etc.?
  2. What do the scripts you're running look like, in general? How large are they? Are they static, or generated on the spot? How many different ones are there?
  3. Do your scripts access host objects and/or types?
  4. Does your application process multiple requests simultaneously? If so, what is your concurrency model?
  5. The ClearScriptV8Scripting class above seems to provide no facility for disposing its internal V8ScriptEngine instance. Is that something you left out, or are you actually not disposing those instances?
Thanks!
Viewing all 2297 articles
Browse latest View live




Latest Images