opal 0.3.25 → 0.3.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +1 -1
- data/README.md +1 -1
- data/Rakefile +1 -6
- data/core/alpha.rb +9 -1
- data/core/class.rb +191 -1
- data/core/{templates.rb → erb.rb} +0 -0
- data/core/kernel.rb +33 -0
- data/core/load_order +1 -5
- data/{vendor/opal_spec_runner.js → core/opal-spec/runner.js} +0 -0
- data/core/{browser.js → parser/browser.js} +0 -0
- data/core/{racc.rb → parser/racc.rb} +0 -0
- data/core/{strscan.rb → parser/strscan.rb} +0 -0
- data/core/runtime.js +3 -8
- data/lib/opal.rb +3 -3
- data/lib/opal/parser.rb +7 -5
- data/lib/opal/rake_task.rb +6 -0
- data/lib/opal/version.rb +1 -1
- data/spec/opal/runtime/class_hierarchy_spec.rb +1 -3
- metadata +8 -15
- data/config.ru +0 -17
- data/core/basic_object.rb +0 -39
- data/core/enumerator.rb +0 -118
- data/core/module.rb +0 -194
- data/core/object.rb +0 -17
- data/core/struct.rb +0 -101
- data/core/top_self.rb +0 -7
data/Gemfile
CHANGED
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -9,12 +9,7 @@ Opal::RakeTask.new do |t|
|
|
9
9
|
t.parser = true # we want to also build opal-parser.js (used in specs)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
task :test do
|
14
|
-
sh "phantomjs vendor/opal_spec_runner.js spec/index.html"
|
15
|
-
end
|
16
|
-
|
17
|
-
task :default => :test
|
12
|
+
task :default => 'opal:test'
|
18
13
|
|
19
14
|
desc "Check file sizes for opal.js runtime"
|
20
15
|
task :sizes do
|
data/core/alpha.rb
CHANGED
data/core/class.rb
CHANGED
@@ -23,6 +23,185 @@ class Class
|
|
23
23
|
}
|
24
24
|
end
|
25
25
|
|
26
|
+
def alias_method(newname, oldname)
|
27
|
+
`#{self}.prototype['$' + newname] = #{self}.prototype['$' + oldname]`
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def ancestors
|
32
|
+
%x{
|
33
|
+
var parent = #{self},
|
34
|
+
result = [];
|
35
|
+
|
36
|
+
while (parent) {
|
37
|
+
result.push(parent);
|
38
|
+
parent = parent._super;
|
39
|
+
}
|
40
|
+
|
41
|
+
return result;
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def append_features(klass)
|
46
|
+
%x{
|
47
|
+
var module = #{self};
|
48
|
+
|
49
|
+
if (!klass.$included_modules) {
|
50
|
+
klass.$included_modules = [];
|
51
|
+
}
|
52
|
+
|
53
|
+
for (var idx = 0, length = klass.$included_modules.length; idx < length; idx++) {
|
54
|
+
if (klass.$included_modules[idx] === module) {
|
55
|
+
return;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
klass.$included_modules.push(module);
|
60
|
+
|
61
|
+
if (!module.$included_in) {
|
62
|
+
module.$included_in = [];
|
63
|
+
}
|
64
|
+
|
65
|
+
module.$included_in.push(klass);
|
66
|
+
|
67
|
+
var donator = module.prototype,
|
68
|
+
prototype = klass.prototype,
|
69
|
+
methods = module._methods;
|
70
|
+
|
71
|
+
for (var i = 0, length = methods.length; i < length; i++) {
|
72
|
+
var method = methods[i];
|
73
|
+
prototype[method] = donator[method];
|
74
|
+
}
|
75
|
+
|
76
|
+
if (klass.$included_in) {
|
77
|
+
klass._donate(methods.slice(), true);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
# Private helper function to define attributes
|
85
|
+
%x{
|
86
|
+
function define_attr(klass, name, getter, setter) {
|
87
|
+
if (getter) {
|
88
|
+
klass.prototype['$' + name] = function() {
|
89
|
+
var res = this[name];
|
90
|
+
return res == null ? nil : res;
|
91
|
+
};
|
92
|
+
|
93
|
+
klass._donate([name]);
|
94
|
+
}
|
95
|
+
|
96
|
+
if (setter) {
|
97
|
+
klass.prototype['$' + name + '='] = function(val) {
|
98
|
+
return this[name] = val;
|
99
|
+
};
|
100
|
+
|
101
|
+
klass._donate([name]);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
def attr_accessor(*attrs)
|
107
|
+
%x{
|
108
|
+
for (var i = 0, length = attrs.length; i < length; i++) {
|
109
|
+
define_attr(#{self}, attrs[i], true, true);
|
110
|
+
}
|
111
|
+
|
112
|
+
return nil;
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
def attr_reader(*attrs)
|
117
|
+
%x{
|
118
|
+
for (var i = 0, length = attrs.length; i < length; i++) {
|
119
|
+
define_attr(#{self}, attrs[i], true, false);
|
120
|
+
}
|
121
|
+
|
122
|
+
return nil;
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
def attr_writer(*attrs)
|
127
|
+
%x{
|
128
|
+
for (var i = 0, length = attrs.length; i < length; i++) {
|
129
|
+
define_attr(#{self}, attrs[i], false, true);
|
130
|
+
}
|
131
|
+
|
132
|
+
return nil;
|
133
|
+
}
|
134
|
+
end
|
135
|
+
|
136
|
+
def attr(name, setter = false)
|
137
|
+
`define_attr(#{self}, name, true, setter)`
|
138
|
+
|
139
|
+
self
|
140
|
+
end
|
141
|
+
|
142
|
+
def define_method(name, &block)
|
143
|
+
%x{
|
144
|
+
if (block === nil) {
|
145
|
+
no_block_given();
|
146
|
+
}
|
147
|
+
|
148
|
+
var jsid = '$' + name;
|
149
|
+
block._jsid = jsid;
|
150
|
+
block._sup = #{self}.prototype[jsid];
|
151
|
+
|
152
|
+
#{self}.prototype[jsid] = block;
|
153
|
+
#{self}._donate([jsid]);
|
154
|
+
|
155
|
+
return nil;
|
156
|
+
}
|
157
|
+
end
|
158
|
+
|
159
|
+
def include(*mods)
|
160
|
+
%x{
|
161
|
+
var i = mods.length - 1, mod;
|
162
|
+
while (i >= 0) {
|
163
|
+
mod = mods[i];
|
164
|
+
i--;
|
165
|
+
|
166
|
+
if (mod === #{self}) {
|
167
|
+
continue;
|
168
|
+
}
|
169
|
+
|
170
|
+
#{ `mod`.append_features self };
|
171
|
+
#{ `mod`.included self };
|
172
|
+
}
|
173
|
+
|
174
|
+
return #{self};
|
175
|
+
}
|
176
|
+
end
|
177
|
+
|
178
|
+
# FIXME
|
179
|
+
def instance_methods
|
180
|
+
[]
|
181
|
+
end
|
182
|
+
|
183
|
+
def included(mod)
|
184
|
+
end
|
185
|
+
|
186
|
+
def inherited(cls)
|
187
|
+
end
|
188
|
+
|
189
|
+
def module_eval(&block)
|
190
|
+
%x{
|
191
|
+
if (block === nil) {
|
192
|
+
no_block_given();
|
193
|
+
}
|
194
|
+
|
195
|
+
return block.call(#{self});
|
196
|
+
}
|
197
|
+
end
|
198
|
+
|
199
|
+
alias class_eval module_eval
|
200
|
+
|
201
|
+
def name
|
202
|
+
`#{self}._name`
|
203
|
+
end
|
204
|
+
|
26
205
|
def new(*args, &block)
|
27
206
|
%x{
|
28
207
|
var obj = new #{self};
|
@@ -34,7 +213,18 @@ class Class
|
|
34
213
|
}
|
35
214
|
end
|
36
215
|
|
37
|
-
def
|
216
|
+
def singleton_class
|
217
|
+
%x{
|
218
|
+
if (#{self}._singleton) {
|
219
|
+
return #{self}._singleton;
|
220
|
+
}
|
221
|
+
|
222
|
+
var meta = new __opal.Class;
|
223
|
+
#{self}._singleton = meta;
|
224
|
+
meta.prototype = #{self};
|
225
|
+
|
226
|
+
return meta;
|
227
|
+
}
|
38
228
|
end
|
39
229
|
|
40
230
|
def superclass
|
File without changes
|
data/core/kernel.rb
CHANGED
@@ -11,6 +11,14 @@ module Kernel
|
|
11
11
|
`#{self} == other`
|
12
12
|
end
|
13
13
|
|
14
|
+
def __send__(symbol, *args, &block)
|
15
|
+
%x{
|
16
|
+
return #{self}['$' + symbol].apply(#{self}, args);
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
alias eql? ==
|
21
|
+
|
14
22
|
def Array(object)
|
15
23
|
%x{
|
16
24
|
if (object.$to_ary) {
|
@@ -62,10 +70,33 @@ module Kernel
|
|
62
70
|
`#{self}._id`
|
63
71
|
end
|
64
72
|
|
73
|
+
def initialize(*)
|
74
|
+
end
|
75
|
+
|
65
76
|
def inspect
|
66
77
|
to_s
|
67
78
|
end
|
68
79
|
|
80
|
+
def instance_eval(string=undefined, &block)
|
81
|
+
%x{
|
82
|
+
if (block === nil) {
|
83
|
+
no_block_given();
|
84
|
+
}
|
85
|
+
|
86
|
+
return block.call(#{self});
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
def instance_exec(*args, &block)
|
91
|
+
%x{
|
92
|
+
if (block === nil) {
|
93
|
+
no_block_given();
|
94
|
+
}
|
95
|
+
|
96
|
+
return block.apply(#{self}, args);
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
69
100
|
def instance_of?(klass)
|
70
101
|
`#{self}._klass === klass`
|
71
102
|
end
|
@@ -178,6 +209,8 @@ module Kernel
|
|
178
209
|
`!!#{self}['$' + name]`
|
179
210
|
end
|
180
211
|
|
212
|
+
alias send __send__
|
213
|
+
|
181
214
|
def singleton_class
|
182
215
|
%x{
|
183
216
|
if (!#{self}._isObject) {
|
data/core/load_order
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/core/runtime.js
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
// The Opal object that is exposed globally
|
2
2
|
var Opal = this.Opal = {};
|
3
3
|
|
4
|
-
// Very root class
|
5
|
-
function BasicObject(){}
|
6
|
-
|
7
4
|
// Core Object class
|
8
5
|
function Object(){}
|
9
6
|
|
@@ -255,13 +252,12 @@ Opal.puts = function(a) { console.log(a); };
|
|
255
252
|
// Initialization
|
256
253
|
// --------------
|
257
254
|
|
258
|
-
boot_defclass('
|
259
|
-
boot_defclass('Object', Object, BasicObject);
|
255
|
+
boot_defclass('Object', Object);
|
260
256
|
boot_defclass('Class', Class, Object);
|
261
257
|
|
262
258
|
Class.prototype = Function.prototype;
|
263
259
|
|
264
|
-
|
260
|
+
Object._klass = Class._klass = Class;
|
265
261
|
|
266
262
|
Module._donate = function(defined) {
|
267
263
|
// ...
|
@@ -323,9 +319,8 @@ function __sdonate(defined) {
|
|
323
319
|
}
|
324
320
|
|
325
321
|
var bridged_classes = Object.$included_in = [];
|
326
|
-
BasicObject.$included_in = bridged_classes;
|
327
322
|
|
328
|
-
|
323
|
+
Object._scope = Opal;
|
329
324
|
Opal.Module = Opal.Class;
|
330
325
|
Opal.Kernel = Object;
|
331
326
|
|
data/lib/opal.rb
CHANGED
@@ -47,7 +47,7 @@ module Opal
|
|
47
47
|
|
48
48
|
[
|
49
49
|
"// Opal v#{Opal::VERSION}",
|
50
|
-
"// http://
|
50
|
+
"// http://opal.github.com",
|
51
51
|
"// Copyright 2012, Adam Beynon",
|
52
52
|
"// Released under the MIT License",
|
53
53
|
"(function(undefined) {",
|
@@ -63,9 +63,9 @@ module Opal
|
|
63
63
|
# @return [String]
|
64
64
|
def self.parser_code
|
65
65
|
[
|
66
|
-
Builder.new(:files => %w(racc.rb strscan.rb), :dir => self.core_dir).build,
|
66
|
+
Builder.new(:files => %w(racc.rb strscan.rb), :dir => File.join(self.core_dir, 'parser')).build,
|
67
67
|
self.build_gem('opal'),
|
68
|
-
File.read(File.join self.core_dir, 'browser.js')
|
68
|
+
File.read(File.join self.core_dir, 'parser', 'browser.js')
|
69
69
|
].join("\n")
|
70
70
|
end
|
71
71
|
|
data/lib/opal/parser.rb
CHANGED
@@ -29,6 +29,7 @@ module Opal
|
|
29
29
|
def parse(source, file = '(file)')
|
30
30
|
@grammar = Grammar.new
|
31
31
|
@requires = []
|
32
|
+
@comments = false
|
32
33
|
@file = file
|
33
34
|
@line = 1
|
34
35
|
@indent = ''
|
@@ -61,7 +62,7 @@ module Opal
|
|
61
62
|
end
|
62
63
|
|
63
64
|
def mid_to_jsid(mid)
|
64
|
-
if
|
65
|
+
if /\=|\+|\-|\*|\/|\!|\?|\<|\>|\&|\||\^|\%|\~|\[/ =~ mid.to_s
|
65
66
|
"['$#{mid}']"
|
66
67
|
else
|
67
68
|
'.$' + mid
|
@@ -698,9 +699,9 @@ module Opal
|
|
698
699
|
spacer = "\n#{@indent}#{INDENT}"
|
699
700
|
cls = "function #{name}() {};"
|
700
701
|
boot = "#{name} = __klass(__base, __super, #{name.inspect}, #{name});"
|
701
|
-
comment = "#{spacer}// line #{ sexp.line }, #{ @file }, class #{ name }
|
702
|
+
comment = "#{spacer}// line #{ sexp.line }, #{ @file }, class #{ name }" if @comment
|
702
703
|
|
703
|
-
"(function(__base, __super){#{comment}#{cls}#{spacer}#{boot}\n#{code}\n#{@indent}})(#{base}, #{sup})"
|
704
|
+
"(function(__base, __super){#{comment}#{spacer}#{cls}#{spacer}#{boot}\n#{code}\n#{@indent}})(#{base}, #{sup})"
|
704
705
|
end
|
705
706
|
|
706
707
|
# s(:sclass, recv, body)
|
@@ -752,9 +753,9 @@ module Opal
|
|
752
753
|
spacer = "\n#{@indent}#{INDENT}"
|
753
754
|
cls = "function #{name}() {};"
|
754
755
|
boot = "#{name} = __module(__base, #{name.inspect}, #{name});"
|
755
|
-
comment = "#{spacer}// line #{ sexp.line }, #{ @file }, module #{ name }
|
756
|
+
comment = "#{spacer}// line #{ sexp.line }, #{ @file }, module #{ name }" if @comment
|
756
757
|
|
757
|
-
"(function(__base){#{comment}#{cls}#{spacer}#{boot}\n#{code}\n#{@indent}})(#{base})"
|
758
|
+
"(function(__base){#{comment}#{spacer}#{cls}#{spacer}#{boot}\n#{code}\n#{@indent}})(#{base})"
|
758
759
|
end
|
759
760
|
|
760
761
|
def process_undef(exp, level)
|
@@ -873,6 +874,7 @@ module Opal
|
|
873
874
|
end
|
874
875
|
|
875
876
|
comment += "\n#{@indent}"
|
877
|
+
comment = nil unless @comments
|
876
878
|
|
877
879
|
if recvr
|
878
880
|
if smethod
|
data/lib/opal/rake_task.rb
CHANGED
@@ -67,6 +67,12 @@ module Opal
|
|
67
67
|
@dependencies.each { |dep| build_gem dep }
|
68
68
|
end
|
69
69
|
|
70
|
+
desc "Run tests through phantomjs"
|
71
|
+
task 'opal:test' do
|
72
|
+
runner = File.join(Opal.core_dir, 'opal-spec', 'runner.js')
|
73
|
+
sh "phantomjs #{runner} spec/index.html"
|
74
|
+
end
|
75
|
+
|
70
76
|
desc "Build opal files, dependencies and specs"
|
71
77
|
task :opal => %w(opal:build opal:dependencies opal:spec)
|
72
78
|
end
|
data/lib/opal/version.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
describe "Class Hierarchy" do
|
2
2
|
it "should have the right superclasses" do
|
3
|
-
|
4
|
-
Object.superclass.should == BasicObject
|
3
|
+
Object.superclass.should == nil
|
5
4
|
Module.superclass.should == Object
|
6
5
|
Class.superclass.should == Object
|
7
6
|
end
|
8
7
|
|
9
8
|
it "should have the right classes" do
|
10
|
-
BasicObject.class.should == Class
|
11
9
|
Object.class.should == Class
|
12
10
|
Class.class.should == Class
|
13
11
|
Module.class.should == Class
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.26
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-18 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Ruby runtime and core library for javascript.
|
15
15
|
email: adam@adambeynon.com
|
@@ -25,36 +25,30 @@ files:
|
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
27
|
- bin/opal
|
28
|
-
- config.ru
|
29
28
|
- core/alpha.rb
|
30
29
|
- core/array.rb
|
31
|
-
- core/basic_object.rb
|
32
30
|
- core/boolean.rb
|
33
|
-
- core/browser.js
|
34
31
|
- core/class.rb
|
35
32
|
- core/comparable.rb
|
36
33
|
- core/enumerable.rb
|
37
|
-
- core/
|
34
|
+
- core/erb.rb
|
38
35
|
- core/error.rb
|
39
36
|
- core/hash.rb
|
40
37
|
- core/json.rb
|
41
38
|
- core/kernel.rb
|
42
39
|
- core/load_order
|
43
|
-
- core/module.rb
|
44
40
|
- core/nil_class.rb
|
45
41
|
- core/numeric.rb
|
46
|
-
- core/
|
42
|
+
- core/opal-spec/runner.js
|
43
|
+
- core/parser/browser.js
|
44
|
+
- core/parser/racc.rb
|
45
|
+
- core/parser/strscan.rb
|
47
46
|
- core/proc.rb
|
48
|
-
- core/racc.rb
|
49
47
|
- core/range.rb
|
50
48
|
- core/regexp.rb
|
51
49
|
- core/runtime.js
|
52
50
|
- core/string.rb
|
53
|
-
- core/strscan.rb
|
54
|
-
- core/struct.rb
|
55
|
-
- core/templates.rb
|
56
51
|
- core/time.rb
|
57
|
-
- core/top_self.rb
|
58
52
|
- lib/opal.rb
|
59
53
|
- lib/opal/builder.rb
|
60
54
|
- lib/opal/erb_parser.rb
|
@@ -346,7 +340,6 @@ files:
|
|
346
340
|
- spec/opal/strscan/check_spec.rb
|
347
341
|
- spec/opal/strscan/scan_spec.rb
|
348
342
|
- spec/spec_helper.rb
|
349
|
-
- vendor/opal_spec_runner.js
|
350
343
|
homepage: http://opalrb.org
|
351
344
|
licenses: []
|
352
345
|
post_install_message:
|
@@ -367,7 +360,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
367
360
|
version: '0'
|
368
361
|
requirements: []
|
369
362
|
rubyforge_project:
|
370
|
-
rubygems_version: 1.8.
|
363
|
+
rubygems_version: 1.8.11
|
371
364
|
signing_key:
|
372
365
|
specification_version: 3
|
373
366
|
summary: Ruby runtime and core library for javascript
|
data/config.ru
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'rack'
|
2
|
-
|
3
|
-
app = lambda do |env|
|
4
|
-
path = env['PATH_INFO']
|
5
|
-
base_path = File.expand_path('../test/index.html', __FILE__)
|
6
|
-
case path
|
7
|
-
when /\.js/
|
8
|
-
contents = File.read(File.join(File.dirname(base_path), '..', path))
|
9
|
-
[200, {'Content-Type' => 'application/x-javascript'}, [contents]]
|
10
|
-
else
|
11
|
-
system 'rake opal'
|
12
|
-
contents = File.read(base_path)
|
13
|
-
[200, {'Content-Type' => 'text/html'}, [contents]]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
run app
|
data/core/basic_object.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
class BasicObject
|
2
|
-
def initialize(*)
|
3
|
-
end
|
4
|
-
|
5
|
-
def ==(other)
|
6
|
-
`#{self} === other`
|
7
|
-
end
|
8
|
-
|
9
|
-
def __send__(symbol, *args, &block)
|
10
|
-
%x{
|
11
|
-
return #{self}['$' + symbol].apply(#{self}, args);
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
|
-
alias send __send__
|
16
|
-
|
17
|
-
alias eql? ==
|
18
|
-
alias equal? ==
|
19
|
-
|
20
|
-
def instance_eval(string=undefined, &block)
|
21
|
-
%x{
|
22
|
-
if (block === nil) {
|
23
|
-
no_block_given();
|
24
|
-
}
|
25
|
-
|
26
|
-
return block.call(#{self});
|
27
|
-
}
|
28
|
-
end
|
29
|
-
|
30
|
-
def instance_exec(*args, &block)
|
31
|
-
%x{
|
32
|
-
if (block === nil) {
|
33
|
-
no_block_given();
|
34
|
-
}
|
35
|
-
|
36
|
-
return block.apply(#{self}, args);
|
37
|
-
}
|
38
|
-
end
|
39
|
-
end
|
data/core/enumerator.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
class Enumerator
|
2
|
-
include Enumerable
|
3
|
-
|
4
|
-
class Yielder
|
5
|
-
def initialize(block)
|
6
|
-
@block = block
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(block)
|
10
|
-
@call = block
|
11
|
-
|
12
|
-
@block.call
|
13
|
-
end
|
14
|
-
|
15
|
-
def yield(value)
|
16
|
-
@call.call(value)
|
17
|
-
end
|
18
|
-
|
19
|
-
alias << yield
|
20
|
-
end
|
21
|
-
|
22
|
-
class Generator
|
23
|
-
attr_reader :enumerator
|
24
|
-
|
25
|
-
def initialize(block)
|
26
|
-
@yielder = Yielder.new(block)
|
27
|
-
end
|
28
|
-
|
29
|
-
def each(&block)
|
30
|
-
@yielder.call(block)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def initialize(object = nil, method = :each, *args, &block)
|
35
|
-
if block_given?
|
36
|
-
@object = Generator.new(block)
|
37
|
-
end
|
38
|
-
|
39
|
-
raise ArgumentError, 'wrong number of argument (0 for 1+)' unless object
|
40
|
-
|
41
|
-
@object = object
|
42
|
-
@method = method
|
43
|
-
@args = args
|
44
|
-
end
|
45
|
-
|
46
|
-
def next
|
47
|
-
_init_cache
|
48
|
-
|
49
|
-
result = @cache[@current] or raise StopIteration, 'iteration reached an end'
|
50
|
-
@current += 1
|
51
|
-
|
52
|
-
result
|
53
|
-
end
|
54
|
-
|
55
|
-
def next_values
|
56
|
-
result = self.next
|
57
|
-
|
58
|
-
result.is_a?(Array) ? result : [result]
|
59
|
-
end
|
60
|
-
|
61
|
-
def peek
|
62
|
-
_init_cache
|
63
|
-
|
64
|
-
@cache[@current] or raise StopIteration, 'iteration reached an end'
|
65
|
-
end
|
66
|
-
|
67
|
-
def peel_values
|
68
|
-
result = self.peek
|
69
|
-
|
70
|
-
result.is_a?(Array) ? result : [result]
|
71
|
-
end
|
72
|
-
|
73
|
-
def rewind
|
74
|
-
_clear_cache
|
75
|
-
end
|
76
|
-
|
77
|
-
def each(&block)
|
78
|
-
return self unless block_given?
|
79
|
-
|
80
|
-
@object.__send__ @method, *@args, &block
|
81
|
-
end
|
82
|
-
|
83
|
-
def each_with_index(&block)
|
84
|
-
with_index &block
|
85
|
-
end
|
86
|
-
|
87
|
-
def with_index(offset = 0)
|
88
|
-
return enum_for :with_index, offset unless block_given?
|
89
|
-
|
90
|
-
current = 0
|
91
|
-
|
92
|
-
each {|*args|
|
93
|
-
next unless current >= offset
|
94
|
-
|
95
|
-
yield *args, current
|
96
|
-
|
97
|
-
current += 1
|
98
|
-
}
|
99
|
-
end
|
100
|
-
|
101
|
-
def with_object(object)
|
102
|
-
return enum_for :with_object, object unless block_given?
|
103
|
-
|
104
|
-
each {|*args|
|
105
|
-
yield *args, object
|
106
|
-
}
|
107
|
-
end
|
108
|
-
|
109
|
-
def _init_cache
|
110
|
-
@current ||= 0
|
111
|
-
@cache ||= to_a
|
112
|
-
end
|
113
|
-
|
114
|
-
def _clear_cache
|
115
|
-
@cache = nil
|
116
|
-
@current = nil
|
117
|
-
end
|
118
|
-
end
|
data/core/module.rb
DELETED
@@ -1,194 +0,0 @@
|
|
1
|
-
class Module
|
2
|
-
|
3
|
-
def alias_method(newname, oldname)
|
4
|
-
`#{self}.prototype['$' + newname] = #{self}.prototype['$' + oldname]`
|
5
|
-
self
|
6
|
-
end
|
7
|
-
|
8
|
-
def ancestors
|
9
|
-
%x{
|
10
|
-
var parent = #{self},
|
11
|
-
result = [];
|
12
|
-
|
13
|
-
while (parent) {
|
14
|
-
result.push(parent);
|
15
|
-
parent = parent._super;
|
16
|
-
}
|
17
|
-
|
18
|
-
return result;
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
def append_features(klass)
|
23
|
-
%x{
|
24
|
-
var module = #{self};
|
25
|
-
|
26
|
-
if (!klass.$included_modules) {
|
27
|
-
klass.$included_modules = [];
|
28
|
-
}
|
29
|
-
|
30
|
-
for (var idx = 0, length = klass.$included_modules.length; idx < length; idx++) {
|
31
|
-
if (klass.$included_modules[idx] === module) {
|
32
|
-
return;
|
33
|
-
}
|
34
|
-
}
|
35
|
-
|
36
|
-
klass.$included_modules.push(module);
|
37
|
-
|
38
|
-
if (!module.$included_in) {
|
39
|
-
module.$included_in = [];
|
40
|
-
}
|
41
|
-
|
42
|
-
module.$included_in.push(klass);
|
43
|
-
|
44
|
-
var donator = module.prototype,
|
45
|
-
prototype = klass.prototype,
|
46
|
-
methods = module._methods;
|
47
|
-
|
48
|
-
for (var i = 0, length = methods.length; i < length; i++) {
|
49
|
-
var method = methods[i];
|
50
|
-
prototype[method] = donator[method];
|
51
|
-
}
|
52
|
-
|
53
|
-
if (klass.$included_in) {
|
54
|
-
klass._donate(methods.slice(), true);
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
self
|
59
|
-
end
|
60
|
-
|
61
|
-
# Private helper function to define attributes
|
62
|
-
%x{
|
63
|
-
function define_attr(klass, name, getter, setter) {
|
64
|
-
if (getter) {
|
65
|
-
klass.prototype['$' + name] = function() {
|
66
|
-
var res = this[name];
|
67
|
-
return res == null ? nil : res;
|
68
|
-
};
|
69
|
-
|
70
|
-
klass._donate([name]);
|
71
|
-
}
|
72
|
-
|
73
|
-
if (setter) {
|
74
|
-
klass.prototype['$' + name + '='] = function(val) {
|
75
|
-
return this[name] = val;
|
76
|
-
};
|
77
|
-
|
78
|
-
klass._donate([name]);
|
79
|
-
}
|
80
|
-
}
|
81
|
-
}
|
82
|
-
|
83
|
-
def attr_accessor(*attrs)
|
84
|
-
%x{
|
85
|
-
for (var i = 0, length = attrs.length; i < length; i++) {
|
86
|
-
define_attr(#{self}, attrs[i], true, true);
|
87
|
-
}
|
88
|
-
|
89
|
-
return nil;
|
90
|
-
}
|
91
|
-
end
|
92
|
-
|
93
|
-
def attr_reader(*attrs)
|
94
|
-
%x{
|
95
|
-
for (var i = 0, length = attrs.length; i < length; i++) {
|
96
|
-
define_attr(#{self}, attrs[i], true, false);
|
97
|
-
}
|
98
|
-
|
99
|
-
return nil;
|
100
|
-
}
|
101
|
-
end
|
102
|
-
|
103
|
-
def attr_writer(*attrs)
|
104
|
-
%x{
|
105
|
-
for (var i = 0, length = attrs.length; i < length; i++) {
|
106
|
-
define_attr(#{self}, attrs[i], false, true);
|
107
|
-
}
|
108
|
-
|
109
|
-
return nil;
|
110
|
-
}
|
111
|
-
end
|
112
|
-
|
113
|
-
def attr(name, setter = false)
|
114
|
-
`define_attr(#{self}, name, true, setter)`
|
115
|
-
|
116
|
-
self
|
117
|
-
end
|
118
|
-
|
119
|
-
def define_method(name, &block)
|
120
|
-
%x{
|
121
|
-
if (block === nil) {
|
122
|
-
no_block_given();
|
123
|
-
}
|
124
|
-
|
125
|
-
var jsid = '$' + name;
|
126
|
-
block._jsid = jsid;
|
127
|
-
block._sup = #{self}.prototype[jsid];
|
128
|
-
|
129
|
-
#{self}.prototype[jsid] = block;
|
130
|
-
#{self}._donate([jsid]);
|
131
|
-
|
132
|
-
return nil;
|
133
|
-
}
|
134
|
-
end
|
135
|
-
|
136
|
-
def include(*mods)
|
137
|
-
%x{
|
138
|
-
var i = mods.length - 1, mod;
|
139
|
-
while (i >= 0) {
|
140
|
-
mod = mods[i];
|
141
|
-
i--;
|
142
|
-
|
143
|
-
if (mod === #{self}) {
|
144
|
-
continue;
|
145
|
-
}
|
146
|
-
|
147
|
-
#{ `mod`.append_features self };
|
148
|
-
#{ `mod`.included self };
|
149
|
-
}
|
150
|
-
|
151
|
-
return #{self};
|
152
|
-
}
|
153
|
-
end
|
154
|
-
|
155
|
-
# FIXME
|
156
|
-
def instance_methods
|
157
|
-
[]
|
158
|
-
end
|
159
|
-
|
160
|
-
def included(mod)
|
161
|
-
end
|
162
|
-
|
163
|
-
def module_eval(&block)
|
164
|
-
%x{
|
165
|
-
if (block === nil) {
|
166
|
-
no_block_given();
|
167
|
-
}
|
168
|
-
|
169
|
-
return block.call(#{self});
|
170
|
-
}
|
171
|
-
end
|
172
|
-
|
173
|
-
alias class_eval module_eval
|
174
|
-
|
175
|
-
def name
|
176
|
-
`#{self}._name`
|
177
|
-
end
|
178
|
-
|
179
|
-
alias public_instance_methods instance_methods
|
180
|
-
|
181
|
-
def singleton_class
|
182
|
-
%x{
|
183
|
-
if (#{self}._singleton) {
|
184
|
-
return #{self}._singleton;
|
185
|
-
}
|
186
|
-
|
187
|
-
var meta = new __opal.Class;
|
188
|
-
#{self}._singleton = meta;
|
189
|
-
meta.prototype = #{self};
|
190
|
-
|
191
|
-
return meta;
|
192
|
-
}
|
193
|
-
end
|
194
|
-
end
|
data/core/object.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
class Object
|
2
|
-
# Kernel included inside runtime.js
|
3
|
-
|
4
|
-
# FIXME
|
5
|
-
def methods
|
6
|
-
[]
|
7
|
-
end
|
8
|
-
|
9
|
-
# FIXME
|
10
|
-
def singleton_methods
|
11
|
-
[]
|
12
|
-
end
|
13
|
-
|
14
|
-
# hack to make bridged classes get basicobject methods
|
15
|
-
alias __send__ __send__
|
16
|
-
alias send send
|
17
|
-
end
|
data/core/struct.rb
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
class Struct
|
2
|
-
def self.new(name, *args)
|
3
|
-
return super unless self == Struct
|
4
|
-
|
5
|
-
if name[0] == name[0].upcase
|
6
|
-
Struct.const_set(name, new(*args))
|
7
|
-
else
|
8
|
-
args.unshift name
|
9
|
-
|
10
|
-
Class.new(self) {
|
11
|
-
args.each { |name| define_struct_attribute name }
|
12
|
-
}
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.define_struct_attribute(name)
|
17
|
-
if self == Struct
|
18
|
-
raise ArgumentError, 'you cannot define attributes to the Struct class'
|
19
|
-
end
|
20
|
-
|
21
|
-
members << name
|
22
|
-
|
23
|
-
define_method name do
|
24
|
-
instance_variable_get "@#{name}"
|
25
|
-
end
|
26
|
-
|
27
|
-
define_method "#{name}=" do |value|
|
28
|
-
instance_variable_set "@#{name}", value
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.members
|
33
|
-
if self == Struct
|
34
|
-
raise ArgumentError, 'the Struct class has no members'
|
35
|
-
end
|
36
|
-
|
37
|
-
@members ||= []
|
38
|
-
end
|
39
|
-
|
40
|
-
include Enumerable
|
41
|
-
|
42
|
-
def initialize(*args)
|
43
|
-
members.each_with_index {|name, index|
|
44
|
-
instance_variable_set "@#{name}", args[index]
|
45
|
-
}
|
46
|
-
end
|
47
|
-
|
48
|
-
def members
|
49
|
-
self.class.members
|
50
|
-
end
|
51
|
-
|
52
|
-
def [](name)
|
53
|
-
if Integer === name
|
54
|
-
raise IndexError, "offset #{name} too large for struct(size:#{members.size})" if name >= members.size
|
55
|
-
|
56
|
-
name = members[name]
|
57
|
-
else
|
58
|
-
raise NameError, "no member '#{name}' in struct" unless members.include?(name.to_sym)
|
59
|
-
end
|
60
|
-
|
61
|
-
instance_variable_get "@#{name}"
|
62
|
-
end
|
63
|
-
|
64
|
-
def []=(name, value)
|
65
|
-
if Integer === name
|
66
|
-
raise IndexError, "offset #{name} too large for struct(size:#{members.size})" if name >= members.size
|
67
|
-
|
68
|
-
name = members[name]
|
69
|
-
else
|
70
|
-
raise NameError, "no member '#{name}' in struct" unless members.include?(name.to_sym)
|
71
|
-
end
|
72
|
-
|
73
|
-
instance_variable_set "@#{name}", value
|
74
|
-
end
|
75
|
-
|
76
|
-
def each
|
77
|
-
members.each { |name| yield self[name] }
|
78
|
-
end
|
79
|
-
|
80
|
-
def each_pair
|
81
|
-
members.each { |name| yield name, self[name] }
|
82
|
-
end
|
83
|
-
|
84
|
-
def eql?(other)
|
85
|
-
hash == other.hash || other.each_with_index.all? {|object, index|
|
86
|
-
self[members[index]] == object
|
87
|
-
}
|
88
|
-
end
|
89
|
-
|
90
|
-
def length
|
91
|
-
members.length
|
92
|
-
end
|
93
|
-
|
94
|
-
alias size length
|
95
|
-
|
96
|
-
def to_a
|
97
|
-
members.map { |name| self[name] }
|
98
|
-
end
|
99
|
-
|
100
|
-
alias values to_a
|
101
|
-
end
|