Add untracked files.
[order_line_extra.git] / hincludes / lib / haxe / Serializer.class.php
1 <?php
2
3 class haxe_Serializer {
4         public function __construct() {
5                 if(!php_Boot::$skip_constructor) {
6                 $this->buf = new StringBuf();
7                 $this->cache = new _hx_array(array());
8                 $this->useCache = haxe_Serializer::$USE_CACHE;
9                 $this->useEnumIndex = haxe_Serializer::$USE_ENUM_INDEX;
10                 $this->shash = new Hash();
11                 $this->scount = 0;
12         }}
13         public function serializeException($e) {
14                 $this->buf->add("x");
15                 $this->serialize($e);
16         }
17         public function serialize($v) {
18                 $»t = (Type::typeof($v));
19                 switch($»t->index) {
20                 case 0:
21                 {
22                         $this->buf->add("n");
23                 }break;
24                 case 1:
25                 {
26                         if(_hx_equal($v, 0)) {
27                                 $this->buf->add("z");
28                                 return;
29                         }
30                         $this->buf->add("i");
31                         $this->buf->add($v);
32                 }break;
33                 case 2:
34                 {
35                         if(Math::isNaN($v)) {
36                                 $this->buf->add("k");
37                         } else {
38                                 if(!Math::isFinite($v)) {
39                                         $this->buf->add((($v < 0) ? "m" : "p"));
40                                 } else {
41                                         $this->buf->add("d");
42                                         $this->buf->add($v);
43                                 }
44                         }
45                 }break;
46                 case 3:
47                 {
48                         $this->buf->add((($v) ? "t" : "f"));
49                 }break;
50                 case 6:
51                 $c = $»t->params[0];
52                 {
53                         if($c == _hx_qtype("String")) {
54                                 $this->serializeString($v);
55                                 return;
56                         }
57                         if($this->useCache && $this->serializeRef($v)) {
58                                 return;
59                         }
60                         switch($c) {
61                         case _hx_qtype("Array"):{
62                                 $ucount = 0;
63                                 $this->buf->add("a");
64                                 $l = _hx_len($v);
65                                 {
66                                         $_g = 0;
67                                         while($_g < $l) {
68                                                 $i = $_g++;
69                                                 if($v[$i] === null) {
70                                                         $ucount++;
71                                                 } else {
72                                                         if($ucount > 0) {
73                                                                 if($ucount === 1) {
74                                                                         $this->buf->add("n");
75                                                                 } else {
76                                                                         $this->buf->add("u");
77                                                                         $this->buf->add($ucount);
78                                                                 }
79                                                                 $ucount = 0;
80                                                         }
81                                                         $this->serialize($v[$i]);
82                                                 }
83                                                 unset($i);
84                                         }
85                                 }
86                                 if($ucount > 0) {
87                                         if($ucount === 1) {
88                                                 $this->buf->add("n");
89                                         } else {
90                                                 $this->buf->add("u");
91                                                 $this->buf->add($ucount);
92                                         }
93                                 }
94                                 $this->buf->add("h");
95                         }break;
96                         case _hx_qtype("List"):{
97                                 $this->buf->add("l");
98                                 $v1 = $v;
99                                 if(null == $v1) throw new HException('null iterable');
100                                 $»it = $v1->iterator();
101                                 while($»it->hasNext()) {
102                                         $i = $»it->next();
103                                         $this->serialize($i);
104                                 }
105                                 $this->buf->add("h");
106                         }break;
107                         case _hx_qtype("Date"):{
108                                 $d = $v;
109                                 $this->buf->add("v");
110                                 $this->buf->add($d->toString());
111                         }break;
112                         case _hx_qtype("Hash"):{
113                                 $this->buf->add("b");
114                                 $v1 = $v;
115                                 if(null == $v1) throw new HException('null iterable');
116                                 $»it = $v1->keys();
117                                 while($»it->hasNext()) {
118                                         $k = $»it->next();
119                                         $this->serializeString($k);
120                                         $this->serialize($v1->get($k));
121                                 }
122                                 $this->buf->add("h");
123                         }break;
124                         case _hx_qtype("IntHash"):{
125                                 $this->buf->add("q");
126                                 $v1 = $v;
127                                 if(null == $v1) throw new HException('null iterable');
128                                 $»it = $v1->keys();
129                                 while($»it->hasNext()) {
130                                         $k = $»it->next();
131                                         $this->buf->add(":");
132                                         $this->buf->add($k);
133                                         $this->serialize($v1->get($k));
134                                 }
135                                 $this->buf->add("h");
136                         }break;
137                         case _hx_qtype("haxe.io.Bytes"):{
138                                 $v1 = $v;
139                                 $i = 0;
140                                 $max = $v1->length - 2;
141                                 $charsBuf = new StringBuf();
142                                 $b64 = haxe_Serializer::$BASE64;
143                                 while($i < $max) {
144                                         $b1 = ord($v1->b[$i++]);
145                                         $b2 = ord($v1->b[$i++]);
146                                         $b3 = ord($v1->b[$i++]);
147                                         $charsBuf->add(_hx_char_at($b64, $b1 >> 2));
148                                         $charsBuf->add(_hx_char_at($b64, ($b1 << 4 | $b2 >> 4) & 63));
149                                         $charsBuf->add(_hx_char_at($b64, ($b2 << 2 | $b3 >> 6) & 63));
150                                         $charsBuf->add(_hx_char_at($b64, $b3 & 63));
151                                         unset($b3,$b2,$b1);
152                                 }
153                                 if($i === $max) {
154                                         $b1 = ord($v1->b[$i++]);
155                                         $b2 = ord($v1->b[$i++]);
156                                         $charsBuf->add(_hx_char_at($b64, $b1 >> 2));
157                                         $charsBuf->add(_hx_char_at($b64, ($b1 << 4 | $b2 >> 4) & 63));
158                                         $charsBuf->add(_hx_char_at($b64, $b2 << 2 & 63));
159                                 } else {
160                                         if($i === $max + 1) {
161                                                 $b1 = ord($v1->b[$i++]);
162                                                 $charsBuf->add(_hx_char_at($b64, $b1 >> 2));
163                                                 $charsBuf->add(_hx_char_at($b64, $b1 << 4 & 63));
164                                         }
165                                 }
166                                 $chars = $charsBuf->b;
167                                 $this->buf->add("s");
168                                 $this->buf->add(strlen($chars));
169                                 $this->buf->add(":");
170                                 $this->buf->add($chars);
171                         }break;
172                         default:{
173                                 $this->cache->pop();
174                                 if(_hx_field($v, "hxSerialize") !== null) {
175                                         $this->buf->add("C");
176                                         $this->serializeString(Type::getClassName($c));
177                                         $this->cache->push($v);
178                                         $v->hxSerialize($this);
179                                         $this->buf->add("g");
180                                 } else {
181                                         $this->buf->add("c");
182                                         $this->serializeString(Type::getClassName($c));
183                                         $this->cache->push($v);
184                                         $this->serializeFields($v);
185                                 }
186                         }break;
187                         }
188                 }break;
189                 case 4:
190                 {
191                         if($this->useCache && $this->serializeRef($v)) {
192                                 return;
193                         }
194                         $this->buf->add("o");
195                         $this->serializeFields($v);
196                 }break;
197                 case 7:
198                 $e = $»t->params[0];
199                 {
200                         if($this->useCache && $this->serializeRef($v)) {
201                                 return;
202                         }
203                         $this->cache->pop();
204                         $this->buf->add((($this->useEnumIndex) ? "j" : "w"));
205                         $this->serializeString(Type::getEnumName($e));
206                         if($this->useEnumIndex) {
207                                 $this->buf->add(":");
208                                 $this->buf->add($v->index);
209                         } else {
210                                 $this->serializeString($v->tag);
211                         }
212                         $this->buf->add(":");
213                         $l = count($v->params);
214                         if($l === 0 || _hx_field($v, "params") === null) {
215                                 $this->buf->add(0);
216                         } else {
217                                 $this->buf->add($l);
218                                 {
219                                         $_g = 0;
220                                         while($_g < $l) {
221                                                 $i = $_g++;
222                                                 $this->serialize($v->params[$i]);
223                                                 unset($i);
224                                         }
225                                 }
226                         }
227                         $this->cache->push($v);
228                 }break;
229                 case 5:
230                 {
231                         throw new HException("Cannot serialize function");
232                 }break;
233                 default:{
234                         throw new HException("Cannot serialize " . Std::string($v));
235                 }break;
236                 }
237         }
238         public function serializeFields($v) {
239                 {
240                         $_g = 0; $_g1 = Reflect::fields($v);
241                         while($_g < $_g1->length) {
242                                 $f = $_g1[$_g];
243                                 ++$_g;
244                                 $this->serializeString($f);
245                                 $this->serialize(Reflect::field($v, $f));
246                                 unset($f);
247                         }
248                 }
249                 $this->buf->add("g");
250         }
251         public function serializeRef($v) {
252                 {
253                         $_g1 = 0; $_g = $this->cache->length;
254                         while($_g1 < $_g) {
255                                 $i = $_g1++;
256                                 if(_hx_equal($this->cache[$i], $v)) {
257                                         $this->buf->add("r");
258                                         $this->buf->add($i);
259                                         return true;
260                                 }
261                                 unset($i);
262                         }
263                 }
264                 $this->cache->push($v);
265                 return false;
266         }
267         public function serializeString($s) {
268                 $x = $this->shash->get($s);
269                 if($x !== null) {
270                         $this->buf->add("R");
271                         $this->buf->add($x);
272                         return;
273                 }
274                 $this->shash->set($s, $this->scount++);
275                 $this->buf->add("y");
276                 $s = rawurlencode($s);
277                 $this->buf->add(strlen($s));
278                 $this->buf->add(":");
279                 $this->buf->add($s);
280         }
281         public function toString() {
282                 return $this->buf->b;
283         }
284         public $useEnumIndex;
285         public $useCache;
286         public $scount;
287         public $shash;
288         public $cache;
289         public $buf;
290         public function __call($m, $a) {
291                 if(isset($this->$m) && is_callable($this->$m))
292                         return call_user_func_array($this->$m, $a);
293                 else if(isset($this->»dynamics[$m]) && is_callable($this->»dynamics[$m]))
294                         return call_user_func_array($this->»dynamics[$m], $a);
295                 else if('toString' == $m)
296                         return $this->__toString();
297                 else
298                         throw new HException('Unable to call «'.$m.'»');
299         }
300         static $USE_CACHE = false;
301         static $USE_ENUM_INDEX = false;
302         static $BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%:";
303         static function run($v) {
304                 $s = new haxe_Serializer();
305                 $s->serialize($v);
306                 return $s->toString();
307         }
308         function __toString() { return $this->toString(); }
309 }