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 #else
00038 #include <pthread.h>
00039 #endif
00040
00041 #ifdef CINVOKE_BUILD
00042 #include "config.h"
00043 #endif
00044
00045 #ifdef HAVE_GC_GC_H
00046 #define HAVE_GC_H
00047 #endif
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
00102 typedef void (*kite_compiled_func_t)(struct kite_thread_t *thread,
00103 struct kite_object_t *this,
00104 struct kite_object_t *args);
00105
00110 typedef struct kite_function_t {
00111 enum
00112 {
00113 FUNC_COMPILED ,
00114 FUNC_BYTECODE
00115 } functype ;
00116 void *funcptr ;
00117 struct kite_object_t *this ;
00118 int numargs ;
00119 struct kite_object_t *arginfo ;
00120 } kite_function_t;
00121
00126 struct kite_symtab_t
00127 {
00128 char *name ;
00129 char *docstring ;
00131 int global ;
00134 struct kite_symtab_t *left ;
00135 struct kite_symtab_t *right ;
00136 struct kite_object_t *value ;
00137 };
00138 typedef struct kite_symtab_t kite_symtab_t;
00139
00140 #include "kite_vm.h"
00147 typedef struct kite_object_t {
00148 #ifndef HAVE_GC_H
00149 unsigned int refcount ;
00150 kite_list_t *gc_entry ;
00151 #endif
00152 enum
00153 {
00154 OBJ_NULL ,
00155 OBJ_INTEGER ,
00156 OBJ_FLOAT ,
00157 OBJ_STRING ,
00158 OBJ_IDENT ,
00159 OBJ_LIST ,
00160 OBJ_BOOLEAN ,
00161 OBJ_METHOD ,
00162 OBJ_CLASS ,
00163 OBJ_INSTANCE
00164 } type ;
00165 kite_thread_t *owner_thread ;
00166 int shareable ;
00167 struct kite_object_t *parent ;
00168 union {
00169 long intvalue ;
00170 double floatvalue ;
00171 struct {
00172 int length ;
00173 char *string ;
00174 } stringvalue;
00175 struct {
00176 struct kite_object_t *car ;
00177 struct kite_object_t *cdr ;
00178 struct kite_object_t *iterator ;
00179 } listvalue;
00180 kite_function_t funcvalue ;
00181 kite_thread_t *threadvalue ;
00182 FILE *filevalue ;
00183 } builtin_data;
00184 struct {
00185 char *name ;
00186 char *docstring ;
00187 struct kite_object_t *inherit_from ;
00188 kite_symtab_t *properties ;
00189 #ifdef WIN32
00190 HANDLE mtx ;
00191 HMODULE dylib_hndl ;
00192 #else
00193 pthread_mutex_t *mtx ;
00194 void *dylib_hndl ;
00195 #endif
00196 } object_data;
00197 } kite_object_t;
00198
00204 KITE_EXPORT kite_object_t *kite_new_null(kite_thread_t *thd);
00205
00212 KITE_EXPORT kite_object_t *kite_new_integer(kite_thread_t *thd, long val);
00213
00220 KITE_EXPORT kite_object_t *kite_new_float(kite_thread_t *thd, double val);
00221
00228 KITE_EXPORT kite_object_t *kite_new_string(kite_thread_t *thd, char *val);
00229
00237 KITE_EXPORT kite_object_t *kite_new_string_with_length(kite_thread_t *thd, char *val, int length);
00238
00245 KITE_EXPORT kite_object_t *kite_new_ident(kite_thread_t *thd, char *val);
00246
00253 KITE_EXPORT kite_object_t *kite_new_boolean(kite_thread_t *thd, int val);
00254
00260 KITE_EXPORT kite_object_t *kite_new_list(kite_thread_t *thd);
00261
00269 KITE_EXPORT kite_object_t *kite_new_method_compiled(kite_thread_t *thd, kite_compiled_func_t func, int numargs);
00270
00281 KITE_EXPORT kite_object_t *kite_new_method_compiled_with_docs(
00282 kite_thread_t *thd, kite_compiled_func_t func, char* desc, int numargs, ...);
00283
00291 KITE_EXPORT kite_object_t *kite_new_method_bytecode(kite_thread_t *thd, kite_opcode_t *func, int numargs);
00292
00303 KITE_EXPORT kite_object_t *kite_new_method_bytecode_with_docs(
00304 kite_thread_t *thd, kite_opcode_t *func, char *desc, int numargs, ...);
00305
00313 KITE_EXPORT kite_object_t *kite_new_class(kite_thread_t *thd, kite_object_t *parent, char *name);
00314
00321 KITE_EXPORT kite_object_t *kite_new_instance(kite_thread_t *thd, kite_object_t *parent);
00322
00333 KITE_EXPORT kite_object_t *kite_new_instance_with_constructor
00334 (kite_thread_t *thd, kite_object_t *parent, kite_object_t *args);
00335
00343 KITE_EXPORT kite_object_t *kite_new_exception(kite_thread_t *thd, char *name, char *message);
00344
00349 KITE_EXPORT void kite_destruct_object(kite_thread_t *, kite_object_t **, int);
00350
00355 KITE_EXPORT void kite_destruct_object_nofree(kite_thread_t *, kite_object_t **, int);
00356
00361 KITE_EXPORT void kite_finalize_object(void *, void *);
00362
00371 KITE_EXPORT void kite_dereference_object(kite_object_t *obj);
00372
00382 KITE_EXPORT kite_object_t *kite_reference_object(kite_object_t *obj);
00383
00384
00385
00386
00387
00388
00397 KITE_EXPORT void kite_add_property
00398 (kite_thread_t *thd, kite_object_t *obj, char *name, int global, char *doc);
00399
00407 KITE_EXPORT void kite_add_method
00408 (kite_thread_t *thd, kite_object_t *obj, char *name, kite_object_t *method);
00409
00418 KITE_EXPORT void kite_add_operator
00419 (kite_thread_t *thd, kite_object_t *obj, int op, kite_object_t *method);
00420
00426 KITE_EXPORT void kite_set_docstring(kite_object_t *obj, char *str);
00427
00434 KITE_EXPORT char *kite_get_docstring(kite_object_t *obj);
00435
00443 KITE_EXPORT void kite_set_arginfo(kite_object_t *obj, kite_object_t *arginfo);
00444
00452 KITE_EXPORT void kite_remove_property
00453 (kite_thread_t *thd, kite_object_t *obj, char *name, int global);
00454
00462 KITE_EXPORT void kite_remove_method
00463 (kite_thread_t *thd, kite_object_t *obj, char *name, int numargs);
00464
00472 KITE_EXPORT void kite_remove_operator(kite_thread_t *thd, kite_object_t *obj, int op);
00473
00481 KITE_EXPORT void kite_set_property
00482 (kite_thread_t *thd, kite_object_t *obj, char *name, kite_object_t *value);
00483
00490 KITE_EXPORT int kite_exists_property(kite_object_t *obj, char *name);
00491
00498 KITE_EXPORT int kite_exists_method(kite_object_t *obj, char *name);
00499
00507 KITE_EXPORT int kite_exists_operator(kite_object_t *obj, int op);
00508
00516 KITE_EXPORT kite_object_t *kite_get_property(kite_object_t *obj, char *name);
00517
00525 KITE_EXPORT kite_object_t *kite_dereference_and_load(kite_thread_t *thd, char *name);
00526
00531 KITE_EXPORT void kite_set_dylib_path(char *path);
00532
00540 KITE_EXPORT void kite_loader_register
00541 (kite_thread_t *thd, char *name, void (*ptr)(kite_thread_t*, kite_object_t*));
00542
00543
00544
00545
00546
00556 #define KITE_FIND_PROPERTY(ret, obj, name, num) \
00557 KITE_FIND_IN_SYMTAB(ret, obj->object_data.properties, name, num)
00558
00567 #define KITE_FIND_METHOD(ret, obj, name, num) \
00568 KITE_FIND_IN_SYMTAB(ret, obj->object_data.properties, name, num)
00569
00579 #define KITE_FIND_IN_SYMTAB(ret, symtab, n, num) \
00580 { \
00581 ret = symtab; \
00582 while(ret) { \
00583 int r = strcmp(n, ret->name); \
00584 if (!r && ret->global == num) { \
00585 break; \
00586 } else if (r < 0 || (!r && ret->global > num)) ret = ret->left; \
00587 else ret = ret->right; \
00588 } \
00589 }
00590
00598 #define KITE_FIND_ANY_IN_SYMTAB(ret, symtab, n) \
00599 { \
00600 ret = symtab; \
00601 while(ret) { \
00602 int r = strcmp(n, ret->name); \
00603 if (!r) { \
00604 break; \
00605 } else if (r < 0) ret = ret->left; \
00606 else ret = ret->right; \
00607 } \
00608 }
00609
00610
00611
00612
00613
00618 kite_symtab_t *kite_new_symtab();
00619
00626 KITE_EXPORT void kite_symtab_insert
00627 (kite_thread_t *thd, kite_symtab_t **symt, kite_symtab_t *entry);
00628
00636 KITE_EXPORT void kite_symtab_remove
00637 (kite_thread_t *thd, kite_symtab_t **symt, char *name, int num);
00638
00644 KITE_EXPORT void kite_copy_symtab(kite_symtab_t **ret, kite_symtab_t *symt);
00645
00652 KITE_EXPORT void kite_destruct_symtab(kite_thread_t *thd, kite_symtab_t **symt, int ref);
00653
00662 KITE_EXPORT void kite_copy_symtab_elements
00663 (kite_thread_t *thd, kite_symtab_t **ret, kite_symtab_t *symt, kite_object_t *p);
00664
00665
00666
00667
00668
00675 KITE_EXPORT kite_object_t *kite_object_name(kite_thread_t *thd, kite_object_t *obj);
00676
00684 KITE_EXPORT kite_object_t *kite_boolean_object(kite_thread_t *thd, kite_object_t *obj);
00685
00693 KITE_EXPORT kite_object_t *kite_string_object(kite_thread_t *thd, kite_object_t *obj);
00694
00702 KITE_EXPORT kite_object_t *kite_int_object(kite_thread_t *thd, kite_object_t *obj);
00703
00711 KITE_EXPORT kite_object_t *kite_float_object(kite_thread_t *thd, kite_object_t *obj);
00712
00719 KITE_EXPORT kite_object_t *kite_list_object(kite_thread_t *thd, kite_object_t *obj);
00720
00721
00722
00723
00724
00731 KITE_EXPORT int kite_list_count(kite_thread_t *thd, kite_object_t *obj);
00732
00739 KITE_EXPORT void kite_append_list(kite_thread_t *thd, kite_object_t *list, kite_object_t *obj);
00740
00741
00742
00743
00744
00751 #define KITE_MODULE_INITIALIZER_NAME(name) \
00752 name ## _load_module
00753
00760 #define KITE_MODULE_INITIALIZER(name) \
00761 void KITE_MODULE_INITIALIZER_NAME(name) (kite_thread_t *, kite_object_t *); \
00762 void* name ## _load_module__wrap(kite_thread_t *thd) { \
00763 kite_loader_register(thd, #name, KITE_MODULE_INITIALIZER_NAME(name)); \
00764 return NULL; \
00765 } \
00766 void KITE_MODULE_INITIALIZER_NAME(name) (kite_thread_t *thread, kite_object_t *parent)
00767
00772 #define KITE_CLASS_METHOD(name) \
00773 void name(kite_thread_t *thd, kite_object_t *this, \
00774 kite_object_t *args)
00775
00779 #define KITE_NO_ARGS (void)args;
00780
00784 #define KITE_THIS_NOT_USED (void)this;
00785
00792 #define KITE_GET_METHOD_ARGUMENT(ret, n) KITE_GET_LIST_ELEMENT(ret, args, n)
00793
00801 #define KITE_GET_LIST_ELEMENT(ret, obj, n) \
00802 { \
00803 int cur = n - 1; \
00804 ret = obj; \
00805 while(ret && cur > 0) { \
00806 cur--; \
00807 ret = ret->builtin_data.listvalue.cdr; \
00808 } \
00809 if (ret) ret = ret->builtin_data.listvalue.car; \
00810 }
00811
00818 #define KITE_REPLACE_LIST_ELEMENT(list, n, obj) \
00819 { \
00820 int cur = n - 1; \
00821 kite_object_t *ret = obj; \
00822 while(ret && cur > 0) { \
00823 cur--; \
00824 if (KITE_LIST_CDR(ret) == NULL) { \
00825 kite_append_list(obj->owner_thread, list, kite_new_null(obj->owner_thread)); \
00826 } \
00827 ret = ret->builtin_data.listvalue.cdr; \
00828 } \
00829 if (ret) { \
00830 kite_dereference_object(KITE_LIST_CAR(ret)); \
00831 KITE_LIST_CAR(ret) = kite_reference_object(obj); \
00832 } else { \
00833 kite_append_list(obj->owner_thread, list, obj); \
00834 } \
00835 }
00836
00842 #define KITE_REMOVE_LIST_ELEMENT(list, n) \
00843 { \
00844 int cur = n - 1; \
00845 kite_object_t *ret = obj; \
00846 while(ret && cur > 0) { \
00847 cur--; \
00848 ret = ret->builtin_data.listvalue.cdr; \
00849 } \
00850 if (ret) { \
00851 kite_dereference_object(KITE_LIST_CAR(ret)); \
00852 if (KITE_LIST_CDR(ret)) { \
00853 kite_object_t *tmp = KITE_LIST_CDR(ret); \
00854 KITE_LIST_CAR(ret) = KITE_LIST_CAR(KITE_LIST_CDR(ret)); \
00855 KITE_LIST_CDR(ret) = KITE_LIST_CDR(KITE_LIST_CDR(ret)); \
00856 kite_dereference_object(tmp); \
00857 } else { \
00858 KITE_LIST_CAR(ret) = NULL; \
00859 } \
00860 } \
00861 }
00862
00869 #define KITE_IS_TYPE(val, t) ((val)->type == t)
00870
00876 #define KITE_GET_OBJECT_TYPE(val) ((val)->type)
00877
00885 #define KITE_GET_PARENT_OBJECT(val) ((val)->object_data.inherit_from)
00886
00893 #define KITE_GET_INTEGER(v) ((v)->builtin_data.intvalue)
00894
00901 #define KITE_GET_FLOAT(v) ((v)->builtin_data.floatvalue)
00902
00909 #define KITE_GET_STRING_VALUE(v) ((v)->builtin_data.stringvalue.string)
00910
00917 #define KITE_GET_STRING_LENGTH(v) ((v)->builtin_data.stringvalue.length)
00918
00926 #define KITE_SET_INTEGER(v, i) KITE_GET_INTEGER(v) = i
00927
00935 #define KITE_SET_FLOAT(v, i) KITE_GET_FLOAT(v) = i
00936
00943 #define KITE_LIST_CAR(v) ((v)->builtin_data.listvalue.car)
00944
00951 #define KITE_LIST_CDR(v) ((v)->builtin_data.listvalue.cdr)
00952
00953 #endif