Git Product home page Git Product logo

wamr-sdk's Introduction

WAMR SDK

A lightweight WAMR backend for Extism

Supports a limited set of features compared to extism/extism:

  • Linking multiple Wasm modules
  • Host functions
  • Memory limits

Building

Requires:

  • CMake
  • C compiler
mkdir build
cd build
cmake ..
make

or:

make

Getting started

  • extism_runtime_init should always be called before creating any plugins, and there is only a single global runtime that host functions can be loaded into
  • The plugins listed in ExtismManifest that depend on other Wasm modules must have all dependencies listed first in the manifest with module names specified.

Creating and calling a plugin

#include <stdio.h>
#include <stdlib.h>
#include <extism-wamr.h>

// Read a file from disk - an implementation of `read_file` can be found 
// in `bin/extism-wamr.c`
uint8_t *read_file(const char *, size_t *);

// Return the input as-is
uint64_t host_reflect(ExtismExecEnv *env, uint64_t x) { return x; }

// Run an Extism plugin and print the output
ExtismStatus run_wasm(const char *wasm_file, const char *func_name, const char *input, size_t input_len){
  char errbuf[1024];
  size_t datalen = 0, len = 0;
  uint8_t *data = read_file(wasm_file, &datalen);
  if (data == NULL) {
    return ExtismStatusErr;
  }

  // Initialize the runtime, this must be done before anything else 
  extism_runtime_init();

  // Specify the modules to be loaded, setting `name` to `NULL` marks a module
  // at the main module
  ExtismWasm wasm = {
      .data = data,
      .length = datalen,
      .name = NULL,
  };
  ExtismManifest manifest;
  extism_manifest_init(&manifest, &wasm, 1, NULL, 0, NULL);

  // Define a host function
  extism_host_function("extism:host/user", "host_reflect", "(I)I", host_reflect,
                       NULL);

  // Create the plugin instance
  ExtismPlugin *plugin = extism_plugin_new(&manifest, errbuf, 1024);
  if (plugin == NULL) {
    fputs("ERROR: ", stderr);
    fputs(errbuf, stderr);
    fputs("\n", stderr);
    free(data);
    extism_runtime_cleanup();
    return ExtismStatusErr;
  }

  // Call `func_name`
  if ((status = extism_plugin_call(plugin, func_name, (const void *)input,
                                   input_len)) != ExtismStatusOk) {
    // Print error if it fails
    const char *s = extism_plugin_error(plugin, &len);
    fprintf(stderr, "ERROR(%d): ", status);
    fwrite(s, len, 1, stderr);
    fputc('\n', stderr);
  } else {
    // Otherwise print the output
    uint8_t *output = extism_plugin_output(plugin, &len);
    if (len > 0) {
      fwrite(output, len, 1, stdout);
      fputc('\n', stdout);
    }
  }

  // Cleanup
  extism_plugin_free(plugin);
  extism_runtime_cleanup();
  free(data);
  return ExtismStatusOk;
}

wamr-sdk's People

Contributors

zshipko avatar nilslice avatar

Stargazers

David Konsumer avatar marco gazzuolo avatar Brady Joslin avatar Tsiry Sandratraina avatar Ankesh Bharti avatar Andrew Chou avatar Nathan Martin avatar aweNousaku avatar Andrew Johnston avatar Ryuta Suzuki avatar astrolemonade avatar Ju Huo avatar Justin Bennett avatar Quinn Millican avatar  avatar  avatar Dominique Saulet avatar

Watchers

Benjamin Eckel avatar  avatar Gavin Hayes avatar  avatar Muhammad Azeez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.