|
22 | 22 | * along with this program; if not, write to the Free Software
|
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
|
24 | 24 |
|
| 25 | + let undefinedHeader = [| |] |
25 | 26 |
|
| 27 | +let some ( x : Obj.t) : Obj.t = |
| 28 | + if Obj.magic x = None then |
| 29 | + Obj.repr (undefinedHeader, 0) |
| 30 | + else |
| 31 | + if x != Obj.repr Js.null && fst (Obj.magic x ) == Obj.repr undefinedHeader then |
| 32 | + Obj.repr (undefinedHeader, snd (Obj.magic x) + 1) |
| 33 | + else x |
26 | 34 |
|
27 |
| -let nullable_to_opt ( x : 'a Js.null_undefined) = |
| 35 | +let nullable_to_opt (type t) ( x : t Js.null_undefined) : t option = |
28 | 36 | if (Obj.magic x) == Js.null || (Obj.magic x) == Js.undefined then
|
29 | 37 | None
|
30 |
| - else Some (Obj.magic x : 'a) |
| 38 | + else Obj.magic (some (Obj.magic x : 'a)) |
31 | 39 |
|
32 |
| -let undefined_to_opt ( x : 'a Js.undefined) = |
| 40 | +let undefined_to_opt (type t) ( x : t Js.undefined) : t option = |
33 | 41 | if (Obj.magic x) == Js.undefined then None
|
34 |
| - else Some (Obj.magic x : 'a) |
| 42 | + else Obj.magic (some (Obj.magic x : 'a)) |
35 | 43 |
|
36 |
| -let null_to_opt ( x : 'a Js.null) = |
| 44 | +let null_to_opt (type t ) ( x : t Js.null) : t option = |
37 | 45 | if (Obj.magic x) == Js.null then None
|
38 |
| - else Some (Obj.magic x : 'a) |
| 46 | + else Obj.magic (some (Obj.magic x : 'a) ) |
| 47 | + |
| 48 | +(* external valFromOption : 'a option -> 'a = |
| 49 | + "#val_from_option" *) |
39 | 50 |
|
40 |
| -external valFromOption : 'a option -> 'a = |
41 |
| - "#val_from_option" |
42 | 51 |
|
43 |
| -let undefinedHeader = [| |] |
44 | 52 |
|
45 | 53 | (** The input is already of [Some] form, [x] is not None,
|
46 | 54 | make sure [x[0]] will not throw *)
|
47 | 55 | let valFromOption (x : Obj.t) : Obj.t =
|
48 |
| - if x != Obj.repr Js_null.empty && fst (Obj.magic x) == Obj.repr undefinedHeader |
| 56 | + if x != Obj.repr Js.null && fst (Obj.magic x) == Obj.repr undefinedHeader |
49 | 57 | then
|
50 | 58 | let depth : int = snd (Obj.magic x) in
|
51 | 59 | if depth = 0 then Obj.magic None
|
52 | 60 | else Obj.magic (undefinedHeader, depth - 1)
|
53 | 61 | else Obj.magic x
|
54 | 62 |
|
55 |
| -let some ( x : Obj.t) : Obj.t = |
56 |
| - if Obj.magic x = None then |
57 |
| - Obj.repr (undefinedHeader, 0) |
58 |
| - else |
59 |
| - if x != Obj.repr Js_null.empty && fst (Obj.magic x ) == Obj.repr undefinedHeader then |
60 |
| - Obj.repr (undefinedHeader, snd (Obj.magic x) + 1) |
61 |
| - else x |
62 |
| - |
63 | 63 |
|
64 |
| -let option_get (x : 'a option) : 'a Js_undefined.t = |
65 |
| - match x with |
| 64 | +let option_get (x : 'a option) : 'a Js.undefined = |
| 65 | + if x = None then Js.undefined |
| 66 | + else Obj.magic (valFromOption (Obj.repr x)) |
| 67 | + (* match x with |
66 | 68 | | None -> Js_undefined.empty
|
67 |
| - | Some x -> Js_undefined.return x |
| 69 | + | Some x -> Js_undefined.return x *) |
68 | 70 |
|
69 | 71 | (** [input] is optional polymorphic variant *)
|
70 |
| -let option_get_unwrap (x : 'a option) : _ Js_undefined.t = |
71 |
| - match x with |
| 72 | +let option_get_unwrap (x : 'a option) : _ Js.undefined = |
| 73 | + if x = None then Js.undefined |
| 74 | + else Obj.magic (Obj.field (Obj.repr (valFromOption (Obj.repr x))) 1 ) |
| 75 | + (* match x with |
72 | 76 | | None -> Js.undefined
|
73 |
| - | Some x -> Js_undefined.return (Obj.field (Obj.repr x) 1) |
| 77 | + | Some x -> Js_undefined.return (Obj.field (Obj.repr x) 1) *) |
74 | 78 |
|
0 commit comments