Chapa 1)


Two ways of creating ".so"s for Lua, one very fast, both using Emacs and eev - subtitles

The main page about this video is here.
Its subtitles in Lua are here.
The rest of this page contains a conversion of the
subtitles in Lua to a slightly more readable format.
The screenshots below are not from the video - they're from here.

00:00 Hi! My name is Eduardo Ochs, I'm the
00:02 author of an Emacs package called eev,
00:04 and the title of this video is:
00:07 Two ways of creating ".so"s
00:10 for Lua, one of them very fast, both
00:13 using Emacs and eev.

00:15 And remember that a ".so"
00:18 is one thing that you can produce
00:21 from a C file - for example you can start
00:23 with a C file like this one, and then
00:26 compile it to a shared object and then it
00:30 can be dynamically loaded by other
00:32 programs - for example from Lua...

00:34 Let me start by the "very fast way"
00:38 way of producing a ".so", in which all the
00:42 details are hidden.

00:44 Suppose that I want to create a ".so"
00:46 that defines a Lua function called
00:47 "minusdiv", and the body of the Lua
00:53 function is this thing here, these three
00:55 lines here. We can run this eepitch block
00:59 here... eepitch is explained in this page,
01:03 by the way...

01:06 we run it by typing f8 on each line...
01:16 this thing here created a C file
01:20 with lots of things besides the body
01:23 of this function, and then compiled it
01:26 using GCC, and then listed the...
01:30 sorry, the file is in the directory
01:35 /tmp/ - this one... and the ".so" is here.
01:40 And it also loaded this file... so now the
01:43 function minusdiv is available... and if
01:46 we type f8 on this line here it says
01:49 that the result of of running minusdiv
01:51 with the arguments 20 and 2 is
01:55 18 and 10.

02:00 The details here are a bit boring...
02:03 they are all in a file called CLua1.lua,
02:07 that has an introduction... and then some
02:10 templates, and then the programs that use
02:13 those templates... and it also has some
02:16 demos in test blocks...

02:19 but it's better to start by this
02:22 other way here - the way 1, in which
02:24 everything is very explicit.

02:25 Suppose that I want to
02:28 create this file here... no, let
02:32 me start from the point in which the
02:35 the file is already created. let me open
02:37 this file here...

02:39 this is its code in C, this is the
02:44 body - these three lines here -
02:46 and somehow I created the rest of
02:50 the C code... and this thing here is
02:53 a test block - is something that I
02:56 can execute with f8s, and that will
02:59 compile the the ".c", the file in
03:04 which I am, generate a ".so", and test this
03:08 ".so".

03:10 Let's run it here... note that
03:13 here we have two different blocks that
03:15 define environment variables, this one
03:18 and this one... if we execute them in order
03:22 then the second block will override the
03:24 definitions from the first one. I'm on a
03:27 Debian system, so that's what I want... but I
03:29 have a friend who is testing this on Mac,
03:32 and what he has to do is to run these three
03:35 three lines here and skip these other lines...

03:38 anyway, I want to run these
03:41 lines here,
03:43 then this thing here...
03:45 now I have my file ".c", my file
03:49 ".so", and now I can run Lua and ask Lua
03:54 to load the ".so" and to run this
03:59 function here here
04:00 to install
04:03 the Lua functions that are defined
04:06 in C in that file...

04:08 and now the function minusdiv is
04:11 available, and I can run it.

04:13 Note that this thing
04:17 loads... this thing tells loadlib to
04:21 run a function with this name, and the
04:24 function with this name is
04:28 defined here...
04:33 and that function
04:39 runs this line to install all the
04:43 libraries listed in this table here, and
04:46 one of these lines
04:49 is an entry for a Lua function
04:52 called minusdiv... the C function
04:56 that corresponds to it is
04:59 this one, and it is defined here.

05:02 So it's a bit boring to write a C
05:05 file like this one - unless you do that
05:08 all the time and you remember all the
05:09 details. I don't... I don't use these
05:12 things often, so for me it was much
05:14 better to create some functions based
05:17 on templates that would generate
05:20 the files for me.

05:22 Let me show how these functions work.

05:26 If I run this function here
05:29 it creates a temporary buffer that will
05:33 generate a file with this name here,
05:36 that defines a function like this...
05:39 in the first moment the function will
05:41 be just a stub... we will have to replace
05:43 its body by something else.

05:46 Let me start by running this. This is my
05:50 temporary buffer, if I run this line
05:53 Emacs switches to C mode, so everything is
05:57 much easier to understand because
06:00 of colors...

06:03 and this line here will copy the rest
06:06 of the file to the kill ring
06:08 and will open this file here.

06:13 Usually this file here is empty...
06:16 let me delete this file...
06:21 so now I'm in the in the typical
06:24 situation, in which this file is empty...

06:28 and if I run this thing here
06:31 then this function, ee-copy-rest, copies
06:34 the rest of this file to the kill ring
06:37 and now I can go to this buffer here
06:39 type C-y, and I get a copy of this thing.

06:43 Now I need to
06:47 replace this body here by something else...
06:51 let me delete it, first let me go back...
06:54 I'm going to use a copy of these lines...

07:03 and I also want to adjust my test...
07:07 the test is this one, the test will be
07:15 this one... and now let's test this thing.

07:20 I need to execute this thing to compile
07:23 the C file and to generate a ".so"...
07:27 here it is, everything works...

07:29 and now I can run this Lua block
07:33 here... it loads the ".so" and tests this
07:38 function... so that's it.

07:42 Let me go back to the "very fast way".

07:45 All these things are
07:48 explained in one of the...
07:52 well, sorry... most of the
07:55 documentation of eev is in sandboxed
07:59 tutorials... the main one is this one here,
08:02 and note that you can write
08:06 anything here, and do tests here...

08:09 and this thing is in a
08:14 temporary buffer - it's not saved in any
08:16 file, so you can do any mess that you
08:18 want here... and we have an intro
08:22 called lua-tutorial. It's in a very
08:25 preliminary stage, and at this moment it
08:28 has... most of its sections are
08:32 about creating ".so" files...

08:36 and it has one section for using CLua1.lua
08:40 "from the inside", in the sense that...
08:49 in a sense that... oh, I'm without
08:52 internet here, so I can't run that demo
08:56 now... but the idea is that we
08:59 download three files in a subdirectory
09:01 of /tmp, and we adjust two environment
09:05 variables - one that tells Lua
09:09 which file to use as an init file, and
09:13 another one that says to Lua to add
09:16 that directory to the PATH...

09:18 and then the instructions will
09:25 explain how to use this thing here from
09:27 its test blocks...

09:29 for example, this block here
09:31 defines my main class, CLua, and if we
09:35 run this test block here...

09:40 I will need this smaller font
09:42 for a few minutes...

09:44 it loads the current file...

09:47 in this block here I have to choose
09:51 if I will compile on a Mac or on Debian...
09:54 I will choose Debian...

09:58 and now we have several tests that
10:01 explain... that show how the data
10:04 structures work, how the things work in
10:08 either in the lower-level functions
10:10 or in the higher-level functions...

10:13 and here we have some basic tests...
10:19 and here we have a higher-level function
10:25 that uses fewer arguments, and that
10:32 defines another way of creating a C file...
10:35 and now we can run this...

10:38 and in this block here we can check that
10:41 we can run buildandload twice
10:44 to define the function foo twice,

10:46 and in the first time the function foo
10:47 will have this definition,
10:49 in which it returns this string, "orig",
10:51 and in the second time we will redefine
10:55 that function...

11:00 so the function foo will be redefined
11:03 to be another function defined in C,
11:06 whose body is this, and that returns
11:09 the string "redefined"...

11:11 So we can use these blocks to test
11:14 several definitions of a Lua function
11:19 defined in C, and we can have a sequence
11:22 of blocks like that one...

11:25 and it's very easy to compare the
11:27 results of running, for example, this,
11:32 and running this...

11:36 and these lines here at the end just show
11:38 the templates corresponding to
11:43 the last execution of buildandload,
11:45 so we can see
11:46 what is the command for loadlib,
11:51 what would be the commands for require,
11:53 that I'm not not using now at
11:56 this moment, I'm using the lower-level
11:59 way of loading a C function...

12:03 and what are the commands for
12:06 compiling the ".c" into a ".so"...
12:10 and what is the code in C, that
12:14 is just this...

12:18 so, let me go back... this section of the
12:22 tutorial explains how to use CLua1.lua...
12:26 I mean, it explains how to
12:31 download CLua1 in a subdirectory of
12:35 /tmp/, and how to run the tests in CLua1...

12:40 and this other block here
12:44 explains how to use
12:46 the faster way "from anywhere".

12:51 Once we have CLua1 installed in /tmp/ -
12:54 I'm putting things in /tmp/ because
12:57 I think that most people are just
12:59 going to try this for 5 minutes, very few
13:02 people will make this more permanent, so
13:05 it's better to put everything in /tmp/...

13:08 and this second block here can be run
13:13 from anywhere... I'm not in /tmp now,
13:19 and I can run this and
13:25 create two definitions of the
13:28 function minusdiv - the first one with
13:31 this C body here, and the second one with
13:35 this C body here, and each one has
13:39 some tests...

13:44 and that's it. I didn't explain
13:48 everything in a lot of detail here
13:50 because I guess that most people are
13:52 going to prefer to read
13:53 the textual instructions in
13:57 this page...
14:00 and I'm going to to start working on
14:04 that now. So this video is an
14:07 introduction... and the precise
14:09 instructions will be here.
14:12 So, that's it!
14:15 Thanks! =)