javascript blob

Nov 3, 2014 at 4:59 PM
What method would be best to get a javascript blob object into a .net byte array?
Coordinator
Nov 4, 2014 at 11:12 AM
Hi Pretzelfisch,

The Blob object is part of the Web API. It isn't part of the JavaScript standard, and ClearScript doesn't provide it.

Did you mean ArrayBuffer by any chance?

Thanks!
Nov 7, 2014 at 5:08 PM
I think I can get the blob to a ArrayBuffer, will that map to a byte array?
Coordinator
Nov 10, 2014 at 2:20 AM
Hi again,

Generally speaking, ClearScript doesn't convert script objects to host objects automatically. Instead, it aims to allow hosts to manipulate script objects directly.

Here's a sample class that demonstrates reading bytes from an ArrayBuffer:
internal class ArrayBufferUtil {
    private readonly dynamic createUint8Array;
    public ArrayBufferUtil(ScriptEngine engine) {
        createUint8Array = engine.Evaluate(@"(function (arrayBuffer) {
            return new Uint8Array(arrayBuffer);
        }).valueOf()");
    }
    public IEnumerable<byte> ToEnumerable(object arrayBuffer) {
        var uint8Array = createUint8Array(arrayBuffer);
        var length = Convert.ToInt32(uint8Array.length);
        for (var i = 0; i < length; i++) {
            yield return Convert.ToByte(uint8Array[i]);
        }
    }
}
And here's how you might use this class to provide a printArrayBuffer function for script code:
var util = new ArrayBufferUtil(engine);
engine.Script.printArrayBuffer = new Action<object>(arrayBuffer => {
    var bytes = util.ToEnumerable(arrayBuffer);
    Console.WriteLine(string.Join(",", bytes));
});
Note that this requires a host-to-engine round trip for each byte. For large buffers a more efficient approach might be to use an encoding such as Base64 to transfer all the data to the host in one hop.

Good luck!
Nov 13, 2014 at 2:21 PM
Hi,

How to evaluate an expression and validate it using the ClearScript.
Coordinator
Nov 13, 2014 at 2:57 PM
Hello!

You can call ScriptEngine.Evaluate() to evaluate a script code expression. What kind of validation are you looking for?

Cheers!
Nov 14, 2014 at 4:00 AM
Edited Nov 14, 2014 at 12:20 PM
Hi

Expression whether true or false ?? by using if condition.

like a javascript as below.

<html>
<body>

<a id="demo"></a>
<script> var a=1; var result; if(1==1) { result=true; } else {result=false;} document.getElementById("demo").innerHTML = result; </script> </body>
</html>

how can we do this by using ClearScript . And how to give the input by user for the expression ???
Coordinator
Nov 14, 2014 at 11:28 AM
Sure, here's a simple C# example:
using (var engine = new V8ScriptEngine()) {
    if (Convert.ToBoolean(engine.Evaluate("Math.PI > 3.0"))) {
        Console.WriteLine("Pi is greater than 3!");
    }
}
As you can see, you can pass script expressions to ScriptEngine.Evaluate() and inspect or convert the result as necessary. Note that ClearScript provides access to script engines only; it doesn't process HTML or implement the Web API.

Good luck!
Nov 14, 2014 at 12:10 PM
Hi,

Thank you. Can you give some example to access the function in CleaeScript as in javaScript like

function myfunction( ){

}
Coordinator
Nov 14, 2014 at 1:42 PM
Suppose you've defined a JavaScript function like this:
engine.Execute("function myFunction(a, b, c) { return a * b + c; }");
Here are a few ways to execute it from the host and print out the return value:
Console.WriteLine(engine.Script.myFunction(1, 2, 3));
Console.WriteLine(engine.Script["myFunction"](4, 5, 6));
Console.WriteLine(engine.Invoke("myFunction", new object[] { 7, 8, 9 }));
Thanks!
Nov 17, 2014 at 8:11 AM
Hi ,

How can we give input from user for the expression .

Thank you.
Coordinator
Nov 17, 2014 at 2:22 PM
Please elaborate. How are you gathering user input? How is it represented? How would you like to use it from script code?
Nov 18, 2014 at 9:04 AM
Hi,

I have records with single field stored in list in a class i.e., name of person . Now i need to give the input expression to evaluate , and this input expression need to check for the records in list.

example :

Name is : siva
Name is : kumar

these are present in list, now we will give input expression name='siva' .

WriteLine("Enter input");
var input = ReadLine();

var result = engine.Evaluate(input = list.ToString());

WriteLine(result);

Thank you .
Coordinator
Nov 18, 2014 at 11:40 AM
Edited Nov 18, 2014 at 11:41 AM
Hi,

Sorry, still not clear:

var input = ReadLine();

var result = engine.Evaluate(input = list.ToString());

Are you expecting the user to enter the script to be executed? Or is the user typing in some data for the script to process?

Thanks!
Nov 19, 2014 at 8:39 AM
Hi,
user is typing in some data for the script to process.
        using (var engine = new V8ScriptEngine())
        {
            WriteLine("Enter input");
            var input = ReadLine(); 

            foreach (PersonList person in list)
            {
             var output= engine.Evaluate((input) = (person.name));
             WriteLine(output);
             }
input is typing by user some data for the script to process i.e., name="siva"

Thanks!
Coordinator
Nov 19, 2014 at 12:40 PM
OK, it looks like you're trying to pass input and person.name to a script function or expression.

If that's correct, there are many ways to do that. Several were already demonstrated (see the myFunction samples above).

Another possibility, if all the arguments are strings, is to insert the arguments into the script code itself:
engine.Evaluate(string.Format("myFunction('{0}', '{1}')", input, person.name));
Good luck!
Nov 20, 2014 at 5:51 AM
Hi,

Yes we are not also passing the input and person.name but also comparing the them i.e., comparing the input to the list(person.name) .

Thanks.
Coordinator
Nov 20, 2014 at 2:06 PM
Hi nsivakumarreddy,

No matter what kind of data you need to pass to your script, you should be able to use one of the patterns above.

Please let us know if you have other questions.

Thank you!