Crate metacall

source ·
Expand description

METACALL is a library that allows calling functions, methods or procedures between programming languages. With METACALL you can transparently execute code from / to any programming language, for example, call TypeScript code from Rust. Click here for installation guide.

General usage example: Let’s consider we have the following Typescript code: sum.ts

export function sum(a: number, b: number): number {
    return a + b;
}

Now let’s jump into Rust:

use metacall::{switch, metacall, loaders};

fn main() {
    // Initialize Metacall at the top
    let _metacall = switch::initialize().unwrap();
     
    // Load the file (Checkout the loaders module for loading multiple files
    // or loading from string)
    loaders::from_single_file("ts", "sum.ts").unwrap();

    // Call the sum function (Also checkout other metacall functions)
    let sum = metacall::<f64>("sum", [1.0, 2.0]).unwrap();

    assert_eq!(sum, 3.0);
}

Modules

  • Contains Metacall language inliners. Usage example: …
  • Contains Metacall loaders from file and memory. Usage example: …

Macros

Structs

  • Represents Metacall Class. You can get this type when returned by a function or get a class by its name with from_name.
  • Represents Metacall exception. You can create an exception with new.
  • Represents Metacall function.
  • Represents MetacallFuture. Keep in mind that it’s not supported to pass a future as an argument. Usage example: …
  • This error happens when it’s not possible to initialize the Metacall core. You can check your logs for more information.
  • Represents NULL.
  • Represents Metacall Object. You can get this type when returned by a function or create one from a class with create_object.
  • Represents Metacall pointer. This type cannot be used in other languages. This type is highly unsafe so be careful!
  • This error may happen when passing contains a null character. You can access the original string and the NulError throughout this struct.
  • Represents Metacall throwable. Keep in mind that it’s not supported to pass a throwable as an argument.

Enums

Traits

  • Trait of any possible object in Metacall. Checkout match_metacall_value macro for matching trait objects of this trait. Let’ see what types we can use with an example: …

Functions

  • Initializes Metacall. Always remember to store the output in a variable to avoid instant drop. For example: …
  • Calls a function with arguments. The generic parameter is the return type of the function you’re calling. Checkout MetacallValue for possible types. For example: …
  • Calls a function same as metacall without passing any arguments. For example: …
  • Calls a function same as metacall but returns a trait object of MetacallValue. This is useful when you don’t know the return type of that function or the function may return multiple types. Checkout match_metacall_value for unwrapping the inner value. For example: …
  • Calls a function same as metacall_untyped without passing any arguments. For example: …

Type Definitions