1 /*
2  * Copyright (C) 2000-2005 Chris Ross and various contributors
3  * Copyright (C) 1999-2000 Chris Ross
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice, this
10  *   list of conditions and the following disclaimer.
11  * o Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * o Neither the name of the ferite software nor the names of its contributors may
15  *   be used to endorse or promote products derived from this software without
16  *   specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __FERITE_H__
32 #define __FERITE_H__
33 
34 #define FERITE_API  extern
35 #if defined(VCWIN32)
36 # undef  FERITE_API
37 # ifdef FERITELIB_EXPORTS
38 #  define FERITE_API __declspec(dllexport)
39 # else
40 #  define FERITE_API __declspec(dllimport)
41 # endif
42 #endif
43 
44 #define FE_USE_GENERATIONAL_GC 1
45 
46 #include <ferite/freq.h>
47 #include <ferite/fstructs.h>
48 
49 #ifdef __cplusplus
50 extern "C"
51 {
52 #endif
53 
54 #define FERITE_NAME       "ferite"
55 #define FERITE_VERSION    "1.0.2"
56 #define FERITE_MAJOR      1
57 #define FERITE_MINOR      0
58 #define FERITE_RELEASE    2
59 
60 FERITE_API int           ferite_init( int argc, char **argv );
61 FERITE_API int           ferite_deinit();
62 FERITE_API void          ferite_set_script_argv( int argc, char **argv );
63 FERITE_API FeriteScript *ferite_new_script();
64 FERITE_API int           ferite_script_load( FeriteScript *script, char *filename );
65 FERITE_API int           ferite_script_load_from_stream( FeriteScript *script, FILE *file );
66 FERITE_API FeriteScript *ferite_script_compile( char *filename );
67 FERITE_API FeriteScript *ferite_script_compile_with_path( char *filename, char **paths );
68 FERITE_API int           ferite_script_execute( FeriteScript *script );
69 FERITE_API FeriteScript *ferite_duplicate_script( FeriteScript *script );
70 FERITE_API int           ferite_script_delete( FeriteScript *script );
71 FERITE_API void          ferite_show_help();
72 
73 #define ferite_register_class( script, ns, name ) ferite_register_inherited_class( script, ns, name, NULL )
74 FERITE_API FeriteClass *ferite_register_inherited_class( FeriteScript *script, FeriteNamespace *ns, char *name, char *parent );
75 FERITE_API int ferite_register_class_function( FeriteScript *script, FeriteClass *classptr, FeriteFunction *f, int is_static );
76 FERITE_API int ferite_register_class_variable( FeriteScript *script, FeriteClass *classptr, FeriteVariable *variable, int is_static );
77 
78    /* user functions for getting parameters */
79 FERITE_API int ferite_get_parameters( FeriteVariable **list, int num_args, ... );
80 FERITE_API int ferite_get_parameter_count( FeriteVariable **list );
81 FERITE_API void *ferite_get_parameter( FeriteVariable **list, int num );
82 FERITE_API int ferite_get_required_parameter_list_size( FeriteScript *script, FeriteFunction *function );
83 
84    /* macros to cover internal functions */
85 #define fe_new_lng( name, val )             ferite_create_number_long_variable( script, name, val, FE_ALLOC )
86 #define fe_new_lng_static( name, val )    ferite_create_number_long_variable( script, name, val, FE_STATIC )
87 #define fe_new_dbl( name, val )             ferite_create_number_double_variable( script, name, val, FE_ALLOC )
88 #define fe_new_dbl_static( name, val )    ferite_create_number_double_variable( script, name, val, FE_STATIC )
89 #define fe_new_str( name, val, len, encoding )  ferite_create_string_variable_from_ptr( script, name, val, len, encoding, FE_ALLOC )
90 #define fe_new_str_static( name, val, len, encoding ) ferite_create_string_variable_from_ptr( script, name, val, len, encoding, FE_STATIC )
91 #define fe_new_obj( name )                  ferite_create_object_variable( script, name, FE_ALLOC )
92 #define fe_new_obj_static( name )    ferite_create_object_variable( script, name, FE_STATIC )
93 #define fe_new_array( name, size )          ferite_create_uarray_variable( script, name, size, FE_ALLOC )
94 #define fe_new_array_static( name, size )  ferite_create_uarray_variable( script, name, size, FE_STATIC )
95 #define fe_new_void( name )                 ferite_create_void_variable( script, name, FE_ALLOC )
96 #define fe_new_void_static( name )    ferite_create_void_variable( script, name, FE_STATIC )
97 
98 #define FE_STRLEN( var )      VAS(var)->length
99 #define FE_STR2PTR( var )      VAS(var)->data
100 
101 #define fe_create_ns_fnc( script, namespace, name, function, signature ) ferite_register_ns_function( script, namespace, ferite_create_external_function( script, name, function, signature ) )
102 #define fe_create_cls_fnc( script, class, name, function, signature, static ) ferite_register_class_function( script, class, ferite_create_external_function( script, name, function, signature ), static )
103 
104    /* macros to make writing functions easier */
105 #define FDECREF( obj ) obj->refcount--
106 #define FINCREF( obj )   obj->refcount++
107 
108    /* these are for return values */
109 
110 #define FE_FALSE 0
111 #define FE_TRUE  !FE_FALSE
112 
113 #define FE_NATIVE_FUNCTION( func_name ) \
114 	FERITE_API FeriteVariable *func_name ( FeriteScript *script, void *__container__, FeriteObject *current_yield_block, FeriteFunction *function, FeriteVariable **params )
115 
116 #define FE_CONTAINER_TO_OBJECT  (FeriteObject*)__container__
117 #define FE_CONTAINER_TO_CLASS   (FeriteClass*)__container__
118 #define FE_CONTAINER_TO_NS      (FeriteNamespace*)__container__
119 
120 #define FE_RETURN_VAR( var ) { \
121    FeriteVariable *ferite_function_return = var; \
122    if( !FE_VAR_IS_DISPOSABLE( ferite_function_return ) ) \
123      MARK_VARIABLE_AS_DISPOSABLE( ferite_function_return ); \
124    return ferite_function_return; }
125 
126 #define FE_RETURN_LONG( val ) \
127    FE_RETURN_VAR( ferite_create_number_long_variable( script,"external_function_return_int", val, FE_STATIC ) )
128 
129 #define FE_RETURN_DOUBLE( val ) \
130    FE_RETURN_VAR( ferite_create_number_double_variable( script, "external_function_return_float", val, FE_STATIC ) )
131 
132 #define FE_RETURN_VOID { \
133    FeriteVariable *ferite_function_return = ferite_create_void_variable( script, "external_function_return_void", FE_STATIC ); \
134    MARK_VARIABLE_AS_DISPOSABLE( ferite_function_return ); \
135    return ferite_function_return; }
136 
137 #define FE_RETURN_STR( str, freeme ) { \
138    FeriteVariable *ferite_function_return= ferite_create_string_variable( script, "external_function_return_string", str, FE_STATIC ); \
139    if( freeme == FE_TRUE ) { ferite_str_destroy( str ); } \
140    MARK_VARIABLE_AS_DISPOSABLE( ferite_function_return ); \
141    return ferite_function_return; }
142 
143 #define FE_RETURN_CSTR( str, freeme ) { \
144     char *__real__str__ = str; \
145     FeriteVariable *ferite_function_return= ferite_create_string_variable_from_ptr( script, "external_function_return_string", __real__str__, 0, FE_CHARSET_DEFAULT, FE_STATIC ); \
146     if( freeme == FE_TRUE ) { ffree( __real__str__ ); } \
147     MARK_VARIABLE_AS_DISPOSABLE( ferite_function_return ); \
148     return ferite_function_return; }
149 
150 #define FE_RETURN_TRUE    FE_RETURN_LONG(FE_TRUE)
151 #define FE_RETURN_FALSE   FE_RETURN_LONG(FE_FALSE)
152 
153 #define FE_RETURN_NULL_OBJECT \
154    FE_RETURN_VAR( ferite_create_object_variable( script, "external_function_return_null_object", FE_STATIC ) )
155 
156 #define FE_RETURN_OBJECT( val ) { \
157    FeriteVariable *ferite_function_return= ferite_create_object_variable( script, "external_function_return_object", FE_STATIC ); \
158    if( val != NULL ) { VAO(ferite_function_return) = val; val->refcount++; } \
159    MARK_VARIABLE_AS_DISPOSABLE( ferite_function_return ); \
160    return ferite_function_return; }
161 
162 #define FE_RETURN_ARRAY( val ) { \
163    FeriteVariable *ferite_function_return= ferite_create_uarray_variable( script, "external_function_return_array", val->size, FE_STATIC ); \
164    ferite_uarray_destroy( script, val ); \
165    VAUA(ferite_function_return) = ferite_uarray_dup( script, val, (void *(*)(FeriteScript*,FeriteVariable*))ferite_duplicate_variable ); \
166    MARK_VARIABLE_AS_DISPOSABLE( ferite_function_return ); \
167    return ferite_function_return; }
168 
169 #define FE_CLEAN_STRING(s)   fcalloc( s, sizeof(char) )
170 
171 #include <ferite/fthread.h>
172 #include <ferite/fglobals.h>
173 #include <ferite/fhash.h>
174 #include <ferite/fdebug.h>
175 #include <ferite/fstack.h>
176 #include <ferite/fns.h>
177 #include <ferite/fvariables.h>
178 #include <ferite/fregex.h>
179 #include <ferite/fops.h>
180 #include <ferite/fopcode.h>
181 #include <ferite/futils.h>
182 #include <ferite/fstring.h>
183 #include <ferite/fmem_jedi.h>
184 #include <ferite/fmem.h>
185 #include <ferite/fgc.h>
186 #include <ferite/ffunction.h>
187 #include <ferite/fmodule.h>
188 #include <ferite/fcompile.h>
189 #include <ferite/foop.h>
190 #include <ferite/ferror.h>
191 #include <ferite/farray.h>
192 #include <ferite/fbuffer.h>
193 
194 #include <ferite/fobj.h> /* As this is the native class 'Obj' we need the macros here for compilation!*/
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif /* __FERITE_H__ */
201