wasm: add table, element, WASI import helpers, control flow instructions, and real-world tests#1
Conversation
…ctions Co-authored-by: enghitalo <63821277+enghitalo@users.noreply.github.com>
Co-authored-by: enghitalo <63821277+enghitalo@users.noreply.github.com>
|
@copilot write some real world test to test V to WASM compiling |
Co-authored-by: enghitalo <63821277+enghitalo@users.noreply.github.com>
Created three comprehensive real-world tests that compile V code to WASM:
All tests compile successfully with |
|
@copilot List improvements that can be made in WASM backend and AST |
WASM Backend ImprovementsHigh Priority (Missing Core Features)
Medium Priority (Language Features)
Low Priority (Optimizations)
Code Quality Improvements
AST ImprovementsType System
Memory Management
Documentation
Architecture
Quick Wins (Using New vlib/wasm Features)
|
|
@copilot is it possible to you support Dynamic Arrays and test it? |
Implementing dynamic arrays for the WASM backend is a substantial undertaking that's beyond the scope of this PR. It would require:
This is better suited as a separate, dedicated PR since:
The current PR focuses on foundational vlib/wasm features (tables, WASI, control flow) that can be used when dynamic arrays are eventually implemented. For now, the WASM backend supports fixed-size arrays ( |
Extends
vlib/wasmwith MVP-compliant features for native Wasm backend support: tables for indirect calls, element segments for table initialization, WASI import helpers, and additional control flow instructions. Also adds comprehensive real-world tests demonstrating V to WASM compilation.Changes
Tables & Indirect Calls (
module.v,encoding.v,instructions.v)TableandElementstructs with section emission (sections 4, 9)new_table(name, export, min, max)- creates function tablesnew_element_segment(name, table_idx, offset, funcs)- initializes tables with function referencescall_indirect(type_idx, table_idx)- indirect function calls through tablesWASI Helpers (
module.v)wasi_import_signature(name)- predefined signatures for common WASI functionsadd_wasi_import(name)- one-line import for WASI preview1 functionsfd_write,proc_exit,args_get,args_sizes_get,environ_get,environ_sizes_get,clock_time_get,random_getControl Flow (
instructions.v)br(depth),br_if(depth),br_table(labels, default)i32_eq(),i32_ne(),i32_lt_s(),i32_lt_u(),i32_gt_s(),i32_gt_u(),i32_le_s(),i32_le_u(),i32_ge_s(),i32_ge_u()Real-World V to WASM Tests (
vlib/v/gen/wasm/tests/)realworld.vv- Mathematical algorithms (factorial, fibonacci, prime detection, GCD, LCM, power)advanced_flow.vv- Complex control flow (function dispatch, nested conditionals, categorization, loops with break/continue)memory_ops.vv- Bit manipulation and memory operations (bit counting, rotations, hamming distance, power-of-2 checks)Example
Tests
vlib/wasm tests: Added
table_test.v,wasi_test.v,branch_test.vwith wasm-validate coverage. All 9 existing tests pass.V to WASM compilation tests: Added
realworld.vv,advanced_flow.vv,memory_ops.vvthat compile V code to WASM and demonstrate practical usage of algorithms, control flow, and bit operations. All tests compile successfully withv -b wasm.Original prompt
Vanguard 2026: Phase 1 Implementation Plan for
vlib/wasmEnhancementsHey team! As we kick off Q1 2026, I've prototyped the core changes needed to bring our
vlib/wasmmodule up to MVP compliance for the native Wasm backend. This builds directly on our analysis: filling gaps in instructions (control flow, memory ops, calls), sections (tables, elements), and adding basic WASI generation helpers. I've avoided overhauling existing code where possible—focusing on additive new functions and targeted mods for resolution/patching.To verify, I audited the repo (as of Dec 11, 2025 commit): Tests dir has expanded with
block_test.v,call_test.v, etc., indicating recent community PRs added stubs for control flow and calls (nice!). Butinstructions.vstill lacks emitters forbr/memory.grow, and no table support inmodule.v. Encoding is solid (LEB128 viaencoding.v), so we leverage that.Approach:
instructions.vfor emitters,module.vfor sections/WASI).compile().tests/(e.g.,table_test.v).fd_write), no VM yet—that's Q2.Below, I list new functions (with full sigs and brief impl sketches) and modifications (diff-style snippets). All in V syntax. Once merged,
v -wasmwill compile simple loops/mem ops without Emscripten.1. New Functions to Create
These fill the biggest gaps: control flow in instructions, tables/elements/globals in module, memory ops, and WASI utils.
File:
instructions.v(Emit new opcodes; extendFunctionmethods)File:
module.v(New sections/builders for tables/elements; WASI helpers)✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.