Friday, July 12, 2013

PEBL in a browser: 3. File system access


 This is third in a series of post about compiling PEBL into javascript using emscripten.


Like most moderately complex programs, PEBL uses files to store information it reads and enables it to run. For even the simplest programs, PEBL must have access to:
  • A script file which defines the experiment
  • A set of files containing located in pebl-lib containing PEBL-defined functions.
Also, if you want any images, text (which need fonts), or the like, these would also be stored in files.


The main obstacle here is that javascript by default doesn't/cannot have access to real files--it all must be embedded within the html page/javascript itself.  emscripten handles this by creating a virtual file system that you can compile into the page via command line arguments.  You can also create this filesystem separately, but I haven't played with this option yet.  So, if you need a file, you can simply specify it on the command line during compile time, using either the --embed-file or the --preload-file options:


~/src/emscripten-incoming/emcc -O2 --closure 0 bin/pebl.bc  --embed-file test.pbl --preload-file pebl.bmp --preload-file DejaVuSans.ttf --embed
-file pebl-lib  -o bin/pebl.html


Here, test.pbl is the script I'm running, pebl.bmp is an image file that test loads, DejaVuSans.ttf is a font file it expects, and pebl-lib is a directory containing various other .pbl files that the PEBL interpreter requires.  For some reason, I had mixed success using --preload-file and --embed-file, and it would only succeed compiling or running when I used --preoad-file on some of the larger files.

Now, there are a few changes I needed to make under the hood for this to work.  Because PEBL already targets multiple platforms and  different platforms have different requirements for file access, I just created a separate execution branch for many of the file access operations.  PEBL has its own path-search code, and because of some troubles finding files within the emscripten branch, I added added additional search locations (instead of just "./" as its root, I also now search "" as the root for any file you are looking for), but overall this was basically a copy of the linux branch, and also handled some other file system commands  I had implemented (GetHomeDirectory() and GetWorkingDirectory()

The biggest problem here was that PEBL by default processes a file specified on the command line.  I had some trouble making this work for some reason, and this is something I'll have to revisit more seriously later. The real issue is whether we should compile the base PEBL system into a single page and let it load whatever experiment file it wants, or if we create a new page for each new experiment we run.  For now, I've opted for the second--the emscripten version will ignore any command-line option and try to load ONLY the script file called test.pbl.

All-in-all, this seems to work for simple on-screen displays.  That is, if you don't care about user input, timing, or interactivity, you can create a display and draw it to the screen.  An example is here:  Note that this will probably run best (and maybe only run) on newer versions of Mozilla.

The next main problem is figuring out how to make PEBL 'asynchronous', the subject of the next post.

No comments: