Skip to content

Comments on wat_generate_guide.md #1

@lum1n0us

Description

@lum1n0us

## 1. Module Structure Template

A module may contain more sections than those listed here, depending on the requirements. A module with all sections might look like:

(module
  ;; Type Section
  (type $add_type (func (param i32 i32) (result i32)))

  ;; Import Section
  (import "env" "external_func" (func $external_func (param i32) (result i32)))

  ;; Function Section
  (func $add (type $add_type) (param $a i32) (param $b i32) (result i32)
    local.get $a
    local.get $b
    i32.add
  )

  ;; Table Section
  (table $table (export "table") 1 funcref)

  ;; Memory Section
  (memory $memory (export "memory") 1)

  ;; Global Section
  (global $global_var (mut i32) (i32.const 42))
  (export "global_var" (global $global_var))

  ;; Export Section
  (export "add" (func $add))

  ;; Element Section
  (elem (i32.const 0) $add)

  ;; Data Section
  (data (i32.const 0) "Hello, WebAssembly!")
)

And several sections may take different forms when targeting various proposals.

You might want to refer to https://webassembly.github.io/spec/core/text/index.html for an official definition, and https://github.com/WebAssembly/proposals for a quick overview of the proposals.


### 2. Memory Declaration Patterns

Only one thing should be noted: there are several proposals that will impact the memory text form in WAT, such as memory64, multi-memory, and shared. If we consider the standard specification more closely, there will be different forms and combinations. One pattern cannot cover all scenarios. There should be several options to choose from based on the targeted proposals.


## When to Generate WAT Files

Personally, the requirement for a WAT involves needing a WASM as a stub, and situations might include, but are not limited to:

  • If the case is triggered by a special opcode
  • If the case is triggered by a special section of a module
  • If the case is triggered by a regular WASM

Otherwise, even if a case involves error conditions or edge cases, a WAT should not be required.


Directory Structure

Also requires a CMakeLists.txt in wasm-apps to compile c/c++ into Wasm.

We're creating a PR to reorganize existing unit test cases. Please refer to the structure in running-mode within this PR.

By the way, we are uncertain whether a BKM or a guide on how to write unit tests can assist AI or not. If yes, I will create one and upload it to this PR.


Standard Test Integration Pattern

There should be Dtors(or teardown()) to release resources allocated by load_wasm_file() and init_exec_env().


CMakeLists.txt Integration

If following above PR, we can simply install a wasm to specific destination.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions