Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: vite example with c stdout (pre-compiled) #454

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

enghitalo
Copy link

c

#include <stdio.h>

int main()
{
    printf("Hello from C");
    return 0;
}

Vite main.ts

import "./style.css";
import { init, runWasix } from "@wasmer/sdk";
import helloUrl from "./hello.wasm?url";

document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
  <div>
    <div class="card">
      <button id="message-button" type="button">Click</button>
      <div id="message" type="text"/>
    </div>
  </div>
`;

async function initialize() {
  await init();
  return WebAssembly.compileStreaming(fetch(helloUrl));
}

async function runWasm(module: WebAssembly.Module) {
  const instance = await runWasix(module, {});

  const result = await instance.wait();
  return result.ok ? result.stdout : null;
}

async function main() {
  const module = await initialize();

  document
    .getElementById("message-button")
    .addEventListener("click", async () => {
      const stdout = await runWasm(module);
      console.log(stdout);
      document.getElementById("message").innerText = stdout;
    });
}

main();

@enghitalo enghitalo changed the title examples: vite example with c stdout examples: vite example with c stdout (pre-compiled) Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant