Hi! My name is Eduardo Ochs, I'm the
author of an Emacs package called eev,
and the title of this video is:
Two ways of creating ".so"s
for Lua, one of them very fast, both
using Emacs and eev.
And remember that a ".so"
is one thing that you can produce
from a C file - for example you can start
with a C file like this one, and then
compile it to a shared object and then it
can be dynamically loaded by other
programs - for example from Lua...
Let me start by the "very fast way"
way of producing a ".so", in which all the
details are hidden.
Suppose that I want to create a ".so"
that defines a Lua function called
"minusdiv", and the body of the Lua
function is this thing here, these three
lines here. We can run this eepitch block
here... eepitch is explained in this page,
by the way...
we run it by typing f8 on each line...
this thing here created a C file
with lots of things besides the body
of this function, and then compiled it
using GCC, and then listed the...
sorry, the file is in the directory
/tmp/ - this one... and the ".so" is here.
And it also loaded this file... so now the
function minusdiv is available... and if
we type f8 on this line here it says
that the result of of running minusdiv
with the arguments 20 and 2 is
18 and 10.
The details here are a bit boring...
they are all in a file called CLua1.lua,
that has an introduction... and then some
templates, and then the programs that use
those templates... and it also has some
demos in test blocks...
but it's better to start by this
other way here - the way 1, in which
everything is very explicit.
Suppose that I want to
create this file here... no, let
me start from the point in which the
the file is already created. let me open
this file here...
this is its code in C, this is the
body - these three lines here -
and somehow I created the rest of
the C code... and this thing here is
a test block - is something that I
can execute with f8s, and that will
compile the the ".c", the file in
which I am, generate a ".so", and test this
".so".
Let's run it here... note that
here we have two different blocks that
define environment variables, this one
and this one... if we execute them in order
then the second block will override the
definitions from the first one. I'm on a
Debian system, so that's what I want... but I
have a friend who is testing this on Mac,
and what he has to do is to run these three
three lines here and skip these other lines...
anyway, I want to run these
lines here,
then this thing here...
now I have my file ".c", my file
".so", and now I can run Lua and ask Lua
to load the ".so" and to run this
function here here
to install
the Lua functions that are defined
in C in that file...
and now the function minusdiv is
available, and I can run it.
Note that this thing
loads... this thing tells loadlib to
run a function with this name, and the
function with this name is
defined here...
and that function
runs this line to install all the
libraries listed in this table here, and
one of these lines
is an entry for a Lua function
called minusdiv... the C function
that corresponds to it is
this one, and it is defined here.
So it's a bit boring to write a C
file like this one - unless you do that
all the time and you remember all the
details. I don't... I don't use these
things often, so for me it was much
better to create some functions based
on templates that would generate
the files for me.
Let me show how these functions work.
If I run this function here
it creates a temporary buffer that will
generate a file with this name here,
that defines a function like this...
in the first moment the function will
be just a stub... we will have to replace
its body by something else.
Let me start by running this. This is my
temporary buffer, if I run this line
Emacs switches to C mode, so everything is
much easier to understand because
of colors...
and this line here will copy the rest
of the file to the kill ring
and will open this file here.
Usually this file here is empty...
let me delete this file...
so now I'm in the in the typical
situation, in which this file is empty...
and if I run this thing here
then this function, ee-copy-rest, copies
the rest of this file to the kill ring
and now I can go to this buffer here
type C-y, and I get a copy of this thing.
Now I need to
replace this body here by something else...
let me delete it, first let me go back...
I'm going to use a copy of these lines...
and I also want to adjust my test...
the test is this one, the test will be
this one... and now let's test this thing.
I need to execute this thing to compile
the C file and to generate a ".so"...
here it is, everything works...
and now I can run this Lua block
here... it loads the ".so" and tests this
function... so that's it.
Let me go back to the "very fast way".
All these things are
explained in one of the...
well, sorry... most of the
documentation of eev is in sandboxed
tutorials... the main one is this one here,
and note that you can write
anything here, and do tests here...
and this thing is in a
temporary buffer - it's not saved in any
file, so you can do any mess that you
want here... and we have an intro
called lua-tutorial. It's in a very
preliminary stage, and at this moment it
has... most of its sections are
about creating ".so" files...
and it has one section for using CLua1.lua
"from the inside", in the sense that...
in a sense that... oh, I'm without
internet here, so I can't run that demo
now... but the idea is that we
download three files in a subdirectory
of /tmp, and we adjust two environment
variables - one that tells Lua
which file to use as an init file, and
another one that says to Lua to add
that directory to the PATH...
and then the instructions will
explain how to use this thing here from
its test blocks...
for example, this block here
defines my main class, CLua, and if we
run this test block here...
I will need this smaller font
for a few minutes...
it loads the current file...
in this block here I have to choose
if I will compile on a Mac or on Debian...
I will choose Debian...
and now we have several tests that
explain... that show how the data
structures work, how the things work in
either in the lower-level functions
or in the higher-level functions...
and here we have some basic tests...
and here we have a higher-level function
that uses fewer arguments, and that
defines another way of creating a C file...
and now we can run this...
and in this block here we can check that
we can run buildandload twice
to define the function foo twice,
and in the first time the function foo
will have this definition,
in which it returns this string, "orig",
and in the second time we will redefine
that function...
so the function foo will be redefined
to be another function defined in C,
whose body is this, and that returns
the string "redefined"...
So we can use these blocks to test
several definitions of a Lua function
defined in C, and we can have a sequence
of blocks like that one...
and it's very easy to compare the
results of running, for example, this,
and running this...
and these lines here at the end just show
the templates corresponding to
the last execution of buildandload,
so we can see
what is the command for loadlib,
what would be the commands for require,
that I'm not not using now at
this moment, I'm using the lower-level
way of loading a C function...
and what are the commands for
compiling the ".c" into a ".so"...
and what is the code in C, that
is just this...
so, let me go back... this section of the
tutorial explains how to use CLua1.lua...
I mean, it explains how to
download CLua1 in a subdirectory of
/tmp/, and how to run the tests in CLua1...
and this other block here
explains how to use
the faster way "from anywhere".
Once we have CLua1 installed in /tmp/ -
I'm putting things in /tmp/ because
I think that most people are just
going to try this for 5 minutes, very few
people will make this more permanent, so
it's better to put everything in /tmp/...
and this second block here can be run
from anywhere... I'm not in /tmp now,
and I can run this and
create two definitions of the
function minusdiv - the first one with
this C body here, and the second one with
this C body here, and each one has
some tests...
and that's it. I didn't explain
everything in a lot of detail here
because I guess that most people are
going to prefer to read
the textual instructions in
this page...
and I'm going to to start working on
that now. So this video is an
introduction... and the precise
instructions will be here.
So, that's it!
Thanks! =)