├── 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('
') + '
' + element.html()); 41 | 42 | if ($('#clear-check').is(':checked')) { 43 | // clears current mrb states 44 | webruby.close(); 45 | webruby = new WEBRUBY({print_level: 2}); 46 | } 47 | 48 | if ($('#clear-input').is(':checked')) { 49 | editor.setValue(''); 50 | } 51 | }); 52 | 53 | window.onbeforeunload = function () { 54 | webruby.close(); 55 | } 56 | }); 57 | }()); 58 | -------------------------------------------------------------------------------- /mruby.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Emscripten: mruby 6 | 7 | 19 | 20 | 21 | 22 | 23 | 24 |

25 | This is the mruby interpreter, compiled from C to JavaScript using Emscripten, 26 | running in your browser (without any plugins). 27 |

28 |

29 |

35 |

36 |
37 |
38 | Enter some mruby code: 39 | Submit

40 | Click this if you want to start a new irb(clear all current states) after submitting your code. 41 |
42 | Check this to clear editor after submitting. 43 |
44 |
5.times { puts "Ruby is awesome!" }
45 |
46 |
47 | 48 | 54 |
55 |
56 | 57 | 58 | 59 | --------------------------------------------------------------------------------