2010/5/7 George Moschovitis <
george.mo...@gmail.com>:
> In narwhal I am using this hack to initialize a ByteString:
>
> var bytes = <java byte[]>;
>
> var ByteString = require("binary").ByteString;
>
> var b = new ByteString();
> b._bytes = bytes;
> b._offset = 0;
> b._length = Number(b._bytes.length);
>
> How can I accomplish something similar in RingoJS? Is there a standard
> CommonJS way to achieve this?
In ringojs, you just do:
var b = new ByteString(bytes);
or, if you explicitly don't want the byte array to be copied (but then
changing the underlying byte[] would break the "immutability contract"
of ByteString):
var b = ByteString.wrap(byte);
also, you can pass a ByteString or ByteArray into any Java method
taking a byte[], it will automatically be unwrapped.
I don't think there's a standard CommonJS way to do this since byte[]
is inherently Java-specific. It would be nice to have a common way of
doing this between ringojs and narwhal/rhino, though.
Hannes