-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathobjectscript.h
More file actions
3435 lines (2738 loc) · 93 KB
/
objectscript.h
File metadata and controls
3435 lines (2738 loc) · 93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __OBJECT_SCRIPT_H__
#define __OBJECT_SCRIPT_H__
/******************************************************************************
* Copyright (C) 2012-2014 Evgeniy Golovin (evgeniy.golovin@unitpoint.ru)
*
* Please feel free to contact me at anytime,
* my email is evgeniy.golovin@unitpoint.ru, skype: egolovin
*
* Latest source code: https://github.com/unitpoint/objectscript
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <malloc/malloc.h>
#endif
#if !defined __GNUC__ || defined IW_SDK
#include <new.h>
#elif defined(__GNUC__)
#include <new>
#else
inline void *operator new(size_t, void * p){ return p; }
inline void operator delete(void *, void *){}
#endif
#if defined _MSC_VER && !defined IW_SDK
#include <vadefs.h>
#endif
#if INTPTR_MAX == INT32_MAX
#define OS_PLATFORM_BITS_VERSION
#elif INTPTR_MAX == INT64_MAX
#define OS_PLATFORM_BITS_VERSION OS_TEXT("-x64")
#else
#define OS_PLATFORM_BITS_VERSION
#endif
#if defined _DEBUG && !defined OS_RELEASE && !defined OS_DEBUG
#define OS_DEBUG
#endif
#ifdef OS_DEBUG
#define OS_DEBUG_VERSION OS_TEXT("-d")
#else
#define OS_DEBUG_VERSION
#endif
#define OS_VERSION OS_TEXT("2.6.5-rc") OS_PLATFORM_BITS_VERSION OS_DEBUG_VERSION
#define OS_COPYRIGHT OS_TEXT("OS ") OS_VERSION OS_TEXT(" Copyright (C) 2012-2015 by Evgeniy Golovin")
#define OS_OPENSOURCE OS_TEXT("ObjectScript is free and open source: https://github.com/unitpoint/objectscript")
// select ObjectScript number type here
#ifndef OS_NUMBER
// #define OS_NUMBER long double
#define OS_NUMBER double
// #define OS_NUMBER float // could be a bit faster
// #define OS_NUMBER int // not recomended, math.random returns float value [0..1]
#endif // OS_NUMBER
#define OS_MATH_POW_OPERATOR(a, b) (OS_NUMBER)::pow((double)(a), (double)(b))
// #define OS_MATH_MOD_OPERATOR(a, b) (OS_NUMBER)((OS_INT)(a) % (OS_INT)(b))
#define OS_MATH_MOD_OPERATOR(a, b) (OS_NUMBER)((double)(a) - ::floor((double)(a)/(double)(b))*(double)(b))
#define OS_CHAR char
#define OS_TEXT(s) s
#define OS_FUNC_VAR_NAME OS_TEXT("_F")
#define OS_THIS_VAR_NAME OS_TEXT("this")
#define OS_ENV_VAR_NAME OS_TEXT("_E")
#define OS_GLOBALS_VAR_NAME OS_TEXT("_G")
#define OS_INT8 signed char
#define OS_BYTE unsigned char
#define OS_INT16 short
#define OS_U16 unsigned short
#if defined __GNUC__
#include <inttypes.h>
#define OS_INT32 int32_t
#define OS_INT64 int64_t
#define OS_U32 uint32_t
#define OS_U64 uint64_t
#elif defined IW_SDK
#define OS_INT32 int32
#define OS_INT64 int64
#define OS_U32 uint32
#define OS_U64 uint64
#else
#define OS_INT32 __int32
#define OS_INT64 __int64
#define OS_U32 unsigned __int32
#define OS_U64 unsigned __int64
#endif
#ifdef OS_EMSCRIPTEN
#define OS_INT OS_INT32
#define OS_UINT OS_U32
#else
#define OS_INT OS_INT64 // dependence on OS_NUMBER
#define OS_UINT OS_U64 // dependence on OS_NUMBER
#endif
#define OS_MEMCMP ::memcmp
#define OS_MEMMOVE ::memmove
#define OS_MEMSET ::memset
#define OS_MEMCPY ::memcpy
#define OS_STRLEN ::strlen
#define OS_STRCMP ::strcmp
#define OS_STRNCMP ::strncmp
#define OS_STRCHR ::strchr
#define OS_STRSTR ::strstr
#define OS_VPRINTF ::vprintf
#define OS_PRINTF ::printf
#define OS_OUTPUT(buf, size) fwrite((const char*)buf, size, 1, stdout)
#define OS_IS_SPACE(c) ((c) > OS_TEXT('\0') && (c) <= OS_TEXT(' '))
#define OS_IS_ALPHA ::isalpha
#define OS_IS_ALNUM(c) ((c) >= OS_TEXT('0') && (c) <= OS_TEXT('9'))
#define OS_IS_SLASH(c) ((c) == OS_TEXT('/') || (c) == OS_TEXT('\\'))
#define OS_CHAR_UPPER(c) toupper(c)
#define OS_CHAR_LOWER(c) tolower(c)
#define OS_TOP_STACK_NULL_VALUES 20
#define OS_DEF_FMT_BUF_LEN (1024*10)
#define OS_PATH_SEPARATOR OS_TEXT("/")
// uncomment it if need
// #define OS_INFINITE_LOOP_OPCODES 100000000
#ifdef OS_DEBUG
#define OS_DEF_MAX_CALL_STACK_SIZE 80
#else
#define OS_DEF_MAX_CALL_STACK_SIZE 200
#endif
#define OS_COMPILED_HEADER OS_TEXT("OBJECTSCRIPT")
#define OS_EXT_SOURCECODE OS_TEXT(".os")
#define OS_EXT_TEMPLATE OS_TEXT(".osh")
#define OS_EXT_TEMPLATE_HTML OS_TEXT(".html")
#define OS_EXT_TEMPLATE_HTM OS_TEXT(".htm")
#define OS_EXT_COMPILED OS_TEXT(".osc")
#define OS_EXT_TEXT_OPCODES OS_TEXT(".ost")
#ifdef OS_DEBUG
#define OS_ASSERT assert
#define OS_DBG_FILEPOS_DECL , const OS_CHAR * dbg_filename, int dbg_line
#define OS_DBG_FILEPOS_PARAM , dbg_filename, dbg_line
#define OS_DBG_FILEPOS , __FILE__, __LINE__
#define OS_DBG_FILEPOS_START_DECL const OS_CHAR * dbg_filename, int dbg_line
#define OS_DBG_FILEPOS_START_PARAM dbg_filename, dbg_line
#define OS_DBG_FILEPOS_START __FILE__, __LINE__
#else
#define OS_ASSERT(a)
#define OS_DBG_FILEPOS_DECL
#define OS_DBG_FILEPOS_PARAM
#define OS_DBG_FILEPOS
#define OS_DBG_FILEPOS_START_DECL
#define OS_DBG_FILEPOS_START_PARAM
#define OS_DBG_FILEPOS_START
#endif // OS_DEBUG
#if defined _MSC_VER // && !defined IW_SDK
#define DEBUG_BREAK __debugbreak()
#elif !defined __GNUC__
#include <signal.h>
#define DEBUG_BREAK raise(SIGTRAP)
// #define DEBUG_BREAK __builtin_trap()
#else
#define DEBUG_BREAK
#endif
#ifndef OS_PROFILE_BEGIN_OPCODE
#define OS_PROFILE_BEGIN_OPCODE(a)
#define OS_PROFILE_END_OPCODE(a)
#endif
#ifndef OS_PROFILE_BEGIN_GC
#define OS_PROFILE_BEGIN_GC
#define OS_PROFILE_END_GC
#endif
namespace ObjectScript
{
template <class T> struct FloatType { typedef float type; };
template <> struct FloatType<double> { typedef double type; };
template <> struct FloatType<long double> { typedef long double type; };
#define OS_FLOAT FloatType<OS_NUMBER>::type
class OS;
typedef void (*OS_UserdataDtor)(OS*, void * data, void * user_param);
typedef int (*OS_CFunction)(OS*, int params, int closure_values, int need_ret_values, void * user_param);
enum OS_ESettings
{
OS_SETTING_CREATE_TEXT_OPCODES,
OS_SETTING_CREATE_TEXT_EVAL_OPCODES,
OS_SETTING_CREATE_DEBUG_INFO,
OS_SETTING_CREATE_COMPILED_FILE,
OS_SETTING_PRIMARY_COMPILED_FILE,
OS_SETTING_SOURCECODE_MUST_EXIST,
};
enum OS_EValueType
{
// OS_VALUE_TYPE_UNKNOWN,
OS_VALUE_TYPE_NULL,
OS_VALUE_TYPE_BOOL,
OS_VALUE_TYPE_NUMBER,
OS_VALUE_TYPE_STRING, // min GC type, don't change order of value types
OS_VALUE_TYPE_ARRAY,
OS_VALUE_TYPE_OBJECT,
OS_VALUE_TYPE_USERDATA,
OS_VALUE_TYPE_USERPTR,
OS_VALUE_TYPE_FUNCTION,
OS_VALUE_TYPE_CFUNCTION
};
enum OS_ESourceCodeType
{
OS_SOURCECODE_AUTO,
OS_SOURCECODE_PLAIN,
OS_SOURCECODE_TEMPLATE
};
enum OS_EFileUseType
{
COMPILE_SOURCECODE_FILE,
LOAD_COMPILED_FILE
};
enum // OS_ValueRegister
{
OS_REGISTER_GLOBALS = 0x10000000,
OS_REGISTER_USERPOOL
};
enum
{
TERMINATED_EXCEPTION_CODE = 1<<30
};
enum OS_EOpcode
{
// binary operators
OP_LOGIC_PTR_EQ, // ===
OP_LOGIC_PTR_NE, // !==
OP_LOGIC_EQ, // ==
OP_LOGIC_NE, // !=
OP_LOGIC_GE, // >=
OP_LOGIC_LE, // <=
OP_LOGIC_GREATER, // >
OP_LOGIC_LESS, // <
OP_BIT_AND, // &
OP_BIT_OR, // |
OP_BIT_XOR, // ^
OP_COMPARE, // <=>
OP_ADD, // +
OP_SUB, // -
OP_MUL, // *
OP_DIV, // /
OP_MOD, // %
OP_LSHIFT, // <<
OP_RSHIFT, // >>
OP_POW, // **
OP_CONCAT, // ..
OP_IN, // in
OP_IS, // is
OP_AS, // as
// unary operators
OP_BIT_NOT, // ~
OP_PLUS, // +
OP_MINUS, // -
OP_LENGTH, // #
};
enum OS_ECallType
{
OS_CALLTYPE_AUTO,
OS_CALLTYPE_FUNC
};
enum OS_ECallEnter
{
OS_CALLENTER_ALLOW_ONLY_ENTER,
OS_CALLENTER_EXECUTE_AND_RETURN,
};
enum OS_ECallThisUsage
{
OS_CALLTHIS_FUNCTION_OVERWRITE,
OS_CALLTHIS_KEEP_STACK_VALUE,
};
class OS
#ifdef OBJECT_SCRIPT_EXTENDS_CLASS
: public OBJECT_SCRIPT_EXTENDS_CLASS
#endif
{
public:
class MemoryManager
{
protected:
int ref_count;
virtual ~MemoryManager();
public:
MemoryManager();
MemoryManager * retain();
void release();
virtual void * malloc(int size OS_DBG_FILEPOS_DECL) = 0;
virtual void free(void * p) = 0;
virtual void setBreakpointId(int id) = 0;
virtual int getAllocatedBytes() = 0;
virtual int getMaxAllocatedBytes() = 0;
virtual int getUsedBytes() = 0;
virtual int getCachedBytes() = 0;
};
struct Utils
{
enum ENumberParseType
{
PARSE_TOKEN, // 0x - hex, 0b - bin, 0 - octal, else - dec
PARSE_INT,
PARSE_FLOAT,
};
static bool parseFloat(const OS_CHAR *& str, OS_FLOAT& val, ENumberParseType parse_type, int int_radix = 10);
static OS_CHAR * numToStr(OS_CHAR*, OS_INT32 value);
static OS_CHAR * numToStr(OS_CHAR*, OS_INT64 value);
static OS_CHAR * numToStr(OS_CHAR*, OS_FLOAT value);
static OS_CHAR * numToStr(OS_CHAR*, OS_FLOAT value, int precision);
static OS_INT strToInt(const OS_CHAR*);
static OS_FLOAT strToFloat(const OS_CHAR*);
static int keyToHash(const void*, int size);
static int keyToHash(const void * buf1, int size1, const void * buf2, int size2);
static int cmp(const void * buf1, int len1, const void * buf2, int len2);
static double round(double a, int precision = 0);
};
class String;
class ObjectScriptExtention;
struct FileHandle {};
// protected:
template<class T>
struct Vector
{
T * buf;
int capacity;
int count;
Vector()
{
buf = NULL;
capacity = 0;
count = 0;
}
~Vector()
{
OS_ASSERT(!buf && !capacity && !count);
}
T& operator [] (int i)
{
OS_ASSERT(i >= 0 && i < count);
return buf[i];
}
const T& operator [] (int i) const
{
OS_ASSERT(i >= 0 && i < count);
return buf[i];
}
T& lastElement()
{
OS_ASSERT(count > 0);
return buf[count-1];
}
const T& lastElement() const
{
OS_ASSERT(count > 0);
return buf[count-1];
}
bool contains(const T& val) const
{
for(int i = count-1; i >= 0; i--){
if(buf[i] == val){
return true;
}
}
return false;
}
int indexOf(const T& val) const
{
for(int i = 0; i < count; i++){
if(buf[i] == val){
return i;
}
}
return -1;
}
};
template<class T> void vectorReserveCapacity(Vector<T>& vec, int new_capacity OS_DBG_FILEPOS_DECL)
{
if(vec.capacity < new_capacity){
vec.capacity = vec.capacity > 0 ? vec.capacity*2 : 4;
if(vec.capacity < new_capacity){
vec.capacity = new_capacity; // (capacity+3) & ~3;
}
T * new_buf = (T*)malloc(sizeof(T)*vec.capacity OS_DBG_FILEPOS_PARAM);
OS_ASSERT(new_buf);
for(int i = 0; i < vec.count; i++){
new (new_buf+i) T(vec.buf[i]);
vec.buf[i].~T();
}
free(vec.buf);
vec.buf = new_buf;
}
}
template<class T> void vectorReserveCapacityExact(Vector<T>& vec, int capacity OS_DBG_FILEPOS_DECL)
{
if(vec.capacity < capacity){
vec.capacity = capacity;
T * new_buf = (T*)malloc(sizeof(T)*vec.capacity OS_DBG_FILEPOS_PARAM);
OS_ASSERT(new_buf);
for(int i = 0; i < vec.count; i++){
new (new_buf+i) T(vec.buf[i]);
vec.buf[i].~T();
}
free(vec.buf);
vec.buf = new_buf;
}
}
template<class T> void vectorAddItem(Vector<T>& vec, const T& val OS_DBG_FILEPOS_DECL)
{
vectorReserveCapacity(vec, vec.count+1 OS_DBG_FILEPOS_PARAM);
new (vec.buf + vec.count++) T(val);
}
template<class T> void vectorClear(Vector<T>& vec)
{
for(int i = 0; i < vec.count; i++){
vec.buf[i].~T();
}
free(vec.buf);
vec.buf = NULL;
vec.capacity = 0;
vec.count = 0;
}
template<class T> void vectorReleaseItems(Vector<T>& vec)
{
for(int i = 0; i < vec.count; i++){
vec.buf[i]->release();
}
// free(vec.buf);
// vec.buf = NULL;
// vec.capacity = 0;
vec.count = 0;
}
template<class T> void vectorDeleteItems(Vector<T*>& vec)
{
for(int i = 0; i < vec.count; i++){
T * item = vec.buf[i];
item->~T();
free(item);
}
// free(vec.buf);
// vec.buf = NULL;
// vec.capacity = 0;
vec.count = 0;
}
template<class T> void vectorInsertAtIndex(Vector<T>& vec, int i, const T& val OS_DBG_FILEPOS_DECL)
{
OS_ASSERT(i >= 0 && i <= vec.count);
vectorReserveCapacity(vec, vec.count+1 OS_DBG_FILEPOS_PARAM);
for(int j = vec.count-1; j >= i; j--){
new (vec.buf+j+1) T(vec.buf[j]);
vec.buf[j].~T();
}
new (vec.buf+i) T(val);
vec.count++;
}
template<class T> void vectorRemoveAtIndex(Vector<T>& vec, int i)
{
OS_ASSERT(i >= 0 && i < vec.count);
// T val = vec.buf[i];
vec.buf[i].~T();
for(i++; i < vec.count; i++){
new (vec.buf+i-1) T(vec.buf[i]);
vec.buf[i].~T();
}
vec.count--;
// return val;
}
template<class T> void vectorPush(Vector<T>& vec, const T& val OS_DBG_FILEPOS_DECL)
{
vectorAddItem(vec, val OS_DBG_FILEPOS_PARAM);
}
template<class T> T vectorPop(Vector<T>& vec)
{
T ret = vec.lastElement();
vectorRemoveAtIndex(vec, vec.count-1);
return ret;
}
template<class T> void releaseObj(T *& obj)
{
if(--obj->ref_count <= 0){
OS_ASSERT(obj->ref_count == 0);
obj->~T();
free(obj);
obj = NULL;
}
}
template<class T> void deleteObj(T *& obj)
{
if(obj){
obj->~T();
free(obj);
obj = NULL;
}
}
template<class T> void destroyObj(T& obj)
{
obj.~T();
}
protected:
friend class OSMemoryManagerOld;
class Core
{
friend class OSMemoryManagerOld;
public:
class StreamReader;
class StreamWriter
{
public:
OS * allocator;
StreamWriter(OS*);
virtual ~StreamWriter();
virtual int getPos() const = 0;
virtual void setPos(int) = 0;
virtual int getSize() const = 0;
virtual void writeFromStream(StreamReader*);
virtual void writeBytes(const void*, int len) = 0;
virtual void writeBytesAtPos(const void*, int len, int pos) = 0;
virtual void writeByte(int);
virtual void writeByteAtPos(int value, int pos);
virtual void writeUVariable(int);
virtual void writeU16(int);
virtual void writeU16AtPos(int value, int pos);
virtual void writeInt8(int);
virtual void writeInt8AtPos(int value, int pos);
virtual void writeInt16(int);
virtual void writeInt16AtPos(int value, int pos);
virtual void writeInt32(int);
virtual void writeInt32AtPos(int value, int pos);
virtual void writeInt64(OS_INT64);
virtual void writeInt64AtPos(OS_INT64 value, int pos);
virtual void writeFloat(float);
virtual void writeFloatAtPos(float value, int pos);
virtual void writeDouble(double);
virtual void writeDoubleAtPos(double value, int pos);
virtual void writeLongDouble(long double);
virtual void writeLongDoubleAtPos(long double value, int pos);
};
class MemStreamWriter: public StreamWriter
{
public:
Vector<OS_BYTE> buffer;
int pos;
MemStreamWriter(OS*);
~MemStreamWriter();
int getPos() const;
void setPos(int);
int getSize() const;
void clear();
void reserveCapacity(int new_capacity);
void writeBytes(const void*, int len);
void writeBytesAtPos(const void*, int len, int pos);
void writeByte(int);
void writeByteAtPos(int value, int pos);
};
class FileStreamWriter: public StreamWriter
{
public:
FileHandle * f;
FileStreamWriter(OS*, const OS_CHAR * filename);
~FileStreamWriter();
int getPos() const;
void setPos(int);
int getSize() const;
void writeBytes(const void*, int len);
void writeBytesAtPos(const void*, int len, int pos);
};
class StreamReader
{
public:
OS * allocator; // if NULL then buffer will not be freed
StreamReader(OS*);
virtual ~StreamReader();
virtual int getPos() const = 0;
virtual void setPos(int) = 0;
virtual int getSize() const = 0;
virtual void movePos(int len) = 0;
virtual bool checkBytes(const void*, int len) = 0;
virtual void * readBytes(void*, int len) = 0;
virtual void * readBytesAtPos(void*, int len, int pos) = 0;
virtual OS_BYTE readByte();
virtual OS_BYTE readByteAtPos(int pos);
virtual int readUVariable();
virtual OS_U16 readU16();
virtual OS_U16 readU16AtPos(int pos);
virtual OS_INT8 readInt8();
virtual OS_INT8 readInt8AtPos(int pos);
virtual OS_INT16 readInt16();
virtual OS_INT16 readInt16AtPos(int pos);
virtual OS_INT32 readInt32();
virtual OS_INT32 readInt32AtPos(int pos);
virtual OS_INT64 readInt64();
virtual OS_INT64 readInt64AtPos(int pos);
virtual float readFloat();
virtual float readFloatAtPos(int pos);
virtual double readDouble();
virtual double readDoubleAtPos(int pos);
virtual long double readLongDouble();
virtual long double readLongDoubleAtPos(int pos);
};
class MemStreamReader: public StreamReader
{
public:
OS_BYTE * buffer;
int size;
// int pos;
OS_BYTE * cur;
// if allocator is NULL then buffer will not be freed
MemStreamReader(OS*, int buf_size);
MemStreamReader(OS*, OS_BYTE * buf, int buf_size);
~MemStreamReader();
int getPos() const;
void setPos(int);
int getSize() const;
void movePos(int len);
bool checkBytes(const void*, int len);
void * readBytes(void*, int len);
void * readBytesAtPos(void*, int len, int pos);
OS_BYTE readByte();
OS_BYTE readByteAtPos(int pos);
OS_INT8 readInt8();
OS_INT16 readInt16();
OS_INT32 readInt32();
};
class FileStreamReader: public StreamReader
{
public:
FileHandle * f;
FileStreamReader(OS*, const OS_CHAR * filename);
~FileStreamReader();
int getPos() const;
void setPos(int);
int getSize() const;
void movePos(int len);
bool checkBytes(const void*, int len);
void * readBytes(void*, int len);
void * readBytesAtPos(void*, int len, int pos);
};
struct GCStringValue;
class String
{
public:
#ifdef OS_DEBUG
const OS_CHAR * str;
#endif
GCStringValue * string;
OS * allocator;
String(OS*);
String(OS * os, GCStringValue*);
String(const String&);
String(OS*, const String&, const String&);
String(OS*, const OS_CHAR*);
String(OS*, const OS_CHAR*, int len);
String(OS*, const OS_CHAR*, int len, const OS_CHAR*, int len2);
String(OS*, const OS_CHAR*, int len, bool trim_left, bool trim_right);
String(OS*, const String&, bool trim_left, bool trim_right);
String(OS*, const void*, int size);
String(OS*, const void * buf1, int len1, const void * buf2, int len2);
String(OS*, const void * buf1, int len1, const void * buf2, int len2, const void * buf3, int len3);
String(OS*, OS_INT value);
String(OS*, OS_FLOAT value);
String(OS*, OS_FLOAT value, int precision);
~String();
static String format(OS*, int temp_buf_len, const OS_CHAR * fmt, ...);
static String formatVa(OS*, int temp_buf_len, const OS_CHAR * fmt, va_list va);
static String format(OS*, const OS_CHAR * fmt, ...);
static String formatVa(OS*, const OS_CHAR * fmt, va_list va);
const OS_CHAR * toChar() const { return string->toChar(); }
operator const OS_CHAR*() const { return string->toChar(); }
OS_CHAR operator[](int i)
{
if(i >= 0 && i < getLen()){
return toChar()[i];
}
return OS_TEXT('\0');
}
int getDataSize() const { return string->data_size; }
int getLen() const { return string->getLen(); }
bool isEmpty() const { return getDataSize() == 0; }
String& operator=(const String&);
bool operator==(const String&) const;
bool operator==(const OS_CHAR*) const;
bool operator==(GCStringValue*) const;
bool operator!=(const String&) const;
bool operator!=(const OS_CHAR*) const;
bool operator!=(GCStringValue*) const;
bool operator<=(const String&) const;
bool operator<=(const OS_CHAR*) const;
bool operator<(const String&) const;
bool operator<(const OS_CHAR*) const;
bool operator>=(const String&) const;
bool operator>=(const OS_CHAR*) const;
bool operator>(const String&) const;
bool operator>(const OS_CHAR*) const;
int cmp(const String&) const;
int cmp(const OS_CHAR*) const;
int getHash() const;
OS_NUMBER toNumber() const;
OS_NUMBER toNumberRadix(int radix) const;
};
class Buffer: public MemStreamWriter
{
protected:
Core::GCStringValue * cache_str;
OS * allocator;
public:
Buffer(OS*);
Buffer(const Buffer&);
~Buffer();
Buffer& append(OS_CHAR);
Buffer& append(const OS_CHAR*);
Buffer& append(const OS_CHAR*, int len);
Buffer& append(const void*, int size);
Buffer& append(const Core::String&);
Buffer& append(const Buffer&);
Buffer& operator+=(const Core::String&);
Buffer& operator+=(const OS_CHAR*);
operator Core::String();
Core::String toString();
OS::String toStringOS();
void clear();
Core::GCStringValue * toGCStringValue();
void freeCacheStr();
void swap(Buffer&);
};
class File
{
protected:
OS * os;
FileHandle * f;
public:
File(OS*);
virtual ~File();
bool open(const OS_CHAR * filename, const OS_CHAR * mode = "rb");
void close();
bool isOpen() const;
int getSize() const;
int getPos() const;
void setPos(int);
String read();
String read(int len);
int write(const void * data, int len);
int write(const Core::String&);
};
class Tokenizer
{
public:
enum Error
{
ERROR_NOTHING,
ERROR_MULTI_LINE_COMMENT, // multi line comment not end
ERROR_CONST_STRING, // string not end
ERROR_CONST_STRING_ESCAPE_CHAR, // string escape error
ERROR_SYNTAX
};
enum TokenType
{
NOTHING,
BEGIN_CODE_BLOCK, // {
END_CODE_BLOCK, // }
BEGIN_BRACKET_BLOCK, // (
END_BRACKET_BLOCK, // )
BEGIN_ARRAY_BLOCK, // [
END_ARRAY_BLOCK, // ]
CODE_SEPARATOR, // ;
PARAM_SEPARATOR, // ,
COMMENT_LINE,
COMMENT_MULTI_LINE,
NAME, // [a..z_$][a..z0..9_$]*
STRING, // ["].*?["]
OUTPUT_STRING,
OUTPUT_NEXT_VALUE,
REGEXP_STRING, // /.*?[^\\]/\w+
BEFORE_INJECT_VAR,
AFTER_INJECT_VAR,
NUMBER, // -?[0..9][.]?[0..9]+(e[+-]?[0..9]+)?
// [not real operators]
OPERATOR,
BINARY_OPERATOR,