C Modules

[ Start > PikeDevel > C Modules ] [ Edit this Page | Viewing Version 34 ]


Creating extensions to the Pike language is a fairly simple task. Extensions can be written in C or C++, and may be hand coded or written using a preprocessed version of C called CMOD. CMOD simplifies the writing of C language modules by providing a number of simplifications, such as automatic handling of method and class registrations, argument handling and so forth. There are a number of limitations to the CMOD preprocessor, but for the majority of extensions, CMOD will work just fine.

A number of (as yet incomplete) documents attempt to describe the API for writing modules:

However, it is often simpler to look at existing code for clues.

The following document provides information about the Pike module build system, and how to create new modules using it: Pike Module Build System

A stub module is available from Bill Welliver's HG Repo . This makes a nice starting point for "monger compliant" modules written either in Pike or C/CMOD.

Testsuites

Info about Writing Testsuites.

Data Structures

Methods
  • check_svalue
  • check_refs
  • add_ref
  • sub_ref
  • swap_values_unlocked
  • MAKE_CONST_STRING
  • array_free_index
  • make_shared_string
Topics
  1. how to allocate a string from C using Pike API
  2. how to create an array/mapping
  3. how to create an object deriving it from some class
  4. how to check whether an object passed to a function has a method you want to call
  5. how to set up a callback from C code
  6. how to invoke a standalone Pike function from C code: see Calling Pike Code
  7. how to invoke an object method from C code
  8. how to pass parameters to a Pike function
  9. how to retrieve the return value of the Pike function
  10. how is the Pike stack organized: see Pike_sp
  11. what are the possible parameters to the ADD_FUNCTION API
  12. what are the type contstants C code can use
  13. what are svalues and how do you use them
  14. how to allocate module-specific storage area
  15. error handling
Module (ie, Java "static") methods in a CMOD

You can have INIT, EXIT and PIKEFUN blocks outside of a PIKECLASS directive. Because modules are already instantiated as objects, this allows your module to have functions as well as classes.

Working with references

Pike utilizes garbage collection and reference counting to handle memory management of pike data objects (strings, arrays, objects, etc). When writing C/CMOD code, you need to tell pike that you explicitly want to keep data around once it passes out of your function, along with the corresponding process of telling pike that you're done with the object afterward. Pike provides methods for doing this (though some methods will add references automatically).

add_ref(svalue val) is used to add a reference to a variable.

free_string(pike_string val) and its corresponding friends is used to subtract references and will free the variable if there are no more references to that variable. The name can be confusing but it's best to think of it from the standpoint of your code not knowing whether other code is using your variable. You're just saying that "it's ok by me if you want to free this value." Pike will actually do that when everone who said they wanted to keep it no longer want the value.

Looping through Mappings

The NEW_MAPPING_LOOP macro allows you to loop through all elements in a mapping. Note that the keypair name (k) is required and is hard coded.

INT32 e; struct keypair *k; NEW_MAPPING_LOOP(mapping->data) { k->ind k->val // both are svalues }

To lookup a string index in a mapping, use the following:

struct svalue *sv;
struct mapping *m;
sv=simple_mapping_string_lookup(m,"name");

This returns the svalue corresponding to the index "name" in the mapping m.

Declaring a new class

This snippet shows the basics of declaration of a new class.

void pike_module_init(void)
{
...
start_new_program();
low_inherit(myparent_program, 0,0,0,0,0);
ADD_STORAGE(myclass_storage);
set_exit_callback( myclass_exit_callback );
ADD_FUNCTION("create", f_myclass__create,tFunc( tInt tInt,tVoid), 0);
add_program_constant("MyClass", (myclass_program=end_program()), 0);
...

Powered by PikeWiki2

 
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University