package melange
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Toolchain to produce JS from Reason/OCaml
Install
dune-project
Dependency
Authors
Maintainers
Sources
melange-6.0.1-414.tbz
sha256=c1cce011864740dc43ee0f4e8130f70725f5e7042942cce653be5b6a8b439fb5
sha512=04fc82d97d5b9632c4ee041414d9fe104556a8c329e7444ffd71017baa00612da103fda91c309fc29a16e143c29d8748e1b1035a74681b61ff65fcfdf7f4e59c
doc/src/melange_ppx/ast_object.ml.html
Source file ast_object.ml
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350(* Copyright (C) 2015-2016 Bloomberg Finance L.P. * * 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, either version 3 of the License, or * (at your option) any later version. * * In addition to the permissions granted to you by the LGPL, you may combine * or link a "work that uses the Library" with a publicly distributed version * of this file to produce a combined library or application, then distribute * that combined work under the terms of your choosing, with no requirement * to comply with the obligations normally placed on you by section 4 of the * LGPL version 3 (or the corresponding section of a later version of the LGPL * should you choose to use a 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. *) open Import open Ast_helper let ffi_of_labels labels = Melange_ffi.External_ffi_types.ffi_obj_create (List.fold_right labels ~init:[] ~f:(fun { txt; _ } arg_kinds -> { Melange_ffi.External_arg_spec.Param.arg_type = Nothing; arg_label = Melange_ffi.External_arg_spec.Obj_label.obj (Melange_ffi.Lam_methname.translate txt); } :: arg_kinds)) let ocaml_object_as_js_object = let local_module_name = "J" in let local_fun_name = "unsafe_expr" in let local_extern_cont_to_obj ~loc ~ffi ~pval_type (cb : expression -> 'a) = Pexp_letmodule ( { txt = Some local_module_name; loc }, Mod.structure ~loc [ Str.primitive ~loc { pval_name = { txt = local_fun_name; loc }; pval_type; pval_loc = loc; pval_prim = Ast_external.pval_prim_default; pval_attributes = [ Ast_attributes.mel_ffi ffi ]; }; ], cb (Exp.ident ~loc { txt = Ldot (Lident local_module_name, local_fun_name); loc }) ) in let generate_val_method_pair ~loc (mapper : Ast_traverse.map) ~mutable_ (val_name : string Asttypes.loc) = let result = Typ.var ~loc val_name.txt in let base = match mutable_ with | Mutable -> [ Of.tag { val_name with txt = val_name.txt ^ Melange_ffi.External_ffi_types.Literals.setter_suffix; } (Ast_typ_uncurry.to_method_type ~loc mapper Nolabel result [%type: unit]); ] | Immutable -> [] in (result, Of.tag val_name result :: base) in fun ~loc (mapper : Ast_traverse.map) (self_pat : pattern) (clfs : class_field list) -> (* Attention: we should avoid type variable conflict for each method Since the method name is unique, there would be no conflict OCaml does not allow duplicate instance variable and duplicate methods, but it does allow duplicates between instance variable and method name, we should enforce such rules {[ object [@u] val x = 3 method x = 3 end ]} should not compile with a meaningful error message *) (* Note mapper is only for API compatible * TODO: we should check label name to avoid conflict *) (* we need calculate the real object type and exposed object type, in some cases there are equivalent for public object type its [@meth] it does not depend on itself while for label argument it is [@this] which depends internal object *) let ( (internal_label_attr_types : object_field list), (public_label_attr_types : object_field list) ) = List.fold_right ~f:(fun ({ pcf_loc = loc; _ } as x : class_field) (label_attr_types, public_label_attr_types) -> match x.pcf_desc with | Pcf_method (label, public_flag, Cfk_concrete (Fresh, e)) -> ( match e.pexp_desc with | Pexp_poly ( { pexp_desc = Pexp_function (_, _, Pfunction_cases _); _ }, None ) -> assert false | Pexp_poly ( { pexp_desc = Pexp_function (args, _, Pfunction_body e); _ }, None ) -> ( let method_type = Ast_typ_uncurry.generate_arg_type ~loc:x.pcf_loc mapper label.txt args e in ( Of.tag label method_type :: label_attr_types, match public_flag with | Public -> Of.tag label method_type :: public_label_attr_types | Private -> public_label_attr_types )) | Pexp_poly (_, Some _) -> Location.raise_errorf ~loc "polymorphic type annotation not supported yet" | Pexp_poly (_, None) -> Location.raise_errorf ~loc "Unsupported JS Object syntax. Methods must take at least \ one argument" | _ -> Location.raise_errorf ~loc "Unsupported syntax in js object") | Pcf_val (label, mutable_, Cfk_concrete (Fresh, _)) -> let _, label_attr = generate_val_method_pair ~loc:x.pcf_loc mapper label ~mutable_ in (List.append label_attr label_attr_types, public_label_attr_types) | Pcf_val (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not support currently" | Pcf_val (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtual flag not support currently" | Pcf_method (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not supported" | Pcf_method (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtural method not supported" | Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _ | Pcf_constraint _ -> Location.raise_errorf ~loc "Only method support currently") clfs ~init:([], []) in let internal_obj_type = Ast_core_type.to_js_type ~loc (Typ.object_ ~loc internal_label_attr_types Closed) in let public_obj_type = Ast_core_type.to_js_type ~loc (Typ.object_ ~loc public_label_attr_types Closed) in let labels, label_types, exprs, _ = List.fold_right ~f:(fun (x : class_field) (labels, label_types, exprs, aliased) -> match x.pcf_desc with | Pcf_method (label, _public_flag, Cfk_concrete (Fresh, e)) -> ( match e.pexp_desc with | Pexp_poly ( { pexp_desc = Pexp_function (_, _, Pfunction_cases _); _ }, None ) -> assert false | Pexp_poly ( ({ pexp_desc = Pexp_function (params, _, Pfunction_body e); _; } as f), None ) -> let alias_type = match aliased with | true -> None | false -> Some internal_obj_type in let label_type = Ast_typ_uncurry.generate_method_type ?alias_type x.pcf_loc mapper label.txt params e in ( label :: labels, label_type :: label_types, { f with pexp_desc = (let f = match List.find ~f:(function | { pparam_desc = Pparam_val _; _ } -> true | { pparam_desc = Pparam_newtype _; _ } -> false) params with | { pparam_desc = Pparam_val ( _, _, { ppat_desc = Ppat_construct ({ txt = Lident "()"; _ }, None); _; } ); _; } -> e | { pparam_desc = Pparam_val (_, _, _); _ } -> f | { pparam_desc = Pparam_newtype _; _ } | (exception Not_found) -> assert false in (* the first argument is `this` *) Ast_uncurry_gen.to_method_callback ~loc mapper [ { pparam_desc = Pparam_val (Nolabel, None, self_pat); pparam_loc = x.pcf_loc; }; ] f); } :: exprs, true ) | Pexp_poly (_, Some _) -> Location.raise_errorf ~loc "polymorphic type annotation not supported yet" | Pexp_poly (_, None) -> Location.raise_errorf ~loc "Unsupported syntax, expect syntax like `method x () = x ` " | _ -> Location.raise_errorf ~loc "Unsupported syntax in js object") | Pcf_val (label, mutable_, Cfk_concrete (Fresh, val_exp)) -> let label_type, _ = generate_val_method_pair ~loc:x.pcf_loc mapper label ~mutable_ in ( label :: labels, label_type :: label_types, mapper#expression val_exp :: exprs, aliased ) | Pcf_val (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not support currently" | Pcf_val (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtual flag not support currently" | Pcf_method (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not supported" | Pcf_method (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtural method not supported" | Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _ | Pcf_constraint _ -> Location.raise_errorf ~loc "Only method support currently") clfs ~init:([], [], [], false) in let pval_type = List.fold_right2 ~f:(fun label label_type acc -> Typ.arrow ~loc:label.Asttypes.loc (Labelled label.Asttypes.txt) label_type acc) labels label_types ~init:public_obj_type in local_extern_cont_to_obj ~loc ~ffi:(ffi_of_labels labels) (fun e -> Exp.apply ~loc e (List.map2 ~f:(fun l expr -> (Labelled l.txt, expr)) labels exprs)) ~pval_type let record_as_js_object = let local_module_name = "J" in let local_fun_name = "unsafe_expr" in let local_external_obj ~loc ~ffi ~pval_type args : expression_desc = Pexp_letmodule ( { txt = Some local_module_name; loc }, Mod.structure ~loc [ Str.primitive ~loc { pval_name = { txt = local_fun_name; loc }; pval_type; pval_loc = loc; pval_prim = [ ""; "" ]; pval_attributes = [ Ast_attributes.mel_ffi ffi ]; }; ], Exp.apply (Exp.ident ~loc { loc; txt = Ldot (Lident local_module_name, local_fun_name) }) (List.map ~f:(fun (l, a) -> (Asttypes.Labelled l, a)) args) ~loc ) in (* Note that OCaml type checker will not allow arbitrary name as type variables, for example: {[ '_x'_ ]} will be recognized as a invalid program *) let from_labels ~loc arity labels : core_type = let tyvars = List.init ~len:arity ~f:(fun i -> Typ.var ~loc ("a" ^ string_of_int i)) in let result_type = Ast_core_type.to_js_type ~loc (Typ.object_ ~loc (List.map2 ~f:(fun x y -> Of.tag x y) labels tyvars) Closed) in List.fold_right2 ~f:(fun label (* {loc ; txt = label } *) tyvar acc -> Typ.arrow ~loc:label.loc (Labelled label.txt) tyvar acc) labels tyvars ~init:result_type in fun ~loc (label_exprs : (Longident.t Asttypes.loc * expression) list) : expression_desc -> let labels, args, arity = List.fold_right ~f:(fun ({ txt; loc }, e) (labels, args, i) -> match txt with | Lident obj_label -> let obj_label = Ast_attributes.iter_process_mel_string_as e.pexp_attributes |> Option.value ~default:obj_label in ( { Asttypes.loc; txt = obj_label } :: labels, (obj_label, e) :: args, i + 1 ) | Ldot _ | Lapply _ -> Location.raise_errorf ~loc "`%%mel.obj' literals only support simple labels") label_exprs ~init:([], [], 0) in local_external_obj ~loc ~ffi:(ffi_of_labels labels) ~pval_type:(from_labels ~loc arity labels) args
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>