Skip to content

SerialHooker/WASM2JS-Transpiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AI_BOOSTED_WASM2JS

A C++ tool that converts WebAssembly (.wasm) binaries to JavaScript, with an optional AI-powered pass using the Gemini API to rename variables and summarize functions.

Features

  • Transpiles .wasm binary files to readable JavaScript (IIFE or ESM format)
  • Reconstructs high-level control flow: if/else, while, do/while, for loops
  • Resolves WebAssembly imports, exports, memories, tables, globals, and data segments
  • AI mode (optional): uses Gemini to:
    • Rename obscure local variables to meaningful camelCase names
    • Generate one-sentence summaries for each function
    • Propose human-readable function names

Dependencies

All dependencies are fetched automatically via CMake FetchContent:

Library Version Purpose
wabt 1.0.37 WebAssembly binary parsing
nlohmann/json v3.11.3 JSON serialization for API requests
cpp-httplib v0.15.3 HTTPS client for Gemini API
OpenSSL system TLS support for HTTPS

Building

Requirements

  • CMake ≥ 3.28
  • C++23-compatible compiler (MSVC 2022 or GCC 13+)
  • OpenSSL (system-installed)

On Windows (MSVC)

cmake -B build -S . -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release

On Linux/macOS (GCC/Clang)

cmake -B build -S . -DCMAKE_BUILD_TYPE=Release
cmake --build build

Usage

Without AI (fast)

Set the GEMINI_API_KEY environment variable (not required for non-AI mode):
./WASM2JS
> enter absolute path of wasm: /path/to/module.wasm
> enter filename of the output file: output.js
> Do you want to use AI to summarize and rename the code? (y/n): n

The converted JavaScript will be saved to ./output/output.js.

With AI (requires Gemini API key)

Set the GEMINI_API_KEY environment variable before running:

Windows (PowerShell):

$env:GEMINI_API_KEY = "your_api_key_here"
.\WASM2JS.exe

Linux/macOS:

export GEMINI_API_KEY="your_api_key_here"
./WASM2JS

Then run the tool and answer y when prompted for AI mode.

Note: You can get a free Gemini API key from Google AI Studio.

Output Format

The tool outputs JavaScript in IIFE format (Immediately Invoked Function Expression):

const imports = { /* ... */ };

(function() {
  // Helper functions (i64, f32, f64, rotl32, rotr32, ...)
  // Memory and table initialization
  // Global variables
  // Transpiled WebAssembly functions
  return {
    exportedFunc1: func42,
    exportedFunc2: func7,
  };
})();

Project Structure

WASM2JS/
├── main.cpp        # Entry point: CLI prompts, calls Parser
├── Parser.h        # Parser class declaration, types, enums
├── Parser.cpp      # Core transpilation logic
├── Utils.h         # Utility functions (string ops, Gemini API client)
├── Utils.cpp       # Utility function implementations
├── pch.h           # Precompiled header (Windows/WinSock definitions)
└── CMakeLists.txt  # Build configuration

License

MIT License. See LICENSE for details.

About

C++ tool that converts WebAssembly (`.wasm`) binaries to JavaScript, with an optional AI-powered pass using the Gemini API to rename variables and summarize functions.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors