Advertisement
Guest User

Untitled

a guest
May 27th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. namespace Tinesware.Unfurling
  2.  
  3. open Otp
  4.  
  5. type public Pid = { Node : string;
  6. Id : int;
  7. Serial : int;
  8. Creation : int }
  9.  
  10. type public Port = { Node : string;
  11. Id : int;
  12. Creation : int }
  13.  
  14. type public Ref = { Node : string;
  15. Creation : int;
  16. Ids : int list }
  17.  
  18. type public Term =
  19. | Double of float
  20. | Integer of bigint
  21. | List of Term list
  22. | Tuple of Term list
  23. | Atom of string
  24. | Binary of byte[]
  25. | Pid of Pid
  26. | Port of Port
  27. | Ref of Ref
  28.  
  29. module Wrapper =
  30.  
  31. let rec ToTerm (term:Erlang.Object) =
  32. match term with
  33. | :? Erlang.Atom as atom -> Term.Atom <| atom.atomValue()
  34. | :? Erlang.Binary as binary -> Term.Binary <| binary.binaryValue()
  35. | :? Erlang.Double as double -> Term.Double <| double.doubleValue()
  36. | :? Erlang.List as list ->
  37. let contents = list.elements()
  38. |> Seq.map ToTerm
  39. |> Seq.toList
  40. Term.List contents
  41. | :? Erlang.Long as long -> Term.Integer <| bigint (long.longValue())
  42. | :? Erlang.Pid as pid -> Term.Pid {Node = pid.node(); Id = pid.id(); Serial = pid.serial(); Creation = pid.creation()}
  43. | :? Erlang.Port as port -> Term.Port {Node = port.node(); Id = port.id(); Creation = port.creation()}
  44. | :? Erlang.Ref as ref -> Term.Ref {Node = ref.node(); Creation = ref.creation(); Ids = ref.ids() |> Array.toList }
  45. | :? Erlang.Tuple as tuple ->
  46. let contents = tuple.elements()
  47. |> Seq.map ToTerm
  48. |> Seq.toList
  49. Term.Tuple contents
  50.  
  51. | _ -> raise <| new System.InvalidOperationException(term.ToString())
  52.  
  53. let rec ToOtpObject (term : Term) =
  54. match term with
  55. | Term.Atom atom -> new Erlang.Atom(atom) :> Erlang.Object
  56. | Term.Binary binary -> new Erlang.Binary(binary) :> Erlang.Object
  57. | Term.Double double -> new Erlang.Double(double) :> Erlang.Object
  58. | Term.Integer long -> new Erlang.Long(int64 long) :> Erlang.Object
  59. | Term.List list -> new Erlang.List( list
  60. |> Seq.map ToOtpObject
  61. |> Seq.toArray ) :> Erlang.Object
  62. | Term.Pid pid -> new Erlang.Pid(pid.Node, pid.Id, pid.Serial, pid.Creation) :> Erlang.Object
  63. | Term.Port port -> new Erlang.Port(port.Node, port.Id, port.Creation) :> Erlang.Object
  64. | Term.Ref ref -> new Erlang.Ref(ref.Node, ref.Ids |> List.toArray, ref.Creation) :> Erlang.Object
  65. | Term.Tuple tuple -> new Erlang.Tuple ( tuple
  66. |> Seq.map ToOtpObject
  67. |> Seq.toArray ) :> Erlang.Object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement