Add overloads with endianness parameter to DataView gets and sets

This commit is contained in:
Joni Katajamäki 2018-10-15 14:32:17 +03:00
parent 2b90532db5
commit 968b5e0154
2 changed files with 106 additions and 0 deletions

View File

@ -541,6 +541,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = getInt16)] #[wasm_bindgen(method, js_name = getInt16)]
pub fn get_int16(this: &DataView, byte_offset: usize) -> i16; pub fn get_int16(this: &DataView, byte_offset: usize) -> i16;
/// The getInt16() method gets a signed 16-bit integer (byte) at the specified
/// byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getInt16)
#[wasm_bindgen(method, js_name = getInt16)]
pub fn get_int16_endian(this: &DataView, byte_offset: usize, little_endian: bool) -> i16;
/// The getUint16() an unsigned 16-bit integer (unsigned byte) at the specified /// The getUint16() an unsigned 16-bit integer (unsigned byte) at the specified
/// byte offset from the start of the view. /// byte offset from the start of the view.
/// ///
@ -548,6 +555,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = getUint16)] #[wasm_bindgen(method, js_name = getUint16)]
pub fn get_uint16(this: &DataView, byte_offset: usize) -> u16; pub fn get_uint16(this: &DataView, byte_offset: usize) -> u16;
/// The getUint16() an unsigned 16-bit integer (unsigned byte) at the specified
/// byte offset from the start of the view.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint16)
#[wasm_bindgen(method, js_name = getUint16)]
pub fn get_uint16_endian(this: &DataView, byte_offset: usize, little_endian: bool) -> u16;
/// The getInt32() method gets a signed 16-bit integer (byte) at the specified /// The getInt32() method gets a signed 16-bit integer (byte) at the specified
/// byte offset from the start of the DataView. /// byte offset from the start of the DataView.
/// ///
@ -555,6 +569,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = getInt32)] #[wasm_bindgen(method, js_name = getInt32)]
pub fn get_int32(this: &DataView, byte_offset: usize) -> i32; pub fn get_int32(this: &DataView, byte_offset: usize) -> i32;
/// The getInt32() method gets a signed 16-bit integer (byte) at the specified
/// byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getInt32)
#[wasm_bindgen(method, js_name = getInt32)]
pub fn get_int32_endian(this: &DataView, byte_offset: usize, little_endian: bool) -> i32;
/// The getUint32() an unsigned 16-bit integer (unsigned byte) at the specified /// The getUint32() an unsigned 16-bit integer (unsigned byte) at the specified
/// byte offset from the start of the view. /// byte offset from the start of the view.
/// ///
@ -562,6 +583,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = getUint32)] #[wasm_bindgen(method, js_name = getUint32)]
pub fn get_uint32(this: &DataView, byte_offset: usize) -> u32; pub fn get_uint32(this: &DataView, byte_offset: usize) -> u32;
/// The getUint32() an unsigned 16-bit integer (unsigned byte) at the specified
/// byte offset from the start of the view.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32)
#[wasm_bindgen(method, js_name = getUint32)]
pub fn get_uint32_endian(this: &DataView, byte_offset: usize, little_endian: bool) -> u32;
/// The getFloat32() method gets a signed 32-bit float (float) at the specified /// The getFloat32() method gets a signed 32-bit float (float) at the specified
/// byte offset from the start of the DataView. /// byte offset from the start of the DataView.
/// ///
@ -569,6 +597,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = getFloat32)] #[wasm_bindgen(method, js_name = getFloat32)]
pub fn get_float32(this: &DataView, byte_offset: usize) -> f32; pub fn get_float32(this: &DataView, byte_offset: usize) -> f32;
/// The getFloat32() method gets a signed 32-bit float (float) at the specified
/// byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat32)
#[wasm_bindgen(method, js_name = getFloat32)]
pub fn get_float32_endian(this: &DataView, byte_offset: usize, little_endian: bool) -> f32;
/// The getFloat64() method gets a signed 32-bit float (float) at the specified /// The getFloat64() method gets a signed 32-bit float (float) at the specified
/// byte offset from the start of the DataView. /// byte offset from the start of the DataView.
/// ///
@ -576,6 +611,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = getFloat64)] #[wasm_bindgen(method, js_name = getFloat64)]
pub fn get_float64(this: &DataView, byte_offset: usize) -> f64; pub fn get_float64(this: &DataView, byte_offset: usize) -> f64;
/// The getFloat64() method gets a signed 32-bit float (float) at the specified
/// byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat64)
#[wasm_bindgen(method, js_name = getFloat64)]
pub fn get_float64_endian(this: &DataView, byte_offset: usize, little_endian: bool) -> f64;
/// The setInt8() method stores a signed 8-bit integer (byte) value at the /// The setInt8() method stores a signed 8-bit integer (byte) value at the
/// specified byte offset from the start of the DataView. /// specified byte offset from the start of the DataView.
/// ///
@ -597,6 +639,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = setInt16)] #[wasm_bindgen(method, js_name = setInt16)]
pub fn set_int16(this: &DataView, byte_offset: usize, value: i16); pub fn set_int16(this: &DataView, byte_offset: usize, value: i16);
/// The setInt16() method stores a signed 16-bit integer (byte) value at the
/// specified byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setInt16)
#[wasm_bindgen(method, js_name = setInt16)]
pub fn set_int16_endian(this: &DataView, byte_offset: usize, value: i16, little_endian: bool);
/// The setUint16() method stores an unsigned 16-bit integer (byte) value at the /// The setUint16() method stores an unsigned 16-bit integer (byte) value at the
/// specified byte offset from the start of the DataView. /// specified byte offset from the start of the DataView.
/// ///
@ -604,6 +653,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = setUint16)] #[wasm_bindgen(method, js_name = setUint16)]
pub fn set_uint16(this: &DataView, byte_offset: usize, value: u16); pub fn set_uint16(this: &DataView, byte_offset: usize, value: u16);
/// The setUint16() method stores an unsigned 16-bit integer (byte) value at the
/// specified byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint16)
#[wasm_bindgen(method, js_name = setUint16)]
pub fn set_uint16_endian(this: &DataView, byte_offset: usize, value: u16, little_endian: bool);
/// The setInt32() method stores a signed 32-bit integer (byte) value at the /// The setInt32() method stores a signed 32-bit integer (byte) value at the
/// specified byte offset from the start of the DataView. /// specified byte offset from the start of the DataView.
/// ///
@ -611,6 +667,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = setInt32)] #[wasm_bindgen(method, js_name = setInt32)]
pub fn set_int32(this: &DataView, byte_offset: usize, value: i32); pub fn set_int32(this: &DataView, byte_offset: usize, value: i32);
/// The setInt32() method stores a signed 32-bit integer (byte) value at the
/// specified byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setInt32)
#[wasm_bindgen(method, js_name = setInt32)]
pub fn set_int32_endian(this: &DataView, byte_offset: usize, value: i32, little_endian: bool);
/// The setUint32() method stores an unsigned 32-bit integer (byte) value at the /// The setUint32() method stores an unsigned 32-bit integer (byte) value at the
/// specified byte offset from the start of the DataView. /// specified byte offset from the start of the DataView.
/// ///
@ -618,6 +681,13 @@ extern "C" {
#[wasm_bindgen(method, js_name = setUint32)] #[wasm_bindgen(method, js_name = setUint32)]
pub fn set_uint32(this: &DataView, byte_offset: usize, value: u32); pub fn set_uint32(this: &DataView, byte_offset: usize, value: u32);
/// The setUint32() method stores an unsigned 32-bit integer (byte) value at the
/// specified byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint32)
#[wasm_bindgen(method, js_name = setUint32)]
pub fn set_uint32_endian(this: &DataView, byte_offset: usize, value: u32, little_endian: bool);
/// The setFloat32() method stores a signed 32-bit float (float) value at the /// The setFloat32() method stores a signed 32-bit float (float) value at the
/// specified byte offset from the start of the DataView. /// specified byte offset from the start of the DataView.
/// ///
@ -625,12 +695,26 @@ extern "C" {
#[wasm_bindgen(method, js_name = setFloat32)] #[wasm_bindgen(method, js_name = setFloat32)]
pub fn set_float32(this: &DataView, byte_offset: usize, value: f32); pub fn set_float32(this: &DataView, byte_offset: usize, value: f32);
/// The setFloat32() method stores a signed 32-bit float (float) value at the
/// specified byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat32)
#[wasm_bindgen(method, js_name = setFloat32)]
pub fn set_float32_endian(this: &DataView, byte_offset: usize, value: f32, little_endian: bool);
/// The setFloat64() method stores a signed 64-bit float (float) value at the /// The setFloat64() method stores a signed 64-bit float (float) value at the
/// specified byte offset from the start of the DataView. /// specified byte offset from the start of the DataView.
/// ///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat64) /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat64)
#[wasm_bindgen(method, js_name = setFloat64)] #[wasm_bindgen(method, js_name = setFloat64)]
pub fn set_float64(this: &DataView, byte_offset: usize, value: f64); pub fn set_float64(this: &DataView, byte_offset: usize, value: f64);
/// The setFloat64() method stores a signed 64-bit float (float) value at the
/// specified byte offset from the start of the DataView.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat64)
#[wasm_bindgen(method, js_name = setFloat64)]
pub fn set_float64_endian(this: &DataView, byte_offset: usize, value: f64, little_endian: bool);
} }
// Error // Error

View File

@ -20,18 +20,40 @@ fn test() {
assert_eq!(v.get_int8(0), 42); assert_eq!(v.get_int8(0), 42);
v.set_uint8(0, 255); v.set_uint8(0, 255);
assert_eq!(v.get_uint8(0), 255); assert_eq!(v.get_uint8(0), 255);
v.set_int16(0, 32767); v.set_int16(0, 32767);
assert_eq!(v.get_int16(0), 32767); assert_eq!(v.get_int16(0), 32767);
v.set_int16_endian(0, 0x1122, true);
assert_eq!(v.get_int16_endian(0, true), 0x1122);
assert_eq!(v.get_int16_endian(0, false), 0x2211);
v.set_uint16(0, 65535); v.set_uint16(0, 65535);
assert_eq!(v.get_uint16(0), 65535); assert_eq!(v.get_uint16(0), 65535);
v.set_uint16_endian(0, 0x1122, true);
assert_eq!(v.get_uint16_endian(0, true), 0x1122);
assert_eq!(v.get_uint16_endian(0, false), 0x2211);
v.set_int32(0, 123456789); v.set_int32(0, 123456789);
assert_eq!(v.get_int32(0), 123456789); assert_eq!(v.get_int32(0), 123456789);
v.set_int32_endian(0, 0x11223344, true);
assert_eq!(v.get_int32_endian(0, true), 0x11223344);
assert_eq!(v.get_int32_endian(0, false), 0x44332211);
v.set_uint32(0, 3_123_456_789); v.set_uint32(0, 3_123_456_789);
assert_eq!(v.get_uint32(0), 3_123_456_789); assert_eq!(v.get_uint32(0), 3_123_456_789);
v.set_uint32_endian(0, 0x11223344, true);
assert_eq!(v.get_uint32_endian(0, true), 0x11223344);
assert_eq!(v.get_uint32_endian(0, false), 0x44332211);
v.set_float32(0, 100.123); v.set_float32(0, 100.123);
assert_eq!(v.get_float32(0), 100.123); assert_eq!(v.get_float32(0), 100.123);
v.set_float32_endian(0, f32::from_bits(0x11223344), true);
assert_eq!(v.get_float32_endian(0, true), f32::from_bits(0x11223344));
assert_eq!(v.get_float32_endian(0, false), f32::from_bits(0x44332211));
v.set_float64(0, 123456789.123456); v.set_float64(0, 123456789.123456);
assert_eq!(v.get_float64(0), 123456789.123456); assert_eq!(v.get_float64(0), 123456789.123456);
v.set_float64_endian(0, f64::from_bits(0x1122334411223344), true);
assert_eq!(v.get_float64_endian(0, true), f64::from_bits(0x1122334411223344));
assert_eq!(v.get_float64_endian(0, false), f64::from_bits(0x4433221144332211));
v.set_int8(0, 42); v.set_int8(0, 42);