Example: Fibonacci

Source code for this example: examples/fibonacci

The example from Introduction, presented in full.

cargo run --package example-fibonacci

lib.js

/**
 * @param {number} n
 * @returns {number}
 */
export const slowFib = (n) =>
  n === 0 ? 0 : n === 1 ? 1 : slowFib(n - 1) + slowFib(n - 2);

main.rs

use ferrosaur::js;

#[js(module("lib.js"))]
struct Math;

#[js(interface)]
impl Math {
    #[js(func)]
    fn slow_fib(&self, n: serde<usize>) -> serde<usize> {}
}

#[tokio::main]
async fn main() -> Result<()> {
    let rt = &mut JsRuntime::try_new(Default::default())?;

    let lib = Math::main_module_init(rt).await?;
    let fib = lib.slow_fib(42, rt)?;
    assert_eq!(fib, 267914296);

    Ok(())
}

use anyhow::Result;
use deno_core::JsRuntime;