0 | module JS.Inheritance
2 | import Control.Monad.Either
4 | import Data.List.Quantifiers.Extra
18 | interface JSType a where
38 | 0 Types : (0 a : Type) -> JSType a => List Type
39 | Types a = a :: parents {a} ++ mixins {a}
45 | public export %inline
46 | up : (0 _ : JSType a) => a -> {auto 0 _ : Elem b (Types a)} -> b
52 | public export %inline
54 | {auto 0 _ : JSType a}
57 | -> {auto 0 _ : Elem b (Types a)}
66 | javascript:lambda:(s,v)=>{
69 | var p = Object.getPrototypeOf(o);
70 | var cn = p.constructor.name;
73 | } else if (cn === "Object") {
81 | prim__hasProtoName : String -> AnyPtr -> Double
100 | interface SafeCast a where
101 | safeCast : {0 x : Type} -> x -> Maybe a
103 | public export %inline
104 | castTo : (0 a : Type) -> SafeCast a => x -> Maybe a
105 | castTo _ v = safeCast v
111 | safeCastAny : (prf : All SafeCast ts) => x -> Maybe (HSum ts)
112 | safeCastAny @{[]} x = Nothing
113 | safeCastAny @{h :: t} x = case safeCast @{h} x of
114 | Just v => Just $
Here v
115 | Nothing => There <$> safeCastAny @{t} x
120 | unsafeCastOnPrototypeName : String -> a -> Maybe b
121 | unsafeCastOnPrototypeName s a =
122 | if prim__hasProtoName s (believe_me a) == 1.0
123 | then Just (believe_me a)
129 | unsafeCastOnTypeof : String -> a -> Maybe b
130 | unsafeCastOnTypeof s a =
131 | if typeof a == s then Just (believe_me a) else Nothing
134 | SafeCast Integer where
135 | safeCast = unsafeCastOnTypeof "bigint"
138 | SafeCast Double where
139 | safeCast = unsafeCastOnTypeof "number"
142 | SafeCast String where
143 | safeCast = unsafeCastOnTypeof "string"
149 | SafeCast Char where
150 | safeCast v = safeCast v >>= \s =>
152 | StrCons x "" => Just x
156 | bounded : Num a => (min : Integer) -> (max : Integer) -> x -> Maybe a
157 | bounded min max ptr =
158 | safeCast ptr >>= \n =>
159 | if n >= min && n <= max
160 | then Just (fromInteger n)
164 | SafeCast Bits8 where
165 | safeCast = bounded 0 0xff
168 | SafeCast Bits16 where
169 | safeCast = bounded 0 0xffff
172 | SafeCast Bits32 where
173 | safeCast = bounded 0 0xffffffff
176 | SafeCast Bits64 where
177 | safeCast = bounded 0 0xffffffffffffffff
180 | SafeCast Int8 where
181 | safeCast = bounded (-
0x80) 0x7f
184 | SafeCast Int16 where
185 | safeCast = bounded (-
0x8000) 0x7fff
188 | SafeCast Int32 where
189 | safeCast = bounded (-
0x80000000) 0x7fffffff
192 | SafeCast Int64 where
193 | safeCast = bounded (-
0x8000000000000000) 0x7fffffffffffffff
197 | safeCast = bounded (-
0x80000000) (0x7fffffff)
200 | tryCast : SafeCast a => (fun : Lazy String) -> x -> JSIO a
201 | tryCast fun val = case safeCast val of
203 | Nothing => throwError $
CastErr fun val
206 | tryCast_ : (0 a : Type) -> SafeCast a => (fun : Lazy String) -> x -> JSIO a
207 | tryCast_ _ = tryCast
210 | castingTo : SafeCast a => (fun : String) -> JSIO x -> JSIO a
211 | castingTo fun io = io >>= tryCast fun