]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/Serialization.scl
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Serialization.scl
1 import "Prelude" hiding (readInteger, readLong, readFloat, readDouble, read)
2 import "Stream"
3 import "Vector"
4 import "ArrayList" as ArrayList
5
6 class IO a where
7     write :: OutputStream -> a -> <Proc> ()
8     read  :: InputStream -> <Proc> a
9     ioSize :: a -> <Proc> Integer
10     
11     ioSize _ = fail "ioSize not implemented"
12
13 instance IO Boolean where
14     write = writeBoolean
15     read = readBoolean
16     ioSize _ = 1
17     
18 instance IO Byte where
19     write = writeByte
20     read = readByte
21     ioSize _ = 1
22     
23 instance IO Character where
24     write = writeCharacter
25     read = readCharacter
26     ioSize _ = 2
27     
28 instance IO Short where
29     write = writeShort
30     read = readShort
31     ioSize _ = 2
32
33 instance IO Integer where
34     write = writeInteger
35     read = readInteger
36     ioSize _ = 4
37     
38 instance IO Long where
39     write = writeLong
40     read = readLong
41     ioSize _ = 8
42     
43 instance IO Float where
44     write = writeFloat
45     read = readFloat
46     ioSize _ = 4
47     
48 instance IO Double where
49     write = writeDouble
50     read = readDouble
51     ioSize _ = 8
52
53 instance IO String where
54     write = writeString
55     read = readString
56     ioSize = ioSizeString
57     
58 instance (IO a) => IO (Maybe a) where
59     read s = if read s 
60              then Just (read s)
61              else Nothing
62     write s (Just v) = do
63         write s True
64         write s v
65     write s Nothing = do
66         write s False
67     ioSize Nothing = 1
68     ioSize (Just v) = 1 + ioSize v
69
70 instance (IO a, IO b) => IO (Either a b) where
71     read s = if read s 
72              then Right (read s)
73              else Left (read s)
74     write s (Left v) = do
75         write s False
76         write s v
77     write s (Right v) = do
78         write s True
79         write s v    
80     ioSize (Left v) = 1 + ioSize v
81     ioSize (Right v) = 1 + ioSize v
82         
83 instance IO () where
84     read s = ()
85     write s _ = ()
86     ioSize _ = 0
87
88 instance (IO a, IO b) => IO (a, b) where
89     read s = do
90         a = read s
91         b = read s
92         (a, b)
93     write s (a, b) = do
94         write s a
95         write s b
96     ioSize (a, b) = ioSize a + ioSize b
97
98 instance (IO a, IO b, IO c) => IO (a, b, c) where
99     read s = do
100         a = read s
101         b = read s
102         c = read s
103         (a, b, c)
104     write s (a, b, c) = do
105         write s a
106         write s b 
107         write s c
108     ioSize (a, b, c) = ioSize a + ioSize b + ioSize c
109
110 instance (IO a, IO b, IO c, IO d) => IO (a, b, c, d) where
111     read s = do
112         a = read s
113         b = read s
114         c = read s
115         d = read s
116         (a, b, c, d)
117     write s (a, b, c, d) = do
118         write s a
119         write s b 
120         write s c
121         write s d
122     ioSize (a, b, c, d) = ioSize a + ioSize b + ioSize c + ioSize d
123     
124 instance (IO a, IO b, IO c, IO d, IO e) => IO (a, b, c, d, e) where
125     read s = do
126         a = read s
127         b = read s
128         c = read s
129         d = read s
130         e = read s
131         (a, b, c, d, e)
132     write s (a, b, c, d, e) = do
133         write s a
134         write s b 
135         write s c
136         write s d
137         write s e
138     ioSize (a, b, c, d, e) = ioSize a + ioSize b + ioSize c + ioSize d + ioSize e
139
140 instance (IO a) => IO [a] where
141     write s v = do
142         writeLength s (length v)
143         for v (write s)
144     read s = ArrayList.freeze (read s) 
145     ioSize vs = ioSizeLength (length vs) + sum [ioSize v | v <- vs]
146
147 instance (IO a) => IO (ArrayList.T a) where
148     write s v = do
149         writeLength s (ArrayList.length v)
150         ArrayList.for v (write s)    
151     read s = do
152         l = readLength s
153         result = ArrayList.newC l
154         forN l (\_ ->
155             ArrayList.add result (read s)
156         ) 
157         result
158     // TODO
159
160 instance (IO a, VecComp a) => IO (Vector a) where
161     @inline
162     write s v = do
163         l = length v
164         writeLength s l
165         forN l (\i -> write s (v ! i))
166     @inline
167     read s = do
168         l = readLength s
169         result = createMVector l
170         forN l (\i -> setMVector result i (read s))
171         freezeMVector result
172     // TODO
173     
174 readByteArray :: IO a => ByteArray -> a
175 readByteArray ar = runProc do
176     s = openForReading ar
177     v = read s
178     close s
179     v
180
181 writeByteArray :: IO a => a -> ByteArray
182 writeByteArray v = runProc do
183     b = createBuffer ()
184     s = openForWriting b
185     write s v
186     close s
187     toByteArray b
188
189 importJava "org.simantics.scl.runtime.io.SclIO" where 
190     writeBoolean :: OutputStream -> Boolean -> <Proc> ()
191     writeByte :: OutputStream -> Byte -> <Proc> ()
192     writeCharacter :: OutputStream -> Character -> <Proc> ()
193     writeShort :: OutputStream -> Short -> <Proc> ()
194     writeInteger :: OutputStream -> Integer -> <Proc> ()
195     writeLong :: OutputStream -> Long -> <Proc> () 
196     writeFloat :: OutputStream -> Float -> <Proc> ()
197     writeDouble :: OutputStream -> Double -> <Proc> ()
198     writeDoubleArray :: OutputStream -> DoubleArray -> <Proc> ()
199     writeString :: OutputStream -> String -> <Proc> ()
200     writeLength :: OutputStream -> Integer -> <Proc> ()
201     
202     readBoolean :: InputStream -> <Proc> Boolean
203     readByte :: InputStream -> <Proc> Byte
204     readCharacter :: InputStream -> <Proc> Character
205     readShort :: InputStream -> <Proc> Short
206     readInteger :: InputStream -> <Proc> Integer
207     readLong :: InputStream -> <Proc> Long
208     readFloat :: InputStream -> <Proc> Float
209     readDouble :: InputStream -> <Proc> Double
210     readDoubleArray :: InputStream -> <Proc> DoubleArray
211     readString :: InputStream -> <Proc> String
212     readLength :: InputStream -> <Proc> Integer
213     
214     ioSizeString :: String -> Integer
215     ioSizeLength :: Integer -> Integer
216     
217     @JavaName readAllByteArrayAndClose
218     readAllByteArray :: InputStream -> <Proc> ByteArray
219     @JavaName readAllStringAndClose
220     readAllString :: InputStream -> <Proc> String