==> tests/builder.c <==1/* The encoders and the section layout of a small module. */
2#include "mesh/check.h"
3#include "wasm/builder.h"
4
5#include <string.h>
6
7int main(void) {
8 wasm_buf b = {0};
9 wb_u32(&b, 624485);
10 CHECK_U(b.len, 3);
11 CHECK_U(b.data[0], 0xe5);
12 CHECK_U(b.data[2], 0x26);
13 b.len = 0;
14 wb_s32(&b, -123456);
15 CHECK_U(b.len, 3);
16 CHECK_U(b.data[0], 0xc0);
17 CHECK_U(b.data[1], 0xbb);
18 CHECK_U(b.data[2], 0x78);
19 b.len = 0;
20 wb_s64(&b, INT64_MIN);
21 CHECK_U(b.len, 10);
22 CHECK_U(b.data[9], 0x7f);
23 b.len = 0;
24 wb_s32(&b, 64);
25 CHECK_U(b.len, 2);
26 wasm_buf_free(&b);
27
28 /* (module (type (func (param i32 i32) (result i32))) (func (type 0) local.get 0 local.get 1 i32.add)
29 * (export "add" (func 0))) as wat2wasm writes it. */
30 static const uint8_t want[] = {0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60, 0x02,
31 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61,
32 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01,
33 0x6a, 0x0b};
34 wasm_builder w = {0};
35 uint32_t t = wasm_builder_type(&w, "ii>i"), f;
36 wasm_buf *body = wasm_builder_func_add(&w, t, "", &f);
37 wb_op_u32(body, 0x20, 0);
38 wb_op_u32(body, 0x20, 1);
39 wb_op(body, 0x6a);
40 wasm_builder_add_export(&w, "add", WASM_EXTERN_FUNC, f);
41 wasm_buf out;
42 wasm_builder_finish(&w, &out);
43 CHECK_U(out.len, sizeof want);
44 CHECK(out.len == sizeof want && memcmp(out.data, want, sizeof want) == 0);
45 wasm_buf_free(&out);
46 wasm_builder_free(&w);
47 return mesh_check_done();
48}