Implement crypto.randomUUID() (#33158)

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2024-08-21 21:56:37 -07:00 committed by GitHub
parent 663a92a5df
commit 9a1051c917
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 16 additions and 65 deletions

View file

@ -7,12 +7,14 @@ use js::jsapi::{JSObject, Type};
use js::rust::CustomAutoRooterGuard;
use js::typedarray::{ArrayBufferView, ArrayBufferViewU8, TypedArray};
use servo_rand::{RngCore, ServoRng};
use uuid::Uuid;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::CryptoBinding::CryptoMethods;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::USVString;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext;
@ -40,7 +42,7 @@ impl Crypto {
impl CryptoMethods for Crypto {
#[allow(unsafe_code)]
// https://dvcs.w3.org/hg/webcrypto-api/raw-file/tip/spec/Overview.html#Crypto-method-getRandomValues
// https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues
fn GetRandomValues(
&self,
_cx: JSContext,
@ -61,6 +63,15 @@ impl CryptoMethods for Crypto {
.map_err(|_| Error::JSFailed)
}
}
// https://w3c.github.io/webcrypto/#Crypto-method-randomUUID
fn RandomUUID(&self) -> USVString {
let uuid = Uuid::new_v4();
uuid.hyphenated()
.encode_lower(&mut Uuid::encode_buffer())
.to_owned()
.into()
}
}
fn is_integer_buffer(array_type: Type) -> bool {