Skip to content

Commit c2cb700

Browse files
committed
use 5.4 stdlib stuff
1 parent c6b56c6 commit c2cb700

8 files changed

Lines changed: 39 additions & 77 deletions

File tree

src/cmd/cmd_call_graph.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ let get_const_global env id =
9797

9898
let eval_const_instr env stack instr =
9999
match instr.Annotated.raw with
100-
| Binary.I32_const n -> ok @@ Stack.push_i32 stack n
101-
| I64_const n -> ok @@ Stack.push_i64 stack n
102-
| F32_const f -> ok @@ Stack.push_f32 stack f
103-
| F64_const f -> ok @@ Stack.push_f64 stack f
104-
| V128_const f -> ok @@ Stack.push_v128 stack f
105-
| I_binop (nn, op) -> ok @@ eval_ibinop stack nn op
106-
| Ref_null t -> ok @@ Stack.push_ref stack (Concrete_ref.null t)
100+
| Binary.I32_const n -> Result.ok @@ Stack.push_i32 stack n
101+
| I64_const n -> Result.ok @@ Stack.push_i64 stack n
102+
| F32_const f -> Result.ok @@ Stack.push_f32 stack f
103+
| F64_const f -> Result.ok @@ Stack.push_f64 stack f
104+
| V128_const f -> Result.ok @@ Stack.push_v128 stack f
105+
| I_binop (nn, op) -> Result.ok @@ eval_ibinop stack nn op
106+
| Ref_null t -> Result.ok @@ Stack.push_ref stack (Concrete_ref.null t)
107107
| Global_get id ->
108108
let* g = get_const_global env id in
109-
ok @@ Stack.push_i32 stack g
110-
| Ref_func id -> ok @@ Stack.push_i32_of_int stack id
109+
Result.ok @@ Stack.push_i32 stack g
110+
| Ref_func id -> Result.ok @@ Stack.push_i32_of_int stack id
111111
| _ -> assert false
112112

113113
let eval_const env exp =

src/infra/syntax.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
(* Written by the Owi programmers *)
44

55
open Prelude.Result
6-
7-
let[@inline] ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e
8-
9-
let[@inline] ( let+ ) o f = match o with Ok v -> Ok (f v) | Error _ as e -> e
10-
11-
let[@inline] ok v = Ok v
6+
include Syntax
127

138
let[@inline] list_iter f l =
149
let rec aux = function

src/infra/syntax.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ val ( let* ) :
1010
val ( let+ ) :
1111
('a, 'err) Prelude.Result.t -> ('a -> 'b) -> ('b, 'err) Prelude.Result.t
1212

13-
val ok : 'a -> ('a, 'err) Prelude.Result.t
14-
1513
val list_iter :
1614
('a -> (unit, 'err) Prelude.Result.t)
1715
-> 'a list

src/link/link.ml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,20 @@ module Eval_const = struct
111111
(* TODO: binary+const instr *)
112112
let instr env stack instr =
113113
match instr.Annotated.raw with
114-
| Binary.I32_const n -> ok @@ Stack.push_i32 stack n
115-
| I64_const n -> ok @@ Stack.push_i64 stack n
116-
| F32_const f -> ok @@ Stack.push_f32 stack f
117-
| F64_const f -> ok @@ Stack.push_f64 stack f
118-
| V128_const f -> ok @@ Stack.push_v128 stack f
119-
| I_binop (nn, op) -> ok @@ ibinop stack nn op
120-
| Ref_null t -> ok @@ Stack.push_ref stack (Concrete_ref.null t)
114+
| Binary.I32_const n -> Result.ok @@ Stack.push_i32 stack n
115+
| I64_const n -> Result.ok @@ Stack.push_i64 stack n
116+
| F32_const f -> Result.ok @@ Stack.push_f32 stack f
117+
| F64_const f -> Result.ok @@ Stack.push_f64 stack f
118+
| V128_const f -> Result.ok @@ Stack.push_v128 stack f
119+
| I_binop (nn, op) -> Result.ok @@ ibinop stack nn op
120+
| Ref_null t -> Result.ok @@ Stack.push_ref stack (Concrete_ref.null t)
121121
| Ref_func f ->
122122
let* f = Link_env.Build.get_func env f in
123123
let value = Concrete_value.Ref (Func (Some f)) in
124-
ok @@ Stack.push stack value
124+
Result.ok @@ Stack.push stack value
125125
| Global_get id ->
126126
let* g = Link_env.Build.get_const_global env id in
127-
ok @@ Stack.push stack g
127+
Result.ok @@ Stack.push stack g
128128
| _ -> assert false
129129

130130
(* TODO: binary+const expr *)
@@ -222,7 +222,7 @@ let load_memory (ls : 'f State.t)
222222
let eval_memory ls (memory : (Binary.Mem.t, Binary.Mem.Type.limits) Origin.t) :
223223
Concrete_memory.t Result.t =
224224
match memory with
225-
| Local (_label, mem_type) -> ok @@ Concrete_memory.init mem_type
225+
| Local (_label, mem_type) -> Result.ok @@ Concrete_memory.init mem_type
226226
| Imported import -> load_memory ls import
227227

228228
let eval_memories ls env memories =
@@ -255,7 +255,7 @@ let load_table (ls : 'f State.t) (import : Binary.Table.Type.t Origin.imported)
255255
let eval_table ls (table : (Binary.Table.t, Binary.Table.Type.t) Origin.t) :
256256
table Result.t =
257257
match table with
258-
| Local { id = label; typ; _ } -> ok @@ Concrete_table.init ?label typ
258+
| Local { id = label; typ; _ } -> Result.ok @@ Concrete_table.init ?label typ
259259
| Imported import -> load_table ls import
260260

261261
let eval_tables ls env tables =
@@ -294,7 +294,7 @@ let load_func (ls : 'f State.t) (import : Binary.block_type Origin.imported) :
294294

295295
let eval_func ls (finished_env : int) func : func Result.t =
296296
match func with
297-
| Origin.Local func -> ok @@ Kind.wasm func finished_env
297+
| Origin.Local func -> Result.ok @@ Kind.wasm func finished_env
298298
| Imported import -> load_func ls import
299299

300300
let eval_functions ls (finished_env : int) env functions =
@@ -311,7 +311,7 @@ let eval_functions ls (finished_env : int) env functions =
311311
let eval_tag ls (_finished_env : int)
312312
(tag : (Binary.Tag.t, Binary.block_type) Origin.t) : Binary.Tag.t Result.t =
313313
match tag with
314-
| Origin.Local tag -> ok tag
314+
| Origin.Local tag -> Ok tag
315315
| Imported import ->
316316
let (Binary.Bt_raw ((None | Some _), import_typ)) = import.typ in
317317
let* tag =
@@ -375,7 +375,7 @@ let define_data env data =
375375
let length = Int32.of_int @@ String.length data.init in
376376
let* offset = get_i32 offset in
377377
let* v = active_data_expr env ~offset ~length ~mem ~data:id in
378-
ok @@ (v :: init)
378+
Result.ok @@ (v :: init)
379379
| Passive -> Ok init
380380
in
381381
(env, init, succ id) )
@@ -410,7 +410,8 @@ let define_elem env elem =
410410
let length = Int32.of_int @@ List.length init in
411411
let* offset = Eval_const.expr env offset in
412412
let* offset = get_i32 offset in
413-
ok @@ (active_elem_expr ~offset ~length ~table ~elem:i :: inits)
413+
Result.ok
414+
@@ (active_elem_expr ~offset ~length ~table ~elem:i :: inits)
414415
| Passive | Declarative -> Ok inits
415416
in
416417
(env, inits, succ i) )

src/link/link_env.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ module Build = struct
9999
let get_const_global (env : t) id =
100100
let* g = get_global env id in
101101
match g.mut with
102-
| Const -> ok g.value
102+
| Const -> Result.ok g.value
103103
| Var -> Error `Constant_expression_required
104104

105105
let get_func (env : t) id =

src/primitives/int32.ml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,6 @@ let extend_s n x =
6060

6161
(* String conversion that allows leading signs and unsigned values *)
6262

63-
(* TODO: replace by Char.Ascii.digit_to_int once on 5.4 *)
64-
let dec_digit = function
65-
| '0' .. '9' as c -> Char.code c - Char.code '0'
66-
| _ -> assert false
67-
[@@inline]
68-
69-
(* TODO: replace by Char.Ascii.hex_digit_to_int once on 5.4 *)
70-
let hex_digit = function
71-
| '0' .. '9' as c -> Char.code c - Char.code '0'
72-
| 'a' .. 'f' as c -> 0xa + Char.code c - Char.code 'a'
73-
| 'A' .. 'F' as c -> 0xa + Char.code c - Char.code 'A'
74-
| _ -> assert false
75-
[@@inline]
76-
7763
let max_upper = unsigned_div minus_one 10l
7864

7965
let max_lower = unsigned_rem minus_one 10l
@@ -95,7 +81,7 @@ let of_string_exn s =
9581
let c = s.[i] in
9682
if Char.equal c '_' then parse_hex (i + 1) num
9783
else begin
98-
let digit = of_int (hex_digit c) in
84+
let digit = of_int (Char.Ascii.hex_digit_to_int c) in
9985
if not (le_u num (lshr minus_one (of_int 4))) then
10086
Fmt.failwith "of_string (int32)"
10187
else parse_hex (i + 1) (logor (shift_left num 4) digit)
@@ -108,7 +94,7 @@ let of_string_exn s =
10894
let c = s.[i] in
10995
if Char.equal c '_' then parse_dec (i + 1) num
11096
else begin
111-
let digit = of_int (dec_digit c) in
97+
let digit = of_int (Char.Ascii.digit_to_int c) in
11298
if not (lt_u num max_upper || (eq num max_upper && le_u digit max_lower))
11399
then Fmt.failwith "of_string (int32)"
114100
else parse_dec (i + 1) (add (mul num 10l) digit)

src/primitives/int64.ml

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,6 @@ let extend_s n x =
7474

7575
(* String conversion that allows leading signs and unsigned values *)
7676

77-
(* TODO: replace by Char.Ascii.digit_to_int once on 5.4 *)
78-
let dec_digit = function
79-
| '0' .. '9' as c -> Char.code c - Char.code '0'
80-
| _ -> assert false
81-
[@@inline]
82-
83-
(* TODO: replace by Char.Ascii.hex_digit_to_int once on 5.4 *)
84-
let hex_digit = function
85-
| '0' .. '9' as c -> Char.code c - Char.code '0'
86-
| 'a' .. 'f' as c -> 0xa + Char.code c - Char.code 'a'
87-
| 'A' .. 'F' as c -> 0xa + Char.code c - Char.code 'A'
88-
| _ -> assert false
89-
[@@inline]
90-
9177
let max_upper = unsigned_div minus_one 10L
9278

9379
let max_lower = unsigned_rem minus_one 10L
@@ -101,7 +87,7 @@ let of_string_exn s =
10187
let c = s.[i] in
10288
if Char.equal c '_' then parse_hex (i + 1) num
10389
else begin
104-
let digit = of_int (hex_digit c) in
90+
let digit = of_int (Char.Ascii.hex_digit_to_int c) in
10591
if not (le_u num (lshr minus_one (of_int 4))) then
10692
Fmt.failwith "of_string (int64)"
10793
else parse_hex (i + 1) (logor (shift_left num 4) digit)
@@ -114,7 +100,7 @@ let of_string_exn s =
114100
let c = s.[i] in
115101
if Char.equal c '_' then parse_dec (i + 1) num
116102
else begin
117-
let digit = of_int (dec_digit c) in
103+
let digit = of_int (Char.Ascii.digit_to_int c) in
118104
if not (lt_u num max_upper || (eq num max_upper && le_u digit max_lower))
119105
then Fmt.failwith "of_string (int64)"
120106
else parse_dec (i + 1) (add (mul num 10L) digit)
@@ -128,17 +114,13 @@ let of_string_exn s =
128114
else parse_dec i zero
129115
in
130116

131-
let parsed =
132-
match s.[0] with
133-
| '+' -> parse_int 1
134-
| '-' ->
135-
let n = parse_int 1 in
136-
if not (le minus_one (sub n one)) then Fmt.failwith "of_string (int64)"
137-
else neg n
138-
| _ -> parse_int 0
139-
in
140-
141-
parsed
117+
match s.[0] with
118+
| '+' -> parse_int 1
119+
| '-' ->
120+
let n = parse_int 1 in
121+
if not (le minus_one (sub n one)) then Fmt.failwith "of_string (int64)"
122+
else neg n
123+
| _ -> parse_int 0
142124

143125
let of_string s = try Some (of_string_exn s) with Failure _ -> None
144126

src/validate/binary_validate.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ end = struct
324324
| Any :: _ -> Ok [ Any ]
325325
| _ :: tl -> Ok tl
326326

327-
let push t stack = ok @@ t @ stack
327+
let push t stack = Result.ok @@ t @ stack
328328

329329
let pop_push modul (Bt_raw (_, (pt, rt)) : block_type) stack =
330330
let pt, rt = (List.rev_map typ_of_pt pt, List.rev_map typ_of_val_type rt) in

0 commit comments

Comments
 (0)