@grain/binaryen.ml 0.8.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. binaryen-archive/.gitignore +1 -0
  2. binaryen-archive/CHANGELOG.md +93 -0
  3. binaryen-archive/README.md +29 -11
  4. binaryen-archive/binaryen.opam +8 -8
  5. binaryen-archive/dune-project +1 -1
  6. binaryen-archive/dune-workspace +1 -1
  7. binaryen-archive/esy.lock/index.json +537 -460
  8. binaryen-archive/esy.lock/opam/{base.v0.14.1 → base.v0.14.2}/opam +4 -4
  9. binaryen-archive/esy.lock/opam/biniou.1.2.1/opam +1 -1
  10. binaryen-archive/esy.lock/opam/{cppo.1.6.7 → cppo.1.6.8}/opam +4 -6
  11. binaryen-archive/esy.lock/opam/{csexp.1.4.0 → csexp.1.5.1}/opam +11 -9
  12. binaryen-archive/esy.lock/opam/{dune-build-info.2.8.2 → dune-build-info.2.9.1}/opam +5 -5
  13. binaryen-archive/esy.lock/opam/{dune-configurator.2.8.2 → dune-configurator.2.9.1}/opam +5 -5
  14. binaryen-archive/esy.lock/opam/{dune.2.8.2 → dune.2.9.1}/opam +4 -4
  15. binaryen-archive/esy.lock/opam/easy-format.1.3.2/opam +1 -1
  16. binaryen-archive/esy.lock/opam/{fix.20201120 → fix.20211231}/opam +7 -5
  17. binaryen-archive/esy.lock/opam/js_of_ocaml-compiler.3.11.0/opam +54 -0
  18. binaryen-archive/esy.lock/opam/js_of_ocaml-ppx.3.11.0/opam +44 -0
  19. binaryen-archive/esy.lock/opam/js_of_ocaml.3.11.0/opam +45 -0
  20. binaryen-archive/esy.lock/opam/{menhir.20201216 → menhir.20211128}/opam +8 -6
  21. binaryen-archive/esy.lock/opam/{menhirLib.20201216 → menhirLib.20211128}/opam +8 -6
  22. binaryen-archive/esy.lock/opam/{menhirSdk.20201216 → menhirSdk.20211128}/opam +8 -6
  23. binaryen-archive/esy.lock/opam/ocaml-compiler-libs.v0.12.4/opam +39 -0
  24. binaryen-archive/esy.lock/opam/{ocaml-lsp-server.1.4.0 → ocaml-lsp-server.1.9.0}/opam +15 -12
  25. binaryen-archive/esy.lock/opam/{ocaml-migrate-parsetree.2.1.0 → ocaml-migrate-parsetree.2.3.0}/opam +13 -10
  26. binaryen-archive/esy.lock/opam/ocaml-version.3.4.0/opam +49 -0
  27. binaryen-archive/esy.lock/opam/ocamlbuild.0.14.0/opam +1 -1
  28. binaryen-archive/esy.lock/opam/{ocamlfind.1.8.1 → ocamlfind.1.9.1}/opam +19 -39
  29. binaryen-archive/esy.lock/opam/ocamlformat-rpc-lib.0.19.0/opam +40 -0
  30. binaryen-archive/esy.lock/opam/ocamlformat.0.18.0/opam +56 -0
  31. binaryen-archive/esy.lock/opam/{odoc.1.5.2 → odoc.1.5.3}/opam +12 -12
  32. binaryen-archive/esy.lock/opam/pp.1.1.2/opam +58 -0
  33. binaryen-archive/esy.lock/opam/ppx_derivers.1.2.1/opam +1 -1
  34. binaryen-archive/esy.lock/opam/ppx_yojson_conv_lib.v0.14.0/opam +2 -2
  35. binaryen-archive/esy.lock/opam/{ppxlib.0.21.0 → ppxlib.0.22.2}/opam +40 -32
  36. binaryen-archive/esy.lock/opam/{re.1.9.0 → re.1.10.3}/opam +8 -4
  37. binaryen-archive/esy.lock/opam/result.1.5/opam +2 -2
  38. binaryen-archive/esy.lock/opam/sexplib0.v0.14.0/opam +2 -2
  39. binaryen-archive/esy.lock/opam/spawn.v0.15.0/opam +56 -0
  40. binaryen-archive/esy.lock/opam/stdio.v0.14.0/opam +2 -2
  41. binaryen-archive/esy.lock/opam/{topkg.1.0.3 → topkg.1.0.4}/opam +18 -22
  42. binaryen-archive/esy.lock/opam/tyxml.4.5.0/opam +42 -0
  43. binaryen-archive/esy.lock/opam/uucp.14.0.0/opam +41 -0
  44. binaryen-archive/esy.lock/opam/{uuseg.13.0.0 → uuseg.14.0.0}/opam +22 -28
  45. binaryen-archive/esy.lock/opam/yojson.1.7.0/opam +1 -1
  46. binaryen-archive/esy.lock/overrides/{opam__s__ocamlfind_opam__c__1.8.1_opam_override/files/findlib-1.8.1.patch → opam__s__ocamlfind_opam__c__1.9.1_opam_override/files/findlib-1.9.1.patch} +0 -0
  47. binaryen-archive/esy.lock/overrides/{opam__s__ocamlfind_opam__c__1.8.1_opam_override → opam__s__ocamlfind_opam__c__1.9.1_opam_override}/package.json +1 -1
  48. binaryen-archive/js/binaryen.es5.js +5 -0
  49. binaryen-archive/js/dune +4 -6
  50. binaryen-archive/js/element_segment.ml +33 -0
  51. binaryen-archive/js/export.ml +37 -0
  52. binaryen-archive/js/expression.ml +935 -178
  53. binaryen-archive/js/function.ml +31 -0
  54. binaryen-archive/js/global.ml +29 -0
  55. binaryen-archive/js/import.ml +24 -0
  56. binaryen-archive/js/literal.ml +8 -4
  57. binaryen-archive/js/module.ml +40 -64
  58. binaryen-archive/js/op.ml +9 -78
  59. binaryen-archive/js/settings.ml +60 -0
  60. binaryen-archive/js/stubs.c +15 -0
  61. binaryen-archive/js/table.ml +29 -0
  62. binaryen-archive/js/type.ml +3 -0
  63. binaryen-archive/package.json +15 -21
  64. binaryen-archive/src/binaryen_stubs_exports.c +78 -0
  65. binaryen-archive/src/binaryen_stubs_expressions.c +1300 -0
  66. binaryen-archive/src/binaryen_stubs_features.c +7 -0
  67. binaryen-archive/src/binaryen_stubs_functions.c +78 -0
  68. binaryen-archive/src/binaryen_stubs_globals.c +63 -0
  69. binaryen-archive/src/binaryen_stubs_imports.c +29 -0
  70. binaryen-archive/src/binaryen_stubs_memory.c +1 -1
  71. binaryen-archive/src/binaryen_stubs_modules.c +0 -122
  72. binaryen-archive/src/binaryen_stubs_ops.c +19 -173
  73. binaryen-archive/src/binaryen_stubs_settings.c +145 -0
  74. binaryen-archive/src/binaryen_stubs_tables.c +145 -0
  75. binaryen-archive/src/binaryen_stubs_types.c +18 -0
  76. binaryen-archive/src/dune +5 -43
  77. binaryen-archive/src/element_segment.ml +13 -0
  78. binaryen-archive/src/export.ml +33 -0
  79. binaryen-archive/src/expression.ml +808 -73
  80. binaryen-archive/src/function.ml +18 -0
  81. binaryen-archive/src/global.ml +14 -0
  82. binaryen-archive/src/import.ml +12 -0
  83. binaryen-archive/src/literal.ml +2 -2
  84. binaryen-archive/src/module.ml +62 -38
  85. binaryen-archive/src/ocaml_helpers.c +0 -8
  86. binaryen-archive/src/ocaml_helpers.h +2 -3
  87. binaryen-archive/src/op.ml +18 -131
  88. binaryen-archive/src/settings.ml +45 -0
  89. binaryen-archive/src/table.ml +26 -0
  90. binaryen-archive/src/type.ml +2 -0
  91. binaryen-archive/test/config/dune +8 -0
  92. binaryen-archive/test/config/ocamlopt_flags.ml +16 -0
  93. binaryen-archive/test/dune +6 -9
  94. binaryen-archive/test/test.expected +76 -0
  95. binaryen-archive/test/test.ml +110 -8
  96. binaryen-archive/virtual/dune +2 -2
  97. binaryen-archive/virtual/element_segment.mli +11 -0
  98. binaryen-archive/virtual/export.mli +22 -0
  99. binaryen-archive/virtual/expression.mli +389 -33
  100. binaryen-archive/virtual/function.mli +16 -0
  101. binaryen-archive/virtual/global.mli +12 -0
  102. binaryen-archive/virtual/import.mli +8 -0
  103. binaryen-archive/virtual/literal.mli +2 -2
  104. binaryen-archive/virtual/module.mli +25 -27
  105. binaryen-archive/virtual/op.mli +9 -53
  106. binaryen-archive/virtual/settings.mli +35 -0
  107. binaryen-archive/virtual/table.mli +19 -0
  108. binaryen-archive/virtual/type.mli +2 -0
  109. binaryen-archive/esy.lock/opam/conf-m4.1/opam +0 -22
  110. binaryen-archive/esy.lock/opam/dot-merlin-reader.4.1/opam +0 -30
  111. binaryen-archive/esy.lock/opam/js_of_ocaml-compiler.3.9.1/opam +0 -40
  112. binaryen-archive/esy.lock/opam/js_of_ocaml-ppx.3.9.0/opam +0 -30
  113. binaryen-archive/esy.lock/opam/js_of_ocaml.3.9.0/opam +0 -31
  114. binaryen-archive/esy.lock/opam/ocaml-compiler-libs.v0.12.3/opam +0 -29
  115. binaryen-archive/esy.lock/opam/ocamlfind.1.8.1/files/ocaml-stub +0 -4
  116. binaryen-archive/esy.lock/opam/ocamlfind.1.8.1/files/ocamlfind.install +0 -6
  117. binaryen-archive/esy.lock/opam/ocamlformat.0.15.1/opam +0 -41
  118. binaryen-archive/esy.lock/opam/tyxml.4.4.0/opam +0 -47
  119. binaryen-archive/esy.lock/opam/uucp.13.0.0/opam +0 -44
  120. binaryen-archive/esy.lock/overrides/opam__s__conf_m4_opam__c__1_opam_override/package.json +0 -6
  121. binaryen-archive/js/features.ml +0 -27
  122. binaryen-archive/js/function_table.ml +0 -11
  123. binaryen-archive/src/binaryen_stubs_function_tables.c +0 -25
  124. binaryen-archive/src/config/dune +0 -8
  125. binaryen-archive/src/config/library_flags.ml +0 -17
  126. binaryen-archive/src/features.ml +0 -49
  127. binaryen-archive/src/function_table.ml +0 -4
  128. binaryen-archive/vendor/binaryen-c.h +0 -2647
  129. binaryen-archive/vendor/js/index.es5.js +0 -5
  130. binaryen-archive/vendor/linux/libbinaryen.a +0 -0
  131. binaryen-archive/vendor/linux/libbinaryen.so +0 -0
  132. binaryen-archive/vendor/macosx/libbinaryen.a +0 -0
  133. binaryen-archive/vendor/macosx/libbinaryen.dylib +0 -0
  134. binaryen-archive/vendor/mingw64/libbinaryen.a +0 -0
  135. binaryen-archive/vendor/mingw64/libbinaryen.dll +0 -0
  136. binaryen-archive/vendor/wasm-delegations.h +0 -85
  137. binaryen-archive/virtual/features.mli +0 -23
  138. binaryen-archive/virtual/function_table.mli +0 -2
@@ -1,10 +1,24 @@
1
1
  (module
2
2
  (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
3
+ (type $none_=>_none (func))
4
+ (type $externref_=>_i32 (func (param externref) (result i32)))
5
+ (type $externref_i32_i32_=>_i32 (func (param externref i32 i32) (result i32)))
6
+ (import "future-wasi" "write" (func $write (param externref i32 i32) (result i32)))
7
+ (global $max_int64 i64 (i64.const 9223372036854775807))
8
+ (global $test_float64_bits f64 (f64.const 1.23))
3
9
  (memory $0 1)
10
+ (table $table 1 1 funcref)
11
+ (elem $elem (i32.const 0) $adder)
4
12
  (export "adder" (func $adder))
5
13
  (export "memory" (memory $0))
14
+ (export "hello" (func $hello))
15
+ (start $start)
6
16
  (func $adder (param $0 i32) (param $1 i32) (result i32)
7
17
  (block $add (result i32)
18
+ (if
19
+ (i32.const 0)
20
+ (unreachable)
21
+ )
8
22
  (i32.add
9
23
  (select
10
24
  (local.get $0)
@@ -17,12 +31,36 @@
17
31
  )
18
32
  )
19
33
  )
34
+ (func $start
35
+ (drop
36
+ (call_indirect $table (type $i32_i32_=>_i32)
37
+ (i32.const 3)
38
+ (i32.const 5)
39
+ (i32.const 0)
40
+ )
41
+ )
42
+ )
43
+ (func $hello (param $0 externref) (result i32)
44
+ (call $write
45
+ (local.get $0)
46
+ (i32.const 0)
47
+ (i32.const 1)
48
+ )
49
+ )
20
50
  )
21
51
  (module
52
+ (type $none_=>_none (func))
53
+ (type $externref_=>_i32 (func (param externref) (result i32)))
22
54
  (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
55
+ (type $externref_i32_i32_=>_i32 (func (param externref i32 i32) (result i32)))
56
+ (import "future-wasi" "write" (func $write (param externref i32 i32) (result i32)))
23
57
  (memory $0 1)
58
+ (table $table 1 1 funcref)
59
+ (elem $elem (i32.const 0) $adder)
24
60
  (export "adder" (func $adder))
25
61
  (export "memory" (memory $0))
62
+ (export "hello" (func $hello))
63
+ (start $start)
26
64
  (func $adder (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
27
65
  (i32.add
28
66
  (select
@@ -35,12 +73,35 @@
35
73
  (local.get $1)
36
74
  )
37
75
  )
76
+ (func $start (; has Stack IR ;)
77
+ (drop
78
+ (call $adder
79
+ (i32.const 3)
80
+ (i32.const 5)
81
+ )
82
+ )
83
+ )
84
+ (func $hello (; has Stack IR ;) (param $0 externref) (result i32)
85
+ (call $write
86
+ (local.get $0)
87
+ (i32.const 0)
88
+ (i32.const 1)
89
+ )
90
+ )
38
91
  )
39
92
  (module
93
+ (type $none_=>_none (func))
94
+ (type $externref_=>_i32 (func (param externref) (result i32)))
40
95
  (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
96
+ (type $externref_i32_i32_=>_i32 (func (param externref i32 i32) (result i32)))
97
+ (import "future-wasi" "write" (func $fimport$0 (param externref i32 i32) (result i32)))
41
98
  (memory $0 1)
99
+ (table $0 1 1 funcref)
100
+ (elem (i32.const 0) $0)
42
101
  (export "adder" (func $0))
43
102
  (export "memory" (memory $0))
103
+ (export "hello" (func $2))
104
+ (start $1)
44
105
  (func $0 (param $0 i32) (param $1 i32) (result i32)
45
106
  (i32.add
46
107
  (select
@@ -53,4 +114,19 @@
53
114
  (local.get $1)
54
115
  )
55
116
  )
117
+ (func $1
118
+ (drop
119
+ (call $0
120
+ (i32.const 3)
121
+ (i32.const 5)
122
+ )
123
+ )
124
+ )
125
+ (func $2 (param $0 externref) (result i32)
126
+ (call $fimport$0
127
+ (local.get $0)
128
+ (i32.const 0)
129
+ (i32.const 1)
130
+ )
131
+ )
56
132
  )
@@ -1,45 +1,147 @@
1
1
  open Binaryen
2
2
 
3
+ (* Testing colors enable *)
4
+ let _ = assert (Settings.are_colors_enabled () == true)
5
+
6
+ let _ = Settings.set_colors_enabled false
7
+
8
+ let _ = assert (Settings.are_colors_enabled () == false)
9
+
10
+ let _ = Settings.set_colors_enabled true
11
+
12
+ (* Testing debug_info enable *)
13
+ let _ = assert (Settings.get_debug_info () == false)
14
+
15
+ let _ = Settings.set_debug_info true
16
+
17
+ let _ = assert (Settings.get_debug_info () == true)
18
+
19
+ let _ = Settings.set_debug_info false
20
+
21
+ (* Testing low_memory_unused enable *)
22
+ let _ = assert (Settings.get_low_memory_unused () == false)
23
+
24
+ let _ = Settings.set_low_memory_unused true
25
+
26
+ let _ = assert (Settings.get_low_memory_unused () == true)
27
+
28
+ let _ = Settings.set_low_memory_unused false
29
+
3
30
  let wasm_mod = Module.create ()
4
31
 
32
+ let _ = Module.set_features wasm_mod [ Module.Feature.all ]
33
+
5
34
  (* Create function type for i32 (i32, i32) *)
6
- let params = Type.create [| Type.int32; Type.int32 |]
35
+ let params () = Type.create [| Type.int32; Type.int32 |]
7
36
 
8
37
  let results = Type.int32
9
38
 
10
- let x () = Expression.local_get wasm_mod 0 Type.int32
39
+ let x () = Expression.Local_get.make wasm_mod 0 Type.int32
11
40
 
12
- let y () = Expression.local_get wasm_mod 1 Type.int32
41
+ let y () = Expression.Local_get.make wasm_mod 1 Type.int32
13
42
 
14
- let load = Expression.load wasm_mod 1 ~signed:true 0 0 Type.int32 (y ())
43
+ let load = Expression.Load.make wasm_mod 1 ~signed:true 0 0 Type.int32 (y ())
15
44
 
16
- let select = Expression.select wasm_mod (Expression.const wasm_mod (Literal.int32 1l)) (x ()) load
45
+ let select =
46
+ Expression.Select.make wasm_mod
47
+ (Expression.Const.make wasm_mod (Literal.int32 1l))
48
+ (x ()) load
17
49
 
18
- let bin = Expression.binary wasm_mod Op.add_int32 select (y ())
50
+ let bin = Expression.Binary.make wasm_mod Op.add_int32 select (y ())
19
51
 
20
- let add = Expression.block wasm_mod ~return_type:Type.int32 "add" [bin]
52
+ let add =
53
+ Expression.Block.make wasm_mod ~return_type:Type.int32 "add"
54
+ [
55
+ Expression.If.make wasm_mod
56
+ (Expression.Const.make wasm_mod (Literal.int32 0l))
57
+ (Expression.Unreachable.make wasm_mod)
58
+ (Expression.Null.make ());
59
+ bin;
60
+ ]
21
61
 
22
62
  (* Create the add function *)
23
- let adder = Function.add_function wasm_mod "adder" params results [||] add
63
+ let adder = Function.add_function wasm_mod "adder" (params ()) results [||] add
64
+
65
+ let call_adder =
66
+ Expression.Call_indirect.make wasm_mod "table"
67
+ (Expression.Const.make wasm_mod (Literal.int32 0l))
68
+ [
69
+ Expression.Const.make wasm_mod (Literal.int32 3l);
70
+ Expression.Const.make wasm_mod (Literal.int32 5l);
71
+ ]
72
+ (params ()) Type.int32
73
+
74
+ let start =
75
+ Function.add_function wasm_mod "start" Type.none Type.none [||]
76
+ (Expression.Drop.make wasm_mod call_adder)
24
77
 
25
78
  let _ = Export.add_function_export wasm_mod "adder" "adder"
26
79
 
80
+ let _ = Table.add_table wasm_mod "table" 1 1
81
+
82
+ let _ =
83
+ Global.add_global wasm_mod "max_int64" Type.int64 false
84
+ (Expression.Const.make wasm_mod (Literal.int64 Int64.max_int))
85
+
86
+ let _ =
87
+ Global.add_global wasm_mod "test_float64_bits" Type.float64 false
88
+ (Expression.Const.make wasm_mod (Literal.float64_bits 0x3FF3AE147AE147AEL))
89
+
90
+ let _ =
91
+ Table.add_active_element_segment wasm_mod "table" "elem" [ "adder" ]
92
+ (Expression.Const.make wasm_mod (Literal.int32 0l))
93
+
94
+ let _ = Function.set_start wasm_mod start
95
+
27
96
  let _ = Memory.set_memory wasm_mod 1 Memory.unlimited "memory" [] false
28
97
 
98
+ (* Create an imported "write" function i32 (externref, i32, i32) *)
99
+ (* Similar to the example here: https://bytecodealliance.org/articles/reference-types-in-wasmtime *)
100
+
101
+ let _ =
102
+ Import.add_function_import wasm_mod "write" "future-wasi" "write"
103
+ (Type.create [| Type.externref; Type.int32; Type.int32 |])
104
+ Type.int32
105
+
106
+ (* Create a function that calls the imported write function *)
107
+ let _ =
108
+ Function.add_function wasm_mod "hello" Type.externref Type.int32 [||]
109
+ (Expression.Call.make wasm_mod "write"
110
+ [
111
+ Expression.Local_get.make wasm_mod 0 Type.externref;
112
+ Expression.Const.make wasm_mod (Literal.int32 0l);
113
+ Expression.Const.make wasm_mod (Literal.int32 1l);
114
+ ]
115
+ Type.int32)
116
+
117
+ let _ = Export.add_function_export wasm_mod "hello" "hello"
118
+
119
+ (* Finally, we print 3 versions of the module to be checked against test.expected *)
120
+
121
+ (* 1. Print the the module as-is *)
122
+
29
123
  let _ = Module.print wasm_mod
30
124
 
125
+ (* 2. Optimize, then print the module *)
126
+
31
127
  let _ = Module.optimize wasm_mod
32
128
 
33
129
  let _ = Module.print wasm_mod
34
130
 
131
+ (* 3. Copy previous module bytes into new module, validate, and print *)
132
+
35
133
  let byts, _ = Module.write wasm_mod None
36
134
 
37
135
  let new_mod = Module.read byts
38
136
 
137
+ let _ = Module.set_features new_mod [ Module.Feature.all ]
138
+
39
139
  let _ = Module.validate new_mod
40
140
 
41
141
  let _ = Module.print new_mod
42
142
 
143
+ (* Dispose the modules 👋 *)
144
+
43
145
  let _ = Module.dispose wasm_mod
44
146
 
45
147
  let _ = Module.dispose new_mod
@@ -1,6 +1,6 @@
1
1
  (library
2
2
  (name binaryen)
3
3
  (public_name binaryen)
4
- (virtual_modules export expression features function function_table global
5
- import literal memory module op type)
4
+ (virtual_modules element_segment export expression function global import
5
+ literal memory module op settings table type)
6
6
  (default_implementation binaryen.native))
@@ -0,0 +1,11 @@
1
+ type t
2
+
3
+ val get_name : t -> string
4
+
5
+ val get_table : t -> string
6
+
7
+ val get_offset : t -> Expression.t
8
+
9
+ val get_length : t -> int
10
+
11
+ val get_data : t -> int -> string
@@ -8,4 +8,26 @@ val add_memory_export : Module.t -> string -> string -> t
8
8
 
9
9
  val add_global_export : Module.t -> string -> string -> t
10
10
 
11
+ val get_export : Module.t -> string -> t
12
+
11
13
  val remove_export : Module.t -> string -> unit
14
+
15
+ val get_num_exports : Module.t -> int
16
+
17
+ val get_export_by_index : Module.t -> int -> t
18
+
19
+ val get_name : t -> string
20
+
21
+ val get_value : t -> string
22
+
23
+ val external_function : int
24
+
25
+ val external_table : int
26
+
27
+ val external_memory : int
28
+
29
+ val external_global : int
30
+
31
+ val external_event : int
32
+
33
+ val export_get_kind : t -> int