opal 0.3.15 → 0.3.16
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/Gemfile +8 -2
- data/README.md +1 -1
- data/Rakefile +51 -57
- data/bin/opal +50 -2
- data/{runtime/corelib → core}/alpha.rb +6 -1
- data/{runtime/corelib → core}/array.rb +38 -42
- data/{runtime/corelib → core}/basic_object.rb +6 -17
- data/core/boolean.rb +40 -0
- data/core/class.rb +69 -0
- data/{runtime/corelib → core}/comparable.rb +0 -0
- data/core/debug.js +59 -0
- data/core/debug.rb +35 -0
- data/core/dir.rb +90 -0
- data/core/enumerable.rb +331 -0
- data/core/enumerator.rb +126 -0
- data/core/error.rb +40 -0
- data/{runtime/corelib → core}/file.rb +6 -3
- data/core/gemlib.rb +30 -0
- data/{runtime/corelib → core}/hash.rb +37 -35
- data/{runtime/corelib → core}/io.rb +0 -0
- data/{runtime/corelib → core}/kernel.rb +26 -21
- data/{runtime/corelib → core}/load_order +2 -5
- data/{runtime/corelib → core}/match_data.rb +2 -4
- data/core/module.rb +204 -0
- data/{runtime/corelib → core}/nil_class.rb +1 -7
- data/{runtime/corelib → core}/numeric.rb +9 -13
- data/core/object.rb +17 -0
- data/{runtime/corelib → core}/proc.rb +2 -13
- data/{runtime/corelib → core}/range.rb +19 -7
- data/{runtime/corelib → core}/rational.rb +0 -0
- data/{runtime/corelib → core}/regexp.rb +2 -6
- data/core/runtime.js +672 -0
- data/{runtime/corelib → core}/string.rb +25 -9
- data/{runtime/corelib → core}/struct.rb +0 -0
- data/core/time.rb +111 -0
- data/{runtime/corelib → core}/top_self.rb +0 -0
- data/{runtime/spec → core_spec}/README.md +0 -0
- data/{runtime/spec → core_spec}/core/array/allocate_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/append_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/assoc_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/at_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/clear_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/collect_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/compact_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/concat_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/constructor_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/count_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/delete_at_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/delete_if_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/delete_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/each_index_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/each_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/element_reference_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/element_set_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/empty_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/eql_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/equal_value_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/fetch_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/first_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/fixtures/classes.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/flatten_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/include_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/insert_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/last_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/length_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/map_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/plus_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/pop_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/push_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/rassoc_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/reject_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/replace_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/reverse_each_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/reverse_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/shared/collect.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/shared/eql.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/shared/length.rb +0 -0
- data/{runtime/spec → core_spec}/core/array/shared/replace.rb +0 -0
- data/{runtime/spec → core_spec}/core/class/new_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/all_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/any_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/collect_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/count_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/detect_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/find_spec.rb +0 -0
- data/core_spec/core/enumerable/first_spec.rb +3 -0
- data/{runtime/spec → core_spec}/core/enumerable/fixtures/classes.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/shared/collect.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/shared/entries.rb +0 -0
- data/{runtime/spec → core_spec}/core/enumerable/shared/find.rb +0 -0
- data/core_spec/core/enumerable/shared/take.rb +31 -0
- data/{runtime/spec → core_spec}/core/enumerable/to_a_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/false/and_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/false/inspect_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/false/or_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/false/to_s_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/false/xor_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/allocate_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/assoc_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/clear_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/clone_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/default_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/delete_if_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/element_reference_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/element_set_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/hash/new_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/matchdata/to_a_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/and_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/inspect_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/nil_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/or_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/to_a_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/to_f_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/to_i_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/to_s_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/nil/xor_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/numeric/equal_value_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/object/is_a_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/object/shared/kind_of.rb +0 -0
- data/{runtime/spec → core_spec}/core/regexp/match_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/regexp/shared/match.rb +0 -0
- data/{runtime/spec → core_spec}/core/symbol/to_proc_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/true/and_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/true/inspect_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/true/or_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/true/to_s_spec.rb +0 -0
- data/{runtime/spec → core_spec}/core/true/xor_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/alias_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/and_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/array_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/block_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/break_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/case_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/defined_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/ensure_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/fixtures/block.rb +0 -0
- data/{runtime/spec → core_spec}/language/fixtures/break.rb +0 -0
- data/{runtime/spec → core_spec}/language/fixtures/defined.rb +0 -0
- data/{runtime/spec → core_spec}/language/fixtures/ensure.rb +0 -0
- data/{runtime/spec → core_spec}/language/fixtures/next.rb +0 -0
- data/{runtime/spec → core_spec}/language/fixtures/send.rb +0 -0
- data/{runtime/spec → core_spec}/language/fixtures/super.rb +0 -0
- data/{runtime/spec → core_spec}/language/hash_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/if_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/loop_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/next_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/or_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/predefined_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/regexp/interpolation_spec.rb +8 -0
- data/{runtime/spec → core_spec}/language/regexp_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/send_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/string_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/super_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/symbol_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/undef_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/unless_spec.rb +0 -0
- data/{runtime/spec → core_spec}/language/until_spec.rb +0 -0
- data/core_spec/language/variables_spec.rb +112 -0
- data/{runtime/spec → core_spec}/language/versions/hash_1.9.rb +0 -0
- data/{runtime/spec → core_spec}/language/while_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/defined_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/function_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/native_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/null_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/number_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/object_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/string_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/typeof_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/opal/undefined_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/true/case_compare_spec.rb +0 -0
- data/{runtime/spec → core_spec}/opal/true/class_spec.rb +0 -0
- data/{docs/spec_runner.html → core_spec/release_runner.html} +4 -3
- data/core_spec/runner.html +16 -0
- data/core_spec/spec_helper.rb +23 -0
- data/lib/opal.rb +85 -2
- data/lib/opal/builder.rb +129 -46
- data/lib/opal/context.rb +47 -26
- data/lib/opal/dependency_builder.rb +113 -14
- data/lib/opal/environment.rb +40 -45
- data/lib/opal/parser/grammar.rb +2296 -2254
- data/lib/opal/parser/grammar.y +27 -8
- data/lib/opal/parser/lexer.rb +12 -3
- data/lib/opal/parser/parser.rb +117 -30
- data/lib/opal/parser/scope.rb +2 -2
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +3 -8
- data/spec/grammar/masgn_spec.rb +37 -0
- metadata +177 -227
- data/index.html +0 -434
- data/lib/opal/command.rb +0 -73
- data/runtime/README.md +0 -25
- data/runtime/corelib/boolean.rb +0 -44
- data/runtime/corelib/class.rb +0 -43
- data/runtime/corelib/complex.rb +0 -2
- data/runtime/corelib/dir.rb +0 -29
- data/runtime/corelib/enumerable.rb +0 -316
- data/runtime/corelib/enumerator.rb +0 -80
- data/runtime/corelib/error.rb +0 -25
- data/runtime/corelib/module.rb +0 -171
- data/runtime/corelib/native.rb +0 -50
- data/runtime/corelib/object.rb +0 -21
- data/runtime/corelib/time.rb +0 -196
- data/runtime/gemlib/alpha.rb +0 -5
- data/runtime/gemlib/kernel.rb +0 -17
- data/runtime/gemlib/load_order +0 -2
- data/runtime/kernel/class.js +0 -256
- data/runtime/kernel/debug.js +0 -42
- data/runtime/kernel/init.js +0 -114
- data/runtime/kernel/load_order +0 -5
- data/runtime/kernel/loader.js +0 -151
- data/runtime/kernel/runtime.js +0 -414
- data/runtime/spec/language/variables_spec.rb +0 -28
- data/runtime/spec/library/stringscanner/scan_spec.rb +0 -36
- data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +0 -49
- data/runtime/spec/spec_helper.rb +0 -25
- data/runtime/stdlib/base64.rb +0 -91
- data/runtime/stdlib/date.rb +0 -4
- data/runtime/stdlib/dev.rb +0 -171
- data/runtime/stdlib/forwardable.rb +0 -33
- data/runtime/stdlib/optparse.rb +0 -0
- data/runtime/stdlib/pp.rb +0 -6
- data/runtime/stdlib/racc/parser.rb +0 -159
- data/runtime/stdlib/rbconfig.rb +0 -0
- data/runtime/stdlib/si.rb +0 -17
- data/runtime/stdlib/strscan.rb +0 -53
- data/runtime/stdlib/uri.rb +0 -111
- data/runtime/stdlib/uri/common.rb +0 -1014
- data/runtime/stdlib/uri/ftp.rb +0 -261
- data/runtime/stdlib/uri/generic.rb +0 -1599
- data/runtime/stdlib/uri/http.rb +0 -106
- data/runtime/stdlib/uri/https.rb +0 -22
- data/runtime/stdlib/uri/ldap.rb +0 -260
- data/runtime/stdlib/uri/ldaps.rb +0 -20
- data/runtime/stdlib/uri/mailto.rb +0 -280
- data/spec/builder/output_path_spec.rb +0 -50
data/core/runtime.js
ADDED
@@ -0,0 +1,672 @@
|
|
1
|
+
var opal = this.opal = {};
|
2
|
+
opal.global = this;
|
3
|
+
|
4
|
+
// Minify common function calls
|
5
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty,
|
6
|
+
$slice = Array.prototype.slice;
|
7
|
+
|
8
|
+
// Types - also added to bridged objects
|
9
|
+
var T_CLASS = 0x0001,
|
10
|
+
T_MODULE = 0x0002,
|
11
|
+
T_OBJECT = 0x0004,
|
12
|
+
T_BOOLEAN = 0x0008,
|
13
|
+
T_STRING = 0x0010,
|
14
|
+
T_ARRAY = 0x0020,
|
15
|
+
T_NUMBER = 0x0040,
|
16
|
+
T_PROC = 0x0080,
|
17
|
+
T_HASH = 0x0100,
|
18
|
+
T_RANGE = 0x0200,
|
19
|
+
T_ICLASS = 0x0400,
|
20
|
+
FL_SINGLETON = 0x0800;
|
21
|
+
|
22
|
+
// Generates unique id for every ruby object
|
23
|
+
var unique_id = 0;
|
24
|
+
|
25
|
+
// Find function body for the super call
|
26
|
+
function find_super(klass, callee, mid) {
|
27
|
+
var cur_method;
|
28
|
+
|
29
|
+
while (klass) {
|
30
|
+
if (klass.$m[mid]) {
|
31
|
+
if (klass.$m[mid] == callee) {
|
32
|
+
cur_method = klass.$m[mid];
|
33
|
+
break;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
klass = klass.$s;
|
37
|
+
}
|
38
|
+
|
39
|
+
if (!(klass && cur_method)) { return null; }
|
40
|
+
|
41
|
+
klass = klass.$s;
|
42
|
+
|
43
|
+
while (klass) {
|
44
|
+
if (klass.$m[mid]) {
|
45
|
+
return klass.$m[mid];
|
46
|
+
}
|
47
|
+
|
48
|
+
klass = klass.$s;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
// Jump return - return in proc body
|
53
|
+
opal.jump = function(value, func) {
|
54
|
+
throw new Error('jump return');
|
55
|
+
};
|
56
|
+
|
57
|
+
// Get constant with given id
|
58
|
+
opal.const_get = function(const_table, id) {
|
59
|
+
if (const_table[id]) {
|
60
|
+
return const_table[id];
|
61
|
+
}
|
62
|
+
|
63
|
+
throw RubyNameError.$new('uninitialized constant ' + id);
|
64
|
+
};
|
65
|
+
|
66
|
+
// Table holds all class variables
|
67
|
+
opal.cvars = {};
|
68
|
+
|
69
|
+
// Array of all procs to be called at_exit
|
70
|
+
var end_procs = [];
|
71
|
+
|
72
|
+
// Call exit blocks in reverse order
|
73
|
+
opal.do_at_exit = function() {
|
74
|
+
var proc;
|
75
|
+
|
76
|
+
while (proc = end_procs.pop()) {
|
77
|
+
proc.call(proc.$S);
|
78
|
+
}
|
79
|
+
};
|
80
|
+
|
81
|
+
// Globals table
|
82
|
+
opal.gvars = {};
|
83
|
+
|
84
|
+
// Define a method alias
|
85
|
+
opal.alias = function(klass, new_name, old_name) {
|
86
|
+
new_name = mid_to_jsid(new_name);
|
87
|
+
old_name = mid_to_jsid(old_name);
|
88
|
+
|
89
|
+
var body = klass.$proto[old_name];
|
90
|
+
|
91
|
+
if (!body) {
|
92
|
+
throw RubyNameError.$new("undefined method `" + old_name + "' for class `" + klass.$name + "'");
|
93
|
+
}
|
94
|
+
|
95
|
+
define_method(klass, new_name, body);
|
96
|
+
return nil;
|
97
|
+
};
|
98
|
+
|
99
|
+
// method missing yielder - used in debug mode to call method_missing.
|
100
|
+
opal.mm = function(jsid) {
|
101
|
+
var mid = jsid_to_mid(jsid);
|
102
|
+
return function() {
|
103
|
+
var args = $slice.call(arguments);
|
104
|
+
args.unshift(mid);
|
105
|
+
return this.$method_missing.apply(this, args);
|
106
|
+
};
|
107
|
+
}
|
108
|
+
|
109
|
+
// Actually define methods
|
110
|
+
var define_method = opal.defn = function(klass, id, body) {
|
111
|
+
// If an object, make sure to use its class
|
112
|
+
if (klass.$flags & T_OBJECT) {
|
113
|
+
klass = klass.$klass;
|
114
|
+
}
|
115
|
+
|
116
|
+
klass.$allocator.prototype[id] = body;
|
117
|
+
|
118
|
+
var included_in = klass.$included_in, includee;
|
119
|
+
|
120
|
+
if (included_in) {
|
121
|
+
for (var i = 0, ii = included_in.length; i < ii; i++) {
|
122
|
+
includee = included_in[i];
|
123
|
+
|
124
|
+
define_method(includee, id, body);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
if (klass.$bridge_prototype) {
|
129
|
+
klass.$bridge_prototype[id] = body;
|
130
|
+
}
|
131
|
+
|
132
|
+
|
133
|
+
return nil;
|
134
|
+
}
|
135
|
+
|
136
|
+
// Fake yielder used when no block given
|
137
|
+
opal.no_proc = function() {
|
138
|
+
throw RubyLocalJumpError.$new('no block given');
|
139
|
+
};
|
140
|
+
|
141
|
+
function define_module(base, id) {
|
142
|
+
var module;
|
143
|
+
|
144
|
+
module = boot_module();
|
145
|
+
module.$name = (base === RubyObject ? id : base.$name + '::' + id)
|
146
|
+
|
147
|
+
make_metaclass(module, RubyModule);
|
148
|
+
|
149
|
+
module.$flags = T_MODULE;
|
150
|
+
module.$included_in = [];
|
151
|
+
|
152
|
+
var const_alloc = function() {};
|
153
|
+
var const_scope = const_alloc.prototype = new base.$const.alloc();
|
154
|
+
module.$const = const_scope;
|
155
|
+
const_scope.alloc = const_alloc;
|
156
|
+
|
157
|
+
base.$const[id] = module;
|
158
|
+
|
159
|
+
return module;
|
160
|
+
}
|
161
|
+
|
162
|
+
// opal define class. 0: regular, 1: module, 2: shift class.
|
163
|
+
opal.klass = function(base, superklass, id, body, type) {
|
164
|
+
var klass;
|
165
|
+
|
166
|
+
switch (type) {
|
167
|
+
case 0:
|
168
|
+
if (base.$flags & T_OBJECT) {
|
169
|
+
base = class_real(base.$klass);
|
170
|
+
}
|
171
|
+
|
172
|
+
if (superklass === nil) {
|
173
|
+
superklass = RubyObject;
|
174
|
+
}
|
175
|
+
|
176
|
+
if (hasOwnProperty.call(base.$const, id)) {
|
177
|
+
klass = base.$const[id];
|
178
|
+
}
|
179
|
+
else {
|
180
|
+
klass = define_class(base, id, superklass);
|
181
|
+
}
|
182
|
+
|
183
|
+
break;
|
184
|
+
|
185
|
+
case 1:
|
186
|
+
if (base.$flags & T_OBJECT) {
|
187
|
+
base = class_real(base.$klass);
|
188
|
+
}
|
189
|
+
|
190
|
+
if (hasOwnProperty.call(base.$const, id)) {
|
191
|
+
klass = base.$const[id];
|
192
|
+
}
|
193
|
+
else {
|
194
|
+
klass = define_module(base, id);
|
195
|
+
}
|
196
|
+
|
197
|
+
break;
|
198
|
+
|
199
|
+
case 2:
|
200
|
+
klass = singleton_class(base);
|
201
|
+
break;
|
202
|
+
}
|
203
|
+
|
204
|
+
return body.call(klass);
|
205
|
+
};
|
206
|
+
|
207
|
+
opal.slice = $slice;
|
208
|
+
|
209
|
+
opal.defs = function(base, id, body) {
|
210
|
+
return define_method(singleton_class(base), id, body);
|
211
|
+
};
|
212
|
+
|
213
|
+
// Undefine one or more methods
|
214
|
+
opal.undef = function(klass) {
|
215
|
+
var args = $slice.call(arguments, 1);
|
216
|
+
|
217
|
+
for (var i = 0, length = args.length; i < length; i++) {
|
218
|
+
var mid = args[i], id = mid_to_jsid[mid];
|
219
|
+
|
220
|
+
delete klass.$m_tbl[id];
|
221
|
+
}
|
222
|
+
};
|
223
|
+
|
224
|
+
// Calls a super method.
|
225
|
+
opal.zuper = function(callee, self, args) {
|
226
|
+
var mid = callee.$rbName,
|
227
|
+
func = find_super(self.$klass, callee, mid);
|
228
|
+
|
229
|
+
if (!func) {
|
230
|
+
throw RubyNoMethodError.$new("super: no superclass method `" + mid + "'"
|
231
|
+
+ " for " + self.$inspect());
|
232
|
+
}
|
233
|
+
|
234
|
+
return func.apply(self, args);
|
235
|
+
};
|
236
|
+
|
237
|
+
var mid_to_jsid = opal.mid_to_jsid = function(mid) {
|
238
|
+
if (method_names[mid]) {
|
239
|
+
return method_names[mid];
|
240
|
+
}
|
241
|
+
|
242
|
+
return '$' + mid.replace('!', '$b').replace('?', '$p').replace('=', '$e');
|
243
|
+
}
|
244
|
+
|
245
|
+
var jsid_to_mid = opal.jsid_to_mid = function(jsid) {
|
246
|
+
if (reverse_method_names[jsid]) {
|
247
|
+
return reverse_method_names[jsid];
|
248
|
+
}
|
249
|
+
|
250
|
+
jsid = jsid.substr(1); // remove '$'
|
251
|
+
|
252
|
+
return jsid.replace('$b', '!').replace('$p', '?').replace('$e', '=');
|
253
|
+
}
|
254
|
+
|
255
|
+
opal.arg_error = function(given, expected) {
|
256
|
+
throw RubyArgError.$new('wrong number of arguments(' + given + ' for ' + expected + ')');
|
257
|
+
};
|
258
|
+
|
259
|
+
// Boot a base class (makes instances).
|
260
|
+
function boot_defclass(superklass) {
|
261
|
+
var cls = function() {
|
262
|
+
this.$id = unique_id++;
|
263
|
+
|
264
|
+
return this;
|
265
|
+
};
|
266
|
+
|
267
|
+
if (superklass) {
|
268
|
+
var ctor = function() {};
|
269
|
+
ctor.prototype = superklass.prototype;
|
270
|
+
|
271
|
+
cls.prototype = new ctor();
|
272
|
+
}
|
273
|
+
|
274
|
+
cls.prototype.constructor = cls;
|
275
|
+
cls.prototype.$flags = T_OBJECT;
|
276
|
+
|
277
|
+
return cls;
|
278
|
+
}
|
279
|
+
|
280
|
+
// Boot actual (meta classes) of core objects.
|
281
|
+
function boot_makemeta(id, klass, superklass) {
|
282
|
+
var meta = function() {
|
283
|
+
this.$id = unique_id++;
|
284
|
+
|
285
|
+
return this;
|
286
|
+
};
|
287
|
+
|
288
|
+
var ctor = function() {};
|
289
|
+
ctor.prototype = superklass.prototype;
|
290
|
+
|
291
|
+
meta.prototype = new ctor();
|
292
|
+
|
293
|
+
var proto = meta.prototype;
|
294
|
+
proto.$included_in = [];
|
295
|
+
proto.$allocator = klass;
|
296
|
+
proto.$flags = T_CLASS;
|
297
|
+
proto.$name = id;
|
298
|
+
proto.$s = superklass;
|
299
|
+
proto.constructor = meta;
|
300
|
+
|
301
|
+
var result = new meta();
|
302
|
+
klass.prototype.$klass = result;
|
303
|
+
result.$proto = klass.prototype;
|
304
|
+
|
305
|
+
return result;
|
306
|
+
}
|
307
|
+
|
308
|
+
// Create generic class with given superclass.
|
309
|
+
function boot_class(superklass) {
|
310
|
+
// instances
|
311
|
+
var cls = function() {
|
312
|
+
this.$id = unique_id++;
|
313
|
+
|
314
|
+
return this;
|
315
|
+
};
|
316
|
+
|
317
|
+
var ctor = function() {};
|
318
|
+
ctor.prototype = superklass.$allocator.prototype;
|
319
|
+
|
320
|
+
cls.prototype = new ctor();
|
321
|
+
|
322
|
+
var proto = cls.prototype;
|
323
|
+
proto.constructor = cls;
|
324
|
+
proto.$flags = T_OBJECT;
|
325
|
+
|
326
|
+
// class itself
|
327
|
+
var meta = function() {
|
328
|
+
this.$id = unique_id++;
|
329
|
+
|
330
|
+
return this;
|
331
|
+
};
|
332
|
+
|
333
|
+
var mtor = function() {};
|
334
|
+
mtor.prototype = superklass.constructor.prototype;
|
335
|
+
|
336
|
+
meta.prototype = new mtor();
|
337
|
+
|
338
|
+
proto = meta.prototype;
|
339
|
+
proto.$allocator = cls;
|
340
|
+
proto.$flags = T_CLASS;
|
341
|
+
proto.constructor = meta;
|
342
|
+
proto.$s = superklass;
|
343
|
+
|
344
|
+
var result = new meta();
|
345
|
+
cls.prototype.$klass = result;
|
346
|
+
|
347
|
+
result.$proto = cls.prototype;
|
348
|
+
|
349
|
+
return result;
|
350
|
+
}
|
351
|
+
|
352
|
+
function boot_module() {
|
353
|
+
// where module "instance" methods go. will never be instantiated so it
|
354
|
+
// can be a regular object
|
355
|
+
var module_cons = function(){};
|
356
|
+
var module_inst = module_cons.prototype;
|
357
|
+
|
358
|
+
// Module itself
|
359
|
+
var meta = function() {
|
360
|
+
this.$id = unique_id++;
|
361
|
+
return this;
|
362
|
+
};
|
363
|
+
|
364
|
+
var mtor = function(){};
|
365
|
+
mtor.prototype = RubyModule.constructor.prototype;
|
366
|
+
meta.prototype = new mtor();
|
367
|
+
|
368
|
+
var proto = meta.prototype;
|
369
|
+
proto.$allocator = module_cons;
|
370
|
+
proto.$flags = T_MODULE;
|
371
|
+
proto.constructor = meta;
|
372
|
+
proto.$s = RubyModule;
|
373
|
+
|
374
|
+
var module = new meta();
|
375
|
+
module.$proto = module_inst;
|
376
|
+
|
377
|
+
return module;
|
378
|
+
}
|
379
|
+
|
380
|
+
// Get actual class ignoring singleton classes and iclasses.
|
381
|
+
function class_real(klass) {
|
382
|
+
while (klass.$flags & FL_SINGLETON) {
|
383
|
+
klass = klass.$s;
|
384
|
+
}
|
385
|
+
|
386
|
+
return klass;
|
387
|
+
}
|
388
|
+
|
389
|
+
// Make metaclass for the given class
|
390
|
+
function make_metaclass(klass, superklass) {
|
391
|
+
if (klass.$flags & T_CLASS) {
|
392
|
+
if ((klass.$flags & T_CLASS) && (klass.$flags & FL_SINGLETON)) {
|
393
|
+
throw RubyException.$new('too much meta: return klass?');
|
394
|
+
}
|
395
|
+
else {
|
396
|
+
var class_id = "#<Class:" + klass.$name + ">",
|
397
|
+
meta = boot_class(superklass);
|
398
|
+
|
399
|
+
meta.$name = class_id;
|
400
|
+
meta.$allocator.prototype = klass.constructor.prototype;
|
401
|
+
meta.$flags |= FL_SINGLETON;
|
402
|
+
|
403
|
+
klass.$klass = meta;
|
404
|
+
|
405
|
+
meta.$const = klass.$const;
|
406
|
+
meta.__attached__ = klass;
|
407
|
+
|
408
|
+
return meta;
|
409
|
+
}
|
410
|
+
}
|
411
|
+
else {
|
412
|
+
return make_singleton_class(klass);
|
413
|
+
}
|
414
|
+
}
|
415
|
+
|
416
|
+
function make_singleton_class(obj) {
|
417
|
+
var orig_class = obj.$klass,
|
418
|
+
class_id = "#<Class:#<" + orig_class.$name + ":" + orig_class.$id + ">>";
|
419
|
+
|
420
|
+
klass = boot_class(orig_class);
|
421
|
+
klass.$name = class_id;
|
422
|
+
|
423
|
+
klass.$flags |= FL_SINGLETON;
|
424
|
+
klass.$bridge_prototype = obj;
|
425
|
+
|
426
|
+
obj.$klass = klass;
|
427
|
+
|
428
|
+
klass.__attached__ = obj;
|
429
|
+
|
430
|
+
klass.$klass = class_real(orig_class).$k;
|
431
|
+
|
432
|
+
return klass;
|
433
|
+
}
|
434
|
+
|
435
|
+
function bridge_class(constructor, flags, id) {
|
436
|
+
var klass = define_class(RubyObject, id, RubyObject),
|
437
|
+
prototype = constructor.prototype;
|
438
|
+
|
439
|
+
klass.$allocator = constructor;
|
440
|
+
klass.$proto = prototype;
|
441
|
+
|
442
|
+
bridged_classes.push(klass);
|
443
|
+
|
444
|
+
prototype.$klass = klass;
|
445
|
+
prototype.$flags = flags;
|
446
|
+
|
447
|
+
return klass;
|
448
|
+
}
|
449
|
+
|
450
|
+
// Define new ruby class
|
451
|
+
function define_class(base, id, superklass) {
|
452
|
+
var klass;
|
453
|
+
|
454
|
+
var class_id = (base === RubyObject ? id : base.$name + '::' + id);
|
455
|
+
|
456
|
+
klass = boot_class(superklass);
|
457
|
+
klass.$name = class_id;
|
458
|
+
|
459
|
+
make_metaclass(klass, superklass.$klass);
|
460
|
+
|
461
|
+
var const_alloc = function() {};
|
462
|
+
var const_scope = const_alloc.prototype = new base.$const.alloc();
|
463
|
+
klass.$const = const_scope;
|
464
|
+
const_scope.alloc = const_alloc;
|
465
|
+
|
466
|
+
base.$const[id] = klass;
|
467
|
+
|
468
|
+
if (superklass.$inherited) {
|
469
|
+
superklass.$inherited(klass);
|
470
|
+
}
|
471
|
+
|
472
|
+
return klass;
|
473
|
+
}
|
474
|
+
|
475
|
+
// Get singleton class of obj
|
476
|
+
function singleton_class(obj) {
|
477
|
+
var klass;
|
478
|
+
|
479
|
+
if (obj.$flags & T_OBJECT) {
|
480
|
+
if ((obj.$flags & T_NUMBER) || (obj.$flags & T_STRING)) {
|
481
|
+
throw RubyTypeError.$new("can't define singleton");
|
482
|
+
}
|
483
|
+
}
|
484
|
+
|
485
|
+
if ((obj.$klass.$flags & FL_SINGLETON) && obj.$klass.__attached__ == obj) {
|
486
|
+
klass = obj.$klass;
|
487
|
+
}
|
488
|
+
else {
|
489
|
+
var class_id = obj.$klass.$name;
|
490
|
+
|
491
|
+
klass = make_metaclass(obj, obj.$klass);
|
492
|
+
}
|
493
|
+
|
494
|
+
return klass;
|
495
|
+
}
|
496
|
+
|
497
|
+
opal.main = function(id) {
|
498
|
+
opal.gvars.$0 = find_lib(id);
|
499
|
+
|
500
|
+
try {
|
501
|
+
top_self.$require(id);
|
502
|
+
|
503
|
+
opal.do_at_exit();
|
504
|
+
}
|
505
|
+
catch (e) {
|
506
|
+
// this is defined in debug.js
|
507
|
+
console.log(e.$klass.$name + ': ' + e.message);
|
508
|
+
console.log("\t" + e.$backtrace().join("\n\t"));
|
509
|
+
}
|
510
|
+
};
|
511
|
+
|
512
|
+
/**
|
513
|
+
* Register a standard file. This can be used to register non-lib files.
|
514
|
+
* For example, specs can be registered here so they are available.
|
515
|
+
*
|
516
|
+
* NOTE: Files should be registered as a full path with given factory.
|
517
|
+
*
|
518
|
+
* Usage:
|
519
|
+
*
|
520
|
+
* opal.file('/spec/foo.rb': function() {
|
521
|
+
* // ...
|
522
|
+
* });
|
523
|
+
*/
|
524
|
+
opal.file = function(file, factory) {
|
525
|
+
FACTORIES[file] = factory;
|
526
|
+
};
|
527
|
+
|
528
|
+
/**
|
529
|
+
* Register a lib.
|
530
|
+
*
|
531
|
+
* Usage:
|
532
|
+
*
|
533
|
+
* opal.lib('my_lib', function() {
|
534
|
+
* // ...
|
535
|
+
* });
|
536
|
+
*
|
537
|
+
* opal.lib('my_lib/foo', function() {
|
538
|
+
* // ...
|
539
|
+
* });
|
540
|
+
*/
|
541
|
+
opal.lib = function(lib, factory) {
|
542
|
+
var file = '/lib/' + lib + '.rb';
|
543
|
+
FACTORIES[file] = factory;
|
544
|
+
LIBS[lib] = file;
|
545
|
+
};
|
546
|
+
|
547
|
+
var FACTORIES = {},
|
548
|
+
FEATURES = [],
|
549
|
+
LIBS = {},
|
550
|
+
LOADER_PATHS = ['', '/lib'],
|
551
|
+
LOADER_CACHE = {};
|
552
|
+
|
553
|
+
function find_lib(id) {
|
554
|
+
var path;
|
555
|
+
|
556
|
+
// try to load a lib path first - i.e. something in our load path
|
557
|
+
if (path = LIBS[id]) return path;
|
558
|
+
|
559
|
+
// find '/opal/x' style libs
|
560
|
+
if (path = LIBS['opal/' + id]) return path;
|
561
|
+
|
562
|
+
// next, incase our require() has a ruby extension..
|
563
|
+
if (FACTORIES['/lib/' +id]) return '/lib/' + id;
|
564
|
+
|
565
|
+
// check if id is full path..
|
566
|
+
if (FACTORIES[id]) return id;
|
567
|
+
|
568
|
+
// full path without '.rb'
|
569
|
+
if (FACTORIES[id + '.rb']) return id + '.rb';
|
570
|
+
|
571
|
+
// check in current working directory.
|
572
|
+
var in_cwd = FS_CWD + '/' + id;
|
573
|
+
|
574
|
+
if (FACTORIES[in_cwd]) return in_cwd;
|
575
|
+
};
|
576
|
+
|
577
|
+
// Current working directory
|
578
|
+
var FS_CWD = '/';
|
579
|
+
|
580
|
+
// Turns a glob string into a regexp
|
581
|
+
|
582
|
+
// Initialization
|
583
|
+
// --------------
|
584
|
+
|
585
|
+
// The *instances* of core objects
|
586
|
+
var BootObject = boot_defclass();
|
587
|
+
var BootModule = boot_defclass(BootObject);
|
588
|
+
var BootClass = boot_defclass(BootModule);
|
589
|
+
|
590
|
+
// The *classes' of core objects
|
591
|
+
var RubyObject = boot_makemeta('Object', BootObject, BootClass);
|
592
|
+
var RubyModule = boot_makemeta('Module', BootModule, RubyObject.constructor);
|
593
|
+
var RubyClass = boot_makemeta('Class', BootClass, RubyModule.constructor);
|
594
|
+
|
595
|
+
// Fix boot classes to use meta class
|
596
|
+
RubyObject.$klass = RubyClass;
|
597
|
+
RubyModule.$klass = RubyClass;
|
598
|
+
RubyClass.$klass = RubyClass;
|
599
|
+
|
600
|
+
// fix superclasses
|
601
|
+
RubyObject.$s = null;
|
602
|
+
RubyModule.$s = RubyObject;
|
603
|
+
RubyClass.$s = RubyModule;
|
604
|
+
|
605
|
+
opal.Object = RubyObject;
|
606
|
+
opal.Module = RubyModule;
|
607
|
+
opal.Class = RubyClass;
|
608
|
+
|
609
|
+
// make object act like a module
|
610
|
+
var bridged_classes = RubyObject.$included_in = [];
|
611
|
+
|
612
|
+
// Top level Object scope (used by object and top_self).
|
613
|
+
var top_const_alloc = function(){};
|
614
|
+
var top_const_scope = top_const_alloc.prototype;
|
615
|
+
top_const_scope.alloc = top_const_alloc;
|
616
|
+
|
617
|
+
RubyObject.$const = opal.constants = top_const_scope;
|
618
|
+
|
619
|
+
var module_const_alloc = function(){};
|
620
|
+
var module_const_scope = new top_const_alloc();
|
621
|
+
module_const_scope.alloc = module_const_alloc;
|
622
|
+
RubyModule.$const = module_const_scope;
|
623
|
+
|
624
|
+
var class_const_alloc = function(){};
|
625
|
+
var class_const_scope = new top_const_alloc();
|
626
|
+
class_const_scope.alloc = class_const_alloc;
|
627
|
+
RubyClass.$const = class_const_scope;
|
628
|
+
|
629
|
+
RubyObject.$const.BasicObject = RubyObject;
|
630
|
+
RubyObject.$const.Object = RubyObject;
|
631
|
+
RubyObject.$const.Module = RubyModule;
|
632
|
+
RubyObject.$const.Class = RubyClass;
|
633
|
+
|
634
|
+
var top_self = opal.top = new RubyObject.$allocator();
|
635
|
+
|
636
|
+
var RubyNilClass = define_class(RubyObject, 'NilClass', RubyObject);
|
637
|
+
var nil = opal.nil = new RubyNilClass.$allocator();
|
638
|
+
|
639
|
+
bridge_class(Array, T_OBJECT | T_ARRAY, 'Array');
|
640
|
+
bridge_class(Number, T_OBJECT | T_NUMBER, 'Numeric');
|
641
|
+
|
642
|
+
bridge_class(String, T_OBJECT | T_STRING, 'String');
|
643
|
+
bridge_class(Boolean, T_OBJECT | T_BOOLEAN, 'Boolean');
|
644
|
+
bridge_class(Function, T_OBJECT | T_PROC, 'Proc');
|
645
|
+
bridge_class(RegExp, T_OBJECT, 'Regexp');
|
646
|
+
|
647
|
+
var RubyMatch = define_class(RubyObject, 'MatchData', RubyObject);
|
648
|
+
var RubyRange = define_class(RubyObject, 'Range', RubyObject);
|
649
|
+
|
650
|
+
var RubyException = bridge_class(Error, T_OBJECT, 'Exception');
|
651
|
+
var RubyStandardError = define_class(RubyObject, 'StandardError', RubyException);
|
652
|
+
var RubyRuntimeError = define_class(RubyObject, 'RuntimeError', RubyException);
|
653
|
+
var RubyLocalJumpError = define_class(RubyObject, 'LocalJumpError', RubyStandardError);
|
654
|
+
var RubyTypeError = define_class(RubyObject, 'TypeError', RubyStandardError);
|
655
|
+
var RubyNameError = define_class(RubyObject, 'NameError', RubyStandardError);
|
656
|
+
var RubyNoMethodError = define_class(RubyObject, 'NoMethodError', RubyNameError);
|
657
|
+
var RubyArgError = define_class(RubyObject, 'ArgumentError', RubyStandardError);
|
658
|
+
var RubyScriptError = define_class(RubyObject, 'ScriptError', RubyException);
|
659
|
+
var RubyLoadError = define_class(RubyObject, 'LoadError', RubyScriptError);
|
660
|
+
var RubyIndexError = define_class(RubyObject, 'IndexError', RubyStandardError);
|
661
|
+
var RubyKeyError = define_class(RubyObject, 'KeyError', RubyIndexError);
|
662
|
+
var RubyRangeError = define_class(RubyObject, 'RangeError', RubyStandardError);
|
663
|
+
var RubyNotImplError = define_class(RubyObject, 'NotImplementedError', RubyException);
|
664
|
+
|
665
|
+
RubyException.$allocator.prototype.toString = function() {
|
666
|
+
return this.$klass.$name + ': ' + this.message;
|
667
|
+
};
|
668
|
+
|
669
|
+
var breaker = opal.breaker = new Error('unexpected break');
|
670
|
+
breaker.$klass = RubyLocalJumpError;
|
671
|
+
breaker.$t = function() { throw this; };
|
672
|
+
|