forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.js
More file actions
36 lines (34 loc) · 925 Bytes
/
factorial.js
File metadata and controls
36 lines (34 loc) · 925 Bytes
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
import * as assert from '../assert.js'
import Builder from '../Builder.js'
const b = new Builder();
b.Type().End()
.Import().End()
.Function().End()
.Export()
.Function("fac")
.End()
.Code()
.Function("fac", { params: ["i32"], ret: "i32" })
.GetLocal(0)
.I32Const(0)
.I32Eq()
.If("void", b =>
b.I32Const(1)
.Return()
)
.GetLocal(0)
.GetLocal(0)
.I32Const(1)
.I32Sub()
.Call(0)
.I32Mul()
.Return()
.End()
.End()
const m = new WebAssembly.Module(b.WebAssembly().get());
const fac = (new WebAssembly.Instance(m)).exports.fac;
assert.eq(fac(0), 1);
assert.eq(fac(1), 1);
assert.eq(fac(2), 2);
assert.eq(fac(4), 24);
assert.throws(() => fac(1e7), RangeError, "Maximum call stack size exceeded.");