File: printexc.wat

package info (click to toggle)
js-of-ocaml 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (146 lines) | stat: -rw-r--r-- 6,690 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;; Wasm_of_ocaml runtime support
;; http://www.ocsigen.org/js_of_ocaml/
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU Lesser General Public License as published by
;; the Free Software Foundation, with linking exception;
;; either version 2.1 of the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

(module
   (import "fail" "caml_is_special_exception"
      (func $caml_is_special_exception (param (ref eq)) (result i32)))
   (import "ints" "caml_format_int"
      (func $caml_format_int
         (param (ref eq)) (param (ref eq)) (result (ref eq))))

   (type $block (array (mut (ref eq))))
   (type $bytes (array (mut i8)))

   (type $buffer
      (struct
         (field (mut i32))
         (field (ref $bytes))))

   (func $add_char (param $buf (ref $buffer)) (param $c i32)
      (local $pos i32)
      (local $data (ref $bytes))
      (local.set $pos (struct.get $buffer 0 (local.get $buf)))
      (local.set $data (struct.get $buffer 1 (local.get $buf)))
      (if (i32.lt_u (local.get $pos) (array.len (local.get $data)))
         (then
            (array.set $bytes (local.get $data) (local.get $pos) (local.get $c))
            (struct.set $buffer 0 (local.get $buf)
               (i32.add (local.get $pos) (i32.const 1))))))

   (func $add_string (param $buf (ref $buffer)) (param $v (ref eq))
      (local $pos i32) (local $len i32)
      (local $data (ref $bytes))
      (local $s (ref $bytes))
      (local.set $pos (struct.get $buffer 0 (local.get $buf)))
      (local.set $data (struct.get $buffer 1 (local.get $buf)))
      (local.set $s (ref.cast (ref $bytes) (local.get $v)))
      (local.set $len (array.len (local.get $s)))
      (if (i32.gt_u (i32.add (local.get $pos) (local.get $len))
                    (array.len (local.get $data)))
         (then
            (local.set $len
                (i32.sub (array.len (local.get $data)) (local.get $pos)))))
      (array.copy $bytes $bytes
         (local.get $data) (local.get $pos)
         (local.get $s) (i32.const 0)
         (local.get $len))
      (struct.set $buffer 0 (local.get $buf)
         (i32.add (local.get $pos) (local.get $len))))

   (func (export "caml_format_exception") (param (ref eq)) (result (ref eq))
      (local $exn (ref $block))
      (local $buf (ref $buffer))
      (local $v (ref eq))
      (local $bucket (ref $block))
      (local $i i32) (local $len i32)
      (local $s (ref $bytes))
      (local.set $exn (ref.cast (ref $block) (local.get 0)))
      (if (result (ref eq))
          (ref.eq (array.get $block (local.get $exn) (i32.const 0))
                  (ref.i31 (i32.const 0)))
         (then
            (local.set $buf
               (struct.new $buffer
                  (i32.const 0)
                  (array.new $bytes (i32.const 0) (i32.const 256))))
            (call $add_string
               (local.get $buf)
               (array.get $block
                  (ref.cast (ref $block)
                     (array.get $block (local.get $exn) (i32.const 1)))
                  (i32.const 1)))
            (local.set $bucket
               (block $continue (result (ref $block))
                  (block $default
                     (br_if $default
                        (i32.ne (array.len (local.get $exn)) (i32.const 3)))
                     (br_if $default
                        (i32.eqz
                           (call $caml_is_special_exception
                              (array.get $block (local.get $exn) (i32.const 1)))))
                     (local.set $v
                        (array.get $block (local.get $exn) (i32.const 2)))
                     (br_if $default
                        (i32.eqz (ref.test (ref $block) (local.get $v))))
                     (local.set $bucket (ref.cast (ref $block) (local.get $v)))
                     (br_if $default
                        (i32.eqz
                           (ref.eq
                              (array.get $block (local.get $bucket) (i32.const 0))
                              (ref.i31 (i32.const 0)))))
                    (local.set $i (i32.const 1))
                    (br $continue (local.get $bucket)))
                 (local.set $i (i32.const 2))
                 (local.get $exn)))
           (local.set $len (array.len (local.get $bucket)))
           (if (i32.lt_u (local.get $i) (local.get $len))
              (then
                 (call $add_char (local.get $buf) (@char "("))
                 (loop $loop
                    (local.set $v
                       (array.get $block (local.get $bucket) (local.get $i)))
                    (if (ref.test (ref i31) (local.get $v))
                       (then
                           (call $add_string (local.get $buf)
                              (call $caml_format_int
                                  (@string "%d")
                                  (ref.cast (ref i31) (local.get $v)))))
                    (else (if (ref.test (ref $bytes) (local.get $v))
                       (then
                          (call $add_char (local.get $buf) (@char "\""))
                          (call $add_string (local.get $buf) (local.get $v))
                          (call $add_char (local.get $buf) (@char "\"")))
                    (else
                       (call $add_char (local.get $buf) (@char "_"))))))
                    (local.set $i (i32.add (local.get $i) (i32.const 1)))
                    (if (i32.lt_u (local.get $i) (local.get $len))
                       (then
                          (call $add_char (local.get $buf) (@char ","))
                          (call $add_char (local.get $buf) (@char " "))
                          (br $loop))))
                 (call $add_char (local.get $buf) (@char ")"))))
            (local.set $s
               (array.new $bytes (i32.const 0)
                  (struct.get $buffer 0 (local.get $buf))))
            (array.copy $bytes $bytes
               (local.get $s) (i32.const 0)
               (struct.get $buffer 1 (local.get $buf)) (i32.const 0)
               (struct.get $buffer 0 (local.get $buf)))
            (local.get $s))
         (else
            (array.get $block (local.get $exn) (i32.const 1)))))
)