package eliom

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file eliom_unwrap.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
# 1 "src/lib/eliom_unwrap.client.ml"
(* Ocsigen
 * http://www.ocsigen.org
 * Copyright (C) 2011 Pierre Chambart
 *
 * 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.
 *)

(* TODO: implement with WeakMap when standardised:
   https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/WeakMap

class type ['a,'b] weakMap = object
  method get : 'a -> 'b Js.optdef Js.meth
  method set : 'a -> 'b -> unit Js.meth
  method has : 'a -> bool Js.t Js.meth
end

let weakMap : ('a,'b) weakMap Js.t Js.constr = Js.Unsafe.global##_WeakMap

let map : (Obj.t,Obj.t) weakMap Js.t = jsnew weakMap ()
*)

open Js_of_ocaml
open Eliom_lib

module Mark : sig
  type t
end = struct
  type t = string
end

(* XXX must be the same as in Ocsigen_wrap *)
type unwrap_id = int

let id_of_int x = x

type unwrapper = {id : unwrap_id; mutable umark : Mark.t} [@@warning "-69"]

let unwrap_table : (Obj.t -> Obj.t option) Js.js_array Js.t =
  new%js Js.array_empty
(* table containing all the unwrapping functions referenced by their id *)

type occurrence = {parent : Obj.t; field : int}

let register_unwrapper' id f =
  if Js.Optdef.test (Js.array_get unwrap_table id)
  then
    failwith (Printf.sprintf ">> the unwrapper id %i is already registered" id);
  let f x = Ocsigen_lib_base.Option.map Obj.repr (f (Obj.obj x)) in
  (* Store unwrapper *)
  Js.array_set unwrap_table id f

let register_unwrapper id f = register_unwrapper' id (fun x -> Some (f x))

let apply_unwrapper unwrapper v =
  Js.Optdef.case
    (Js.array_get unwrap_table unwrapper.id)
    (fun () -> None) (* Use late unwrapping! *)
    (fun f -> f v)

let late_unwrap_value old_value new_value =
  let old_value = Obj.repr old_value in
  List.iter
    (fun {parent; field} ->
       Obj.set_field parent (field - 1) (Obj.repr new_value))
    (Obj.obj (Obj.field (Obj.field old_value (Obj.size old_value - 1)) 2))

external raw_unmarshal_and_unwrap :
   (unwrapper -> _ -> _ option)
  -> string
  -> int
  -> _
  = "caml_unwrap_value_from_string"

let unwrap s i =
  if !Eliom_config.debug_timings
  then Firebug.console ## (time (Js.string "unwrap"));
  let res = raw_unmarshal_and_unwrap apply_unwrapper s i in
  if !Eliom_config.debug_timings
  then Firebug.console ## (timeEnd (Js.string "unwrap"));
  res

let unwrap_js s = unwrap (Js.to_bytestring s) 0
OCaml

Innovation. Community. Security.