Skip to content

Commit

Permalink
Always check the constructor name for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 5, 2024
1 parent 25d9233 commit 04a539e
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/backends/wasm_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use crate::util::{inner_u32, inner_u64};
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
compile_error!("`wasm_js` backend can be enabled only for OS-less WASM targets!");

use js_sys::{JsString, Object, SharedArrayBuffer, Uint8Array, WebAssembly::Memory};
use js_sys::{JsString, Object, Uint8Array, WebAssembly::Memory};
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};

// Size of our temporary Uint8Array buffer used with WebCrypto methods
Expand All @@ -43,19 +43,10 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// `SharedArrayBuffer` is only available with COOP & COEP. But even without its
// possible to create a shared `WebAssembly.Memory`, so we check for that via
// the constructor name.
//
// Keep in mind that `crossOriginIsolated` is not available on Node.js, in
// which case we can still use `instanceof` because `SharedArrayBuffer` is
// always available.
let shared = match CROSS_ORIGIN_ISOLATED.with(Option::clone) {
Some(true) | None => buffer.is_instance_of::<SharedArrayBuffer>(),
Some(false) => {
let constructor_name = Object::from(buffer).constructor().name();
SHARED_ARRAY_BUFFER_NAME.with(|name| &constructor_name == name)
}
};

let val = if shared {
let constructor_name = Object::from(buffer).constructor().name();
let val = if SHARED_ARRAY_BUFFER_NAME
.with(|sab_name| &constructor_name == sab_name)
{
MEMORY_KIND_SHARED
} else {
MEMORY_KIND_NOT_SHARED
Expand Down Expand Up @@ -115,9 +106,6 @@ extern "C" {
fn get_random_values(this: &Crypto, buf: &Uint8Array) -> Result<(), JsValue>;
#[wasm_bindgen(method, js_name = getRandomValues, catch)]
fn get_random_values_ref(this: &Crypto, buf: &mut [u8]) -> Result<(), JsValue>;
// Returns the [`crossOriginIsolated`](https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated) global property.
#[wasm_bindgen(thread_local_v2, js_namespace = globalThis, js_name = crossOriginIsolated)]
static CROSS_ORIGIN_ISOLATED: Option<bool>;
#[wasm_bindgen(thread_local_v2, static_string)]
static SHARED_ARRAY_BUFFER_NAME: JsString = "SharedArrayBuffer";
}

0 comments on commit 04a539e

Please sign in to comment.