|
| 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 | +(** Note that we have to provide a drop in replacement, since compiler internally will |
| 26 | + spit out ("CamlinternalMod".[init_mod|update_mod] unless we intercept it |
| 27 | + in the lambda layer |
| 28 | + *) |
| 29 | +let init_mod (loc : string * int * int) (shape : CamlinternalMod.shape) = |
| 30 | + let undef_module _ = raise (Undefined_recursive_module loc) in |
| 31 | + let rec loop (shape : CamlinternalMod.shape) (struct_ : Obj.t array) idx = |
| 32 | + match shape with |
| 33 | + | Function -> struct_.(idx)<-(Obj.magic undef_module) |
| 34 | + | Lazy -> struct_.(idx)<- (Obj.magic (lazy undef_module)) |
| 35 | + | Class -> struct_.(idx)<- (Obj.magic (CamlinternalOO.dummy_class loc)) |
| 36 | + | Module comps |
| 37 | + -> |
| 38 | + let v = (Obj.magic [||]) in |
| 39 | + struct_.(idx)<- v ; |
| 40 | + let len = Array.length comps in |
| 41 | + for i = 0 to len - 1 do |
| 42 | + loop comps.(i) v i |
| 43 | + done |
| 44 | + | Value v -> |
| 45 | + struct_.(idx) <- v in |
| 46 | + let res = (Obj.magic [||] : Obj.t array) in |
| 47 | + loop shape res 0 ; |
| 48 | + res.(0) |
| 49 | + |
| 50 | +external caml_update_dummy : Obj.t -> Obj.t -> unit = "caml_update_dummy" |
| 51 | +(* Note the [shape] passed between [init_mod] and [update_mod] is always the same |
| 52 | + and we assume [module] is encoded as an array |
| 53 | + *) |
| 54 | +let update_mod (shape : CamlinternalMod.shape) (o : Obj.t) (n : Obj.t) : unit = |
| 55 | + let rec aux (shape : CamlinternalMod.shape) o n parent i = |
| 56 | + match shape with |
| 57 | + | Function |
| 58 | + -> Obj.set_field parent i n |
| 59 | + |
| 60 | + | Lazy |
| 61 | + | Class -> |
| 62 | + caml_update_dummy o n |
| 63 | + | Module comps |
| 64 | + -> |
| 65 | + for i = 0 to Array.length comps - 1 do |
| 66 | + aux comps.(i) (Obj.field o i) (Obj.field n i) o i |
| 67 | + done |
| 68 | + | Value _ -> () in |
| 69 | + match shape with |
| 70 | + | Module comps -> |
| 71 | + for i = 0 to Array.length comps - 1 do |
| 72 | + aux comps.(i) (Obj.field o i) (Obj.field n i) o i |
| 73 | + done |
| 74 | + | _ -> assert false |
0 commit comments