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

added Atomics and SharedArrayBuffer #1463

Merged
merged 6 commits into from
Apr 26, 2019
Merged
Changes from all commits
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
182 changes: 182 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,46 @@ extern "C" {
pub fn slice_with_end(this: &ArrayBuffer, begin: u32, end: u32) -> ArrayBuffer;
}

// SharedArrayBuffer
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type SharedArrayBuffer;

/// The `SharedArrayBuffer` object is used to represent a generic,
/// fixed-length raw binary data buffer, similar to the `ArrayBuffer`
/// object, but in a way that they can be used to create views
/// on shared memory. Unlike an `ArrayBuffer`, a `SharedArrayBuffer`
/// cannot become detached.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer)
#[wasm_bindgen(constructor)]
pub fn new(length: u32) -> SharedArrayBuffer;

/// The byteLength accessor property represents the length of
/// an `SharedArrayBuffer` in bytes. This is established when
/// the `SharedArrayBuffer` is constructed and cannot be changed.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/byteLength)
#[wasm_bindgen(method, getter, js_name = byteLength)]
pub fn byte_length(this: &SharedArrayBuffer) -> u32;

/// The `slice()` method returns a new `SharedArrayBuffer` whose contents
/// are a copy of this `SharedArrayBuffer`'s bytes from begin, inclusive,
/// up to end, exclusive.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/slice)
#[wasm_bindgen(method)]
pub fn slice(this: &SharedArrayBuffer, begin: u32) -> SharedArrayBuffer;

/// Like `slice()` but with the `end` argument.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/slice)
#[wasm_bindgen(method, js_name = slice)]
pub fn slice_with_end(this: &SharedArrayBuffer, begin: u32, end: u32) -> SharedArrayBuffer;
}

// Array Iterator
#[wasm_bindgen]
extern "C" {
Expand All @@ -463,6 +503,148 @@ extern "C" {
pub fn values(this: &Array) -> Iterator;
}

/// The `Atomics` object provides atomic operations as static methods.
/// They are used with `SharedArrayBuffer` objects.
///
/// The Atomic operations are installed on an `Atomics` module. Unlike
/// the other global objects, `Atomics` is not a constructor. You cannot
/// use it with a new operator or invoke the `Atomics` object as a
/// function. All properties and methods of `Atomics` are static
/// (as is the case with the Math object, for example).
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics)
#[allow(non_snake_case)]
pub mod Atomics {
use super::*;

#[wasm_bindgen]
extern "C" {
/// The static `Atomics.add()` method adds a given value at a given
/// position in the array and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/add)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn add(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;

/// The static `Atomics.and()` method computes a bitwise AND with a given
/// value at a given position in the array, and returns the old value
/// at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/and)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn and(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;

/// The static `Atomics.compareExchange()` method exchanges a given
/// replacement value at a given position in the array, if a given expected
/// value equals the old value. It returns the old value at that position
/// whether it was equal to the expected value or not.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/compareExchange)
#[wasm_bindgen(js_namespace = Atomics, catch, js_name = compareExchange)]
pub fn compare_exchange(
typed_array: &JsValue,
index: u32,
expected_value: i32,
replacement_value: i32,
) -> Result<i32, JsValue>;

/// The static `Atomics.exchange()` method stores a given value at a given
/// position in the array and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/exchange)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn exchange(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;

/// The static `Atomics.isLockFree()` method is used to determine
/// whether to use locks or atomic operations. It returns true,
/// if the given size is one of the `BYTES_PER_ELEMENT` property
/// of integer `TypedArray` types.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/isLockFree)
#[wasm_bindgen(js_namespace = Atomics, js_name = isLockFree)]
pub fn is_lock_free(size: u32) -> bool;

/// The static `Atomics.load()` method returns a value at a given
/// position in the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/load)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn load(typed_array: &JsValue, index: u32) -> Result<i32, JsValue>;

/// The static `Atomics.notify()` method notifies up some agents that
/// are sleeping in the wait queue.
/// Note: This operation works with a shared `Int32Array` only.
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/notify)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn notify(typed_array: &Int32Array, index: u32, count: u32) -> Result<u32, JsValue>;

/// The static `Atomics.or()` method computes a bitwise OR with a given value
/// at a given position in the array, and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/or)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn or(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;

/// The static `Atomics.store()` method stores a given value at the given
/// position in the array and returns that value.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/store)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn store(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;

/// The static `Atomics.sub()` method substracts a given value at a
/// given position in the array and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/sub)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn sub(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;

/// The static `Atomics.wait()` method verifies that a given
/// position in an `Int32Array` still contains a given value
/// and if so sleeps, awaiting a wakeup or a timeout.
/// It returns a string which is either "ok", "not-equal", or "timed-out".
/// Note: This operation only works with a shared `Int32Array`
/// and may not be allowed on the main thread.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn wait(typed_array: &Int32Array, index: u32, value: i32) -> Result<JsString, JsValue>;

/// Like `wait()`, but with timeout
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait)
#[wasm_bindgen(js_namespace = Atomics, catch, js_name = wait)]
pub fn wait_with_timeout(
typed_array: &Int32Array,
index: u32,
value: i32,
timeout: f64,
) -> Result<JsString, JsValue>;

/// The static `Atomics.xor()` method computes a bitwise XOR
/// with a given value at a given position in the array,
/// and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/xor)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn xor(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
}
}

// Boolean
#[wasm_bindgen]
extern "C" {
Expand Down