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

New Post: javascript blob

$
0
0
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:
internalclass ArrayBufferUtil {
    privatereadonlydynamic 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++) {
            yieldreturn 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!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>