From e2044c74a3ff4faf02688a195a77c5740a75e417 Mon Sep 17 00:00:00 2001 From: Haoxiang Fei Date: Tue, 22 Apr 2025 10:08:09 +0800 Subject: [PATCH 1/2] fix: use `!` for async application --- src/js/async.mbt | 12 ++++++------ src/js/async_deprecated.mbt | 4 ++-- src/js/async_test.mbt | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/js/async.mbt b/src/js/async.mbt index 54d9672..da8c428 100644 --- a/src/js/async.mbt +++ b/src/js/async.mbt @@ -26,7 +26,7 @@ extern "js" fn Promise::wait_ffi( ///| pub async fn Promise::wait(self : Promise) -> Value! { - suspend!!(fn(k, ke) { Promise::wait_ffi(self, k, fn(e) { ke(Error_(e)) }) }) + suspend!(fn(k, ke) { Promise::wait_ffi(self, k, fn(e) { ke(Error_(e)) }) }) } ///| @@ -39,7 +39,7 @@ pub async fn Promise::wait(self : Promise) -> Value! { /// /// If you don't care about the result of the operation, you can use `spawn_detach` instead. pub fn Promise::unsafe_new[T](op : async () -> T!) -> Promise { - Promise::new_ffi(fn() { Value::cast_from(op!!()) }) + Promise::new_ffi(fn() { Value::cast_from(op!()) }) } ///| @@ -50,7 +50,7 @@ extern "js" fn Promise::new_ffi(op : async () -> Value!) -> Promise = pub fn spawn_detach[T, E : Error](op : async () -> T!E) -> Unit { async_run(fn() { try { - op!!() |> ignore + op!() |> ignore } catch { _ => () } @@ -65,21 +65,21 @@ pub extern "js" fn Promise::all(promises : Array[Promise]) -> Promise = "(ps) => ///| /// Wraps each given `async fn` in a `Promise` and waits for all of them to resolve. pub async fn async_all![T](ops : Array[async () -> T!]) -> Array[T] { - async_all_raw!!(ops.map(fn(op) { async fn!() { Value::cast_from(op!!()) } })).map( + async_all_raw!(ops.map(fn(op) { async fn!() { Value::cast_from(op!()) } })).map( Value::cast, ) } ///| async fn async_all_raw!(ops : Array[async () -> Value!]) -> Array[Value] { - Promise::all(ops.map(Promise::unsafe_new)).wait!!().cast() + Promise::all(ops.map(Promise::unsafe_new)).wait!().cast() } ///| pub fn async_test(op : async () -> Unit!) -> Unit { async_run(async fn() { try { - op!!() + op!() } catch { e => { println("ERROR in `async_test`: \{e}") diff --git a/src/js/async_deprecated.mbt b/src/js/async_deprecated.mbt index f3b6d66..74e4437 100644 --- a/src/js/async_deprecated.mbt +++ b/src/js/async_deprecated.mbt @@ -12,13 +12,13 @@ extern "js" fn async_wrap_ffi( ///| #deprecated("use Promise::wait instead") pub async fn async_wrap(op : AsyncOp) -> Value! { - suspend!!(fn(k, ke) { async_wrap_ffi(op, k, fn(e) { ke(Error_(e)) }) }) + suspend!(fn(k, ke) { async_wrap_ffi(op, k, fn(e) { ke(Error_(e)) }) }) } ///| #deprecated("use Promise::new instead") pub fn async_unwrap[T](op : async () -> T!) -> Promise { - Promise::new_ffi(fn() { Value::cast_from(op!!()) }) + Promise::new_ffi(fn() { Value::cast_from(op!()) }) } ///| diff --git a/src/js/async_test.mbt b/src/js/async_test.mbt index f5979ed..6bcda65 100644 --- a/src/js/async_test.mbt +++ b/src/js/async_test.mbt @@ -5,7 +5,7 @@ async fn op1() -> String? { ///| async fn op2() -> String? { - guard op1!!() is Some(prefix) + guard op1!() is Some(prefix) Some(prefix + ", World") } @@ -18,7 +18,7 @@ async fn op3() -> String? { test "Promise::wait" { @js.async_test(fn!() { // Promise::unsafe_new + Promise::wait is a noop. - let res = @js.Promise::unsafe_new(fn() { op1!!() }).wait!!() + let res = @js.Promise::unsafe_new(fn() { op1!() }).wait!() assert_eq!(res.cast(), Some("Hello")) }) } @@ -27,7 +27,7 @@ test "Promise::wait" { test "async_all" { @js.async_test(fn!() { assert_eq!( - @js.async_all!!([fn() { op2!!() }, fn() { op1!!() }, fn() { op3!!() }]), + @js.async_all!([fn() { op2!() }, fn() { op1!() }, fn() { op3!() }]), [Some("Hello, World"), Some("Hello"), None], ) }) From f5789329345b7ea8fa25e3188d76db7e1f37ccde Mon Sep 17 00:00:00 2001 From: rami3l Date: Thu, 24 Apr 2025 11:37:22 +0800 Subject: [PATCH 2/2] fix: use new syntax for async fn type --- src/js/async.mbt | 14 +++++++------- src/js/async_deprecated.mbt | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/js/async.mbt b/src/js/async.mbt index da8c428..849c97d 100644 --- a/src/js/async.mbt +++ b/src/js/async.mbt @@ -4,7 +4,7 @@ pub async fn suspend[T, E : Error]( ) -> T!E = "%async.suspend" ///| -pub fn async_run(f : async () -> Unit) -> Unit = "%async.run" +pub fn async_run(f : () -> Unit!Async) -> Unit = "%async.run" ///| /// # Safety @@ -38,16 +38,16 @@ pub async fn Promise::wait(self : Promise) -> Value! { /// This makes sure that when `op` errs out, the error is caught by the MoonBit runtime. /// /// If you don't care about the result of the operation, you can use `spawn_detach` instead. -pub fn Promise::unsafe_new[T](op : async () -> T!) -> Promise { +pub fn Promise::unsafe_new[T](op : () -> T!Error + Async) -> Promise { Promise::new_ffi(fn() { Value::cast_from(op!()) }) } ///| -extern "js" fn Promise::new_ffi(op : async () -> Value!) -> Promise = +extern "js" fn Promise::new_ffi(op : () -> Value!Error + Async) -> Promise = #| (op) => new Promise((k, ke) => op(k, ke)) ///| -pub fn spawn_detach[T, E : Error](op : async () -> T!E) -> Unit { +pub fn spawn_detach[T, E : Error](op : () -> T!E + Async) -> Unit { async_run(fn() { try { op!() |> ignore @@ -64,19 +64,19 @@ pub extern "js" fn Promise::all(promises : Array[Promise]) -> Promise = "(ps) => ///| /// Wraps each given `async fn` in a `Promise` and waits for all of them to resolve. -pub async fn async_all![T](ops : Array[async () -> T!]) -> Array[T] { +pub async fn async_all![T](ops : Array[() -> T!Error + Async]) -> Array[T] { async_all_raw!(ops.map(fn(op) { async fn!() { Value::cast_from(op!()) } })).map( Value::cast, ) } ///| -async fn async_all_raw!(ops : Array[async () -> Value!]) -> Array[Value] { +async fn async_all_raw!(ops : Array[() -> Value!Error + Async]) -> Array[Value] { Promise::all(ops.map(Promise::unsafe_new)).wait!().cast() } ///| -pub fn async_test(op : async () -> Unit!) -> Unit { +pub fn async_test(op : () -> Unit!Error + Async) -> Unit { async_run(async fn() { try { op!() diff --git a/src/js/async_deprecated.mbt b/src/js/async_deprecated.mbt index 74e4437..c05fa8b 100644 --- a/src/js/async_deprecated.mbt +++ b/src/js/async_deprecated.mbt @@ -17,12 +17,12 @@ pub async fn async_wrap(op : AsyncOp) -> Value! { ///| #deprecated("use Promise::new instead") -pub fn async_unwrap[T](op : async () -> T!) -> Promise { +pub fn async_unwrap[T](op : () -> T!Error + Async) -> Promise { Promise::new_ffi(fn() { Value::cast_from(op!()) }) } ///| #deprecated("use Promise::unsafe_new instead") -pub fn Promise::new[T](op : async () -> T!) -> Promise { +pub fn Promise::new[T](op : () -> T!Error + Async) -> Promise { Promise::unsafe_new(op) }