API documentation  2.0rc1
types.h
1 /****************************************************************
2  *
3  * Copyright (C) 2012-2018 Alessandro Pignotti <alessandro@leaningtech.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  ***************************************************************/
20 
21 #ifndef _CHEERP_TYPES_H_2043d438
22 #define _CHEERP_TYPES_H_2043d438
23 
24 #include <utility>
25 #include <string>
26 #include <limits>
27 #include <cheerpintrin.h>
28 #include "jsobject.h"
29 
30 namespace [[cheerp::genericjs]] client
31 {
32 
33 template<class T>
34 class TArray;
35 
36 class EventListener;
37 class RegExp;
38 
39 class String: public Object
40 {
41 private:
42  template<typename C>
43  static String* fromCharPtr(const C* s)
44  {
45  String* ret=new String();
46  for(;*s;s++)
47  {
48  ret=ret->concat(*String::fromCharCode(*s));
49  }
50  return ret;
51  }
52  template<typename... Args>
53  String* concat(const String&, Args&&... args);
54  String* concat();
55 public:
56  String() throw();
57  //Utility constructor to use an existing String
58  String(const String*) throw();
59  String(long a) throw();
60  String(unsigned long a) throw();
61  String(int a) throw();
62  String(unsigned int a) throw();
63  String(float a) throw();
64  String(double a) throw();
65  String(const char* s) __attribute__((noinline)) :String(fromCharPtr<char>(s))
66  {
67  }
68  String(const wchar_t* s) __attribute__((noinline)) :String(fromCharPtr<wchar_t>(s))
69  {
70  }
71  template<typename... Args>
72  __attribute__((always_inline)) String* concat(Args&&... args)
73  {
74  return concat(static_cast<const String&>(static_cast<Args&&>(args))...);
75  }
76  String* substr(int start) const;
77  String* substr(int start, int length) const;
78  String* substring(int start) const;
79  String* substring(int start, int end) const;
80  String* replace(const String&, const String&) const;
81  String* replace(const String&, EventListener*) const;
82  String* replace(RegExp*, const String&) const;
83  String* replace(RegExp*, EventListener*) const;
84  int charCodeAt(int index) const;
85  String* charAt(int index) const;
86  int get_length() const;
87  int indexOf(const String&) const;
88  int indexOf(const String&, int) const;
89  int lastIndexOf(const String&) const;
90  int lastIndexOf(const String&, int) const;
91  TArray<String>* split(const String&) const;
92  TArray<String>* split(const String&, int limit) const;
93  TArray<String>* split(RegExp*) const;
94  TArray<String>* split(RegExp*, int limit) const;
95  static String* fromCharCode(int c) [[cheerp::static]];
96  bool startsWith(const String&) const;
97  bool endsWith(const String&) const;
98  int localeCompare(const String&) const;
99  TArray<String>* match(const String&) const;
100  TArray<String>* match(RegExp*) const;
101  int search(const String&) const;
102  int search(RegExp*) const;
103  String* slice(int start) const;
104  String* slice(int start, int end) const;
105  String* toLowerCase() const;
106  String* toLocaleLowerCase() const;
107  String* toUpperCase() const;
108  String* toLocaleUpperCase() const;
109  String* trim() const;
110  String* padEnd(int) const;
111  String* padEnd(int, const String&) const;
112  String* padStart(int) const;
113  String* padStart(int, const String&) const;
114  explicit operator std::string() const
115  {
116  //This assume an ascii string
117  //TODO: Try wstring or similar
118  std::string ret;
119  ret.resize(get_length());
120  char* ptr = &ret[0];
121  for(int i=0;i<get_length();i++)
122  ptr[i] = charCodeAt(i);
123  return ret;
124  }
125  static client::String* fromUtf8(const char * in, size_t len = std::numeric_limits<size_t>::max())
126  {
127  client::String* out = new client::String();
128  unsigned int codepoint;
129  while (len > 0 && *in != 0)
130  {
131  unsigned char ch = static_cast<unsigned char>(*in);
132  // ASCII range
133  if (ch <= 0x7f)
134  codepoint = ch;
135  // Continuation bytes
136  else if (ch <= 0xbf)
137  codepoint = (codepoint << 6) | (ch & 0x3f);
138  // Start of 2-bytes sequence
139  else if (ch <= 0xdf)
140  codepoint = ch & 0x1f;
141  // Start of 3-bytes sequence
142  else if (ch <= 0xef)
143  codepoint = ch & 0x0f;
144  // Start of 4-bytes sequence
145  else
146  codepoint = ch & 0x07;
147  ++in;
148  --len;
149  // NOTE: we are assuming that invalid codepoints will be handled
150  // in a sensible way by javascript strings
151  if (len == 0 || ((*in & 0xc0) != 0x80))
152  {
153  if (codepoint <= 0xffff)
154  out = out->concat(client::String::fromCharCode(codepoint));
155  else
156  {
157  // surrogate pair
158  codepoint -= 0x10000;
159  unsigned int highSurrogate = (codepoint >> 10) + 0xd800;
160  unsigned int lowSurrogate = (codepoint & 0x3ff) + 0xdc00;
161  out = out->concat(client::String::fromCharCode(highSurrogate));
162  out = out->concat(client::String::fromCharCode(lowSurrogate));
163  }
164  }
165  }
166  return out;
167  }
168 
169 };
170 
171 class Array: public Object
172 {
173 public:
174  template<typename... Args>
175  Array(Args... args);
176  Object*& operator[](int index)
177  {
178  return __builtin_cheerp_make_regular<Object*>(this, 0)[index];
179  }
180  Object* operator[](int index) const
181  {
182  return __builtin_cheerp_make_regular<Object*>(this, 0)[index];
183  }
184  int indexOf(Object* searchElement) const;
185  int indexOf(Object* searchElement, int fromIndex) const;
186  int indexOf(double searchElement) const;
187  int indexOf(double searchElement, int fromIndex) const;
188  int lastIndexOf(Object* searchElement) const;
189  int lastIndexOf(Object* searchElement, int fromIndex) const;
190  int lastIndexOf(double searchElement) const;
191  int lastIndexOf(double searchElement, int fromIndex) const;
192  template<typename... Args>
193  int push(Args... args);
194  int get_length() const;
195  Array* splice(int start);
196  template<typename... Args>
197  Array* splice(int start, int deleteCount, Args... args);
198  Array* slice() const;
199  Array* slice(int start) const;
200  Array* slice(int start, int end) const;
201  Object* shift();
202  void unshift(client::Object*);
203  Object* pop();
204  String* toString() const;
205  String* toLocaleString() const;
206  template<typename... Args>
207  Array* concat(Args&&... args) const;
208  String* join(const String&) const;
209  Array* reverse();
210  Array* sort(EventListener* callback);
211  bool every(EventListener* callback);
212  bool some(EventListener* callback);
213  void forEach(EventListener* callback);
214  Array* map(EventListener* callback);
215  Array* filter(EventListener* callback);
216  Object* reduce(EventListener* callback);
217  Object* reduceRight(EventListener* callback);
218  static bool isArray(Object*) [[cheerp::static]];
219 };
220 
221 template<class T>
222 class TArray: public Array
223 {
224 public:
225  template<typename... Args>
226  TArray(Args... args):Array(args...)
227  {
228  }
229  T*& operator[](int index)
230  {
231  return (T*&)Array::operator[](index);
232  }
233  T* operator[](int index) const
234  {
235  return (T*)Array::operator[](index);
236  }
237 };
238 
239 class Map: public Object {
240 public:
241  Map();
242  int get_size();
243  void clear();
244  template<typename K, typename V, typename std::enable_if<
245  (std::is_arithmetic<K>::value || std::is_pointer<K>::value) &&
246  (std::is_arithmetic<V>::value || std::is_pointer<V>::value), int>::type = 0>
247  void set(K k, V v);
248  template<typename K, typename V, typename std::enable_if<
249  (std::is_arithmetic<K>::value || std::is_pointer<K>::value) &&
250  (std::is_arithmetic<V>::value || std::is_pointer<V>::value), int>::type = 0>
251  V get(K k);
252  template<typename K, typename std::enable_if<
253  (std::is_arithmetic<K>::value || std::is_pointer<K>::value), int>::type = 0>
254  bool has(K k);
255  template<typename K, typename std::enable_if<
256  (std::is_arithmetic<K>::value || std::is_pointer<K>::value), int>::type = 0>
257  bool delete_(K k)
258  {
259  bool res;
260  __asm__("%1.delete(%2)" : "=r"(res) : "r"(this), "r"(k));
261  return res;
262  }
263  void forEach(EventListener* callback);
264  //TODO: declare methods entries, keys and values
265 };
266 
267 template<typename K, typename V>
268 class TMap: public Map {
269  static_assert(std::is_arithmetic<V>::value || std::is_pointer<V>::value, "Value has to be pointer or arithmetic");
270  static_assert(std::is_arithmetic<K>::value || std::is_pointer<K>::value, "Key has to be pointer or arithmetic");
271  public:
272  TMap(): Map()
273  {
274  }
275  void set(K k, V v)
276  {
277  Map::set<K,V>(k,v);
278  }
279  V get(K k)
280  {
281  return Map::get<K,V>(k);
282  }
283  bool has(K k)
284  {
285  return Map::has<K>(k);
286  }
287  bool delete_(K k)
288  {
289  return Map::delete_<K>(k);
290  }
291 };
292 
293 class Number: public Object
294 {
295 public:
296  Number(double);
297  client::String* toString(int base = 10);
298 };
299 
300 class Function: public Object
301 {
302 public:
303  template<typename... Args>
304  Function(Args... args);
305  String* get_name();
306 };
307 
308 typedef unsigned int UnsignedShort;
309 typedef unsigned int UnsignedLong;
310 typedef signed int Long;
311 typedef double UnsignedLongLong;
312 typedef void Void;
313 typedef unsigned int Boolean;
314 typedef double Double;
315 typedef Object* Any;
316 
317 }
318 
319 #endif
TMap()
Definition: types.h:272
unsigned int UnsignedLong
Definition: types.h:309
T * operator[](int index) const
Definition: types.h:233
Definition: types.h:34
TArray(Args... args)
Definition: types.h:226
static client::String * fromUtf8(const char *in, size_t len=std::numeric_limits< size_t >::max())
Definition: types.h:125
void Void
Definition: types.h:312
bool has(K k)
Definition: types.h:283
String * toString()
bool delete_(K k)
Definition: types.h:287
Definition: clientlib.h:1245
Definition: jsobject.h:32
__attribute__((always_inline)) String *concat(Args &&... args)
Definition: types.h:72
Definition: types.h:171
T *& operator[](int index)
Definition: types.h:229
unsigned int UnsignedShort
Definition: types.h:308
Definition: clientlib.h:4724
Definition: types.h:39
Definition: clientlib.h:27
Definition: types.h:268
double length
Object * operator[](int index) const
Definition: types.h:180
Definition: types.h:239
double Double
Definition: types.h:314
unsigned int Boolean
Definition: types.h:313
String(const char *s) __attribute__((noinline))
Definition: types.h:65
Definition: types.h:293
Definition: types.h:300
bool delete_(K k)
Definition: types.h:257
double UnsignedLongLong
Definition: types.h:311
Object *& operator[](int index)
Definition: types.h:176
String(const wchar_t *s) __attribute__((noinline))
Definition: types.h:68
signed int Long
Definition: types.h:310