opal 0.3.11 → 0.3.15
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/Gemfile +10 -0
- data/LICENSE +20 -0
- data/README.md +11 -116
- data/Rakefile +126 -0
- data/bin/opal +1 -2
- data/docs/spec_runner.html +16 -0
- data/index.html +434 -0
- data/lib/opal.rb +14 -15
- data/lib/opal/builder.rb +46 -148
- data/lib/opal/command.rb +45 -115
- data/lib/opal/context.rb +139 -78
- data/lib/opal/dependency_builder.rb +34 -0
- data/lib/opal/environment.rb +92 -0
- data/lib/opal/parser/grammar.rb +4915 -0
- data/lib/opal/{parser.y → parser/grammar.y} +430 -284
- data/lib/opal/parser/lexer.rb +1329 -0
- data/lib/opal/parser/parser.rb +1460 -0
- data/lib/opal/parser/scope.rb +140 -0
- data/lib/opal/parser/sexp.rb +17 -0
- data/lib/opal/version.rb +2 -1
- data/opal.gemspec +23 -0
- data/opal.js +3149 -4162
- data/runtime/README.md +25 -0
- data/runtime/corelib/alpha.rb +10 -0
- data/runtime/corelib/array.rb +962 -0
- data/runtime/corelib/basic_object.rb +66 -0
- data/runtime/corelib/boolean.rb +44 -0
- data/runtime/corelib/class.rb +43 -0
- data/runtime/corelib/comparable.rb +25 -0
- data/runtime/corelib/complex.rb +2 -0
- data/runtime/corelib/dir.rb +29 -0
- data/runtime/corelib/enumerable.rb +316 -0
- data/runtime/corelib/enumerator.rb +80 -0
- data/runtime/corelib/error.rb +25 -0
- data/runtime/corelib/file.rb +80 -0
- data/runtime/corelib/hash.rb +503 -0
- data/runtime/corelib/io.rb +44 -0
- data/runtime/corelib/kernel.rb +237 -0
- data/runtime/corelib/load_order +29 -0
- data/runtime/corelib/match_data.rb +37 -0
- data/runtime/corelib/module.rb +171 -0
- data/runtime/corelib/native.rb +50 -0
- data/runtime/corelib/nil_class.rb +47 -0
- data/runtime/corelib/numeric.rb +219 -0
- data/runtime/corelib/object.rb +21 -0
- data/runtime/corelib/proc.rb +42 -0
- data/runtime/corelib/range.rb +38 -0
- data/runtime/corelib/rational.rb +16 -0
- data/runtime/corelib/regexp.rb +63 -0
- data/runtime/corelib/string.rb +185 -0
- data/runtime/corelib/struct.rb +97 -0
- data/runtime/corelib/time.rb +196 -0
- data/runtime/corelib/top_self.rb +7 -0
- data/runtime/gemlib/alpha.rb +5 -0
- data/runtime/gemlib/kernel.rb +17 -0
- data/runtime/gemlib/load_order +2 -0
- data/runtime/kernel/class.js +256 -0
- data/runtime/kernel/debug.js +42 -0
- data/runtime/kernel/init.js +114 -0
- data/runtime/kernel/load_order +5 -0
- data/runtime/kernel/loader.js +151 -0
- data/runtime/kernel/runtime.js +414 -0
- data/runtime/spec/README.md +34 -0
- data/runtime/spec/core/array/allocate_spec.rb +15 -0
- data/runtime/spec/core/array/append_spec.rb +31 -0
- data/runtime/spec/core/array/assoc_spec.rb +29 -0
- data/runtime/spec/core/array/at_spec.rb +38 -0
- data/runtime/spec/core/array/clear_spec.rb +22 -0
- data/runtime/spec/core/array/collect_spec.rb +3 -0
- data/runtime/spec/core/array/compact_spec.rb +42 -0
- data/runtime/spec/core/array/concat_spec.rb +21 -0
- data/runtime/spec/core/array/constructor_spec.rb +24 -0
- data/runtime/spec/core/array/count_spec.rb +11 -0
- data/runtime/spec/core/array/delete_at_spec.rb +31 -0
- data/runtime/spec/core/array/delete_if_spec.rb +24 -0
- data/runtime/spec/core/array/delete_spec.rb +26 -0
- data/runtime/spec/core/array/each_index_spec.rb +33 -0
- data/runtime/spec/core/array/each_spec.rb +11 -0
- data/runtime/spec/core/array/element_reference_spec.rb +136 -0
- data/runtime/spec/core/array/element_set_spec.rb +7 -0
- data/runtime/spec/core/array/empty_spec.rb +10 -0
- data/runtime/spec/core/array/eql_spec.rb +3 -0
- data/runtime/spec/core/array/equal_value_spec.rb +3 -0
- data/runtime/spec/core/array/fetch_spec.rb +26 -0
- data/runtime/spec/core/array/first_spec.rb +54 -0
- data/runtime/spec/core/array/fixtures/classes.rb +8 -0
- data/runtime/spec/core/array/flatten_spec.rb +41 -0
- data/runtime/spec/core/array/include_spec.rb +20 -0
- data/runtime/spec/core/array/insert_spec.rb +59 -0
- data/runtime/spec/core/array/last_spec.rb +57 -0
- data/runtime/spec/core/array/length_spec.rb +3 -0
- data/runtime/spec/core/array/map_spec.rb +3 -0
- data/runtime/spec/core/array/plus_spec.rb +16 -0
- data/runtime/spec/core/array/pop_spec.rb +79 -0
- data/runtime/spec/core/array/push_spec.rb +19 -0
- data/runtime/spec/core/array/rassoc_spec.rb +12 -0
- data/runtime/spec/core/array/reject_spec.rb +54 -0
- data/runtime/spec/core/array/replace_spec.rb +3 -0
- data/runtime/spec/core/array/reverse_each_spec.rb +18 -0
- data/runtime/spec/core/array/reverse_spec.rb +9 -0
- data/runtime/spec/core/array/shared/collect.rb +53 -0
- data/runtime/spec/core/array/shared/eql.rb +19 -0
- data/runtime/spec/core/array/shared/length.rb +6 -0
- data/runtime/spec/core/array/shared/replace.rb +31 -0
- data/runtime/spec/core/class/new_spec.rb +19 -0
- data/runtime/spec/core/enumerable/all_spec.rb +102 -0
- data/runtime/spec/core/enumerable/any_spec.rb +115 -0
- data/runtime/spec/core/enumerable/collect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/count_spec.rb +29 -0
- data/runtime/spec/core/enumerable/detect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/find_spec.rb +3 -0
- data/runtime/spec/core/enumerable/fixtures/classes.rb +26 -0
- data/runtime/spec/core/enumerable/shared/collect.rb +12 -0
- data/runtime/spec/core/enumerable/shared/entries.rb +7 -0
- data/runtime/spec/core/enumerable/shared/find.rb +49 -0
- data/runtime/spec/core/enumerable/to_a_spec.rb +7 -0
- data/runtime/spec/core/false/and_spec.rb +11 -0
- data/runtime/spec/core/false/inspect_spec.rb +7 -0
- data/runtime/spec/core/false/or_spec.rb +11 -0
- data/runtime/spec/core/false/to_s_spec.rb +7 -0
- data/runtime/spec/core/false/xor_spec.rb +11 -0
- data/runtime/spec/core/hash/allocate_spec.rb +15 -0
- data/runtime/spec/core/hash/assoc_spec.rb +29 -0
- data/runtime/spec/core/hash/clear_spec.rb +21 -0
- data/runtime/spec/core/hash/clone_spec.rb +12 -0
- data/runtime/spec/core/hash/default_spec.rb +6 -0
- data/runtime/spec/core/hash/delete_if_spec.rb +15 -0
- data/runtime/spec/core/hash/element_reference_spec.rb +16 -0
- data/runtime/spec/core/hash/element_set_spec.rb +8 -0
- data/runtime/spec/core/hash/new_spec.rb +13 -0
- data/runtime/spec/core/matchdata/to_a_spec.rb +7 -0
- data/runtime/spec/core/nil/and_spec.rb +12 -0
- data/runtime/spec/core/nil/inspect_spec.rb +8 -0
- data/runtime/spec/core/nil/nil_spec.rb +8 -0
- data/runtime/spec/core/nil/or_spec.rb +12 -0
- data/runtime/spec/core/nil/to_a_spec.rb +8 -0
- data/runtime/spec/core/nil/to_f_spec.rb +12 -0
- data/runtime/spec/core/nil/to_i_spec.rb +12 -0
- data/runtime/spec/core/nil/to_s_spec.rb +8 -0
- data/runtime/spec/core/nil/xor_spec.rb +12 -0
- data/runtime/spec/core/numeric/equal_value_spec.rb +11 -0
- data/runtime/spec/core/object/is_a_spec.rb +2 -0
- data/runtime/spec/core/object/shared/kind_of.rb +0 -0
- data/runtime/spec/core/regexp/match_spec.rb +23 -0
- data/runtime/spec/core/regexp/shared/match.rb +11 -0
- data/runtime/spec/core/symbol/to_proc_spec.rb +8 -0
- data/runtime/spec/core/true/and_spec.rb +11 -0
- data/runtime/spec/core/true/inspect_spec.rb +7 -0
- data/runtime/spec/core/true/or_spec.rb +11 -0
- data/runtime/spec/core/true/to_s_spec.rb +7 -0
- data/runtime/spec/core/true/xor_spec.rb +11 -0
- data/runtime/spec/language/alias_spec.rb +25 -0
- data/runtime/spec/language/and_spec.rb +62 -0
- data/runtime/spec/language/array_spec.rb +68 -0
- data/runtime/spec/language/block_spec.rb +105 -0
- data/runtime/spec/language/break_spec.rb +49 -0
- data/runtime/spec/language/case_spec.rb +165 -0
- data/runtime/spec/language/defined_spec.rb +80 -0
- data/runtime/spec/language/ensure_spec.rb +82 -0
- data/runtime/spec/language/fixtures/block.rb +19 -0
- data/runtime/spec/language/fixtures/break.rb +39 -0
- data/runtime/spec/language/fixtures/defined.rb +9 -0
- data/runtime/spec/language/fixtures/ensure.rb +37 -0
- data/runtime/spec/language/fixtures/next.rb +46 -0
- data/runtime/spec/language/fixtures/send.rb +36 -0
- data/runtime/spec/language/fixtures/super.rb +43 -0
- data/runtime/spec/language/hash_spec.rb +43 -0
- data/runtime/spec/language/if_spec.rb +278 -0
- data/runtime/spec/language/loop_spec.rb +32 -0
- data/runtime/spec/language/next_spec.rb +128 -0
- data/runtime/spec/language/or_spec.rb +65 -0
- data/runtime/spec/language/predefined_spec.rb +21 -0
- data/runtime/spec/language/regexp/interpolation_spec.rb +9 -0
- data/runtime/spec/language/regexp_spec.rb +7 -0
- data/runtime/spec/language/send_spec.rb +105 -0
- data/runtime/spec/language/string_spec.rb +4 -0
- data/runtime/spec/language/super_spec.rb +18 -0
- data/runtime/spec/language/symbol_spec.rb +41 -0
- data/runtime/spec/language/undef_spec.rb +16 -0
- data/runtime/spec/language/unless_spec.rb +44 -0
- data/runtime/spec/language/until_spec.rb +137 -0
- data/runtime/spec/language/variables_spec.rb +28 -0
- data/runtime/spec/language/versions/hash_1.9.rb +20 -0
- data/runtime/spec/language/while_spec.rb +175 -0
- data/runtime/spec/library/stringscanner/scan_spec.rb +36 -0
- data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +49 -0
- data/runtime/spec/opal/opal/defined_spec.rb +15 -0
- data/runtime/spec/opal/opal/function_spec.rb +11 -0
- data/runtime/spec/opal/opal/native_spec.rb +16 -0
- data/runtime/spec/opal/opal/null_spec.rb +10 -0
- data/runtime/spec/opal/opal/number_spec.rb +11 -0
- data/runtime/spec/opal/opal/object_spec.rb +16 -0
- data/runtime/spec/opal/opal/string_spec.rb +11 -0
- data/runtime/spec/opal/opal/typeof_spec.rb +9 -0
- data/runtime/spec/opal/opal/undefined_spec.rb +10 -0
- data/runtime/spec/opal/true/case_compare_spec.rb +12 -0
- data/runtime/spec/opal/true/class_spec.rb +10 -0
- data/runtime/spec/spec_helper.rb +25 -0
- data/runtime/stdlib/base64.rb +91 -0
- data/runtime/stdlib/date.rb +4 -0
- data/{stdlib → runtime/stdlib}/dev.rb +0 -0
- data/runtime/stdlib/forwardable.rb +33 -0
- data/runtime/stdlib/optparse.rb +0 -0
- data/runtime/stdlib/pp.rb +6 -0
- data/{stdlib → runtime/stdlib}/racc/parser.rb +0 -0
- data/runtime/stdlib/rbconfig.rb +0 -0
- data/runtime/stdlib/si.rb +17 -0
- data/runtime/stdlib/strscan.rb +53 -0
- data/runtime/stdlib/uri.rb +111 -0
- data/runtime/stdlib/uri/common.rb +1014 -0
- data/runtime/stdlib/uri/ftp.rb +261 -0
- data/runtime/stdlib/uri/generic.rb +1599 -0
- data/runtime/stdlib/uri/http.rb +106 -0
- data/runtime/stdlib/uri/https.rb +22 -0
- data/runtime/stdlib/uri/ldap.rb +260 -0
- data/runtime/stdlib/uri/ldaps.rb +20 -0
- data/runtime/stdlib/uri/mailto.rb +280 -0
- data/spec/builder/build_source_spec.rb +52 -0
- data/spec/builder/fixtures/build_source/adam.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/a.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/b.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
- data/spec/builder/fixtures/build_source/beynon.rb +0 -0
- data/spec/builder/fixtures/build_source/charles.js +0 -0
- data/spec/builder/fixtures/build_source/foo/a.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/b.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/x.js +0 -0
- data/spec/builder/fixtures/build_source/foo/y.js +0 -0
- data/spec/builder/output_path_spec.rb +50 -0
- data/spec/grammar/alias_spec.rb +26 -0
- data/spec/grammar/and_spec.rb +13 -0
- data/spec/grammar/array_spec.rb +22 -0
- data/spec/grammar/attrasgn_spec.rb +28 -0
- data/spec/grammar/begin_spec.rb +38 -0
- data/spec/grammar/block_spec.rb +12 -0
- data/spec/grammar/break_spec.rb +17 -0
- data/spec/grammar/call_spec.rb +58 -0
- data/spec/grammar/class_spec.rb +35 -0
- data/spec/grammar/const_spec.rb +13 -0
- data/spec/grammar/cvar_spec.rb +11 -0
- data/spec/grammar/def_spec.rb +60 -0
- data/spec/grammar/false_spec.rb +17 -0
- data/spec/grammar/file_spec.rb +7 -0
- data/spec/grammar/gvar_spec.rb +13 -0
- data/spec/grammar/hash_spec.rb +17 -0
- data/spec/grammar/iasgn_spec.rb +9 -0
- data/spec/grammar/if_spec.rb +26 -0
- data/spec/grammar/iter_spec.rb +59 -0
- data/spec/grammar/ivar_spec.rb +9 -0
- data/spec/grammar/lasgn_spec.rb +8 -0
- data/spec/grammar/line_spec.rb +8 -0
- data/spec/grammar/lvar_spec.rb +38 -0
- data/spec/grammar/module_spec.rb +27 -0
- data/spec/grammar/nil_spec.rb +17 -0
- data/spec/grammar/not_spec.rb +27 -0
- data/spec/grammar/op_asgn1_spec.rb +23 -0
- data/spec/grammar/op_asgn2_spec.rb +23 -0
- data/spec/grammar/or_spec.rb +13 -0
- data/spec/grammar/return_spec.rb +17 -0
- data/spec/grammar/sclass_spec.rb +20 -0
- data/spec/grammar/self_spec.rb +17 -0
- data/spec/grammar/str_spec.rb +96 -0
- data/spec/grammar/super_spec.rb +20 -0
- data/spec/grammar/true_spec.rb +17 -0
- data/spec/grammar/undef_spec.rb +15 -0
- data/spec/grammar/unless_spec.rb +13 -0
- data/spec/grammar/while_spec.rb +15 -0
- data/spec/grammar/xstr_spec.rb +116 -0
- data/spec/grammar/yield_spec.rb +20 -0
- data/spec/spec_helper.rb +9 -0
- metadata +346 -21
- data/lib/opal/bundle.rb +0 -34
- data/lib/opal/lexer.rb +0 -902
- data/lib/opal/nodes.rb +0 -2150
- data/lib/opal/parser.rb +0 -4894
- data/lib/opal/rake/bundle_task.rb +0 -63
- data/opal-parser.js +0 -8343
- data/stdlib/strscan.rb +0 -52
- data/templates/init/Rakefile +0 -7
- data/templates/init/index.html +0 -17
- data/templates/init/lib/__NAME__.rb +0 -2
@@ -0,0 +1,17 @@
|
|
1
|
+
module Kernel
|
2
|
+
def require(path)
|
3
|
+
%x{
|
4
|
+
var resolved = opal_filesystem.require(#{path}, #{$:});
|
5
|
+
|
6
|
+
if (resolved === false) {
|
7
|
+
return false;
|
8
|
+
}
|
9
|
+
else if (!resolved) {
|
10
|
+
#{ raise LoadError, "cannot load file -- #{path}" };
|
11
|
+
}
|
12
|
+
else {
|
13
|
+
return true;
|
14
|
+
}
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,256 @@
|
|
1
|
+
// Root of all objects and classes inside opal
|
2
|
+
function RootObject() {};
|
3
|
+
|
4
|
+
RootObject.prototype.toString = function() {
|
5
|
+
if (this.$flags & T_OBJECT) {
|
6
|
+
return "#<" + (this.$klass).__classid__ + ":0x" + this.$id + ">";
|
7
|
+
}
|
8
|
+
else {
|
9
|
+
return '<' + this.__classid__ + ' ' + this.$id + '>';
|
10
|
+
}
|
11
|
+
};
|
12
|
+
|
13
|
+
// Boot a base class (makes instances).
|
14
|
+
function boot_defclass(superklass) {
|
15
|
+
var cls = function() {
|
16
|
+
this.$id = unique_id++;
|
17
|
+
|
18
|
+
return this;
|
19
|
+
};
|
20
|
+
|
21
|
+
if (superklass) {
|
22
|
+
var ctor = function() {};
|
23
|
+
ctor.prototype = superklass.prototype;
|
24
|
+
|
25
|
+
cls.prototype = new ctor();
|
26
|
+
}
|
27
|
+
else {
|
28
|
+
cls.prototype = new RootObject();
|
29
|
+
}
|
30
|
+
|
31
|
+
cls.prototype.constructor = cls;
|
32
|
+
cls.prototype.$flags = T_OBJECT;
|
33
|
+
|
34
|
+
return cls;
|
35
|
+
}
|
36
|
+
|
37
|
+
// Boot actual (meta classes) of core objects.
|
38
|
+
function boot_makemeta(id, klass, superklass) {
|
39
|
+
var meta = function() {
|
40
|
+
this.$id = unique_id++;
|
41
|
+
|
42
|
+
return this;
|
43
|
+
};
|
44
|
+
|
45
|
+
var ctor = function() {};
|
46
|
+
ctor.prototype = superklass.prototype;
|
47
|
+
|
48
|
+
meta.prototype = new ctor();
|
49
|
+
|
50
|
+
var proto = meta.prototype;
|
51
|
+
proto.$included_in = [];
|
52
|
+
proto.$m = {};
|
53
|
+
proto.$methods = [];
|
54
|
+
proto.$allocator = klass;
|
55
|
+
proto.$flags = T_CLASS;
|
56
|
+
proto.__classid__ = id;
|
57
|
+
proto.$s = superklass;
|
58
|
+
proto.constructor = meta;
|
59
|
+
|
60
|
+
var result = new meta();
|
61
|
+
klass.prototype.$klass = result;
|
62
|
+
result.$proto = klass.prototype;
|
63
|
+
|
64
|
+
return result;
|
65
|
+
}
|
66
|
+
|
67
|
+
// Create generic class with given superclass.
|
68
|
+
function boot_class(superklass) {
|
69
|
+
// instances
|
70
|
+
var cls = function() {
|
71
|
+
this.$id = unique_id++;
|
72
|
+
|
73
|
+
return this;
|
74
|
+
};
|
75
|
+
|
76
|
+
var ctor = function() {};
|
77
|
+
ctor.prototype = superklass.$allocator.prototype;
|
78
|
+
|
79
|
+
cls.prototype = new ctor();
|
80
|
+
|
81
|
+
var proto = cls.prototype;
|
82
|
+
proto.constructor = cls;
|
83
|
+
proto.$flags = T_OBJECT;
|
84
|
+
|
85
|
+
// class itself
|
86
|
+
var meta = function() {
|
87
|
+
this.$id = unique_id++;
|
88
|
+
|
89
|
+
return this;
|
90
|
+
};
|
91
|
+
|
92
|
+
var mtor = function() {};
|
93
|
+
mtor.prototype = superklass.constructor.prototype;
|
94
|
+
|
95
|
+
meta.prototype = new mtor();
|
96
|
+
|
97
|
+
proto = meta.prototype;
|
98
|
+
proto.$allocator = cls;
|
99
|
+
proto.$flags = T_CLASS;
|
100
|
+
proto.$m = {};
|
101
|
+
proto.$methods = [];
|
102
|
+
proto.constructor = meta;
|
103
|
+
proto.$s = superklass;
|
104
|
+
|
105
|
+
var result = new meta();
|
106
|
+
cls.prototype.$klass = result;
|
107
|
+
|
108
|
+
result.$proto = cls.prototype;
|
109
|
+
|
110
|
+
return result;
|
111
|
+
}
|
112
|
+
|
113
|
+
function boot_module() {
|
114
|
+
// where module "instance" methods go. will never be instantiated so it
|
115
|
+
// can be a regular object
|
116
|
+
var module_cons = function(){};
|
117
|
+
var module_inst = module_cons.prototype;
|
118
|
+
|
119
|
+
// Module itself
|
120
|
+
var meta = function() {
|
121
|
+
this.$id = unique_id++;
|
122
|
+
return this;
|
123
|
+
};
|
124
|
+
|
125
|
+
var mtor = function(){};
|
126
|
+
mtor.prototype = RubyModule.constructor.prototype;
|
127
|
+
meta.prototype = new mtor();
|
128
|
+
|
129
|
+
var proto = meta.prototype;
|
130
|
+
proto.$allocator = module_cons;
|
131
|
+
proto.$flags = T_MODULE;
|
132
|
+
proto.constructor = meta;
|
133
|
+
proto.$s = RubyModule;
|
134
|
+
|
135
|
+
var module = new meta();
|
136
|
+
module.$proto = module_inst;
|
137
|
+
|
138
|
+
return module;
|
139
|
+
}
|
140
|
+
|
141
|
+
// Get actual class ignoring singleton classes and iclasses.
|
142
|
+
function class_real(klass) {
|
143
|
+
while (klass.$flags & FL_SINGLETON) {
|
144
|
+
klass = klass.$s;
|
145
|
+
}
|
146
|
+
|
147
|
+
return klass;
|
148
|
+
}
|
149
|
+
|
150
|
+
// Make metaclass for the given class
|
151
|
+
function make_metaclass(klass, superklass) {
|
152
|
+
if (klass.$flags & T_CLASS) {
|
153
|
+
if ((klass.$flags & T_CLASS) && (klass.$flags & FL_SINGLETON)) {
|
154
|
+
raise(RubyException, "too much meta: return klass?");
|
155
|
+
}
|
156
|
+
else {
|
157
|
+
var class_id = "#<Class:" + klass.__classid__ + ">",
|
158
|
+
meta = boot_class(superklass);
|
159
|
+
|
160
|
+
meta.__classid__ = class_id;
|
161
|
+
meta.$allocator.prototype = klass.constructor.prototype;
|
162
|
+
meta.$flags |= FL_SINGLETON;
|
163
|
+
|
164
|
+
klass.$klass = meta;
|
165
|
+
|
166
|
+
meta.$const = klass.$const;
|
167
|
+
meta.__attached__ = klass;
|
168
|
+
|
169
|
+
return meta;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
else {
|
173
|
+
return make_singleton_class(klass);
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
function make_singleton_class(obj) {
|
178
|
+
var orig_class = obj.$klass,
|
179
|
+
class_id = "#<Class:#<" + orig_class.__classid__ + ":" + orig_class.$id + ">>";
|
180
|
+
|
181
|
+
klass = boot_class(orig_class);
|
182
|
+
klass.__classid__ = class_id;
|
183
|
+
|
184
|
+
klass.$flags |= FL_SINGLETON;
|
185
|
+
klass.$bridge_prototype = obj;
|
186
|
+
|
187
|
+
obj.$klass = klass;
|
188
|
+
|
189
|
+
klass.__attached__ = obj;
|
190
|
+
|
191
|
+
klass.$klass = class_real(orig_class).$k;
|
192
|
+
|
193
|
+
return klass;
|
194
|
+
}
|
195
|
+
|
196
|
+
function bridge_class(constructor, flags, id) {
|
197
|
+
var klass = define_class(RubyObject, id, RubyObject),
|
198
|
+
prototype = constructor.prototype;
|
199
|
+
|
200
|
+
klass.$allocator = constructor;
|
201
|
+
klass.$proto = prototype;
|
202
|
+
|
203
|
+
bridged_classes.push(klass);
|
204
|
+
|
205
|
+
prototype.$klass = klass;
|
206
|
+
prototype.$flags = flags;
|
207
|
+
|
208
|
+
return klass;
|
209
|
+
}
|
210
|
+
|
211
|
+
// Define new ruby class
|
212
|
+
function define_class(base, id, superklass) {
|
213
|
+
var klass;
|
214
|
+
|
215
|
+
var class_id = (base === RubyObject ? id : base.__classid__ + '::' + id);
|
216
|
+
|
217
|
+
klass = boot_class(superklass);
|
218
|
+
klass.__classid__ = class_id;
|
219
|
+
|
220
|
+
make_metaclass(klass, superklass.$klass);
|
221
|
+
|
222
|
+
var const_alloc = function() {};
|
223
|
+
var const_scope = const_alloc.prototype = new base.$const.alloc();
|
224
|
+
klass.$const = const_scope;
|
225
|
+
const_scope.alloc = const_alloc;
|
226
|
+
|
227
|
+
base.$const[id] = klass;
|
228
|
+
|
229
|
+
if (superklass.m$inherited) {
|
230
|
+
superklass.m$inherited(null, klass);
|
231
|
+
}
|
232
|
+
|
233
|
+
return klass;
|
234
|
+
}
|
235
|
+
|
236
|
+
// Get singleton class of obj
|
237
|
+
function singleton_class(obj) {
|
238
|
+
var klass;
|
239
|
+
|
240
|
+
if (obj.$flags & T_OBJECT) {
|
241
|
+
if ((obj.$flags & T_NUMBER) || (obj.$flags & T_STRING)) {
|
242
|
+
raise(RubyTypeError, "can't define singleton");
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
if ((obj.$klass.$flags & FL_SINGLETON) && obj.$klass.__attached__ == obj) {
|
247
|
+
klass = obj.$klass;
|
248
|
+
}
|
249
|
+
else {
|
250
|
+
var class_id = obj.$klass.__classid__;
|
251
|
+
|
252
|
+
klass = make_metaclass(obj, obj.$klass);
|
253
|
+
}
|
254
|
+
|
255
|
+
return klass;
|
256
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
function exc_backtrace(err) {
|
2
|
+
var old = Error.prepareStackTrace;
|
3
|
+
Error.prepareStackTrace = prepare_backtrace;
|
4
|
+
|
5
|
+
var backtrace = err.stack;
|
6
|
+
Error.prepareStackTrace = old;
|
7
|
+
|
8
|
+
if (backtrace && backtrace.join) {
|
9
|
+
return backtrace;
|
10
|
+
}
|
11
|
+
|
12
|
+
return ["No backtrace available"];
|
13
|
+
}
|
14
|
+
|
15
|
+
function prepare_backtrace(error, stack) {
|
16
|
+
var code = [], f, b, k, name, self;
|
17
|
+
|
18
|
+
for (var i = 0; i < stack.length; i++) {
|
19
|
+
f = stack[i];
|
20
|
+
b = f.getFunction();
|
21
|
+
name = f.getMethodName();
|
22
|
+
self = f.getThis();
|
23
|
+
|
24
|
+
if (!self.$klass || !name) {
|
25
|
+
continue;
|
26
|
+
}
|
27
|
+
|
28
|
+
self = (self.$flags & T_OBJECT ?
|
29
|
+
class_real(self.$klass).__classid__ + '#' :
|
30
|
+
self.__classid__ + '.');
|
31
|
+
|
32
|
+
code.push("from " + self + jsid_to_mid(name) + ' at ' + f.getFileName() + ":" + f.getLineNumber());
|
33
|
+
}
|
34
|
+
|
35
|
+
return code;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Print error backtrace to console
|
39
|
+
opal.backtrace = opal.bt = function(err) {
|
40
|
+
console.log(err.$klass.__classid__ + ": " + err.message);
|
41
|
+
console.log("\t" + exc_backtrace(err).join("\n\t"));
|
42
|
+
};
|
@@ -0,0 +1,114 @@
|
|
1
|
+
// Initialization
|
2
|
+
// --------------
|
3
|
+
|
4
|
+
// The *instances* of core objects
|
5
|
+
var BootObject = boot_defclass();
|
6
|
+
var BootModule = boot_defclass(BootObject);
|
7
|
+
var BootClass = boot_defclass(BootModule);
|
8
|
+
|
9
|
+
// The *classes' of core objects
|
10
|
+
var RubyObject = boot_makemeta('Object', BootObject, BootClass);
|
11
|
+
var RubyModule = boot_makemeta('Module', BootModule, RubyObject.constructor);
|
12
|
+
var RubyClass = boot_makemeta('Class', BootClass, RubyModule.constructor);
|
13
|
+
|
14
|
+
// Fix boot classes to use meta class
|
15
|
+
RubyObject.$klass = RubyClass;
|
16
|
+
RubyModule.$klass = RubyClass;
|
17
|
+
RubyClass.$klass = RubyClass;
|
18
|
+
|
19
|
+
// fix superclasses
|
20
|
+
RubyObject.$s = null;
|
21
|
+
RubyModule.$s = RubyObject;
|
22
|
+
RubyClass.$s = RubyModule;
|
23
|
+
|
24
|
+
opal.Object = RubyObject;
|
25
|
+
opal.Module = RubyModule;
|
26
|
+
opal.Class = RubyClass;
|
27
|
+
|
28
|
+
// make object act like a module
|
29
|
+
var bridged_classes = RubyObject.$included_in = [];
|
30
|
+
|
31
|
+
// Top level Object scope (used by object and top_self).
|
32
|
+
var top_const_alloc = function(){};
|
33
|
+
var top_const_scope = top_const_alloc.prototype;
|
34
|
+
top_const_scope.alloc = top_const_alloc;
|
35
|
+
|
36
|
+
RubyObject.$const = opal.constants = top_const_scope;
|
37
|
+
|
38
|
+
var module_const_alloc = function(){};
|
39
|
+
var module_const_scope = new top_const_alloc();
|
40
|
+
module_const_scope.alloc = module_const_alloc;
|
41
|
+
RubyModule.$const = module_const_scope;
|
42
|
+
|
43
|
+
var class_const_alloc = function(){};
|
44
|
+
var class_const_scope = new top_const_alloc();
|
45
|
+
class_const_scope.alloc = class_const_alloc;
|
46
|
+
RubyClass.$const = class_const_scope;
|
47
|
+
|
48
|
+
RubyObject.$const.BasicObject = RubyObject;
|
49
|
+
RubyObject.$const.Object = RubyObject;
|
50
|
+
RubyObject.$const.Module = RubyModule;
|
51
|
+
RubyObject.$const.Class = RubyClass;
|
52
|
+
|
53
|
+
RubyModule.$allocator.prototype.$donate = function(methods) {
|
54
|
+
var included_in = this.$included_in, includee, method, table = this.$proto, dest;
|
55
|
+
|
56
|
+
if (included_in) {
|
57
|
+
for (var i = 0, length = included_in.length; i < length; i++) {
|
58
|
+
includee = included_in[i];
|
59
|
+
dest = includee.$proto;
|
60
|
+
for (var j = 0, jj = methods.length; j < jj; j++) {
|
61
|
+
method = methods[j];
|
62
|
+
// if (!dest[method]) {
|
63
|
+
dest[method] = table[method];
|
64
|
+
// }
|
65
|
+
}
|
66
|
+
// if our includee is itself included in another module/class then it
|
67
|
+
// should also donate its new methods
|
68
|
+
if (includee.$included_in) {
|
69
|
+
includee.$donate(methods);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
73
|
+
};
|
74
|
+
|
75
|
+
var top_self = opal.top = new RubyObject.$allocator();
|
76
|
+
|
77
|
+
var RubyNilClass = define_class(RubyObject, 'NilClass', RubyObject);
|
78
|
+
var nil = opal.nil = new RubyNilClass.$allocator();
|
79
|
+
|
80
|
+
var RubyArray = bridge_class(Array, T_OBJECT | T_ARRAY, 'Array');
|
81
|
+
var RubyNumeric = bridge_class(Number, T_OBJECT | T_NUMBER, 'Numeric');
|
82
|
+
|
83
|
+
var RubyHash = bridge_class(Hash, T_OBJECT, 'Hash');
|
84
|
+
|
85
|
+
var RubyString = bridge_class(String, T_OBJECT | T_STRING, 'String');
|
86
|
+
var RubyBoolean = bridge_class(Boolean, T_OBJECT | T_BOOLEAN, 'Boolean');
|
87
|
+
var RubyProc = bridge_class(Function, T_OBJECT | T_PROC, 'Proc');
|
88
|
+
var RubyRegexp = bridge_class(RegExp, T_OBJECT, 'Regexp');
|
89
|
+
|
90
|
+
var RubyMatch = define_class(RubyObject, 'MatchData', RubyObject);
|
91
|
+
var RubyRange = define_class(RubyObject, 'Range', RubyObject);
|
92
|
+
|
93
|
+
var RubyException = bridge_class(Error, T_OBJECT, 'Exception');
|
94
|
+
var RubyStandardError = define_class(RubyObject, 'StandardError', RubyException);
|
95
|
+
var RubyRuntimeError = define_class(RubyObject, 'RuntimeError', RubyException);
|
96
|
+
var RubyLocalJumpError = define_class(RubyObject, 'LocalJumpError', RubyStandardError);
|
97
|
+
var RubyTypeError = define_class(RubyObject, 'TypeError', RubyStandardError);
|
98
|
+
var RubyNameError = define_class(RubyObject, 'NameError', RubyStandardError);
|
99
|
+
var RubyNoMethodError = define_class(RubyObject, 'NoMethodError', RubyNameError);
|
100
|
+
var RubyArgError = define_class(RubyObject, 'ArgumentError', RubyStandardError);
|
101
|
+
var RubyScriptError = define_class(RubyObject, 'ScriptError', RubyException);
|
102
|
+
var RubyLoadError = define_class(RubyObject, 'LoadError', RubyScriptError);
|
103
|
+
var RubyIndexError = define_class(RubyObject, 'IndexError', RubyStandardError);
|
104
|
+
var RubyKeyError = define_class(RubyObject, 'KeyError', RubyIndexError);
|
105
|
+
var RubyRangeError = define_class(RubyObject, 'RangeError', RubyStandardError);
|
106
|
+
var RubyNotImplError = define_class(RubyObject, 'NotImplementedError', RubyException);
|
107
|
+
|
108
|
+
RubyException.$allocator.prototype.toString = function() {
|
109
|
+
return this.$klass.__classid__ + ': ' + this.message;
|
110
|
+
};
|
111
|
+
|
112
|
+
var breaker = opal.breaker = new Error('unexpected break');
|
113
|
+
breaker.$klass = RubyLocalJumpError;
|
114
|
+
breaker.$t = function() { throw this; };
|