From 4e6f967f06e520013b9a6e8dcac954c76301a483 Mon Sep 17 00:00:00 2001 From: Eric Walker Date: Tue, 2 Jul 2024 11:09:04 -0400 Subject: [PATCH] Switch from cryptox to crypto namespace Also drop javy_ prefix in anticipation of switching to winter cg. --- crates/config/src/lib.rs | 4 ++-- crates/core/src/runtime.rs | 2 +- crates/javy/src/apis/{cryptox => crypto}/mod.rs | 15 +++++++-------- crates/javy/src/apis/mod.rs | 4 ++-- crates/javy/src/config.rs | 10 +++++----- crates/javy/src/runtime.rs | 8 ++++---- 6 files changed, 21 insertions(+), 22 deletions(-) rename crates/javy/src/apis/{cryptox => crypto}/mod.rs (89%) diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index f1dcbbfd..2fdc6133 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -31,7 +31,7 @@ bitflags! { const JAVY_STREAM_IO = 1 << 2; const REDIRECT_STDOUT_TO_STDERR = 1 << 3; const TEXT_ENCODING = 1 << 4; - const JAVY_CRYPTOX = 1 << 5; + const CRYPTO = 1 << 5; } } @@ -45,6 +45,6 @@ mod tests { assert!(Config::JAVY_STREAM_IO == Config::from_bits(1 << 2).unwrap()); assert!(Config::REDIRECT_STDOUT_TO_STDERR == Config::from_bits(1 << 3).unwrap()); assert!(Config::TEXT_ENCODING == Config::from_bits(1 << 4).unwrap()); - assert!(Config::JAVY_CRYPTOX == Config::from_bits(1 << 5).unwrap()); + assert!(Config::CRYPTO == Config::from_bits(1 << 5).unwrap()); } } diff --git a/crates/core/src/runtime.rs b/crates/core/src/runtime.rs index 1e52c74b..416e39d0 100644 --- a/crates/core/src/runtime.rs +++ b/crates/core/src/runtime.rs @@ -13,7 +13,7 @@ pub(crate) fn new(shared_config: SharedConfig) -> Result { // fix forward. .override_json_parse_and_stringify(false) .javy_json(false) - .javy_cryptox(shared_config.contains(SharedConfig::JAVY_CRYPTOX)); + .crypto(shared_config.contains(SharedConfig::CRYPTO)); Runtime::new(std::mem::take(config)) } diff --git a/crates/javy/src/apis/cryptox/mod.rs b/crates/javy/src/apis/crypto/mod.rs similarity index 89% rename from crates/javy/src/apis/cryptox/mod.rs rename to crates/javy/src/apis/crypto/mod.rs index 862db964..d42e3764 100644 --- a/crates/javy/src/apis/cryptox/mod.rs +++ b/crates/javy/src/apis/crypto/mod.rs @@ -10,14 +10,13 @@ use hmac::{Hmac, Mac}; /// An implemetation of crypto APIs to optimize fuel. /// Currently, hmacSHA256 is the only function implemented. -pub struct Cryptox; +pub struct Crypto; -impl Intrinsic for Cryptox { +impl Intrinsic for Crypto { unsafe fn add_intrinsic(ctx: std::ptr::NonNull) { - register(Ctx::from_raw(ctx)).expect("`Cryptox` APIs to succeed") + register(Ctx::from_raw(ctx)).expect("`Crypto` APIs to succeed") } } - fn register(this: Ctx<'_>) -> Result<()> { let globals = this.globals(); @@ -32,7 +31,7 @@ fn register(this: Ctx<'_>) -> Result<()> { }), )?; - globals.set("cryptox", crypto_obj)?; + globals.set("crypto", crypto_obj)?; Ok::<_, Error>(()) } @@ -72,14 +71,14 @@ mod tests { #[test] fn test_text_encoder_decoder() -> Result<()> { let mut config = Config::default(); - config.javy_cryptox(true); + config.crypto(true); let runtime = Runtime::new(config)?; runtime.context().with(|this| { let result: Value<'_> = this.eval( r#" let expectedHex = "97d2a569059bbcd8ead4444ff99071f4c01d005bcefe0d3567e1be628e5fdcd9"; - let result = cryptox.hmacSHA256("my secret and secure key", "input message"); + let result = crypto.hmacSHA256("my secret and secure key", "input message"); expectedHex === result; "#, )?; @@ -89,4 +88,4 @@ mod tests { })?; Ok(()) } -} \ No newline at end of file +} diff --git a/crates/javy/src/apis/mod.rs b/crates/javy/src/apis/mod.rs index 9b9d8202..f1b023d9 100644 --- a/crates/javy/src/apis/mod.rs +++ b/crates/javy/src/apis/mod.rs @@ -62,7 +62,7 @@ pub(crate) mod json; pub(crate) mod random; pub(crate) mod stream_io; pub(crate) mod text_encoding; -pub(crate) mod cryptox; +pub(crate) mod crypto; pub(crate) use console::*; #[cfg(feature = "json")] @@ -70,4 +70,4 @@ pub(crate) use json::*; pub(crate) use random::*; pub(crate) use stream_io::*; pub(crate) use text_encoding::*; -pub(crate) use cryptox::*; +pub(crate) use crypto::*; diff --git a/crates/javy/src/config.rs b/crates/javy/src/config.rs index f482ecbe..c678f53c 100644 --- a/crates/javy/src/config.rs +++ b/crates/javy/src/config.rs @@ -19,6 +19,7 @@ bitflags! { const OPERATORS = 1 << 12; const BIGNUM_EXTENSION = 1 << 13; const TEXT_ENCODING = 1 << 14; + const CRYPTO = 1 << 15; } } @@ -38,7 +39,6 @@ bitflags! { pub(crate) struct JavyIntrinsics: u32 { const STREAM_IO = 1; const JSON = 1 << 1; - const CRYPTOX = 1 << 2; } } @@ -180,11 +180,11 @@ impl Config { self } - /// Whether the `Javy.CRYPTOX` intrinsic will be available. + /// Whether the `crypto` intrinsic will be available. /// Enabled by default. - // #[cfg(feature = "cryptox")] - pub fn javy_cryptox(&mut self, enable: bool) -> &mut Self { - self.javy_intrinsics.set(JavyIntrinsics::CRYPTOX, enable); + // #[cfg(feature = "crypto")] + pub fn crypto(&mut self, enable: bool) -> &mut Self { + self.intrinsics.set(JSIntrinsics::CRYPTO, enable); self } diff --git a/crates/javy/src/runtime.rs b/crates/javy/src/runtime.rs index c94e113f..cdcb8d4a 100644 --- a/crates/javy/src/runtime.rs +++ b/crates/javy/src/runtime.rs @@ -1,7 +1,7 @@ // use crate::quickjs::JSContextRef; use super::from_js_error; use crate::{ - apis::{Cryptox, Console, NonStandardConsole, Random, StreamIO, TextEncoding}, + apis::{Crypto, Console, NonStandardConsole, Random, StreamIO, TextEncoding}, config::{JSIntrinsics, JavyIntrinsics}, Config, }; @@ -145,10 +145,10 @@ impl Runtime { } } - if javy_intrinsics.contains(JavyIntrinsics::CRYPTOX) { - // #[cfg(feature = "cryptox")] + if intrinsics.contains(JSIntrinsics::CRYPTO) { + // #[cfg(feature = "crypto")] unsafe { - Cryptox::add_intrinsic(ctx.as_raw()) + Crypto::add_intrinsic(ctx.as_raw()) } } });