-
Notifications
You must be signed in to change notification settings - Fork 7
/
types.h
1707 lines (1542 loc) · 45.8 KB
/
types.h
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
/*
* This file is part of the Hybris programming language interpreter.
*
* Copyleft of Simone Margaritelli aka evilsocket <evilsocket@gmail.com>
*
* Hybris is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hybris is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Hybris. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _HOBJECT_H_
# define _HOBJECT_H_
#include "common.h"
#include "gc.h"
#include "itree.h"
#include <stdarg.h>
#include <string>
#include <vector>
#include <pcre.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
using std::vector;
using std::string;
typedef unsigned char byte;
/*
* Pre declarations of useful types.
*/
class Node;
class MemorySegment;
typedef struct _vm_t vm_t;
typedef MemorySegment vmem_t;
typedef MemorySegment vframe_t;
/*
* Helper macro to obtain the address of a pointer
*/
#ifndef H_ADDRESS_OF
# define H_ADDRESS_OF(o) reinterpret_cast<ulong>(o)
#endif
/*
* Object memory attributes
*/
#define H_OA_NONE 0 // 00000000
#define H_OA_CONSTANT 1 // 00000001
/*
* This macro define an object header elements.
* Should be used in EVERY type declaration at the beginning
* of the object structure itself to be down-casted to a base object.
*
* type : type descriptor as pointer (for type checking)
* referenced : tell the vm to use a reference to this object instead of a clone
* gc_size : size in bytes of the entire object
* gc_mark : mark-&-sweep gc flag
* gc_count : number of times the object passed the garbage collection
* attributes : object memory attributes mask
*/
#define BASE_OBJECT_HEADER struct _object_type_t *type; \
bool referenced; \
size_t gc_size; \
bool gc_mark; \
size_t gc_count; \
size_t attributes
/*
* Default object header initialization macro .
*/
#define BASE_OBJECT_HEADER_INIT(t) gc_mark(false), \
referenced(false), \
gc_size(0), \
gc_count(0), \
attributes(H_OA_NONE), \
type(&t ## _Type)
/*
* This macro compare the object type structure pointer with a given
* type, assuming that all type structure pointers are declared as :
* static Object {type}_Type = { ... }
* to determine if the object it's of the given type .
*/
#define ob_is_typeof( o, t ) ((o)->type->code == (t ## _Type).code)
/*
* Determine whenever two objects are of the same type.
*/
#define ob_same_type( a, b ) ((a)->type->code == (b)->type->code)
/*
* Macros to assert an object type.
*/
#define ob_type_assert(o,t,f) if( o->type->code != t ){ \
hyb_error( H_ET_SYNTAX, "Unexpected '%s' variable for function " f ", expected '%s'", o->type->name, ob_type_to_string(t) ); \
}
#define ob_types_assert(o,t1,t2,f) if( o->type->code != t1 && o->type->code != t2 ){ \
hyb_error( H_ET_SYNTAX, "Unexpected '%s' variable for function " f ", expected '%s' or '%s'", o->type->name, ob_type_to_string(t1), ob_type_to_string(t2) ); \
}
#define ob_argv_type_assert( i, t, f ) ob_type_assert( vm_argv(i), t, f )
#define ob_argv_types_assert( i, t1, t2, f ) ob_types_assert( vm_argv(i), t1, t2, f )
/*
* Macros to declare a type and then to implement its type structure.
*
* NOTE: The DECLARE_TYPE macro can be used inside type modules too
* to use specific features of a type that it's being implemented
* elsewhere in the code.
*
* For instance :
*
* DECLARE_TYPE(String);
*
* return String_Type.from_int(me);
*/
#define DECLARE_TYPE(t) extern object_type_t t ## _Type
#define IMPLEMENT_TYPE(t) object_type_t t ## _Type =
/*
* Macro to downcast any type structure to the base one.
*/
#define ob_dcast(o) (Object *)(o)
/*
* Type checking and casting macros.
*/
#define ob_is_boolean(o) ob_is_typeof(o,Boolean)
#define ob_bool_ucast(o) ((Boolean *)(o))
#define ob_bool_val(o) (((Boolean *)(o))->value)
#define ob_is_int(o) ob_is_typeof(o,Integer)
#define ob_is_alias(o) ob_is_typeof(o,Alias)
#define ob_is_extern(o) ob_is_typeof(o,Extern)
#define ob_int_ucast(o) (Integer *)(o)
#define ob_alias_ucast(o) (Alias *)(o)
#define ob_extern_ucast(o) (Extern *)(o)
#define ob_int_val(o) (((Integer *)(o))->value)
#define ob_alias_val(o) (((Alias *)(o))->value)
#define ob_extern_val(o) (((Extern *)(o))->value)
#define ob_is_float(o) ob_is_typeof(o,Float)
#define ob_float_ucast(o) ((Float *)(o))
#define ob_float_val(o) (((Float *)(o))->value)
#define ob_is_char(o) ob_is_typeof(o,Char)
#define ob_char_ucast(o) ((Char *)(o))
#define ob_char_val(o) ((Char *)(o))->value
#define ob_is_string(o) ob_is_typeof(o,String)
#define ob_string_ucast(o) ((String *)(o))
#define ob_string_val(o) ((String *)(o))->value
#define ob_lpcstr_val(o) (ob_string_val(o).c_str())
#define ob_is_binary(o) ob_is_typeof(o,Binary)
#define ob_binary_ucast(o) ((Binary *)(o))
#define ob_is_vector(o) ob_is_typeof(o,Vector)
#define ob_vector_ucast(o) ((Vector *)(o))
#define ob_vector_val(o) (((Vector *)(o)))
#define ob_is_map(o) ob_is_typeof(o,Map)
#define ob_map_ucast(o) ((Map *)(o))
#define ob_map_val(o) (Map *)(o)
#define ob_is_struct(o) ob_is_typeof(o,Structure)
#define ob_struct_ucast(o) ((Structure *)(o))
#define ob_is_class(o) ob_is_typeof(o,Class)
#define ob_class_ucast(o) ((Class *)(o))
#define ob_class_val(o) ((Class *)o)
#define ob_is_reference(o) ob_is_typeof(o,Reference)
#define ob_ref_ucast(o) ((Reference *)(o))
#define ob_reference_val(o) ((Reference *)o)
#define ob_is_handle(o) ob_is_typeof(o,Handle)
#define ob_handle_ucast(o) ((Handle *)(o))
#define ob_handle_val(o) ((Handle *)o)->value
/*
* A macro to cast any pointer into an Integer representing its address.
*/
#define PTR_TO_INT_OBJ(p) gc_new_integer( H_ADDRESS_OF(p) )
/*
* Pre declaration to implement basic object type .
*/
struct _object_type_t;
/*
* Basic object used only to downcast more complex object types
*/
typedef struct _Object {
BASE_OBJECT_HEADER;
}
Object;
/*
* Function pointer prototype for objects builtin methods (if any).
*/
typedef Object * (*ob_type_builtin_method_t)( vm_t *, Object *, vframe_t * );
/*
* Type builtin methods map.
*/
typedef struct {
string name;
ob_type_builtin_method_t *method;
}
ob_builtin_method_t;
/*
* End marker for builtin methods map.
*/
#define OB_BUILIN_METHODS_END_MARKER { "", NULL }
/*
* Class for builtin methods lookup.
*/
class ob_builtin_methods_t : public ITree<ob_type_builtin_method_t> {
public :
INLINE ob_builtin_methods_t( ob_builtin_method_t methods[] ) : ITree<ob_type_builtin_method_t>() {
size_t i;
for( i = 0; methods[i].method != NULL; ++i ){
insert( (char *)methods[i].name.c_str(), methods[i].method );
}
}
};
/*
* Static array used for types with no builtin methods.
*/
static ob_builtin_method_t NO_BUILTIN_METHODS[] = { OB_BUILIN_METHODS_END_MARKER };
#ifndef H_ACCESS_SPECIFIER
enum access_t {
asPublic = 0,
asPrivate,
asProtected
};
# define H_ACCESS_SPECIFIER
#endif
/*
* Type function pointers descriptors .
*/
// get the type name of the object
typedef const char *(*ob_typename_function_t) ( Object * );
// get the n-th object referenced by this
typedef Object * (*ob_traverse_function_t) ( Object *, int );
// free object inner buffer if any
typedef void (*ob_free_function_t) ( Object * );
// return the size of the object, or its items if it's a collection
typedef size_t (*ob_size_function_t) ( Object * );
// serialize the object, if size = 0 ignore it and take the object default size
typedef byte * (*ob_serialize_function_t) ( Object *, size_t );
// deserialize the object from a buffer
typedef Object * (*ob_deserialize_function_t) ( Object *, byte *, size_t );
// write an object to a file descriptor
typedef Object * (*ob_to_fd_t) ( Object *, int, size_t );
// read an object from a file descriptor
typedef Object * (*ob_from_fd_t) ( Object *, int, size_t );
// {a} == {b} ?
typedef int (*ob_cmp_function_t) ( Object *, Object * );
// get the integer value rapresentation of the object
typedef long (*ob_ivalue_function_t) ( Object * );
// get the float value rapresentation of the object
typedef double (*ob_fvalue_function_t) ( Object * );
// get the logical value (true or false) rapresentation of the object
typedef bool (*ob_lvalue_function_t) ( Object * );
// get a string rapresentation of the object for interal use
typedef string (*ob_svalue_function_t) ( Object * );
// print the object to stdout
typedef void (*ob_print_function_t) ( Object *, int );
// read the object from stdin
typedef void (*ob_scanf_function_t) ( Object * );
// <operator>{a}
typedef Object * (*ob_unary_function_t) ( Object * );
// {a}<operator>{b}
typedef Object * (*ob_binary_function_t) ( Object *, Object * );
// {a}<operator{b} '=' {c}
typedef Object * (*ob_ternary_function_t) ( Object *, Object *, Object * );
// functions to manage structure and class attributes
typedef void (*ob_define_attribute_function_t) ( Object *, char *, access_t, bool );
typedef access_t (*ob_attribute_access_function_t) ( Object *, char * );
typedef bool (*ob_attribute_is_static_function_t) ( Object *, char * );
typedef void (*ob_set_attribute_access_function_t) ( Object *, char *, access_t );
typedef void (*ob_add_attribute_function_t) ( Object *, char * );
typedef Object * (*ob_get_attribute_function_t) ( Object *, char *, bool );
typedef void (*ob_set_attribute_function_t) ( Object *, char *, Object * );
// functions to manage class methods
typedef void (*ob_define_method_function_t) ( Object *, char *, Node * );
typedef Node * (*ob_get_method_function_t) ( Object *, char *, int );
typedef Object * (*ob_call_method_function_t) ( vm_t *, vframe_t *, Object *, char *, char *, Node * );
/*
* Object type codes enumeration.
* otEndMarker is used to mark the last allowed type in
* vm_function_t::types.
*/
enum H_OBJECT_TYPE {
otEndMarker = -1,
otVoid = 0,
otBoolean,
otInteger,
otFloat,
otChar,
otString,
otBinary,
otVector,
otMap,
otAlias,
otExtern,
otHandle,
otStructure,
otClass,
otReference
};
/*
* Set the object_type_t::size field to zero for collections, the
* ob_get_size will return the number of elements instead the field.
*/
#define OB_COLLECTION_SIZE 0
INLINE const char *ob_type_to_string( H_OBJECT_TYPE type ){
switch(type){
case otVoid : return "void";
case otBoolean : return "boolean";
case otInteger : return "integer";
case otFloat : return "float";
case otChar : return "char";
case otString : return "string";
case otBinary : return "binary";
case otVector : return "vector";
case otMap : return "map";
case otAlias : return "alias";
case otExtern : return "extern";
case otHandle : return "handle";
case otStructure : return "structure";
case otClass : return "class";
case otReference : return "reference";
}
/*
* We should never get here!
*/
assert(false);
}
/*
* Object type description structure .
*
* Each type implements its own functions to handle arithmetic, logical, ecc
* operations or sets them to 0 (NULL) in case the type can't handle that
* operation.
* Any type function pointer shouldn't be used directly but with its relative
* ob_* function to avoid sigsegvs on null function pointers.
*/
typedef struct _object_type_t {
/*
* Type code among H_OBJECT_TYPE enumeration values used
* for type comparision.
*/
H_OBJECT_TYPE code;
/*
* Type name.
*/
const char *name;
/*
* Type basic size, if the object is a collection size is set to 0
* and ob_get_size will return the number of its elements.
*/
size_t size;
/*
* Type builtin methods map.
*/
ob_builtin_methods_t builtin_methods;
/** generic function pointers **/
ob_typename_function_t type_name;
ob_traverse_function_t traverse;
ob_unary_function_t clone;
ob_free_function_t free;
ob_size_function_t get_size;
ob_serialize_function_t serialize;
ob_deserialize_function_t deserialize;
ob_to_fd_t to_fd;
ob_from_fd_t from_fd;
ob_cmp_function_t cmp;
ob_ivalue_function_t ivalue;
ob_fvalue_function_t fvalue;
ob_lvalue_function_t lvalue;
ob_svalue_function_t svalue;
ob_print_function_t print;
ob_scanf_function_t scanf;
ob_unary_function_t to_string;
ob_unary_function_t to_int;
ob_binary_function_t range;
ob_binary_function_t regexp;
/** arithmetic operators **/
ob_binary_function_t assign;
ob_unary_function_t factorial;
ob_unary_function_t increment;
ob_unary_function_t decrement;
ob_unary_function_t minus;
ob_binary_function_t add;
ob_binary_function_t sub;
ob_binary_function_t mul;
ob_binary_function_t div;
ob_binary_function_t mod;
ob_binary_function_t inplace_add;
ob_binary_function_t inplace_sub;
ob_binary_function_t inplace_mul;
ob_binary_function_t inplace_div;
ob_binary_function_t inplace_mod;
/** bitwise operators **/
ob_binary_function_t bw_and;
ob_binary_function_t bw_or;
ob_unary_function_t bw_not;
ob_binary_function_t bw_xor;
ob_binary_function_t bw_lshift;
ob_binary_function_t bw_rshift;
ob_binary_function_t bw_inplace_and;
ob_binary_function_t bw_inplace_or;
ob_binary_function_t bw_inplace_xor;
ob_binary_function_t bw_inplace_lshift;
ob_binary_function_t bw_inplace_rshift;
/** logic operators **/
ob_unary_function_t l_not;
ob_binary_function_t l_same;
ob_binary_function_t l_diff;
ob_binary_function_t l_less;
ob_binary_function_t l_greater;
ob_binary_function_t l_less_or_same;
ob_binary_function_t l_greater_or_same;
ob_binary_function_t l_or;
ob_binary_function_t l_and;
/** collection operators **/
ob_binary_function_t cl_push;
ob_binary_function_t cl_push_reference;
ob_unary_function_t cl_pop;
ob_binary_function_t cl_remove;
ob_binary_function_t cl_at;
ob_ternary_function_t cl_set;
ob_ternary_function_t cl_set_reference;
/** structure and class operators **/
ob_define_attribute_function_t define_attribute;
ob_attribute_access_function_t attribute_access;
ob_attribute_is_static_function_t attribute_is_static;
ob_set_attribute_access_function_t set_attribute_access;
ob_add_attribute_function_t add_attribute;
ob_get_attribute_function_t get_attribute;
ob_set_attribute_function_t set_attribute;
ob_set_attribute_function_t set_attribute_reference;
/** class operators **/
ob_define_method_function_t define_method;
ob_get_method_function_t get_method;
ob_call_method_function_t call_method;
}
object_type_t;
/**
* Those are the function interfaces that will check for function availability
* on each type, type compatibility, reference counting and so on .
* They MUST be used on every access on type functions instead of the function pointers directly.
*/
/*
* Macro to easily get a builtin method function pointer, given its name,
* from the inner ITree each type has .
*/
#define ob_get_builtin_method( obj, method_id ) obj->type->builtin_methods.find(method_id)
/*
* Return the type name of the object.
*/
const char *ob_typename( Object * o );
/*
* Get the n-th object referenced by 'o'.
*/
Object *ob_traverse( Object *o, int index );
/*
* Create a clone of the object.
*/
Object* ob_clone( Object *o );
/*
* Eventually free object inner elements (for colletions) and decrement its reference counter.
*/
bool ob_free( Object *o );
/*
* Return the size of the object or, in case it's a collection, the number of its elements.
*/
size_t ob_get_size( Object *o );
/*
* Serialize the object to a binary stream.
*/
byte * ob_serialize( Object *, size_t );
/*
* Deserialize the object from a binary stream.
*/
Object *ob_deserialize( Object *, byte *, size_t );
/*
* Write 'size' bytes of the object to a generic file descriptor, otherwise if
* size is not specified (size = 0), write it all.
*/
Object *ob_to_fd( Object *, int, size_t );
/*
* Read 'size' bytes from a generic file descriptor and create an object, if
* size is not specified (size = 0), read 'ob_get_size' bytes or until a newline
* byte is reached if the object is a string.
*/
Object *ob_from_fd( Object *, int, size_t );
/*
* Compare two objects, return :
*
* 0 if o == cmp
* 1 if o > cmp
* -1 if o < cmp
*/
int ob_cmp( Object *o, Object * cmp );
/*
* Get the integer representation of an object.
*/
long ob_ivalue( Object *o );
/*
* Get the float representation of an object.
*/
double ob_fvalue( Object *o );
/*
* Get the logical representation of an object, if object is a collection
* return ob_get_size(0) != 0.
*/
bool ob_lvalue( Object *o );
/*
* Get the string representation of an object.
*/
string ob_svalue( Object *o );
/*
* Print an object to stdout preceeded by 'tabs' tabs.
*/
void ob_print( Object *o, int tabs = 0 );
/*
* Read from stdin and create an object accordingly to the format.
*/
void ob_input( Object *o );
/*
* Convert an object to string type.
*/
Object *ob_to_string( Object *o );
/*
* Convert an object to integer type.
*/
Object *ob_to_int( Object *o );
/*
* Create a vector representing the range [a,b].
*/
Object *ob_range( Object *a, Object *b );
/*
* Apply a regular expression 'b' to the object 'a' and return
* matches with the operator a ~= b.
*/
Object *ob_apply_regexp( Object *a, Object *b );
/*
* Clone the object 'b', increment its references, call ob_free(a) and
* assign the clone to 'a'.
*/
Object *ob_assign( Object *a, Object *b );
/*
* Compute the factorial of the object.
*/
Object *ob_factorial( Object *o );
/*
* Increment the object value by 1.
*/
Object *ob_increment( Object *o );
/*
* Decrement the object value by 1.
*/
Object *ob_decrement( Object *o );
/*
* Return -o.
*/
Object *ob_uminus( Object *o );
/*
* Compute a + b.
*/
Object *ob_add( Object *a, Object *b );
/*
* Compute a - b.
*/
Object *ob_sub( Object *a, Object *b );
/*
* Compute a * b.
*/
Object *ob_mul( Object *a, Object *b );
/*
* Compute a / b.
*/
Object *ob_div( Object *a, Object *b );
/*
* Compute a % b.
*/
Object *ob_mod( Object *a, Object *b );
/*
* Compute a += b.
*/
Object *ob_inplace_add( Object *a, Object *b );
/*
* Compute a -= b.
*/
Object *ob_inplace_sub( Object *a, Object *b );
/*
* Compute a *= b.
*/
Object *ob_inplace_mul( Object *a, Object *b );
/*
* Compute a /= b.
*/
Object *ob_inplace_div( Object *a, Object *b );
/*
* Compute a %= b.
*/
Object *ob_inplace_mod( Object *a, Object *b );
/*
* Compute a & b.
*/
Object *ob_bw_and( Object *a, Object *b );
/*
* Compute a | b.
*/
Object *ob_bw_or( Object *a, Object *b );
/*
* Compute ~o.
*/
Object *ob_bw_not( Object *o );
/*
* Compute a ^ b.
*/
Object *ob_bw_xor( Object *a, Object *b );
/*
* Compute a << b.
*/
Object *ob_bw_lshift( Object *a, Object *b );
/*
* Compute a >> b.
*/
Object *ob_bw_rshift( Object *a, Object *b );
/*
* Compute a &= b.
*/
Object *ob_bw_inplace_and( Object *a, Object *b );
/*
* Compute a |= b.
*/
Object *ob_bw_inplace_or( Object *a, Object *b );
/*
* Compute a ^= b.
*/
Object *ob_bw_inplace_xor( Object *a, Object *b );
/*
* Compute a <<= b.
*/
Object *ob_bw_inplace_lshift( Object *a, Object *b );
/*
* Compute a >>= b.
*/
Object *ob_bw_inplace_rshift( Object *a, Object *b );
/*
* Compute !o.
*/
Object *ob_l_not( Object *o );
/*
* Compute a == b.
*/
Object *ob_l_same( Object *a, Object *b );
/*
* Compute a != b.
*/
Object *ob_l_diff( Object *a, Object *b );
/*
* Compute a < b.
*/
Object *ob_l_less( Object *a, Object *b );
/*
* Compute a > b.
*/
Object *ob_l_greater( Object *a, Object *b );
/*
* Compute a <= b.
*/
Object *ob_l_less_or_same( Object *a, Object *b );
/*
* Compute a >= b.
*/
Object *ob_l_greater_or_same( Object *a, Object *b );
/*
* Compute a || b.
*/
Object *ob_l_or( Object *a, Object *b );
/*
* Compute a && b.
*/
Object *ob_l_and( Object *a, Object *b );
/*
* Create a clone of 'b' and call ob_cl_push_reference( a, clone ) to
* push the clone inside the collection 'a'.
*/
Object *ob_cl_push( Object *a, Object *b );
/*
* Push the object 'b' inside the collection 'a', incrementing the
* reference counter of 'b'.
*/
Object *ob_cl_push_reference( Object *a, Object *b );
/*
* Remove the last element from the collection 'o' and return it.
*/
Object *ob_cl_pop( Object *o );
/*
* Remove the element at index/key 'b' from the collection 'a' and return it.
*/
Object *ob_cl_remove( Object *a, Object *b );
/*
* Get the item at index/key 'b' from the collection 'a'.
*/
Object *ob_cl_at( Object *a, Object *b );
/*
* Create a clone of 'c' and call ob_cl_set_reference( a, b, clone ) to
* put the clone at the index/key 'b' of the collection 'a'.
*/
Object *ob_cl_set( Object *a, Object *b, Object *c );
/*
* Put the object 'c' at the index/key 'b' inside the collection 'a' and
* increment 'c' reference counter.
*/
Object *ob_cl_set_reference( Object *a, Object *b, Object *c );
/*
* Return the access level for a given attribute.
*/
access_t ob_attribute_access( Object *o, char * a );
/*
* Return true if the given attribute exists and it's static,
* otherwise return false.
*/
bool ob_attribute_is_static( Object *o, char *a );
/*
* Define a new attribute inside the structure or the class.
*/
void ob_define_attribute( Object *o, char *name, access_t a, bool is_static = false );
/*
* Set the access level for a given attribute.
*/
void ob_set_attribute_access( Object *o, char *name, access_t a );
/*
* Define a new attribute 'a' for the structure 's'.
*/
void ob_add_attribute( Object *s, char *a );
/*
* Get the value of the attribute 'a' from the structure 's'.
*/
Object *ob_get_attribute( Object *s, char *a, bool with_descriptor = true );
/*
* Create a clone of 'v' and call ob_set_attribute_reference( s, a, clone )
* to set the value of the attribute 'a' inside the structure 's'.
*/
void ob_set_attribute( Object *s, char *a, Object *v );
/*
* Set the value of the attribute 'a' inside the structure 's' and increment
* 'v' reference counter.
*/
void ob_set_attribute_reference( Object *s, char *a, Object *v );
/*
* Define the method 'name' with give 'code' inside the 'c' class.
*/
void ob_define_method( Object *c, char *name, Node *code );
/*
* Get the method 'name' from the 'c' class with 'argc' arguments.
* If argc is not -1, the function will search for the "best match"
* among methods with the same name and returns it, otherwise will
* return the first method found.
*/
Node *ob_get_method( Object *c, char *name, int argc = -1 );
/*
* Execute a class method.
*/
Object *ob_call_method( vm_t *vm, vframe_t *frame, Object *owner, char *owner_id, char *method_id, Node *argv );
/**
* Types definition.
*/
DECLARE_TYPE(Boolean);
typedef struct _Boolean {
BASE_OBJECT_HEADER;
bool value;
_Boolean( bool v ) : BASE_OBJECT_HEADER_INIT(Boolean), value(v) {
}
}
Boolean;
DECLARE_TYPE(Integer);
typedef struct _Integer {
BASE_OBJECT_HEADER;
long value;
_Integer( long v ) : BASE_OBJECT_HEADER_INIT(Integer), value(v) {
}
}
Integer;
/*
* Alias and Extern types, are basically Integers, but used as local function
* pointer (the alias) and external (dynamically loaded dlls) functions
* pointer (the extern).
*/
DECLARE_TYPE(Alias);
typedef struct _Alias {
BASE_OBJECT_HEADER;
long value;
_Alias( long v ) : BASE_OBJECT_HEADER_INIT(Alias), value(v) {
}
}
Alias;
DECLARE_TYPE(Extern);
typedef struct _Extern {
BASE_OBJECT_HEADER;
long value;
_Extern( long v ) : BASE_OBJECT_HEADER_INIT(Extern), value(v) {
}
}
Extern;
DECLARE_TYPE(Float);
typedef struct _Float {
BASE_OBJECT_HEADER;
double value;
_Float( double v ) : BASE_OBJECT_HEADER_INIT(Float), value(v) {
}
}
Float;
DECLARE_TYPE(Char);
typedef struct _Char {
BASE_OBJECT_HEADER;
char value;
_Char( char v ) : BASE_OBJECT_HEADER_INIT(Char), value(v) {
}
}
Char;
DECLARE_TYPE(String);
size_t string_replace( string &source, const string find, string replace );
void string_parse_pcre( string& raw, string& regex, int& opts );
typedef struct _String {
BASE_OBJECT_HEADER;
size_t items;
string value;
_String( char *v ) : items(0), BASE_OBJECT_HEADER_INIT(String) {
value = v;
items = value.size();
}
}
String;
DECLARE_TYPE(Binary);
typedef struct _Binary {
BASE_OBJECT_HEADER;
size_t items;
vector<Object *> value;
_Binary() : items(0), BASE_OBJECT_HEADER_INIT(Binary) {
// define to test space reservation optimization
#ifdef RESERVED_VECTORS_SPACE
value.reserve( RESERVE_VECTORS_SPACE );
#endif
}
_Binary( vector<unsigned char>& data ) : items(0), BASE_OBJECT_HEADER_INIT(Binary) {
// define to test space reservation optimization
#ifdef RESERVED_VECTORS_SPACE
value.reserve( RESERVE_VECTORS_SPACE );
#endif
vector<unsigned char>::iterator i;
vv_foreach( vector<unsigned char>, i, data ){
ob_cl_push_reference( (Object *)this, (Object *)gc_new_char(*i) );
}
}
}
Binary;
typedef vector<Object *>::iterator BinaryIterator;
DECLARE_TYPE(Vector);
typedef struct _Vector {
BASE_OBJECT_HEADER;
size_t items;
vector<Object *> value;
_Vector() : items(0), BASE_OBJECT_HEADER_INIT(Vector) {
// define to test space reservation optimization
#ifdef RESERVED_VECTORS_SPACE
value.reserve( RESERVE_VECTORS_SPACE );
#endif
}
}
Vector;
typedef vector<Object *>::iterator VectorIterator;
DECLARE_TYPE(Map);
int map_find( Object *m, Object *key );
typedef struct _Map {
BASE_OBJECT_HEADER;
size_t items;
vector<Object *> keys;
vector<Object *> values;
_Map() : items(0), BASE_OBJECT_HEADER_INIT(Map) {
// define to test space reservation optimization
#ifdef RESERVED_VECTORS_SPACE
keys.reserve( RESERVE_VECTORS_SPACE );
values.reserve( RESERVE_VECTORS_SPACE );
#endif
}
}
Map;
typedef vector<Object *>::iterator MapIterator;
DECLARE_TYPE(Structure);
typedef struct _Structure {
BASE_OBJECT_HEADER;
size_t items;
ITree<Object> s_attributes;
_Structure() : items(0), BASE_OBJECT_HEADER_INIT(Structure) {
}
}
Structure;
typedef ITree<Object>::iterator StructureAttributeIterator;
DECLARE_TYPE(Class);
typedef struct _class_attribute_t {
string name;
bool is_static;
access_t access;
Object *value;
pthread_mutex_t mutex;
_class_attribute_t( string n, access_t a, Object *v, bool _static = false ) :
name(n),
access(a),
value(v),
is_static(_static) {
pthread_mutex_init( &mutex, NULL );
}
INLINE void lock(){
if( is_static ){ pthread_mutex_lock(&mutex); }
}
INLINE void unlock(){
if( is_static ){ pthread_mutex_unlock(&mutex); }
}
}
class_attribute_t;
typedef vector<Node *> prototypes_t;
typedef struct _class_method_t {
string name;
/*
* A class could have more methods with the same name but
* different parameters, so we have to hold a vector of Nodes.
*/
prototypes_t prototypes;
_class_method_t( string n, Node *m ) : name(n) {
prototypes.push_back(m);
}
_class_method_t( string n, prototypes_t& p ) : name(n), prototypes(p) {
}
}