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,50 @@
|
|
1
|
+
module Native
|
2
|
+
class Object
|
3
|
+
include Native
|
4
|
+
|
5
|
+
def [](name)
|
6
|
+
`#@native[name]`
|
7
|
+
end
|
8
|
+
|
9
|
+
def []=(name, value)
|
10
|
+
`#@native[name] = value`
|
11
|
+
end
|
12
|
+
|
13
|
+
def nil?
|
14
|
+
`#@native === null || #@native === undefined`
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(name, *args)
|
18
|
+
return super unless Opal.function? `#@native[name]`
|
19
|
+
|
20
|
+
__native_send__ name, *args
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included (klass)
|
25
|
+
class << klass
|
26
|
+
def from_native (object)
|
27
|
+
instance = allocate
|
28
|
+
instance.instance_variable_set :@native, object
|
29
|
+
|
30
|
+
instance
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(native)
|
36
|
+
@native = native
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_native
|
40
|
+
@native
|
41
|
+
end
|
42
|
+
|
43
|
+
def native_send(name, *args)
|
44
|
+
return method_missing(name, *args) unless Opal.function? `#@native[name]`
|
45
|
+
|
46
|
+
`#@native[name].apply(#@native, args)`
|
47
|
+
end
|
48
|
+
|
49
|
+
alias_method :__native_send__, :native_send
|
50
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class NilClass
|
2
|
+
def &(other)
|
3
|
+
false
|
4
|
+
end
|
5
|
+
|
6
|
+
def |(other)
|
7
|
+
`other !== false && other !== nil`
|
8
|
+
end
|
9
|
+
|
10
|
+
def ^(other)
|
11
|
+
`other !== false && other !== nil`
|
12
|
+
end
|
13
|
+
|
14
|
+
def ==(other)
|
15
|
+
`this === other`
|
16
|
+
end
|
17
|
+
|
18
|
+
def inspect
|
19
|
+
'nil'
|
20
|
+
end
|
21
|
+
|
22
|
+
def nil?
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_a
|
27
|
+
[]
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_i
|
31
|
+
0
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_f
|
35
|
+
0.0
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_native
|
39
|
+
`var result; return result;`
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
''
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
NIL = nil
|
@@ -0,0 +1,219 @@
|
|
1
|
+
class Numeric
|
2
|
+
def +(other)
|
3
|
+
`this + other`
|
4
|
+
end
|
5
|
+
|
6
|
+
def -(other)
|
7
|
+
`this - other`
|
8
|
+
end
|
9
|
+
|
10
|
+
def *(other)
|
11
|
+
`this * other`
|
12
|
+
end
|
13
|
+
|
14
|
+
def /(other)
|
15
|
+
`this / other`
|
16
|
+
end
|
17
|
+
|
18
|
+
def %(other)
|
19
|
+
`this % other`
|
20
|
+
end
|
21
|
+
|
22
|
+
def &(other)
|
23
|
+
`this & other`
|
24
|
+
end
|
25
|
+
|
26
|
+
def |(other)
|
27
|
+
`this | other`
|
28
|
+
end
|
29
|
+
|
30
|
+
def ^(other)
|
31
|
+
`this ^ other`
|
32
|
+
end
|
33
|
+
|
34
|
+
def <(other)
|
35
|
+
`this < other`
|
36
|
+
end
|
37
|
+
|
38
|
+
def <=(other)
|
39
|
+
`this <= other`
|
40
|
+
end
|
41
|
+
|
42
|
+
def >(other)
|
43
|
+
`this > other`
|
44
|
+
end
|
45
|
+
|
46
|
+
def >=(other)
|
47
|
+
`this >= other`
|
48
|
+
end
|
49
|
+
|
50
|
+
def <<(count)
|
51
|
+
`this << count`
|
52
|
+
end
|
53
|
+
|
54
|
+
def >>(count)
|
55
|
+
`this >> count`
|
56
|
+
end
|
57
|
+
|
58
|
+
def +@
|
59
|
+
`+this`
|
60
|
+
end
|
61
|
+
|
62
|
+
def -@
|
63
|
+
`-this`
|
64
|
+
end
|
65
|
+
|
66
|
+
def ~
|
67
|
+
`~this`
|
68
|
+
end
|
69
|
+
|
70
|
+
def **(other)
|
71
|
+
`Math.pow(this, other)`
|
72
|
+
end
|
73
|
+
|
74
|
+
def ==(other)
|
75
|
+
`this.valueOf() === other.valueOf()`
|
76
|
+
end
|
77
|
+
|
78
|
+
def <=>(other)
|
79
|
+
%x{
|
80
|
+
if (#{!Opal.number?(other)}) {
|
81
|
+
return nil;
|
82
|
+
}
|
83
|
+
|
84
|
+
return this < other ? -1 : (this > other ? 1 : 0);
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
def abs
|
89
|
+
`Math.abs(this)`
|
90
|
+
end
|
91
|
+
|
92
|
+
def ceil
|
93
|
+
`Math.ceil(this)`
|
94
|
+
end
|
95
|
+
|
96
|
+
def downto(finish, &block)
|
97
|
+
return enum_for :downto, finish unless block_given?
|
98
|
+
|
99
|
+
%x{
|
100
|
+
for (var i = this; i >= finish; i--) {
|
101
|
+
if ($yield.call($context, null, i) === $breaker) {
|
102
|
+
return $breaker.$v;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
return this;
|
107
|
+
}
|
108
|
+
end
|
109
|
+
|
110
|
+
def even?
|
111
|
+
`this % 2 === 0`
|
112
|
+
end
|
113
|
+
|
114
|
+
def floor
|
115
|
+
`Math.floor(this)`
|
116
|
+
end
|
117
|
+
|
118
|
+
def hash
|
119
|
+
`this.toString()`
|
120
|
+
end
|
121
|
+
|
122
|
+
def integer?
|
123
|
+
`this % 1 === 0`
|
124
|
+
end
|
125
|
+
|
126
|
+
alias_method :magnitude, :abs
|
127
|
+
|
128
|
+
alias_method :modulo, :%
|
129
|
+
|
130
|
+
def next
|
131
|
+
`this + 1`
|
132
|
+
end
|
133
|
+
|
134
|
+
def nonzero?
|
135
|
+
`this.valueOf() === 0 ? nil : this`
|
136
|
+
end
|
137
|
+
|
138
|
+
def odd?
|
139
|
+
`this % 2 !== 0`
|
140
|
+
end
|
141
|
+
|
142
|
+
def pred
|
143
|
+
`this - 1`
|
144
|
+
end
|
145
|
+
|
146
|
+
alias_method :succ, :next
|
147
|
+
|
148
|
+
def times(&block)
|
149
|
+
return enum_for :times unless block
|
150
|
+
|
151
|
+
%x{
|
152
|
+
for (var i = 0; i <= this; i++) {
|
153
|
+
if ($yield.call($context, null, i) === $breaker) {
|
154
|
+
return $breaker.$v;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
return this;
|
159
|
+
}
|
160
|
+
end
|
161
|
+
|
162
|
+
def to_f
|
163
|
+
`parseFloat(this)`
|
164
|
+
end
|
165
|
+
|
166
|
+
def to_i
|
167
|
+
`parseInt(this)`
|
168
|
+
end
|
169
|
+
|
170
|
+
def to_native
|
171
|
+
`this.valueOf()`
|
172
|
+
end
|
173
|
+
|
174
|
+
def to_s(base = 10)
|
175
|
+
`this.toString(base)`
|
176
|
+
end
|
177
|
+
|
178
|
+
def upto(finish, &block)
|
179
|
+
return enum_for :upto, finish unless block_given?
|
180
|
+
|
181
|
+
%x{
|
182
|
+
for (var i = 0; i <= finish; i++) {
|
183
|
+
if ($yield.call($context, null, i) === $breaker) {
|
184
|
+
return $breaker.$v;
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
return this;
|
189
|
+
}
|
190
|
+
end
|
191
|
+
|
192
|
+
def zero?
|
193
|
+
`this.valueOf() === 0`
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
class Integer
|
198
|
+
def self.===(obj)
|
199
|
+
%x{
|
200
|
+
if (#{!Opal.number?(obj)}) {
|
201
|
+
return false;
|
202
|
+
}
|
203
|
+
|
204
|
+
return other % 1 === 0;
|
205
|
+
}
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
class Float
|
210
|
+
def self.===(obj)
|
211
|
+
%x{
|
212
|
+
if (#{!Opal.number?(obj)}) {
|
213
|
+
return false;
|
214
|
+
}
|
215
|
+
|
216
|
+
return obj % 1 !== 0;
|
217
|
+
}
|
218
|
+
end
|
219
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Object
|
2
|
+
include Kernel
|
3
|
+
|
4
|
+
def methods
|
5
|
+
`this.$klass.$methods`
|
6
|
+
end
|
7
|
+
|
8
|
+
alias_method :private_methods, :methods
|
9
|
+
|
10
|
+
alias_method :protected_methods, :methods
|
11
|
+
|
12
|
+
alias_method :public_methods, :methods
|
13
|
+
|
14
|
+
def singleton_methods
|
15
|
+
raise NotImplementedError, 'Object#singleton_methods not yet implemented'
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_native
|
19
|
+
raise TypeError, 'no specialized #to_native has been implemented'
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Proc
|
2
|
+
def self.new(&block)
|
3
|
+
raise ArgumentError, 'tried to create Proc object without a block' unless block_given?
|
4
|
+
|
5
|
+
block
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_proc
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(*args)
|
13
|
+
`this.apply(this.$S, $slice.call(arguments))`
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_native
|
17
|
+
%x{
|
18
|
+
return function() {
|
19
|
+
var args = Array.slice.call(arguments);
|
20
|
+
args.unshift(null); // block
|
21
|
+
|
22
|
+
return this.apply(this.$S, args);
|
23
|
+
};
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_proc
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
"#<Proc:0x0000000>"
|
33
|
+
end
|
34
|
+
|
35
|
+
def lambda?
|
36
|
+
`this.$lambda ? true : false`
|
37
|
+
end
|
38
|
+
|
39
|
+
def arity
|
40
|
+
`this.length - 1`
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Range
|
2
|
+
def begin
|
3
|
+
`this.begin`
|
4
|
+
end
|
5
|
+
|
6
|
+
def end
|
7
|
+
`this.end`
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method :first, :begin
|
11
|
+
alias_method :min, :begin
|
12
|
+
|
13
|
+
alias_method :last, :end
|
14
|
+
alias_method :max, :end
|
15
|
+
|
16
|
+
def initialize(min, max, exclude = false)
|
17
|
+
@begin = `this.begin = min`
|
18
|
+
@end = `this.end = max`
|
19
|
+
@exclude = `this.exclude = exclude`
|
20
|
+
end
|
21
|
+
|
22
|
+
# FIXME: currently hardcoded to assume range holds numerics
|
23
|
+
def ===(obj)
|
24
|
+
`return obj >= this.begin && obj <= this.end`
|
25
|
+
end
|
26
|
+
|
27
|
+
def exclude_end?
|
28
|
+
`this.exclude`
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
`this.begin + (this.exclude ? '...' : '..') + this.end`
|
33
|
+
end
|
34
|
+
|
35
|
+
def inspect
|
36
|
+
`this.begin + (this.exclude ? '...' : '..') + this.end`
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Rational
|
2
|
+
attr_reader :numerator, :denominator
|
3
|
+
|
4
|
+
def initialize (numerator, denominator = 1)
|
5
|
+
@numerator = numerator
|
6
|
+
@denominator = denominator
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
"#{numerator}#{"/#{denominator}" if denominator}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def inspect
|
14
|
+
"(#{to_s})"
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class Regexp
|
2
|
+
def self.escape(string)
|
3
|
+
`string.replace(/([.*+?^=!:${}()|[\]\\/\\])/g, '\\$1')`
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.new(string, options = undefined)
|
7
|
+
`new RegExp(string, options)`
|
8
|
+
end
|
9
|
+
|
10
|
+
def ==(other)
|
11
|
+
`other.constructor == RegExp && this.toString() === other.toString()`
|
12
|
+
end
|
13
|
+
|
14
|
+
def ===(obj)
|
15
|
+
`this.test(obj)`
|
16
|
+
end
|
17
|
+
|
18
|
+
def =~(string)
|
19
|
+
%x{
|
20
|
+
var result = this.exec(string);
|
21
|
+
|
22
|
+
if (result) {
|
23
|
+
var match = new RubyMatch.$allocator();
|
24
|
+
match.$data = result;
|
25
|
+
#{$~ = `match`};
|
26
|
+
}
|
27
|
+
else {
|
28
|
+
#{$~ = nil};
|
29
|
+
}
|
30
|
+
|
31
|
+
return result ? result.index : nil;
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
alias_method :eql?, :==
|
36
|
+
|
37
|
+
def inspect
|
38
|
+
`this.toString()`
|
39
|
+
end
|
40
|
+
|
41
|
+
def match(pattern)
|
42
|
+
%x{
|
43
|
+
var result = this.exec(pattern);
|
44
|
+
|
45
|
+
if (result) {
|
46
|
+
var match = new RubyMatch.$allocator();
|
47
|
+
match.$data = result;
|
48
|
+
return #{$~ = `match`};
|
49
|
+
}
|
50
|
+
else {
|
51
|
+
return #{$~ = nil};
|
52
|
+
}
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_native
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_s
|
61
|
+
`this.source`
|
62
|
+
end
|
63
|
+
end
|