js-ctypes from javascript objects
Asked Answered
J

1

2

I'm working on a Firefox extension that receives binary images as ArrayBuffers of uint8_t.

In my extension I load a .dll file that has a function that I need to use on that received image. The function takes a ctype.uint8_t.ptr parameter and returns a ctype.uint8_t.ptr value.

I can't seem to find a way of converting the ArrayBuffer to this particular ctype so that I can pass it along to the function. Is there a correct way to do this?

Using ImplicitConvert() gives an Error: argument must be a nonnegative integer.

Jokjakarta answered 15/10, 2013 at 11:5 Comment(0)
R
2

You should be able to do just:

var a = new Uint8Array(1<<10);
var ptr = new ctypes.uint8_t.ptr(a.buffer);

The stuff is not documented it seems, but there are some tests that demonstrate this.

Randy answered 15/10, 2013 at 16:38 Comment(6)
Might be working but I get the following error : TypeError: expected type pointer, got (void 0)Markova
I assume that somehow I end up having invalid data in a.buffer. Is there a way to treat for this? According to the length attribute of the new Uint8Array I create, the length is 19.Markova
It works. Thank you for your answer. But I'm still curious about how to check the array prior to trying to point to it. I can use try and catch blocks but knowing beforehand might be more effective.Markova
I don't know anything about your code and therefore cannot reproduce your problem, so I won't speculate here. ;)Randy
Hey @Sebastian-LaurenţiuPlesciuc and nmair the link to the "some tests" is broken, i was wondering if this method still works today and where these test cases are. This is really really cool!Retentive
@Retentive Hi there! Here's a working link to the tests : github.com/krad-radio/mozilla-krad/blob/master/toolkit/…Markova

© 2022 - 2024 — McMap. All rights reserved.