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§

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

Macros§

match_metacall_value
Matches MetacallValue trait object. For example: …
match_metacall_value_mut
Same as match_metacall_value but gives a mutable reference. For example: …
match_metacall_value_ref
Same as match_metacall_value but gives a reference. For example: …

Structs§

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

Enums§

MetacallClassFromNameError
This error may happen when trying to get a class by its name.
MetacallError
This error may happen when trying to call a function.
MetacallGetAttributeError
This error may happen when trying to get a class/object attribute.
MetacallLoaderError
This error may happen when loading a code. Check your logs for more information if you get FromFileFailure or FromMemoryFailure error variant.
MetacallSetAttributeError
This error may happen when trying to set a class/object attribute. Check your logs if you get SetAttributeFailure error variant.
MetacallThrowableValue
Different types of Throwable value.

Traits§

MetacallValue
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§

initialize
Initializes Metacall. Always remember to store the output in a variable to avoid instant drop. For example: …
metacall
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: …
metacall_no_arg
Calls a function same as metacall without passing any arguments. For example: …
metacall_untyped
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: …
metacall_untyped_no_arg
Calls a function same as metacall_untyped without passing any arguments. For example: …

Type Aliases§

MetacallFutureHandler
Function pointer type used for resolving/rejecting Metacall futures. The first argument is the result and the second argument is the data that you may want to access when the function gets called. Checkout MetacallFuture resolve or MetacallFuture reject for usage.