Skip to content

Commit

Permalink
Switch from cryptox to crypto namespace
Browse files Browse the repository at this point in the history
Also drop javy_ prefix in anticipation of switching to winter cg.
  • Loading branch information
ewalk153 committed Jul 2, 2024
1 parent f379f9a commit 4e6f967
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion crates/core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) fn new(shared_config: SharedConfig) -> Result<Runtime> {
// 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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<qjs::JSContext>) {
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();

Expand All @@ -32,7 +31,7 @@ fn register(this: Ctx<'_>) -> Result<()> {
}),
)?;

globals.set("cryptox", crypto_obj)?;
globals.set("crypto", crypto_obj)?;

Ok::<_, Error>(())
}
Expand Down Expand Up @@ -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;
"#,
)?;
Expand All @@ -89,4 +88,4 @@ mod tests {
})?;
Ok(())
}
}
}
4 changes: 2 additions & 2 deletions crates/javy/src/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ 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")]
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::*;
10 changes: 5 additions & 5 deletions crates/javy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bitflags! {
const OPERATORS = 1 << 12;
const BIGNUM_EXTENSION = 1 << 13;
const TEXT_ENCODING = 1 << 14;
const CRYPTO = 1 << 15;
}
}

Expand All @@ -38,7 +39,6 @@ bitflags! {
pub(crate) struct JavyIntrinsics: u32 {
const STREAM_IO = 1;
const JSON = 1 << 1;
const CRYPTOX = 1 << 2;
}
}

Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions crates/javy/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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())
}
}
});
Expand Down

0 comments on commit 4e6f967

Please sign in to comment.