<|start_filename|>integrations/js/wasi/server/examples/hello-world/package-lock.json<|end_filename|>
{
  "name": "hello-world",
  "version": "1.0.0",
  "lockfileVersion": 1,
  "requires": true,
  "dependencies": {
    "@wasmer/wasi": {
      "version": "0.10.0",
      "resolved": "https://registry.npmjs.org/@wasmer/wasi/-/wasi-0.10.0.tgz",
      "integrity": "<KEY>
      "requires": {
        "browser-process-hrtime": "^1.0.0",
        "buffer-es6": "^4.9.3",
        "path-browserify": "^1.0.0",
        "randomfill": "^1.0.4"
      }
    },
    "browser-process-hrtime": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz",
      "integrity": "<KEY>
    },
    "buffer-es6": {
      "version": "4.9.3",
      "resolved": "https://registry.npmjs.org/buffer-es6/-/buffer-es6-4.9.3.tgz",
      "integrity": "sha1-8mNHuC33b9N+GLy1KIxJcM/VxAQ="
    },
    "path-browserify": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",
      "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g=="
    },
    "randombytes": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
      "integrity": "<KEY>
      "requires": {
        "safe-buffer": "^5.1.0"
      }
    },
    "randomfill": {
      "version": "1.0.4",
      "resolved": "https://registry.npmjs.org/randomfill/-/randomfill-1.0.4.tgz",
      "integrity": "<KEY>
      "requires": {
        "randombytes": "^2.0.5",
        "safe-buffer": "^5.1.0"
      }
    },
    "safe-buffer": {
      "version": "5.2.0",
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz",
      "integrity": "<KEY>
    }
  }
}


<|start_filename|>integrations/shared/wat/Makefile<|end_filename|>
%.wasm: %.wat
	wat2wasm $^ -o $@

all: wasi/helloworld.wasm wasi/clocktimeget.wasm


<|start_filename|>integrations/shared/c/exit-early.c<|end_filename|>
extern void interrupt_execution();
extern void should_not_be_called();

int exit_early() {
  
  // Interrupt the execution of this function
  // By calling the intended function from the host application
  interrupt_execution();

  // This will never called, and never get returned
  should_not_be_called();
  return 24;
}


<|start_filename|>integrations/shared/c/Makefile<|end_filename|>
all:
	# This compilation requires Wasienv
	# You can install it here:
	# https://github.com/wasienv/wasienv

	wasmcc ./add.c -o add.wasm
	wasmcc ./passing-data.c -o passing-data.wasm
	wasmcc ./handling-errors.c -o handling-errors.wasm

	# Allow external undefined functions (For our import functions)
	# with "-Wl,--allow-undefined"
	# https://lld.llvm.org/WebAssembly.html

	wasmcc -Wl,--allow-undefined ./host-functions.c -o host-functions.wasm
	wasmcc -Wl,--allow-undefined ./exit-early.c -o exit-early.wasm


<|start_filename|>integrations/js/wasi/server/examples/transforming-modules/package.json<|end_filename|>
{
  "name": "transforming-modules",
  "version": "1.0.0",
  "description": "The server-side transforming Wasm modules example",
  "main": "index.js",
  "scripts": {
    "dev": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@wasmer/wasi": "^0.10.0",
    "@wasmer/wasm-transformer": "^0.10.0"
  }
}


<|start_filename|>integrations/shared/c/host-functions.c<|end_filename|>
extern int get_counter();
extern int add_to_counter(int value_to_add);

int increment_counter_loop(int number_of_times) {

  int current_counter = get_counter();
  
  for(int i = 0; i < number_of_times; i++) {
    current_counter = add_to_counter(1);
  }

  return current_counter;
}


<|start_filename|>integrations/js/wasi/server/examples/hello-world/index.js<|end_filename|>
const fs = require("fs");
const { WASI } = require("@wasmer/wasi");
let nodeBindings = require("@wasmer/wasi/lib/bindings/node");

const wasmFilePath = "../../../../../shared/wat/wasi/helloworld.wasm";

nodeBindings = nodeBindings.default || nodeBindings;

// Instantiate a new WASI Instance
let wasi = new WASI({
  args: [wasmFilePath],
  env: {},
  bindings: nodeBindings
});

// Async function to run our Wasm module/instance
const startWasiTask = async pathToWasmFile => {
  // Fetch our Wasm File
  let wasmBytes = new Uint8Array(fs.readFileSync(pathToWasmFile)).buffer;

  // Instantiate the WebAssembly file
  let wasmModule = await WebAssembly.compile(wasmBytes);
  let instance = await WebAssembly.instantiate(wasmModule, {
    ...wasi.getImports(wasmModule)
  });

  // Start the WASI instance
  wasi.start(instance);
};

// Everything starts here
startWasiTask(wasmFilePath);


<|start_filename|>integrations/shared/c/add.c<|end_filename|>

// Function that takes in an integer value, adds one to it, and returns the new value
int add_one(int value) {
    value += 1;
    return value;
}


<|start_filename|>integrations/shared/c/handling-errors.c<|end_filename|>
int throw_wasm_error() {
  int value_one = 10;
  int value_two = 0;
  return value_one / value_two;
}


<|start_filename|>integrations/shared/c/passing-data.c<|end_filename|>
// Define the maximum size of our buffer
const int MAX_BUFFER_SIZE = 100;

// Define a fixed size byte array
// That we can use as a buffer for passing data between
// The host application, and this guest Wasm module
unsigned char byte_buffer[MAX_BUFFER_SIZE];

// Function to return a pointer to our buffer
unsigned char*  get_wasm_memory_buffer_pointer() {
  return byte_buffer;
}

// Function that will fetch the data in the byteBuffer as a string,
// And then concatenate the string "Wasm is Cool",
// And write the concatednated string back to the byteBuffer.
int add_wasm_is_cool(int string_length) {

  // Define our total concatenated string
  char total_string[MAX_BUFFER_SIZE];

  // Read in the string that was passed in by the
  // host application into the byteBuffer

  // Ensure we have enough space to process the string and return an error if we don't.
  if (string_length > MAX_BUFFER_SIZE - 1) {
          return -1;
  }

  // Loop over the buffer, break out if we
  // Find the end of the string,
  // Otherwise add the characters to the total_tring.
  int i = 0;
  for(i = 0; i < string_length; i++) {
    // Add the character in the buffer to our total string
    total_string[i] = byte_buffer[i];
  }
  // Record the length of the passed string
  int passed_string_length = i;

  // Define our Wasm is cool string, and get its length
  char wasm_is_cool[] = " Wasm is cool!";
  int wasm_is_cool_length = sizeof(wasm_is_cool);

  // Loop over the Wasm is cool string, concatenate it to the total string
  for (int i = 0; i < wasm_is_cool_length; i++) {
    total_string[passed_string_length + i] = wasm_is_cool[i];
  }

  // Get the length of the total string for conveinience.
  int total_string_length = passed_string_length + wasm_is_cool_length;

  // Add the trailing end of string character
  total_string[total_string_length] = 0;

  // Write the string back to our buffer
  for (int i = 0; i < total_string_length; i++) {
    byte_buffer[i] = total_string[i];
  }

  // Return the length of the total string
  return total_string_length;
}


<|start_filename|>integrations/js/wasi/browser/examples/hello-world/index.js<|end_filename|>
import { WASI } from '@wasmer/wasi/lib'
import browserBindings from '@wasmer/wasi/lib/bindings/browser'
import { WasmFs } from '@wasmer/wasmfs'

const wasmFilePath = '/helloworld.wasm'  // Path to our WASI module
const echoStr      = 'Hello World!'    // Text string to echo

// Instantiate new WASI and WasmFs Instances
// IMPORTANT:
// Instantiating WasmFs is only needed when running in a browser.
// When running on the server, NodeJS's native FS module is assigned by default
const wasmFs = new WasmFs()

let wasi = new WASI({
  // Arguments passed to the Wasm Module
  // The first argument is usually the filepath to the executable WASI module
  // we want to run.
  args: [wasmFilePath, echoStr],

  // Environment variables that are accesible to the WASI module
  env: {},

  // Bindings that are used by the WASI Instance (fs, path, etc...)
  bindings: {
    ...browserBindings,
    fs: wasmFs.fs
  }
})

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Async function to run our WASI module/instance
const startWasiTask =
  async pathToWasmFile => {
    // Fetch our Wasm File
    let response  = await fetch(pathToWasmFile)
    let wasmBytes = new Uint8Array(await response.arrayBuffer())

    // IMPORTANT:
    // Some WASI module interfaces use datatypes that cannot yet be transferred
    // between environments (for example, you can't yet send a JavaScript BigInt
    // to a WebAssembly i64).  Therefore, the interface to such modules has to
    // be transformed using `@wasmer/wasm-transformer`, which we will cover in
    // a later example

    // Instantiate the WebAssembly file
    let wasmModule = await WebAssembly.compile(wasmBytes);
    let instance = await WebAssembly.instantiate(wasmModule, {
      ...wasi.getImports(wasmModule)
    });
  
    wasi.start(instance)                      // Start the WASI instance
    let stdout = await wasmFs.getStdOut()     // Get the contents of stdout
    document.write(`Standard Output: ${stdout}`) // Write stdout data to the DOM
  }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Everything starts here
startWasiTask(wasmFilePath)

<|start_filename|>integrations/shared/rust/Makefile<|end_filename|>
%.wasm: %.rs
	rustc $^ -o $@ --target=wasm32-unknown-unknown

all: add.wasm passing-data.wasm host-functions.wasm handling-errors.wasm exit-early.wasm


<|start_filename|>integrations/js/wasi/server/examples/transforming-modules/index.js<|end_filename|>
const fs  = require("fs")
const { WASI } = require("@wasmer/wasi")
let nodeBindings = require("@wasmer/wasi/lib/bindings/node");
const { lowerI64Imports } = require("@wasmer/wasm-transformer")

nodeBindings = nodeBindings.default || nodeBindings;

const wasmFilePath = "../../../../../shared/wat/wasi/clocktimeget.wasm";

// Instantiate a new WASI Instance
let wasi = new WASI({
  args: [wasmFilePath],
  env: {},
  bindings: {
    ...nodeBindings,
    fs: fs
  }
})

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Async function to run our Wasm module/instance
const startWasiTask =
  async pathToWasmFile => {
    // Fetch the Wasm module and transform its interface
    let wasmBytes        = new Uint8Array(fs.readFileSync(pathToWasmFile))
    let loweredWasmBytes = lowerI64Imports(wasmBytes)

    // Instantiate the WebAssembly file
    let wasmModule = await WebAssembly.compile(loweredWasmBytes);
    let instance = await WebAssembly.instantiate(wasmModule, {
      ...wasi.getImports(wasmModule)
    });
  
    // Start the WASI instance
    wasi.start(instance)
  }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Everything starts here
startWasiTask(wasmFilePath)
