From 968b5e0154914f70ec3f6ec8a1c7f656af136d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joni=20Katajam=C3=A4ki?= Date: Mon, 15 Oct 2018 14:32:17 +0300 Subject: [PATCH] Add overloads with endianness parameter to DataView gets and sets --- crates/js-sys/src/lib.rs | 84 ++++++++++++++++++++++++++++ crates/js-sys/tests/wasm/DataView.rs | 22 ++++++++ 2 files changed, 106 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 4aeafd008..e480db3b0 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -541,6 +541,13 @@ extern "C" { #[wasm_bindgen(method, js_name = getInt16)] 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 /// byte offset from the start of the view. /// @@ -548,6 +555,13 @@ extern "C" { #[wasm_bindgen(method, js_name = getUint16)] 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 /// byte offset from the start of the DataView. /// @@ -555,6 +569,13 @@ extern "C" { #[wasm_bindgen(method, js_name = getInt32)] 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 /// byte offset from the start of the view. /// @@ -562,6 +583,13 @@ extern "C" { #[wasm_bindgen(method, js_name = getUint32)] 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 /// byte offset from the start of the DataView. /// @@ -569,6 +597,13 @@ extern "C" { #[wasm_bindgen(method, js_name = getFloat32)] 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 /// byte offset from the start of the DataView. /// @@ -576,6 +611,13 @@ extern "C" { #[wasm_bindgen(method, js_name = getFloat64)] 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 /// specified byte offset from the start of the DataView. /// @@ -597,6 +639,13 @@ extern "C" { #[wasm_bindgen(method, js_name = setInt16)] 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 /// specified byte offset from the start of the DataView. /// @@ -604,6 +653,13 @@ extern "C" { #[wasm_bindgen(method, js_name = setUint16)] 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 /// specified byte offset from the start of the DataView. /// @@ -611,6 +667,13 @@ extern "C" { #[wasm_bindgen(method, js_name = setInt32)] 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 /// specified byte offset from the start of the DataView. /// @@ -618,6 +681,13 @@ extern "C" { #[wasm_bindgen(method, js_name = setUint32)] 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 /// specified byte offset from the start of the DataView. /// @@ -625,12 +695,26 @@ extern "C" { #[wasm_bindgen(method, js_name = setFloat32)] 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 /// 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(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 diff --git a/crates/js-sys/tests/wasm/DataView.rs b/crates/js-sys/tests/wasm/DataView.rs index 84fdf0f63..565f67ec1 100644 --- a/crates/js-sys/tests/wasm/DataView.rs +++ b/crates/js-sys/tests/wasm/DataView.rs @@ -20,18 +20,40 @@ fn test() { assert_eq!(v.get_int8(0), 42); v.set_uint8(0, 255); assert_eq!(v.get_uint8(0), 255); + v.set_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); 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); 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); 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); 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); 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);