0 | module Text.WebIDL.Codegen.Members
3 | import Text.WebIDL.Codegen.Args
4 | import Text.WebIDL.Codegen.Rules
5 | import Text.WebIDL.Codegen.Types
6 | import Text.WebIDL.Codegen.Util
7 | import Text.WebIDL.Types
16 | jsType : Identifier -> Supertypes -> String
17 | jsType n (MkSupertypes parents ms) =
18 | let mixins = sortedNubOn id ms
23 | , line "public export"
24 | , line "JSType \{n} where"
25 | , indent 2 $
prettyCon Open "parents =" [list (map (line . value) parents)]
27 | , indent 2 $
prettyCon Open "mixins =" [list (map (line . value) mixins)]
35 | constants : List CGConst -> List String
36 | constants = map (render80 . const) . sortBy (comparing name)
39 | const : {opts : _} -> CGConst -> Doc opts
40 | const (MkConst t n v) =
44 | , line "public export"
45 | , line "\{n} :" <++> constTpe t
46 | , line "\{n} =" <++> prettyConst v
54 | primCallback : CGCallback -> String
55 | primCallback (MkCallback n _ t as) =
56 | callbackFFI n (primMarshallCallback n) (callbackFFI $
length as) as t
59 | callback : CGCallback -> String
60 | callback (MkCallback n _ t as) =
61 | callbackAPI n (marshallCallback n) (primMarshallCallback n) as t
74 | -> (Doc opts, Doc opts)
75 | attrImpl msg s g a (Mandatory _ (Simple $
MaybeNull x)) =
76 | ( line "Attribute False Maybe" <++> ret App (Simple $
NotNull x)
77 | , prettyCon Open "fromNullablePrim" [msg,s,g,a]
80 | attrImpl msg s g a (Mandatory _ (Union $
MaybeNull x)) =
81 | ( line "Attribute False Maybe" <++> ret App (Union $
NotNull x)
82 | , prettyCon Open "fromNullablePrim" [msg,s,g,a]
85 | attrImpl msg s g a (Mandatory _ t) =
86 | ( line "Attribute True Prelude.id" <++> ret App t
87 | , prettyCon Open "fromPrim" [msg,s,g,a]
90 | attrImpl msg s g a (VarArg _ t) =
91 | ( line "Attribute True Prelude.id" <++> prettyCon App "VarArg" [ffi App t]
92 | , prettyCon Open "fromPrim" [msg,s,g,a]
95 | attrImpl msg s g a (Optional _ t d) =
96 | case deflt (safeCast t) App t d of
98 | ( line "Attribute False Optional" <++> ret App t
99 | , prettyCon Open "fromUndefOrPrimNoDefault" [msg,s,g,a]
102 | ( line "Attribute True Optional" <++> ret App t
103 | , prettyCon Open "fromUndefOrPrim" [msg,s,g,x,a]
113 | attrRW k n o t rt =
114 | let implName := attrGetter k n
115 | primGet := line "\{primAttrGetter k n}"
116 | primSet := line "\{primAttrSetter k n}"
117 | msg := namespacedIdent o (fromString $
"get" ++ n.value)
118 | po := kindToString o
119 | up := if isParent o then "(v :> \{po})" else "v"
121 | (tpe,impl) := attrImpl (line msg) primGet primSet (line up) t
128 | [ line "{auto 0 _ : JSType t}"
129 | , line "{auto 0 _ : Elem \{po} (Types t)}"
132 | else typeDecl implName tpe [line "\{po}"]
134 | in render80 . indent 2 $
139 | , line "\{implName} v =" <++> impl
146 | obj : Kind -> CGArg
147 | obj k = Mandatory (MkArgName "obj") (fromKind k)
149 | function : (Nat,CGFunction) -> String
150 | function (k,Getter o i t) = fun o (getter k) (primGetter k) [obj o, i] t
152 | function (k,Setter o i v) =
153 | fun o (setter k) (primSetter k) [obj o, i, v] Undefined
155 | function (k,Regular n o as t) = fun o (op k n) (primOp k n) (obj o :: as) t
157 | function (k,Static n o as t) = fun o (op k n) (primOp k n) as t
159 | function (k,Attribute n o t rt) = attrRW k n o t rt
161 | function (k,AttributeGet n o t) =
162 | fun o (attrGetter k n) (primAttrGetter k n) [obj o] t
164 | function (k,StaticAttributeSet n o t) =
165 | fun o (attrSetter k n) (primAttrSetter k n) [t] Undefined
167 | function (k,StaticAttributeGet n o t) =
168 | fun o (attrGetter k n) (primAttrGetter k n) [] t
170 | function (k,DictConstructor o as) =
171 | fun o (constr k) (primConstr k) as (fromKind o)
173 | function (k,Constructor o as) =
174 | fun o (constr k) (primConstr k) as (fromKind o)
176 | hasVarArg : Args -> Bool
177 | hasVarArg (VarArg _ _ :: []) = True
178 | hasVarArg [] = False
179 | hasVarArg (_ :: xs) = hasVarArg xs
181 | prim : (Nat,CGFunction) -> String
182 | prim (k,Getter o i t) = funFFI (primGetter k) getterFFI [obj o, i] t
183 | prim (k,Setter o i v) = funFFI (primSetter k) setterFFI [obj o, i, v] Undefined
184 | prim (k,Regular n o args t) =
185 | let as = obj o :: args
186 | in if hasVarArg args
187 | then funFFI (primOp k n) (funFFIVarArg n $
length args) as t
188 | else funFFI (primOp k n) (funFFI n $
length args) as t
190 | prim (k,Static n o as t) =
192 | then funFFI (primOp k n) (staticFunFFIVarArg o n $
length as) as t
193 | else funFFI (primOp k n) (staticFunFFI o n $
length as) as t
195 | prim (k,Attribute n o t rt) =
197 | [ funFFI (primAttrGetter k n) (attrGetFFI n) [obj o] rt
199 | , funFFI (primAttrSetter k n) (attrSetFFI n) [obj o, t] Undefined
202 | prim (k,AttributeGet n o t) =
203 | funFFI (primAttrGetter k n) (attrGetFFI n) [obj o] t
205 | prim (k,StaticAttributeSet n o t) =
206 | funFFI (primAttrSetter k n) (staticAttrSetFFI o n) [t] Undefined
208 | prim (k,StaticAttributeGet n o t) =
209 | funFFI (primAttrGetter k n) (staticAttrGetFFI o n) [] t
211 | prim (k,DictConstructor o as) =
212 | funFFI (primConstr k) (dictConFFI $
map argName as) as (fromKind o)
214 | prim (k,Constructor o as) =
216 | then funFFI (primConstr k) (conFFIVarArg o $
length as) as (fromKind o)
217 | else funFFI (primConstr k) (conFFI o $
length as) as (fromKind o)
222 | tagFunctions : List CGFunction -> List (Nat,CGFunction)
223 | tagFunctions = go 0
226 | go : Nat -> List CGFunction -> List (Nat,CGFunction)
228 | go k (x :: []) = [(k,x)]
229 | go k (x :: t@(y :: ys)) =
230 | if priority x == priority y
231 | then (k,x) :: go (S k) t
232 | else (k,x) :: go 0 t
235 | functions : List CGFunction -> List String
236 | functions = map function . tagFunctions . sortBy (comparing priority)
239 | primFunctions : List CGFunction -> List String
240 | primFunctions = map prim . tagFunctions . sortBy (comparing priority)