Skip to content

Commit

Permalink
Merge pull request #966 from katis/dataview_endian_overloads
Browse files Browse the repository at this point in the history
Add overloads with endianness parameter to DataView gets and sets
  • Loading branch information
alexcrichton authored Oct 15, 2018
2 parents 2b90532 + 83f9f54 commit 79c050a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
86 changes: 85 additions & 1 deletion crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,41 +541,83 @@ 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.
///
/// [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(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.
///
/// [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(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.
///
/// [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(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.
///
/// [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(this: &DataView, byte_offset: usize) -> f32;

/// The getFloat64() 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.
///
/// [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 64-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(this: &DataView, byte_offset: usize) -> f64;

/// The getFloat64() method gets a signed 64-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.
///
Expand All @@ -597,40 +639,82 @@ 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.
///
/// [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(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.
///
/// [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(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.
///
/// [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(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.
///
/// [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(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
Expand Down
22 changes: 22 additions & 0 deletions crates/js-sys/tests/wasm/DataView.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 79c050a

Please sign in to comment.