Skip to content

Instantly share code, notes, and snippets.

@dherman
Created July 22, 2022 20:51
Show Gist options
  • Save dherman/bbc6a6fcbd8682b4dea6bdd3a98c9307 to your computer and use it in GitHub Desktop.
Save dherman/bbc6a6fcbd8682b4dea6bdd3a98c9307 to your computer and use it in GitHub Desktop.
trait TypedArrayExt {
fn set_info<'cx, C: Context<'cx>>(&self, cx: &mut C, info: Handle<'cx, JsObject>) -> NeonResult<()>;
fn get_info<'cx, C: Context<'cx>>(&self, cx: &mut C) -> JsResult<'cx, JsObject> {
let info = cx.empty_object();
self.set_info(cx, info)?;
Ok(info)
}
}
impl<T: Binary> TypedArrayExt for JsTypedArray<T> {
fn set_info<'cx, C: Context<'cx>>(&self, cx: &mut C, info: Handle<'cx, JsObject>) -> NeonResult<()> {
let byte_offset = self.byte_offset(cx);
let byte_offset = cx.number(byte_offset as u32);
let len = self.len(cx);
let len = cx.number(len as u32);
let byte_length = self.byte_length(cx);
let byte_length = cx.number(byte_length as u32);
let buffer = self.buffer(cx);
info.set(cx, "byteOffset", byte_offset)?;
info.set(cx, "length", len)?;
info.set(cx, "byteLength", byte_length)?;
info.set(cx, "buffer", buffer)?;
Ok(())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment