├── README.md
├── events.js
└── mruby.html
/README.md:
--------------------------------------------------------------------------------
1 | This project runs a mruby interpreter in a browser. It depends on [webruby](https://github.com/xxuejie/webruby).
2 |
3 | A live demo of this project is at [http://qiezi.me/projects/mruby-web-irb/mruby.html](http://qiezi.me/projects/mruby-web-irb/mruby.html). Feel free to try it!
4 |
5 | # How to build an interpreter
6 |
7 | 1. Build [webruby](https://github.com/xxuejie/webruby) using any gem configuration. But remember to use loading mode 2.
8 |
9 | **NOTE**: If you do not know what is loading mode, feel free to ignore this since loading mode 2 is the default. However, a detailed description on loading mode is at [here](https://github.com/xxuejie/webruby/blob/master/rakelib/functions.rb#L3).
10 |
11 | 2. Copy the generated `webruby.js` file to current folder(and override existing file).
12 |
13 | 3. Now you are good to go!
--------------------------------------------------------------------------------
/events.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | var lines = [], printed = false, webruby, load_string_func;
3 |
4 | // Taken from http://stackoverflow.com/a/901144
5 | function getParameterByName(name) {
6 | name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
7 | var regex = new RegExp("[\\?&]" + name + "=([^]*)"),
8 | results = regex.exec(location.search);
9 | return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
10 | }
11 |
12 | function getQueryLevel() {
13 | var level = parseInt(getParameterByName('level')) || 2;
14 | level = Math.min(2, level);
15 | level = Math.max(0, level);
16 | return level;
17 | }
18 |
19 | window.Module = {};
20 | window.Module['print'] = function (x) {
21 | lines.push(x);
22 | printed = true;
23 | };
24 |
25 | $(document).ready(function() {
26 | webruby = new WEBRUBY({print_level: getQueryLevel()});
27 |
28 | $("#submit-button").click(function() {
29 | lines = [];
30 | printed = false;
31 |
32 | webruby.run_source(editor.getValue());
33 |
34 | if (!printed) {
35 | window.Module['print']('(no output)');
36 | }
37 |
38 | var element = $("#output");
39 | if (!element) return; // perhaps during startup
40 | element.html(lines.join('
') + '
25 | This is the mruby interpreter, compiled from C to JavaScript using Emscripten, 26 | running in your browser (without any plugins). 27 |
28 |29 |