|
| 1 | +(* Copyright (C) 2015-2016 Bloomberg Finance L.P. |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +(** This file will also be exported to external users |
| 31 | + Attention: it should not have any code, all its code will be inlined so that |
| 32 | + there will be never |
| 33 | + {[ require('js')]} |
| 34 | +*) |
| 35 | +external typeof : 'a -> string = "js_typeof" |
| 36 | + |
| 37 | +external to_json_string : 'a -> string = "js_json_stringify" |
| 38 | + |
| 39 | +external log : 'a -> unit = "js_dump" |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +type any = Obj.t |
| 46 | + |
| 47 | +external erase : 'a -> any = "%identity" |
| 48 | +external cast : any -> 'a = "%identity" |
| 49 | + |
| 50 | +(* Note [to_opt null] will be [null : 'a opt opt]*) |
| 51 | + |
| 52 | +module Null = struct |
| 53 | + type + 'a t |
| 54 | + external to_opt : 'a t -> 'a option = "js_from_nullable" |
| 55 | + external return : 'a -> 'a t = "%identity" |
| 56 | + external test : 'a t -> bool = "js_is_nil" |
| 57 | + external empty : 'a t = "null" [@@bs.val] |
| 58 | +end |
| 59 | + |
| 60 | +module Def = struct |
| 61 | + type + 'a t |
| 62 | + external to_opt : 'a t -> 'a option = "js_from_def" |
| 63 | + external return : 'a -> 'a t = "%identity" |
| 64 | + external test : 'a t -> bool = "js_is_undef" |
| 65 | + external empty : 'a t = "undefined" [@@bs.val] |
| 66 | +end |
| 67 | + |
| 68 | +module Null_def = struct |
| 69 | + type + 'a t |
| 70 | + external to_opt : 'a t -> 'a option = "js_from_nullable_def" |
| 71 | + external return : 'a -> 'a t = "%identity" |
| 72 | + external test : 'a t -> bool = "js_is_nil_undef" |
| 73 | + external empty : 'a t = "undefined" [@@bs.val] |
| 74 | +end |
| 75 | + |
| 76 | +type boolean |
| 77 | + |
| 78 | + |
| 79 | +external true_ : boolean = "true" [@@bs.val] |
| 80 | +external false_ : boolean = "false" [@@bs.val] |
| 81 | + |
| 82 | +external to_bool : boolean -> bool = "js_boolean_to_bool" |
| 83 | +external to_number : 'a -> int = "js_anything_to_number" (* + conversion*) |
| 84 | +external string_of_nativeint : nativeint -> string = "js_anything_to_string" |
| 85 | + |
| 86 | +external string_of_char : char -> string = "js_string_of_char" |
| 87 | +(** TODO: check with {!String.of_char} |
| 88 | + it's quite common that we have |
| 89 | + {[ Js.String.of_char x.[0] ]} |
| 90 | + It would be nice to generate code as below |
| 91 | + {[ x[0] |
| 92 | + ]} |
| 93 | +*) |
| 94 | +module String = struct |
| 95 | + external of_char : char -> string = "String.fromCharCode" |
| 96 | + [@@bs.call] |
| 97 | + external toUpperCase : string -> string = "toUpperCase" [@@bs.send] |
| 98 | + external of_int : int -> base:int -> string = "toString" [@@bs.send] |
| 99 | + external of_nativeint : nativeint -> base:int -> string = "toString" [@@bs.send] |
| 100 | + external slice : string -> int -> int -> string = "slice" |
| 101 | + [@@bs.send] |
| 102 | + external slice_rest : string -> int -> string = "slice" |
| 103 | + [@@bs.send] |
| 104 | + external index_of : string -> string -> int = "indexOf" |
| 105 | + [@@bs.send] |
| 106 | + external append : string -> string -> string = "js_string_append" |
| 107 | + external of_small_int_array : int array -> string = "js_string_of_small_int_array" |
| 108 | + external of_small_int32_array : int32 array -> string = "js_string_of_small_int_array" |
| 109 | + external lastIndexOf : string -> string -> int = "lastIndexOf" |
| 110 | + [@@bs.send] |
| 111 | + external of_any : 'a -> string = "js_anything_to_string" |
| 112 | + |
| 113 | + |
| 114 | + (***********************) |
| 115 | + (* replaced primitives *) |
| 116 | + external length : string -> int = "%string_length" |
| 117 | + external get : string -> int -> char = "%string_safe_get" |
| 118 | + external create : int -> bytes = "caml_create_string" |
| 119 | + external unsafe_get : string -> int -> char = "%string_unsafe_get" |
| 120 | + external unsafe_set : bytes -> int -> char -> unit = "%bytes_unsafe_set" |
| 121 | + external unsafe_blit : string -> int -> bytes -> int -> int -> unit |
| 122 | + = "caml_blit_string" "noalloc" |
| 123 | + external unsafe_fill : bytes -> int -> int -> char -> unit |
| 124 | + = "caml_fill_string" "noalloc" |
| 125 | + |
| 126 | +end |
| 127 | + |
| 128 | +module Array = struct |
| 129 | + external new_uninitialized : int -> 'a array = "js_create_array" |
| 130 | + external append : 'a array -> 'a array -> 'a array = "js_array_append" |
| 131 | + external make : int -> 'a -> 'a array = "caml_make_vect" |
| 132 | +end |
| 133 | +module Bytes = struct |
| 134 | + external to_int_array : bytes -> int array = "%identity" |
| 135 | + external of_int_array : int array -> bytes = "%identity" |
| 136 | + external new_uninitialized : int -> bytes = "js_create_array" |
| 137 | + |
| 138 | + (***********************) |
| 139 | + (* replaced primitives *) |
| 140 | + (* Note that we explicitly define [unsafe_set] instead of |
| 141 | + using {!Bytes.unsafe_set} since for some standard libraries, |
| 142 | + it might point to ["%string_unsafe_set"] |
| 143 | + *) |
| 144 | + external unsafe_set : bytes -> int -> char -> unit = "%bytes_unsafe_set" |
| 145 | + external unsafe_get : bytes -> int -> char = "%bytes_unsafe_get" |
| 146 | + external length : bytes -> int = "%bytes_length" |
| 147 | +end |
| 148 | +module Float = struct |
| 149 | + external nan : float = "NaN" |
| 150 | + [@@bs.val ] |
| 151 | + |
| 152 | + external to_fixed : float -> int -> string = "toFixed" |
| 153 | + [@@bs.send] |
| 154 | + |
| 155 | + external is_finite : float -> bool = "isFinite" |
| 156 | + [@@bs.call ] |
| 157 | + |
| 158 | + external is_nan : float -> bool = "isNaN" |
| 159 | + [@@bs.call ] |
| 160 | + |
| 161 | + external exp : float -> float = "Math.exp" |
| 162 | + [@@bs.call ] |
| 163 | + |
| 164 | + external log : float -> float = "Math.log" |
| 165 | + [@@bs.call ] |
| 166 | + |
| 167 | + external to_exponential : float -> prec:int -> string = "toExponential" |
| 168 | + [@@bs.send] |
| 169 | + |
| 170 | + external log2 : float = "Math.LN2" [@@ bs.val ] |
| 171 | + external max : float -> float -> float = "Math.max" |
| 172 | + [@@bs.call] |
| 173 | + external random : unit -> float = "Math.random" |
| 174 | + [@@bs.call ] |
| 175 | + external of_any : 'a -> float = "js_anything_to_number" |
| 176 | +end |
| 177 | + |
| 178 | +module Caml_obj = struct |
| 179 | + external set_tag : any -> int -> unit = "caml_obj_set_tag" |
| 180 | + external set_length : any -> int -> unit = "js_obj_set_length" |
| 181 | + external length : any -> int = "js_obj_length" |
| 182 | + external tag : any -> int = "caml_obj_tag" |
| 183 | + external set_tag : any -> int -> unit = "caml_obj_set_tag" |
| 184 | + external uninitialized_object : int -> int -> any = "js_uninitialized_object" |
| 185 | + external is_instance_array : any -> bool = |
| 186 | + "js_is_instance_array" (* use Array.isArray instead*) |
| 187 | + external size_of_any : any -> 'a Def.t = |
| 188 | + "length" [@@bs.get] |
| 189 | + external tag_of_any : any -> 'a Def.t = |
| 190 | + "tag" [@@bs.get] |
| 191 | + external magic : 'a -> 'b = "%identity" |
| 192 | +end |
| 193 | + |
| 194 | +module Caml_int64 = struct |
| 195 | + external discard_sign : int64 -> int64 = "js_int64_discard_sign" |
| 196 | + external div_mod : int64 -> int64 -> int64 * int64 = "js_int64_div_mod" |
| 197 | + external to_hex : int64 -> string = "js_int64_to_hex" |
| 198 | +end |
0 commit comments