00001 #ifndef KITE_OBJS__KITEOBJECT_H
00002 #define KITE_OBJS__KITEOBJECT_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031
00032 #ifdef WIN32
00033 #undef __MINGW32__
00034 #define WIN32_LEAN_AND_MEAN
00035
00036 #include <windows.h>
00037 #ifdef HAVE_GC_GC_H
00038 #define HAVE_GC_H
00039 #endif
00040 #else
00041 #include <pthread.h>
00042 #endif
00043
00044 #ifdef CINVOKE_BUILD
00045 #include "config.h"
00046 #endif
00047
00048 #ifdef HAVE_GC_H
00049 #define GC_THREADS
00050 #ifdef WIN32
00051 #define WIN32_THREADS
00052 #define GC_NOT_DLL
00053
00054 #include <gc.h>
00055 #else
00056 #define GC_PTHREADS
00057 #ifndef __APPLE__
00058 void * GC_dlopen(const char *path, int mode);
00059 #else
00060 #define GC_DARWIN_THREADS
00061 #endif
00062 #include <gc/gc.h>
00063 #endif
00064 #endif
00065
00066
00067
00068
00069
00070 #ifndef FALSE
00071 #define FALSE 0
00072 #endif
00073 #ifndef TRUE
00074 #define TRUE 1
00075 #endif
00076
00077
00078 #ifdef WIN32
00079 #define DYLIB_EXTENSION ".dll"
00080 #define KITE_EXPORT __declspec(dllexport)
00081 #else
00082 #ifdef __APPLE__
00083 #define DYLIB_EXTENSION ".dylib"
00084 #else
00085 #define DYLIB_EXTENSION ".so"
00086 #endif
00087 #define KITE_EXPORT
00088 #endif
00089
00090
00091
00092
00093 struct kite_object_t;
00094 struct kite_thread_t;
00095 struct kite_opcode_t;
00096
00103 typedef void (*kite_compiled_func_t)(struct kite_thread_t *thread,
00104 struct kite_object_t *this,
00105 struct kite_object_t *args);
00106
00111 typedef struct kite_function_t {
00112 enum
00113 {
00114 FUNC_COMPILED ,
00115 FUNC_BYTECODE
00116 } functype ;
00117 void *funcptr ;
00118 struct kite_object_t *this ;
00119 int numargs ;
00120 struct kite_object_t *arginfo ;
00121 size_t length ;
00122 } kite_function_t;
00123
00128 struct kite_symtab_t
00129 {
00130 char *name ;
00131 char *docstring ;
00133 int global ;
00136 struct kite_symtab_t *left ;
00137 struct kite_symtab_t *right ;
00138 struct kite_object_t *value ;
00139 };
00140 typedef struct kite_symtab_t kite_symtab_t;
00141
00142 #include "kite_vm.h"
00143
00144 enum kite_object_type_t
00145 {
00146 OBJ_INTEGER ,
00147 OBJ_FLOAT ,
00148 OBJ_BOOLEAN ,
00149 OBJ_NUMBER_OF_BASE_TYPES ,
00150 OBJ_NULL = OBJ_NUMBER_OF_BASE_TYPES ,
00151 OBJ_STRING ,
00152 OBJ_IDENT ,
00153 OBJ_LIST ,
00154 OBJ_METHOD ,
00155 OBJ_CLASS ,
00156 OBJ_INSTANCE ,
00157 OBJ_NUMBER_OF_TYPES
00158 };
00159
00166 typedef struct kite_basic_object_t {
00167 enum kite_object_type_t type ;
00168 int shareable ;
00169 union {
00170 long intvalue ;
00171 double floatvalue ;
00172 } builtin_data;
00173 } kite_basic_object_t;
00174
00181 typedef struct kite_object_t {
00182 #ifndef HAVE_GC_H
00183 unsigned int refcount ;
00184 kite_list_t *gc_entry ;
00185 #endif
00186 enum kite_object_type_t type ;
00187 int shareable ;
00188 union {
00189 long intvalue ;
00190 double floatvalue ;
00191 struct {
00192 int length ;
00193 char *string ;
00194 } stringvalue;
00195 struct {
00196 struct kite_object_t *car ;
00197 struct kite_object_t *cdr ;
00198 struct kite_object_t *iterator ;
00199 } listvalue;
00200 kite_function_t funcvalue ;
00201 kite_thread_t *threadvalue ;
00202 FILE *filevalue ;
00203 } builtin_data;
00204 kite_thread_t *owner_thread ;
00205 struct kite_object_t *parent ;
00206 struct {
00207 char *name ;
00208 char *docstring ;
00209 struct kite_object_t *inherit_from ;
00210 kite_symtab_t *properties ;
00211 #ifdef WIN32
00212 HANDLE mtx ;
00213 HMODULE dylib_hndl ;
00214 #else
00215 pthread_mutex_t *mtx ;
00216 void *dylib_hndl ;
00217 #endif
00218 } object_data;
00219 } kite_object_t;
00220
00226 KITE_EXPORT kite_object_t *kite_new_null(kite_thread_t *thd);
00227
00234 KITE_EXPORT kite_object_t *kite_new_integer(kite_thread_t *thd, long val);
00235
00242 KITE_EXPORT kite_object_t *kite_new_float(kite_thread_t *thd, double val);
00243
00250 KITE_EXPORT kite_object_t *kite_new_string(kite_thread_t *thd, char *val);
00251
00259 KITE_EXPORT kite_object_t *kite_new_string_with_length(kite_thread_t *thd, char *val, int length);
00260
00267 KITE_EXPORT kite_object_t *kite_new_ident(kite_thread_t *thd, char *val);
00268
00275 KITE_EXPORT kite_object_t *kite_new_boolean(kite_thread_t *thd, int val);
00276
00282 KITE_EXPORT kite_object_t *kite_new_list(kite_thread_t *thd);
00283
00291 KITE_EXPORT kite_object_t *kite_new_method_compiled(kite_thread_t *thd, kite_compiled_func_t func, int numargs);
00292
00303 KITE_EXPORT kite_object_t *kite_new_method_compiled_with_docs(
00304 kite_thread_t *thd, kite_compiled_func_t func, char* desc, int numargs, ...);
00305
00313 KITE_EXPORT kite_object_t *kite_new_method_bytecode(kite_thread_t *thd, struct kite_opcode_t *func, int numargs);
00314
00325 KITE_EXPORT kite_object_t *kite_new_method_bytecode_with_docs(
00326 kite_thread_t *thd, struct kite_opcode_t *func, char *desc, int numargs, ...);
00327
00335 KITE_EXPORT kite_object_t *kite_new_class(kite_thread_t *thd, kite_object_t *parent, char *name);
00336
00343 KITE_EXPORT kite_object_t *kite_new_instance(kite_thread_t *thd, kite_object_t *parent);
00344
00355 KITE_EXPORT kite_object_t *kite_new_instance_with_constructor
00356 (kite_thread_t *thd, kite_object_t *parent, kite_object_t *args);
00357
00365 KITE_EXPORT kite_object_t *kite_new_exception(kite_thread_t *thd, char *name, char *message);
00366
00371 KITE_EXPORT void kite_destruct_object(kite_thread_t *, kite_object_t **, int);
00372
00377 KITE_EXPORT void kite_destruct_object_nofree(kite_thread_t *, kite_object_t **, int);
00378
00383 KITE_EXPORT void kite_finalize_object(void *, void *);
00384
00393 KITE_EXPORT void kite_dereference_object(kite_object_t *obj);
00394
00404 KITE_EXPORT kite_object_t *kite_reference_object(kite_object_t *obj);
00405
00406
00407
00408
00409
00410
00419 KITE_EXPORT void kite_add_property
00420 (kite_thread_t *thd, kite_object_t *obj, char *name, int global, char *doc);
00421
00429 KITE_EXPORT void kite_add_method
00430 (kite_thread_t *thd, kite_object_t *obj, char *name, kite_object_t *method);
00431
00440 KITE_EXPORT void kite_add_operator
00441 (kite_thread_t *thd, kite_object_t *obj, int op, kite_object_t *method);
00442
00448 KITE_EXPORT void kite_set_docstring(kite_object_t *obj, char *str);
00449
00456 KITE_EXPORT char *kite_get_docstring(kite_object_t *obj);
00457
00465 KITE_EXPORT void kite_set_arginfo(kite_object_t *obj, kite_object_t *arginfo);
00466
00474 KITE_EXPORT void kite_remove_property
00475 (kite_thread_t *thd, kite_object_t *obj, char *name, int global);
00476
00484 KITE_EXPORT void kite_remove_method
00485 (kite_thread_t *thd, kite_object_t *obj, char *name, int numargs);
00486
00494 KITE_EXPORT void kite_remove_operator(kite_thread_t *thd, kite_object_t *obj, int op);
00495
00503 KITE_EXPORT void kite_set_property
00504 (kite_thread_t *thd, kite_object_t *obj, char *name, kite_object_t *value);
00505
00512 KITE_EXPORT int kite_exists_property(kite_object_t *obj, char *name);
00513
00520 KITE_EXPORT int kite_exists_method(kite_object_t *obj, char *name);
00521
00529 KITE_EXPORT int kite_exists_operator(kite_object_t *obj, int op);
00530
00538 KITE_EXPORT kite_object_t *kite_get_property(kite_object_t *obj, char *name);
00539
00547 KITE_EXPORT kite_object_t *kite_dereference_and_load(kite_thread_t *thd, char *name);
00548
00553 KITE_EXPORT void kite_set_dylib_path(char *path);
00554
00562 KITE_EXPORT void kite_loader_register
00563 (kite_thread_t *thd, char *name, void (*ptr)(kite_thread_t*, kite_object_t*));
00564
00565
00566
00567
00568
00578 #define KITE_FIND_PROPERTY(ret, obj, name, num) \
00579 KITE_FIND_IN_SYMTAB(ret, obj->object_data.properties, name, num)
00580
00589 #define KITE_FIND_METHOD(ret, obj, name, num) \
00590 KITE_FIND_IN_SYMTAB(ret, obj->object_data.properties, name, num)
00591
00601 #define KITE_FIND_IN_SYMTAB(ret, symtab, n, num) \
00602 { \
00603 ret = symtab; \
00604 while(ret) { \
00605 int r = strcmp(n, ret->name); \
00606 if (!r && ret->global == num) { \
00607 break; \
00608 } else if (r < 0 || (!r && ret->global > num)) ret = ret->left; \
00609 else ret = ret->right; \
00610 } \
00611 }
00612
00620 #define KITE_FIND_ANY_IN_SYMTAB(ret, symtab, n) \
00621 { \
00622 ret = symtab; \
00623 while(ret) { \
00624 int r = strcmp(n, ret->name); \
00625 if (!r) { \
00626 break; \
00627 } else if (r < 0) ret = ret->left; \
00628 else ret = ret->right; \
00629 } \
00630 }
00631
00632
00633
00634
00635
00640 kite_symtab_t *kite_new_symtab();
00641
00648 KITE_EXPORT void kite_symtab_insert
00649 (kite_thread_t *thd, kite_symtab_t **symt, kite_symtab_t *entry);
00650
00658 KITE_EXPORT void kite_symtab_remove
00659 (kite_thread_t *thd, kite_symtab_t **symt, char *name, int num);
00660
00666 KITE_EXPORT void kite_copy_symtab(kite_symtab_t **ret, kite_symtab_t *symt);
00667
00674 KITE_EXPORT void kite_destruct_symtab(kite_thread_t *thd, kite_symtab_t **symt, int ref);
00675
00684 KITE_EXPORT void kite_copy_symtab_elements
00685 (kite_thread_t *thd, kite_symtab_t **ret, kite_symtab_t *symt, kite_object_t *p);
00686
00687
00688
00689
00690
00697 KITE_EXPORT kite_object_t *kite_object_name(kite_thread_t *thd, kite_object_t *obj);
00698
00706 KITE_EXPORT kite_object_t *kite_boolean_object(kite_thread_t *thd, kite_object_t *obj);
00707
00715 KITE_EXPORT kite_object_t *kite_string_object(kite_thread_t *thd, kite_object_t *obj);
00716
00724 KITE_EXPORT kite_object_t *kite_int_object(kite_thread_t *thd, kite_object_t *obj);
00725
00733 KITE_EXPORT kite_object_t *kite_float_object(kite_thread_t *thd, kite_object_t *obj);
00734
00741 KITE_EXPORT kite_object_t *kite_list_object(kite_thread_t *thd, kite_object_t *obj);
00742
00743
00744
00745
00746
00753 KITE_EXPORT int kite_list_count(kite_thread_t *thd, kite_object_t *obj);
00754
00761 KITE_EXPORT void kite_append_list(kite_thread_t *thd, kite_object_t *list, kite_object_t *obj);
00762
00763
00764
00765
00766
00773 #define KITE_MODULE_INITIALIZER_NAME(name) \
00774 name ## _load_module
00775
00782 #define KITE_MODULE_INITIALIZER(name) \
00783 void KITE_MODULE_INITIALIZER_NAME(name) (kite_thread_t *, kite_object_t *); \
00784 void* name ## _load_module__wrap(kite_thread_t *thd) { \
00785 kite_loader_register(thd, #name, KITE_MODULE_INITIALIZER_NAME(name)); \
00786 return NULL; \
00787 } \
00788 void KITE_MODULE_INITIALIZER_NAME(name) (kite_thread_t *thread, kite_object_t *parent)
00789
00794 #define KITE_CLASS_METHOD(name) \
00795 void name(kite_thread_t *thd, kite_object_t *this, \
00796 kite_object_t *args)
00797
00801 #define KITE_NO_ARGS (void)args;
00802
00806 #define KITE_THIS_NOT_USED (void)this;
00807
00814 #define KITE_GET_METHOD_ARGUMENT(ret, n) KITE_GET_LIST_ELEMENT(ret, args, n)
00815
00823 #define KITE_GET_LIST_ELEMENT(ret, obj, n) \
00824 { \
00825 int cur = n - 1; \
00826 ret = obj; \
00827 while(ret && cur > 0) { \
00828 cur--; \
00829 ret = ret->builtin_data.listvalue.cdr; \
00830 } \
00831 if (ret) ret = ret->builtin_data.listvalue.car; \
00832 }
00833
00840 #define KITE_REPLACE_LIST_ELEMENT(list, n, obj) \
00841 { \
00842 int cur = n - 1; \
00843 kite_object_t *ret = obj; \
00844 while(ret && cur > 0) { \
00845 cur--; \
00846 if (KITE_LIST_CDR(ret) == NULL) { \
00847 kite_append_list(obj->owner_thread, list, kite_new_null(obj->owner_thread)); \
00848 } \
00849 ret = ret->builtin_data.listvalue.cdr; \
00850 } \
00851 if (ret) { \
00852 kite_dereference_object(KITE_LIST_CAR(ret)); \
00853 KITE_LIST_CAR(ret) = kite_reference_object(obj); \
00854 } else { \
00855 kite_append_list(obj->owner_thread, list, obj); \
00856 } \
00857 }
00858
00864 #define KITE_REMOVE_LIST_ELEMENT(list, n) \
00865 { \
00866 int cur = n - 1; \
00867 kite_object_t *ret = obj; \
00868 while(ret && cur > 0) { \
00869 cur--; \
00870 ret = ret->builtin_data.listvalue.cdr; \
00871 } \
00872 if (ret) { \
00873 kite_dereference_object(KITE_LIST_CAR(ret)); \
00874 if (KITE_LIST_CDR(ret)) { \
00875 kite_object_t *tmp = KITE_LIST_CDR(ret); \
00876 KITE_LIST_CAR(ret) = KITE_LIST_CAR(KITE_LIST_CDR(ret)); \
00877 KITE_LIST_CDR(ret) = KITE_LIST_CDR(KITE_LIST_CDR(ret)); \
00878 kite_dereference_object(tmp); \
00879 } else { \
00880 KITE_LIST_CAR(ret) = NULL; \
00881 } \
00882 } \
00883 }
00884
00891 #define KITE_IS_TYPE(val, t) ((val)->type == t)
00892
00898 #define KITE_GET_OBJECT_TYPE(val) ((val)->type)
00899
00907 #define KITE_GET_PARENT_OBJECT(val) ((val)->object_data.inherit_from)
00908
00915 #define KITE_GET_INTEGER(v) (((kite_basic_object_t*)v)->builtin_data.intvalue)
00916
00923 #define KITE_GET_FLOAT(v) (((kite_basic_object_t*)v)->builtin_data.floatvalue)
00924
00931 #define KITE_GET_STRING_VALUE(v) ((v)->builtin_data.stringvalue.string)
00932
00939 #define KITE_GET_STRING_LENGTH(v) ((v)->builtin_data.stringvalue.length)
00940
00948 #define KITE_SET_INTEGER(v, i) KITE_GET_INTEGER(v) = i
00949
00957 #define KITE_SET_FLOAT(v, i) KITE_GET_FLOAT(v) = i
00958
00965 #define KITE_LIST_CAR(v) ((v)->builtin_data.listvalue.car)
00966
00973 #define KITE_LIST_CDR(v) ((v)->builtin_data.listvalue.cdr)
00974
00975 #endif