+
Skip to content

moonbit-community/jmop

Repository files navigation

jmop

doc

Note: This module is experimental and its API may change in future releases.

jmop (JavaScript & MoonBit interoperation) is an FFI module for MoonBit's js backend that enables safe interaction with JavaScript objects and functions. It emphasizes null and undefined safety, helping you avoid common pitfalls when working with javascript.

Features

  • Null and undefined safety: Prevents accidental access to missing values.
  • Fail-fast behavior: Errors are detected and reported immediately.
  • Traceable runtime errors: Clear error messages make debugging easier.

Usage Examples

///|
typealias @jmop.(Object, Promise, Function)

///|
fn init {
  @jmop.global_this.set(
    "test",
    Object::new({
      "number1": 100,
      "number2": 50,
      "object": Object::new({
        "add": Function::new(fn(x : Int, y) { x + y }),
        "sub": Function::new(fn(x : Int, y) { x - y }),
      }),
    }),
  ) catch {
    e => println(e)
  }
}
///|
test "object and function" {
  let js_obj = @jmop.global_this.get_object("test")
  let n1 = js_obj.get_number("number1")
  let n2 = js_obj.get_number("number2")
  inspect(js_obj.get_object("object").call("add", [n1, n2]), content="150")
  inspect(js_obj.get_object("object").call("sub", [n1, n2]), content="50")
  inspect(js_obj.get_object("object").call("add", ["123", 1]), content="1231")
}

Try the following test by clicking the Test button:

///|
test "promise" {
  @jmop.try_launch(() => {
    let js_promise = Promise::new(1)
    let r = js_promise
      .map(x => x + 1)
      .bind(x => Promise::new(x.to_string()))
      .wait()
    println(r) // 2
  })
}
///|
test "fail-fast: get undefined field" {
  global_this.get_object("test").get("undefined_obj") |> ignore
}

///|
test "fail-fast: create nested promise" {
  @jmop.try_launch(() => Promise::new(Promise::new(1)).wait() |> ignore)
}

About

A safe JavaScript interop tool for MoonBit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载