1 <?xml version='1.0' encoding="ISO-8859-1"?> 2 <chapter id="chapter-intro"> 3 <title>Background</title> 4 5 <para> 6 GObject, and its lower-level type system, GType, are used by GTK+ and most GNOME libraries to 7 provide: 8 <itemizedlist> 9 <listitem><para>object-oriented C-based APIs and</para></listitem> 10 <listitem><para>automatic transparent API bindings to other compiled 11 or interpreted languages.</para></listitem> 12 </itemizedlist> 13 </para> 14 15 <para> 16 A lot of programmers are used to working with compiled-only or dynamically interpreted-only 17 languages and do not understand the challenges associated with cross-language interoperability. 18 This introduction tries to provide an insight into these challenges and briefly describes 19 the solution chosen by GLib. 20 </para> 21 22 <para> 23 The following chapters go into greater detail into how GType and GObject work and 24 how you can use them as a C programmer. It is useful to keep in mind that 25 allowing access to C objects from other interpreted languages was one of the major design 26 goals: this can often explain the sometimes rather convoluted APIs and features present 27 in this library. 28 </para> 29 30 <sect1> 31 <title>Data types and programming</title> 32 33 <para> 34 One could say (I have seen such definitions used in some textbooks on programming language theory) 35 that a programming language is merely a way to create data types and manipulate them. Most languages 36 provide a number of language-native types and a few primitives to create more complex types based 37 on these primitive types. 38 </para> 39 40 <para> 41 In C, the language provides types such as <emphasis>char</emphasis>, <emphasis>long</emphasis>, 42 <emphasis>pointer</emphasis>. During compilation of C code, the compiler maps these 43 language types to the compiler's target architecture machine types. If you are using a C interpreter 44 (I have never seen one myself but it is possible :), the interpreter (the program which interprets 45 the source code and executes it) maps the language types to the machine types of the target machine at 46 runtime, during the program execution (or just before execution if it uses a Just In Time compiler engine). 47 </para> 48 49 <para> 50 Perl and Python are interpreted languages which do not really provide type definitions similar 51 to those used by C. Perl and Python programmers manipulate variables and the type of the variables 52 is decided only upon the first assignment or upon the first use which forces a type on the variable. 53 The interpreter also often provides a lot of automatic conversions from one type to the other. For example, 54 in Perl, a variable which holds an integer can be automatically converted to a string given the 55 required context: 56 <programlisting> 57 my $tmp = 10; 58 print "this is an integer converted to a string:" . $tmp . "\n"; 59 </programlisting> 60 Of course, it is also often possible to explicitly specify conversions when the default conversions provided 61 by the language are not intuitive. 62 </para> 63 64 </sect1> 65 66 <sect1> 67 <title>Exporting a C API</title> 68 69 <para> 70 C APIs are defined by a set of functions and global variables which are usually exported from a 71 binary. C functions have an arbitrary number of arguments and one return value. Each function is thus 72 uniquely identified by the function name and the set of C types which describe the function arguments 73 and return value. The global variables exported by the API are similarly identified by their name and 74 their type. 75 </para> 76 77 <para> 78 A C API is thus merely defined by a set of names to which a set of types are associated. If you know the 79 function calling convention and the mapping of the C types to the machine types used by the platform you 80 are on, you can resolve the name of each function to find where the code associated to this function 81 is located in memory, and then construct a valid argument list for the function. Finally, all you have to 82 do is trigger a call to the target C function with the argument list. 83 </para> 84 85 <para> 86 For the sake of discussion, here is a sample C function and the associated 32 bit x86 87 assembly code generated by GCC on my Linux box: 88 <programlisting> 89 static void function_foo (int foo) 90 {} 91 92 int main (int argc, char *argv[]) 93 { 94 95 function_foo (10); 96 97 return 0; 98 } 99 100 push $0xa 101 call 0x80482f4 <function_foo> 102 </programlisting> 103 The assembly code shown above is pretty straightforward: the first instruction pushes 104 the hexadecimal value 0xa (decimal value 10) as a 32-bit integer on the stack and calls 105 <function>function_foo</function>. As you can see, C function calls are implemented by 106 gcc by native function calls (this is probably the fastest implementation possible). 107 </para> 108 109 <para> 110 Now, let's say we want to call the C function <function>function_foo</function> from 111 a Python program. To do this, the Python interpreter needs to: 112 <itemizedlist> 113 <listitem><para>Find where the function is located. This probably means finding the binary generated by the C compiler 114 which exports this function.</para></listitem> 115 <listitem><para>Load the code of the function in executable memory.</para></listitem> 116 <listitem><para>Convert the Python parameters to C-compatible parameters before calling 117 the function.</para></listitem> 118 <listitem><para>Call the function with the right calling convention.</para></listitem> 119 <listitem><para>Convert the return values of the C function to Python-compatible 120 variables to return them to the Python code.</para></listitem> 121 </itemizedlist> 122 </para> 123 124 <para> 125 The process described above is pretty complex and there are a lot of ways to make it entirely automatic 126 and transparent to C and Python programmers: 127 <itemizedlist> 128 <listitem><para>The first solution is to write by hand a lot of glue code, once for each function exported or imported, 129 which does the Python-to-C parameter conversion and the C-to-Python return value conversion. This glue code is then 130 linked with the interpreter which allows Python programs to call Python functions which delegate work to 131 C functions.</para></listitem> 132 <listitem><para>Another, nicer solution is to automatically generate the glue code, once for each function exported or 133 imported, with a special compiler which 134 reads the original function signature.</para></listitem> 135 <listitem><para>The solution used by GLib is to use the GType library which holds at runtime a description of 136 all the objects manipulated by the programmer. This so-called <emphasis>dynamic type</emphasis> 137 <footnote> 138 <para> 139 There are numerous different implementations of dynamic type systems: all C++ 140 compilers have one, Java and .NET have one too. A dynamic type system allows you 141 to get information about every instantiated object at runtime. It can be implemented 142 by a process-specific database: every new object created registers the characteristics 143 of its associated type in the type system. It can also be implemented by introspection 144 interfaces. The common point between all these different type systems and implementations 145 is that they all allow you to query for object metadata at runtime. 146 </para> 147 </footnote> 148 library is then used by special generic glue code to automatically convert function parameters and 149 function calling conventions between different runtime domains.</para></listitem> 150 </itemizedlist> 151 The greatest advantage of the solution implemented by GType is that the glue code sitting at the runtime domain 152 boundaries is written once: the figure below states this more clearly. 153 <figure> 154 <mediaobject> 155 <imageobject> <!-- this is for HTML output --> 156 <imagedata fileref="glue.png" format="PNG" align="center"/> 157 </imageobject> 158 <imageobject> <!-- this is for PDF output --> 159 <imagedata fileref="glue.jpg" format="JPG" align="center"/> 160 </imageobject> 161 </mediaobject> 162 </figure> 163 164 Currently, there exist at least Python and Perl generic glue code which makes it possible to use 165 C objects written with GType directly in Python or Perl, with a minimum amount of work: there 166 is no need to generate huge amounts of glue code either automatically or by hand. 167 </para> 168 169 <para> 170 Although that goal was arguably laudable, its pursuit has had a major influence on 171 the whole GType/GObject library. C programmers are likely to be puzzled at the complexity 172 of the features exposed in the following chapters if they forget that the GType/GObject library 173 was not only designed to offer OO-like features to C programmers but also transparent 174 cross-language interoperability. 175 </para> 176 177 </sect1> 178 179 </chapter> 180