Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloads with endianness parameter to DataView gets and sets #966

Merged
merged 2 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions 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 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.
///
/// [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 32-bit float (float) at the specified
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this bit size is lost in the wrong comment

/// 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