Skip to content

Commit

Permalink
WIP - progress on promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ewalk153 committed Jul 5, 2024
1 parent 3bc415d commit 247e59c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
3 changes: 2 additions & 1 deletion crates/core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub(crate) fn new(shared_config: SharedConfig) -> Result<Runtime> {
.javy_json(false)
// For the time being, ship crypto as off by default
// Later, we may enable it with: .crypto(shared_config.contains(SharedConfig::CRYPTO))
.crypto(cfg!(feature = "experimental_crypto"));
// .crypto(cfg!(feature = "experimental_crypto"));
.crypto(true);

Runtime::new(std::mem::take(config))
}
52 changes: 42 additions & 10 deletions crates/javy/src/apis/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::quickjs::{context::Intrinsic, qjs, Ctx, Function, Object, String as JSString, Value};
use crate::quickjs::{context::Intrinsic, qjs, Ctx, CatchResultExt, Function, Object, String as JSString, Value, Promise, function::Func, function::This};
use crate::{hold, hold_and_release, to_js_error, val_to_string, Args};
use anyhow::{bail, Error, Result};

Expand Down Expand Up @@ -38,7 +38,7 @@ fn register(this: Ctx<'_>) -> Result<()> {
/// Arg[0] - secret
/// Arg[1] - message
/// returns - hex encoded string of hmac.
fn hmac_sha256(args: Args<'_>) -> Result<Value<'_>> {
fn hmac_sha256(args: Args<'_>) -> Result<Promise<'_>> {
let (ctx, args) = args.release();

if args.len() != 3 {
Expand All @@ -64,8 +64,31 @@ fn hmac_sha256(args: Args<'_>) -> Result<Value<'_>> {
// Convert result to JSString
let js_string_digest = JSString::from_str(ctx.clone(), &string_digest?)
.map_err(|e| rquickjs::Exception::throw_type(&ctx, &format!("Failed to convert result to JSString: {}", e)))?;
//Value::from_string(js_string_digest);
let string = Value::from_string(js_string_digest);
// let promise = Promise::from_value(string)
// .map_err(|e| rquickjs::Exception::throw_type(&ctx, &format!("Failed to convert value to promise: {}", e)))?;

// let promise = Promise::from_value(string);
Ok(build_promise_from_value(string)?)
}

Ok(Value::from_string(js_string_digest))
fn build_promise_from_value(value: Value<'_>) -> Result<Promise<'_>> {
let ctx = value.ctx();
let (promise, resolve, _) = Promise::new(&ctx).unwrap();
let cb = Func::new( || {
"hello world"
});

promise
.get::<_, Function>("then")
.catch(&ctx)
.unwrap()
.call::<_, ()>((This(promise.clone()), cb))
.catch(&ctx)
.unwrap();

return Ok(promise)
}

/// hmac_sha256_result applies the HMAC sha256 algorithm for signing.
Expand Down Expand Up @@ -94,7 +117,9 @@ mod tests {
let result: Value<'_> = this.eval(
r#"
let expectedHex = "97d2a569059bbcd8ead4444ff99071f4c01d005bcefe0d3567e1be628e5fdcd9";
let result = crypto.subtle.sign({name: "HMAC", hash: "sha-256"}, "my secret and secure key", "input message");
let result = null;
crypto.subtle.sign({name: "HMAC", hash: "sha-256"}, "my secret and secure key", "input message").then(function(sig) { result = sig });
console.log(result);
result === expectedHex;
"#,
)?;
Expand Down Expand Up @@ -129,17 +154,24 @@ mod tests {
let runtime = Runtime::new(config)?;

runtime.context().with(|this| {
let result: Value<'_> = this.eval(
let result = this.eval::<Value<'_>, _>(
r#"
// matched tested behavior in node v18
let expectedHex = "c06ae855290abd8f397af6975e9c2f72fe27a90a3e0f0bb73b4f991567501980";
let result = crypto.subtle.sign({name: "HMAC", hash: "sha-256"}, "\uD800\uD800\uD800\uD800\uD800", "\uD800\uD800\uD800\uD800\uD800");
console.log(result);
console.log("Match?", result === expectedHex);
let result = null;
result = crypto.subtle.sign({name: "HMAC", hash: "sha-256"}, "\uD800\uD800\uD800\uD800\uD800", "\uD800\uD800\uD800\uD800\uD800")
// .then(function(signature) {
// result = signature;
// });
// console.log(result);
// console.log("Match?", result === expectedHex);
result === expectedHex;
"#,
)?;
assert!(result.as_bool().unwrap());
);
// assert!(result.is_err());
// let e = result.map_err(|e| from_js_error(this.clone(), e)).unwrap_err();
// assert_eq!("", e.to_string());
assert!(result.unwrap().as_bool().unwrap());
Ok::<_, Error>(())
})?;
Ok(())
Expand Down
9 changes: 9 additions & 0 deletions p.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const promiseA = new Promise((resolve, reject) => {
resolve(777);
});
// At this point, "promiseA" is already settled.
async f () => {
await promiseA.then((val) => console.log("asynchronous logging has val:", val));
}
f();
console.log("immediate logging");

0 comments on commit 247e59c

Please sign in to comment.