Skip to content

corani/qjs-wasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

qjs-wasm

Quick demo showing QuickJS embedded in Go, with a small WebAssembly helper.

This repository demonstrates using fastschema/qjs to run JavaScript handlers inside Go HTTP routes, and a tiny wazero-backed WebAssembly module for an add function.

The main goal of this experiment is to see how we can call functions exported by other WASM modules from the JavaScript that is executed by QuickJS.

Highlights

  • Exposes JS module functions from server/handlers.js as HTTP endpoints.
  • Uses a pool of QuickJS runtimes for concurrency.
  • Builds a small WebAssembly module (server/add/main.wasm) and uses wazero to call it from Go.

Prerequisites

  • Go 1.25+ (confirm with go version)
  • bash for running build.sh (or run the commands manually)

Getting started

  1. Build the project and the WASM module:

    ./build.sh
  2. Run the server:

    # server listens on :8080
    ./bin/server
  3. Try the endpoints in your browser or with curl:

    curl http://localhost:8080/about
    curl http://localhost:8080/contact
    curl http://localhost:8080/hostname
    # Add with query parameters x and y
    curl "http://localhost:8080/add?x=5&y=7"

Project layout

  • build.sh — convenience script that builds the WASM helper and server binary.
  • server/ — main server code.
    • main.go — boots QuickJS, precompiles handlers.js, creates a runtime pool and registers HTTP routes.
    • handlers.js — JavaScript module that exports handler functions used as HTTP endpoints.
    • add.go — Go glue that loads server/add/main.wasm into wazero and exposes Add to QuickJS.
    • add/main.go — small Go file compiled to WASM; exports add.
  • bin/ — output directory for the built server binary (bin/server).
  • .cache/wazero cache directory (created automatically).

Notes & troubleshooting

  • The server precompiles handlers.js and places exported functions under the global handlers object. Each exported function becomes an HTTP route at /<functionName>.
  • Handler functions that expect arguments receive a single map built from URL query parameters. For example, the add handler expects x and y.
  • The WASM module server/add/main.wasm is embedded into the Go binary using //go:embed in server/add.go. Rebuild with build.sh if you change server/add/main.go.

Contact

If you have questions about this demo, contact the repository owner.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published