Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Tinesware.Unfurling
- open Otp
- type public Pid = { Node : string;
- Id : int;
- Serial : int;
- Creation : int }
- type public Port = { Node : string;
- Id : int;
- Creation : int }
- type public Ref = { Node : string;
- Creation : int;
- Ids : int list }
- type public Term =
- | Double of float
- | Integer of bigint
- | List of Term list
- | Tuple of Term list
- | Atom of string
- | Binary of byte[]
- | Pid of Pid
- | Port of Port
- | Ref of Ref
- module Wrapper =
- let rec ToTerm (term:Erlang.Object) =
- match term with
- | :? Erlang.Atom as atom -> Term.Atom <| atom.atomValue()
- | :? Erlang.Binary as binary -> Term.Binary <| binary.binaryValue()
- | :? Erlang.Double as double -> Term.Double <| double.doubleValue()
- | :? Erlang.List as list ->
- let contents = list.elements()
- |> Seq.map ToTerm
- |> Seq.toList
- Term.List contents
- | :? Erlang.Long as long -> Term.Integer <| bigint (long.longValue())
- | :? Erlang.Pid as pid -> Term.Pid {Node = pid.node(); Id = pid.id(); Serial = pid.serial(); Creation = pid.creation()}
- | :? Erlang.Port as port -> Term.Port {Node = port.node(); Id = port.id(); Creation = port.creation()}
- | :? Erlang.Ref as ref -> Term.Ref {Node = ref.node(); Creation = ref.creation(); Ids = ref.ids() |> Array.toList }
- | :? Erlang.Tuple as tuple ->
- let contents = tuple.elements()
- |> Seq.map ToTerm
- |> Seq.toList
- Term.Tuple contents
- | _ -> raise <| new System.InvalidOperationException(term.ToString())
- let rec ToOtpObject (term : Term) =
- match term with
- | Term.Atom atom -> new Erlang.Atom(atom) :> Erlang.Object
- | Term.Binary binary -> new Erlang.Binary(binary) :> Erlang.Object
- | Term.Double double -> new Erlang.Double(double) :> Erlang.Object
- | Term.Integer long -> new Erlang.Long(int64 long) :> Erlang.Object
- | Term.List list -> new Erlang.List( list
- |> Seq.map ToOtpObject
- |> Seq.toArray ) :> Erlang.Object
- | Term.Pid pid -> new Erlang.Pid(pid.Node, pid.Id, pid.Serial, pid.Creation) :> Erlang.Object
- | Term.Port port -> new Erlang.Port(port.Node, port.Id, port.Creation) :> Erlang.Object
- | Term.Ref ref -> new Erlang.Ref(ref.Node, ref.Ids |> List.toArray, ref.Creation) :> Erlang.Object
- | Term.Tuple tuple -> new Erlang.Tuple ( tuple
- |> Seq.map ToOtpObject
- |> Seq.toArray ) :> Erlang.Object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement