[ Prev ] [ Index ] [ Next ]

Blocks in C Externals

Created Wednesday 19 November 2008


Allow block variable to be inserted in C external using the @blc notation.


Many C libraries (especially expat, an XML parser) works by setting callbacks that are called when some events occurs. Currently, the binding of such libraries in Lisaac is a pain because:

For all these reasons we need a way to have those callback functions generated by the compiler.

Language additions

This specification states that including BLOCKs in C external would be possible. The following code would be allowed:

Section SELF

  + local_blc :{I1, I2; O1, O2};

Section Public

  - slot blc:{I1, I2; O1, O2} <-
  ( + blc2:{POINTER, I1, I2; O1, O2}
    + p :POINTER;

    local_blc := blc;

    blc2 := { (ptr:POINTER, i1:I1, i2:I2;
      + self :SELF;
      self := CONVERT[POINTER,SELF].on ptr;
      self.local_blc.value(i1, i2)
    p := CONVERT[SELF,POINTER].on Self;
    `define_callback(@blc2, @p)`;

You can notice two blocks there: blc and blc2. Because blc2 is included in a C external, a function matching the block would be automatically created (just as if the call to blc2.value were included in an External slot). This function would look like:

static O2 blc2__HT3(void* ptr_D98, I1 i1_ST5, I2 i2_KHX, O1 *o1_HIE){
  /* Pseudocode */
  SELF self_M5H;
  self_M5H = (SELF) ptr_D98;
  /* include the code nessesary for self.local_blc.value here */

Of course, inside blc2, we can't access the Self context. So it would not be allowed to access to the Self context (or perhaps, Self would just be equal to SELF although this could be confusing). Just as the same, local variables are not allowed either.

Note: some of these limitation could be removed if we make BLOCKs into closures (that can hold upvalues, that is local variables that persist after their scope has ended because a function is still using them).

Backlinks: :Computer:Lisaac:Specs :Computer:Lisaac:Specs:Future Work:Upvalues for BLOCKs