summaryrefslogtreecommitdiff
path: root/README.rst
blob: 446e58d8c8e8d9394bfaaa3a3071ed59527545c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
.. image:: https://travis-ci.org/WebAssembly/sexpr-wasm-prototype.svg?branch=master
    :target: https://travis-ci.org/WebAssembly/sexpr-wasm-prototype
    :alt: Build Status

sexpr-wasm
==========

Translates from WebAssembly `s-expressions
<https://github.com/WebAssembly/spec>`_ to `v8-native-prototype
<https://github.com/WebAssembly/v8-native-prototype>`_ bytecode.

Cloning
-------

Clone as normal, but don't forget to update/init submodules as well::

  $ git clone https://github.com/WebAssembly/sexpr-wasm-prototype
  $ git submodule update --init

This will fetch the v8-native-prototype repo, which is needed for some tests.

Building
--------

Building just sexpr-wasm::

  $ make
  mkdir out
  cc -Wall -Werror -g -c -o out/sexpr-wasm.o src/sexpr-wasm.c
  cc -Wall -Werror -g -c -o out/wasm-parse.o src/wasm-parse.c
  cc -Wall -Werror -g -c -o out/wasm-gen.o src/wasm-gen.c
  cc -o out/sexpr-wasm out/sexpr-wasm.o out/wasm-parse.o out/wasm-gen.o

If you make changes to src/hash.txt, you'll need to install gperf as well. On
Debian-based systems::

  $ sudo apt-get install gperf
  ...
  $ touch src/hash.txt
  $ make
  gperf --compare-strncmp --readonly-tables --struct-type src/hash.txt --output-file src/hash.h
  ...

Building v8-native-prototype
----------------------------

The v8-native-prototype lets you run the generated bytecode. Some of the tests
rely on this repo. To build it::

  $ scripts/build-d8.sh
  ...

When it is finished, there will be a d8 executable in the
``third_party/v8-native-prototype/v8/v8/out/Release`` directory.

Running
-------

First write some WebAssembly s-expressions::

  $ cat > test.wasm << HERE
  (module
    (export "test" 0)
    (func (result i32)
      (i32.add (i32.const 1) (i32.const 2))))
  HERE

Then run sexpr-wasm to build v8-native-prototype bytecode::

  $ out/sexpr-wasm test.wasm -o test.bin

This can be loaded into d8 using JavaScript like this::

  $ third_party/v8-native-prototype/v8/v8/out/Release/d8
  V8 version 4.7.0 (candidate)
  d8> buffer = readbuffer('test.bin');
  [object ArrayBuffer]
  d8> module = WASM.instantiateModule(buffer, {});
  {memory: [object ArrayBuffer], test: function test() { [native code] }}
  d8> module.test()
  3

If you just want to run a quick test, you can use the run-d8.py script instead::

  $ test/run-d8.py test.wasm
  test() = 3

Tests
-----

To run tests::

  $ make test
  [+251|-0|%100] (0.55s)

In this case, there were 251 passed tests and no failed tests, which took .55
seconds to run.

You can also run the Python test runner script directly::

  $ test/run-tests.py
  [+251|-0|%100] (0.40s)

  $ test/run-tests.py -v
  + bad-string-escape.txt (0.002s)
  + bad-string-hex-escape.txt (0.004s)
  + bad-string-eof.txt (0.005s)
  + bad-toplevel.txt (0.003s)
  ...

To run a subset of the tests, use a glob-like syntax::

  $ test/run-tests.py const -v
  + dump/const.txt (0.003s)
  + expr/bad-const-i32-garbage.txt (0.002s)
  + expr/bad-const-f32-trailing.txt (0.004s)
  + expr/bad-const-i32-overflow.txt (0.003s)
  + expr/bad-const-i32-trailing.txt (0.002s)
  + expr/bad-const-i32-just-negative-sign.txt (0.003s)
  + expr/const.txt (0.003s)
  + expr/bad-const-i32-underflow.txt (0.004s)
  + expr/bad-const-i64-overflow.txt (0.005s)
  [+9|-0|%100] (0.02s)

  $ test/run-tests.py expr*const*i32 -v
  + expr/bad-const-i32-garbage.txt (0.004s)
  + expr/bad-const-i32-overflow.txt (0.002s)
  + expr/bad-const-i32-trailing.txt (0.002s)
  + expr/bad-const-i32-just-negative-sign.txt (0.002s)
  + expr/bad-const-i32-underflow.txt (0.002s)
  [+5|-0|%100] (0.01s)