forked from opal/opal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopal-parser.rb
More file actions
63 lines (55 loc) · 1.45 KB
/
opal-parser.rb
File metadata and controls
63 lines (55 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# parser uses String#unpack
require 'corelib/string/unpack'
require 'opal/compiler'
require 'opal/erb'
require 'opal/version'
module Kernel
def eval(str)
str = Opal.coerce_to!(str, String, :to_str)
default_eval_options = { file: '(eval)', eval: true }
compiling_options = __OPAL_COMPILER_CONFIG__.merge(default_eval_options)
code = Opal.compile str, compiling_options
%x{
return (function(self) {
return eval(#{code});
})(self)
}
end
def require_remote(url)
%x{
var r = new XMLHttpRequest();
r.open("GET", url, false);
r.send('');
}
eval `r.responseText`
end
end
%x{
Opal.compile = function(str, options) {
if (options) {
options = Opal.hash(options);
}
return Opal.Opal.$compile(str, options);
};
Opal['eval'] = function(str, options) {
return eval(Opal.compile(str, options));
};
function run_ruby_scripts() {
var tag, tags = document.getElementsByTagName('script');
for (var i = 0, len = tags.length; i < len; i++) {
tag = tags[i];
if (tag.type === "text/ruby") {
if (tag.src) Opal.Kernel.$require_remote(tag.src);
if (tag.innerHTML) Opal.Kernel.$eval(tag.innerHTML);
}
}
}
if (typeof(document) !== 'undefined') {
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', run_ruby_scripts, false);
}
else {
window.attachEvent('onload', run_ruby_scripts);
}
}
}