A Little Background
I really love Ruby, yet I'm not that much into Rails. Rails is great indeed, I really learned so much while playing around with Rails. In fact, more than half of my knowledge using Ruby was learned reading the Rails source code. However, I just feel bored writing again and again applications working on a database-_- Whatever new features my app has and whatever facinating features Rails provide, basically I just keep writing code to creating entries in db, reading these entries out, updating and deleting them occasionally(Thank you, CRUD!). After all, the MVC architecture is there. It is simply not fun for me>_<
So I always wonder around looking for interesting stuffs outside of the Rails world. Celluloid, Gosu, Fluentd are all fun to play with. The latest toy around is mruby, a lightweight implementation Ruby. It works for embedded system and can be linked into existing softwares. Well, that sounds like Lua, but as a former-Lisp fan, Ruby is more interesting, isn't it? Sorry I've gone a little off-topic, I could write another post describing my feelings on different languages, but that's not the point today:)
mruby compiling process works like this:
- Use bison to parse
- Compile every
src, the generated object files are then archived into
tools/mrbc/mrbc.cand link with
lib/libmruby_core.cto generate the core mruby compiler
- Use the mruby compiler to standard libraries in
mrblib, the generated bytecode is attached as an array to
mrblib/init_mrblib.c. The newly created source code is called
mrblib/mrblib.cand add to
lib/libmruby_core.a. The result is called
We need to compile all source code
src together with the generated file
mrblib/mrblib.c. For simplicity and the unstable state of mruby, I just require building the entire mruby first in my Makefile:
C compiler vs C++ compiler
In short, it was a tough and mysterious road-_-
The first version compiles okay, but it keeps running into
RangeError: Maximum call stack size exceeded error. While browsing for wiki page of emscripten, one sentence in CodeGuidlinesAndLimitations caught my eye.
"Nonportable code that uses low-level features of the native environment, like native stack manipulation (e.g. in conjunction with setjmp/longjmp. We support normal setjmp/longjmp, but not with stack replacing etc.)."
I suspect this may be the reason, what supports my idea is one commit in emscripted-ruby, which is a port for ruby 1.8.7 onto the browser. It basically eliminates all setjmp/longjmp calls with c++ exception. So I decided this may be the direction and started coding right way. Well, now it turns out this is a mistake, I should've done more investigation. Anyway, you will see the mistakes I made.
Then I spent around ten hours figuring out a way to patch mruby to use c++ exception instead of setjmp/longjmp with all tests passed. The compiler is also changed from
em++. Well, now the mruby compiles and works:
This was Wednesday night and I went to read the FreeBSD book for exam. On Thursday morning, I accidentally compiles the code with
em++ but without my patches for setjmp. The code happened to work! Even if I revert the code back to the initial revision with all original build settings, I still cannot reproduce the RangeError error. Well, it remains a mystery and all I can guess is that something is wrong with my installation of Node.js at that day. It might use a different setting that day...
Then another problem came: when compiling with
emcc, the code compiles okay, but would run forever without terminating; It would only work when compiling with
em++... This is also strange, I will look into this later.
So the lesson I learned is: do more investigation before pumping out coding. There may be another reason.
Well, this is really fun. I've sent a post on emscripten-discuss. And I will spend some time on this.
Besides that, a few interesting follow-up works are already in my head:
- OpenGL ES 2/WebGL binding
- Repl in the browser
- MobiRuby for the browser(well, this may be too ambitious)
Anyway, it is really fun working on this.
If you read all the way to here(I would not read such a long article myself-_-), I want to say sorry for my English. I'm only fluent in Chinese, but I just want more people to be able to read about this:)