Integrating with C code

Under the Hood:

How Twilight's generated C code works
and integration of Twilight code with native code:

Twilight generates native C code which
can be compiled with a C or C++ compiler. The code block:

inline(c) {
"""
/*Your C code here*/
"""
}

Allows you to integrate C code into the
build process. You can include headers by placing an inline(c) block
outside of all class definitions in a file with the required C code
(preprocessor #include<>s), you may add include directories to
the build path with -extInclude directives and externally libraries
with -extLib directives. To declare variables in a method include an
inline(c) which contains only the declarations and which begins with
/*-attr- -dec-*/, like so:

inline(c) {
"""
/*-attr- -dec-*/
TWLONG* twvl_long;
"""
}

The primitive types, int, long, float,
etc, are stored in the Twilight object in the slot defined with the
macro twrcps. A Twilight object is actually a void** ptr (void*
array) of slots where the 0 slot refers to the class definition
structure and the remaining slots refer to members. The exact
position of the first member may change in future, hence the macro
twrcps. For primitive classes there is a member, vint, vfloat, vchar,
vstring, etc, which has been defined as the first member which is
"holding" the position in the array, which is actually
being addressed directly. In the case of values which are not
themselves pointers, such as int, float, long, etc, the actually
value is stored in the position. This imposes the requirement that
the sizeof all of these types must be <= sizeof void* pointer,
which is the case on all target platforms (and most any sane
platform). The long type is "special" and is defined with
the macro TWLONG. It must be the set to the integral value which can
hold a pointer address, currently long on all platforms, will need to
be "long long" on Windows 64bit when that port completes.
Twilight should be fine on 64bit on all platforms except Windows at
present, although that has not been tested recently. In the case of
string or array the array position for twrcps is a pointer to the
char* or void** memory area which contains the string or array value.

For example, the method to decrement
(ival--) @Math:Int is:

sub decrement() @Int {
   inline(c) {
   """
/*-attr- -dec-*/
int* twvl_int;
   """
   }
   @Int toRet = copy();
   inline(c) {
   """
twvl_int = (int*) (twvl_toRet + twrcps);
(*twvl_int)--;
   """
   }
   return(toRet);
}

craig_welch2 – Sat, 2006 – 09 – 02 06:45