lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I found a cool trick that I wanted to share... It is a short program that can be used by C and Lua at the same time. From the point of view of C it is as a header file you can #include. From the point of view of Lua, it is a module you can require(), which produces a string with the contents of said header file. I promise I have an excuse for why I did this!

   #define HACK \
   return [=[
       int foo() { return 42; }
   //]=]

How this works: In Lua, if the first line of the file starts with # then it is treated as a shebang line and skipped. In C, the backslash line continuation makes the second line be part of the `#define HACK` directive, which eats the opening [=[. The C99 comment at the end eats the closing ]=].

-- Hugo