opal 0.8.1 → 0.9.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -2
- data/.gitmodules +3 -3
- data/.jshintrc +17 -20
- data/.travis.yml +22 -11
- data/CHANGELOG.md +51 -1
- data/CODE_OF_CONDUCT.md +15 -0
- data/CONTRIBUTING.md +125 -9
- data/Gemfile +1 -1
- data/Guardfile +2 -2
- data/README.md +95 -29
- data/Rakefile +1 -1
- data/benchmark/benchmarks +103 -0
- data/benchmark/bm_array_flatten.rb +9 -0
- data/benchmark/bm_array_intersection_numbers.rb +7 -0
- data/benchmark/bm_array_intersection_objects.rb +7 -0
- data/benchmark/bm_array_intersection_strings.rb +7 -0
- data/benchmark/bm_array_join_ary.rb +9 -0
- data/benchmark/bm_array_minus_numbers.rb +7 -0
- data/benchmark/bm_array_minus_objects.rb +7 -0
- data/benchmark/bm_array_minus_strings.rb +7 -0
- data/benchmark/bm_array_union_numbers.rb +7 -0
- data/benchmark/bm_array_union_objects.rb +7 -0
- data/benchmark/bm_array_union_strings.rb +7 -0
- data/benchmark/bm_array_uniq_bang_numbers.rb +5 -0
- data/benchmark/bm_array_uniq_bang_objects.rb +5 -0
- data/benchmark/bm_array_uniq_bang_strings.rb +5 -0
- data/benchmark/bm_array_uniq_numbers.rb +5 -0
- data/benchmark/bm_array_uniq_objects.rb +5 -0
- data/benchmark/bm_array_uniq_strings.rb +5 -0
- data/benchmark/bm_dispatch_bind_table.rb +57 -0
- data/benchmark/bm_dispatch_code_gen.rb +65 -0
- data/benchmark/bm_dispatch_code_gen_if.rb +64 -0
- data/benchmark/bm_dispatch_hardcoded.rb +44 -0
- data/benchmark/bm_dispatch_send.rb +38 -0
- data/benchmark/bm_dispatch_send_table.rb +57 -0
- data/benchmark/bm_hash_assoc_object.rb +11 -0
- data/benchmark/bm_hash_assoc_string.rb +9 -0
- data/benchmark/bm_hash_clone_object.rb +9 -0
- data/benchmark/bm_hash_clone_string.rb +9 -0
- data/benchmark/bm_hash_delete_object.rb +11 -0
- data/benchmark/bm_hash_delete_string.rb +9 -0
- data/benchmark/bm_hash_each_key_object.rb +9 -0
- data/benchmark/bm_hash_each_key_string.rb +9 -0
- data/benchmark/bm_hash_each_object.rb +9 -0
- data/benchmark/bm_hash_each_string.rb +9 -0
- data/benchmark/bm_hash_each_value_object.rb +9 -0
- data/benchmark/bm_hash_each_value_string.rb +9 -0
- data/benchmark/bm_hash_element_reference_object.rb +11 -0
- data/benchmark/bm_hash_element_reference_string.rb +9 -0
- data/benchmark/bm_hash_element_set_object.rb +5 -0
- data/benchmark/bm_hash_element_set_string.rb +5 -0
- data/benchmark/bm_hash_equal_value_object.rb +14 -0
- data/benchmark/bm_hash_equal_value_string.rb +11 -0
- data/benchmark/bm_hash_fetch_object.rb +11 -0
- data/benchmark/bm_hash_fetch_string.rb +9 -0
- data/benchmark/bm_hash_flatten_object.rb +9 -0
- data/benchmark/bm_hash_flatten_string.rb +9 -0
- data/benchmark/bm_hash_has_key_object.rb +11 -0
- data/benchmark/bm_hash_has_key_string.rb +9 -0
- data/benchmark/bm_hash_has_value_object.rb +9 -0
- data/benchmark/bm_hash_has_value_string.rb +9 -0
- data/benchmark/bm_hash_hash_object.rb +9 -0
- data/benchmark/bm_hash_hash_string.rb +9 -0
- data/benchmark/bm_hash_inspect_object.rb +9 -0
- data/benchmark/bm_hash_inspect_string.rb +9 -0
- data/benchmark/bm_hash_invert_object.rb +9 -0
- data/benchmark/bm_hash_invert_string.rb +9 -0
- data/benchmark/bm_hash_keep_if_object.rb +9 -0
- data/benchmark/bm_hash_keep_if_string.rb +9 -0
- data/benchmark/bm_hash_key_object.rb +9 -0
- data/benchmark/bm_hash_key_string.rb +9 -0
- data/benchmark/bm_hash_keys_object.rb +9 -0
- data/benchmark/bm_hash_keys_string.rb +9 -0
- data/benchmark/bm_hash_literal_mixed_large.rb +3 -0
- data/benchmark/bm_hash_literal_mixed_small.rb +3 -0
- data/benchmark/bm_hash_literal_object_large.rb +4 -0
- data/benchmark/bm_hash_literal_object_small.rb +3 -0
- data/benchmark/bm_hash_literal_string_large.rb +4 -0
- data/benchmark/bm_hash_literal_string_small.rb +3 -0
- data/benchmark/bm_hash_merge_object.rb +22 -0
- data/benchmark/bm_hash_merge_string.rb +18 -0
- data/benchmark/bm_hash_rassoc_object.rb +9 -0
- data/benchmark/bm_hash_rassoc_string.rb +9 -0
- data/benchmark/bm_hash_rehash_object.rb +9 -0
- data/benchmark/bm_hash_rehash_string.rb +9 -0
- data/benchmark/bm_hash_reject_bang_object.rb +9 -0
- data/benchmark/bm_hash_reject_bang_string.rb +9 -0
- data/benchmark/bm_hash_reject_object.rb +9 -0
- data/benchmark/bm_hash_reject_string.rb +9 -0
- data/benchmark/bm_hash_replace_object.rb +18 -0
- data/benchmark/bm_hash_replace_string.rb +14 -0
- data/benchmark/bm_hash_select_bang_object.rb +9 -0
- data/benchmark/bm_hash_select_bang_string.rb +9 -0
- data/benchmark/bm_hash_select_object.rb +9 -0
- data/benchmark/bm_hash_select_string.rb +9 -0
- data/benchmark/bm_hash_shift_object.rb +10 -0
- data/benchmark/bm_hash_shift_string.rb +10 -0
- data/benchmark/bm_hash_to_a_object.rb +9 -0
- data/benchmark/bm_hash_to_a_string.rb +9 -0
- data/benchmark/bm_hash_to_h_object.rb +10 -0
- data/benchmark/bm_hash_to_h_string.rb +10 -0
- data/benchmark/bm_hash_values_object.rb +9 -0
- data/benchmark/bm_hash_values_string.rb +9 -0
- data/benchmark/run.rb +48 -0
- data/bin/opal-mspec +1 -1
- data/bin/opal-repl +4 -4
- data/docs/compiled_ruby.md +214 -56
- data/docs/configuring_gems.md +2 -2
- data/docs/faq.md +2 -2
- data/docs/getting_started.md +19 -2
- data/docs/jquery.md +5 -5
- data/docs/opal_parser.md +53 -0
- data/docs/unsupported_features.md +2 -2
- data/docs/upgrading.md +22 -0
- data/docs/using_sprockets.md +15 -0
- data/examples/rack/config.ru +13 -0
- data/examples/sinatra/config.ru +4 -5
- data/lib/mspec/opal/runner.rb +54 -11
- data/lib/opal.rb +1 -1
- data/lib/opal/builder.rb +1 -1
- data/lib/opal/builder_processors.rb +1 -1
- data/lib/opal/cli.rb +17 -13
- data/lib/opal/cli_options.rb +1 -1
- data/lib/opal/compiler.rb +12 -0
- data/lib/opal/config.rb +4 -0
- data/lib/opal/nodes/arglist.rb +5 -7
- data/lib/opal/nodes/call.rb +6 -1
- data/lib/opal/nodes/call_special.rb +74 -0
- data/lib/opal/nodes/def.rb +35 -28
- data/lib/opal/nodes/definitions.rb +3 -5
- data/lib/opal/nodes/for.rb +13 -0
- data/lib/opal/nodes/helpers.rb +15 -1
- data/lib/opal/nodes/if.rb +5 -5
- data/lib/opal/nodes/iter.rb +6 -1
- data/lib/opal/nodes/literal.rb +1 -1
- data/lib/opal/nodes/logic.rb +2 -2
- data/lib/opal/nodes/masgn.rb +1 -2
- data/lib/opal/nodes/module.rb +2 -1
- data/lib/opal/nodes/rescue.rb +10 -1
- data/lib/opal/nodes/scope.rb +8 -2
- data/lib/opal/nodes/singleton_class.rb +1 -1
- data/lib/opal/nodes/top.rb +11 -0
- data/lib/opal/nodes/variables.rb +4 -4
- data/lib/opal/parser.rb +21 -3
- data/lib/opal/parser/grammar.rb +3115 -2961
- data/lib/opal/parser/grammar.y +29 -6
- data/lib/opal/parser/lexer.rb +18 -8
- data/lib/opal/sprockets.rb +85 -0
- data/lib/opal/sprockets/processor.rb +11 -35
- data/lib/opal/sprockets/server.rb +3 -15
- data/lib/opal/version.rb +2 -2
- data/opal.gemspec +4 -4
- data/opal/README.md +9 -0
- data/opal/corelib/array.rb +433 -181
- data/opal/corelib/basic_object.rb +48 -4
- data/opal/corelib/boolean.rb +15 -6
- data/opal/corelib/class.rb +6 -5
- data/opal/corelib/comparable.rb +12 -0
- data/opal/corelib/complex.rb +282 -0
- data/opal/corelib/constants.rb +9 -0
- data/opal/corelib/enumerable.rb +83 -34
- data/opal/corelib/enumerator.rb +3 -1
- data/opal/corelib/error.rb +49 -10
- data/opal/corelib/file.rb +1 -0
- data/opal/corelib/hash.rb +353 -577
- data/opal/corelib/helpers.rb +20 -0
- data/opal/corelib/kernel.rb +114 -59
- data/opal/corelib/math.rb +470 -0
- data/opal/corelib/method.rb +11 -2
- data/opal/corelib/module.rb +96 -96
- data/opal/corelib/{nil_class.rb → nil.rb} +20 -1
- data/opal/corelib/number.rb +751 -0
- data/opal/corelib/numeric.rb +77 -437
- data/opal/corelib/proc.rb +81 -1
- data/opal/corelib/process.rb +27 -0
- data/opal/corelib/rational.rb +358 -0
- data/opal/corelib/regexp.rb +156 -27
- data/opal/corelib/runtime.js +724 -335
- data/opal/corelib/string.rb +93 -104
- data/opal/corelib/string/encoding.rb +177 -0
- data/opal/corelib/string/inheritance.rb +2 -0
- data/opal/corelib/struct.rb +105 -18
- data/opal/corelib/time.rb +267 -146
- data/opal/corelib/unsupported.rb +216 -0
- data/opal/corelib/variables.rb +0 -6
- data/opal/opal.rb +8 -22
- data/opal/opal/base.rb +9 -0
- data/opal/opal/mini.rb +17 -0
- data/spec/README.md +1 -1
- data/spec/filters/bugs/array.rb +38 -136
- data/spec/filters/bugs/{basic_object.rb → basicobject.rb} +14 -15
- data/spec/filters/bugs/class.rb +6 -12
- data/spec/filters/bugs/complex.rb +3 -0
- data/spec/filters/bugs/date.rb +162 -10
- data/spec/filters/bugs/enumerable.rb +31 -58
- data/spec/filters/bugs/enumerator.rb +42 -0
- data/spec/filters/bugs/exception.rb +66 -10
- data/spec/filters/bugs/float.rb +17 -0
- data/spec/filters/bugs/hash.rb +11 -97
- data/spec/filters/bugs/inheritance.rb +5 -0
- data/spec/filters/bugs/integer.rb +28 -0
- data/spec/filters/bugs/kernel.rb +304 -12
- data/spec/filters/bugs/language.rb +133 -399
- data/spec/filters/bugs/language_opal.rb +88 -0
- data/spec/filters/bugs/module.rb +203 -62
- data/spec/filters/bugs/numeric.rb +32 -0
- data/spec/filters/bugs/proc.rb +39 -0
- data/spec/filters/bugs/range.rb +148 -0
- data/spec/filters/bugs/regexp.rb +168 -0
- data/spec/filters/bugs/set.rb +46 -3
- data/spec/filters/bugs/singleton.rb +1 -2
- data/spec/filters/bugs/string.rb +59 -90
- data/spec/filters/bugs/strscan.rb +80 -0
- data/spec/filters/bugs/struct.rb +10 -20
- data/spec/filters/bugs/time.rb +17 -184
- data/spec/filters/bugs/unboundmethod.rb +22 -0
- data/spec/filters/unsupported/array.rb +163 -0
- data/spec/filters/unsupported/basicobject.rb +14 -0
- data/spec/filters/unsupported/bignum.rb +46 -0
- data/spec/filters/unsupported/class.rb +4 -0
- data/spec/filters/unsupported/delegator.rb +5 -0
- data/spec/filters/unsupported/enumerable.rb +11 -0
- data/spec/filters/unsupported/enumerator.rb +8 -9
- data/spec/filters/unsupported/fixnum.rb +14 -0
- data/spec/filters/unsupported/float.rb +41 -7
- data/spec/filters/unsupported/freeze.rb +45 -0
- data/spec/filters/unsupported/hash.rb +50 -0
- data/spec/filters/unsupported/integer.rb +3 -0
- data/spec/filters/unsupported/kernel.rb +31 -0
- data/spec/filters/unsupported/language.rb +17 -0
- data/spec/filters/unsupported/matchdata.rb +30 -0
- data/spec/filters/unsupported/math.rb +3 -0
- data/spec/filters/unsupported/module.rb +5 -3
- data/spec/filters/unsupported/pathname.rb +3 -0
- data/spec/filters/unsupported/privacy.rb +136 -0
- data/spec/filters/unsupported/proc.rb +3 -0
- data/spec/filters/unsupported/regexp.rb +59 -0
- data/spec/filters/unsupported/set.rb +4 -0
- data/spec/filters/unsupported/{marshal.rb → singleton.rb} +4 -2
- data/spec/filters/unsupported/{mutable_strings.rb → string.rb} +456 -336
- data/spec/filters/unsupported/struct.rb +3 -0
- data/spec/filters/unsupported/symbol.rb +5 -0
- data/spec/filters/unsupported/taint.rb +16 -0
- data/spec/filters/unsupported/thread.rb +5 -0
- data/spec/filters/unsupported/time.rb +197 -16
- data/spec/lib/cli_spec.rb +14 -4
- data/spec/lib/compiler_spec.rb +9 -1
- data/spec/lib/parser/call_spec.rb +18 -0
- data/spec/lib/parser/not_spec.rb +2 -8
- data/spec/lib/sprockets_spec.rb +24 -0
- data/spec/opal/core/array/intersection_spec.rb +38 -0
- data/spec/opal/core/array/minus_spec.rb +38 -0
- data/spec/opal/core/array/union_spec.rb +38 -0
- data/spec/opal/core/array/uniq_spec.rb +49 -0
- data/spec/opal/core/exception_spec.rb +7 -0
- data/spec/opal/core/fixtures/require_tree_with_dot/file 1.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_with_dot/file 2.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_with_dot/file 3.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_with_dot/index.rb +3 -0
- data/spec/opal/core/hash/internals_spec.rb +332 -0
- data/spec/opal/core/helpers_spec.rb +14 -0
- data/spec/opal/core/kernel/freeze_spec.rb +1 -1
- data/spec/opal/core/kernel/raise_spec.rb +13 -0
- data/spec/opal/core/kernel/require_tree_spec.rb +9 -0
- data/spec/opal/core/language/class_spec.rb +55 -0
- data/spec/opal/core/language/fixtures/send.rb +1 -0
- data/spec/opal/core/language/keyword_arguments_spec.rb +11 -0
- data/spec/opal/core/language/send_spec.rb +5 -0
- data/spec/opal/core/method/to_proc_spec.rb +28 -0
- data/spec/opal/core/module/name_spec.rb +0 -17
- data/spec/opal/core/runtime/bridged_classes_spec.rb +2 -2
- data/spec/opal/core/runtime/eval_spec.rb +1 -1
- data/spec/opal/core/runtime/method_missing_spec.rb +6 -0
- data/spec/opal/core/runtime_spec.rb +51 -0
- data/spec/opal/stdlib/js_spec.rb +66 -0
- data/spec/opal/stdlib/native/hash_spec.rb +36 -0
- data/spec/rubyspecs +152 -273
- data/spec/spec_helper.rb +10 -11
- data/stdlib/base64.rb +9 -9
- data/stdlib/benchmark.rb +551 -4
- data/stdlib/console.rb +94 -0
- data/stdlib/date.rb +1 -1
- data/stdlib/encoding.rb +1 -170
- data/stdlib/js.rb +56 -0
- data/stdlib/json.rb +9 -14
- data/stdlib/math.rb +1 -370
- data/stdlib/native.rb +133 -63
- data/stdlib/nodejs/file.rb +5 -0
- data/stdlib/nodejs/fileutils.rb +13 -6
- data/stdlib/nodejs/node_modules/js-yaml/node_modules/argparse/README.md +1 -1
- data/stdlib/opal-parser.rb +1 -2
- data/stdlib/ostruct.rb +65 -6
- data/stdlib/pp.rb +2 -4
- data/stdlib/rbconfig.rb +1 -3
- data/stdlib/strscan.rb +164 -28
- data/tasks/benchmarking.rake +88 -0
- data/tasks/testing.rake +181 -55
- data/{lib/mspec/opal/special_calls.rb → tasks/testing/mspec_special_calls.rb} +1 -1
- data/{lib/mspec/opal/sprockets.js → tasks/testing/phantomjs1-sprockets.js} +17 -6
- data/test/opal/test_keyword.rb +590 -0
- data/vendored-minitest/minitest.rb +2 -2
- data/vendored-minitest/test/unit.rb +5 -0
- metadata +229 -62
- data/benchmarks/operators.rb +0 -11
- data/benchmarks/prova.js.rb +0 -13
- data/docs/libraries.md +0 -36
- data/lib/mspec/opal/new.html.erb +0 -1
- data/lib/mspec/opal/rake_task.rb +0 -248
- data/opal/corelib/match_data.rb +0 -128
- data/spec/filters/bugs/math.rb +0 -95
- data/spec/filters/bugs/nil.rb +0 -7
- data/spec/filters/bugs/opal.rb +0 -9
- data/spec/filters/bugs/regular_expressions.rb +0 -41
- data/spec/filters/bugs/stringscanner.rb +0 -33
- data/spec/filters/unsupported/encoding.rb +0 -102
- data/spec/filters/unsupported/frozen.rb +0 -92
- data/spec/filters/unsupported/hash_compare_by_identity.rb +0 -16
- data/spec/filters/unsupported/integer_size.rb +0 -59
- data/spec/filters/unsupported/method_added.rb +0 -10
- data/spec/filters/unsupported/private_constants.rb +0 -30
- data/spec/filters/unsupported/private_methods.rb +0 -55
- data/spec/filters/unsupported/random.rb +0 -4
- data/spec/filters/unsupported/rational_numbers.rb +0 -4
- data/spec/filters/unsupported/regular_expressions.rb +0 -137
- data/spec/filters/unsupported/ruby_exe.rb +0 -5
- data/spec/filters/unsupported/symbols.rb +0 -17
- data/spec/filters/unsupported/tainted.rb +0 -180
- data/spec/filters/unsupported/trusted.rb +0 -88
- data/stdlib/process.rb +0 -10
- data/tasks/documenting.rake +0 -37
data/stdlib/console.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'native'
|
2
|
+
|
3
|
+
# Manipulate the browser console.
|
4
|
+
#
|
5
|
+
# @see https://developer.mozilla.org/en-US/docs/Web/API/console
|
6
|
+
class Console
|
7
|
+
include Native
|
8
|
+
|
9
|
+
# Clear the console.
|
10
|
+
def clear
|
11
|
+
`#@native.clear()`
|
12
|
+
end
|
13
|
+
|
14
|
+
# Print a stacktrace from the call site.
|
15
|
+
def trace
|
16
|
+
`#@native.trace()`
|
17
|
+
end
|
18
|
+
|
19
|
+
# Log the passed objects based on an optional initial format.
|
20
|
+
def log(*args)
|
21
|
+
`#@native.log.apply(#@native, args)`
|
22
|
+
end
|
23
|
+
|
24
|
+
# Log the passed objects based on an optional initial format as informational
|
25
|
+
# log.
|
26
|
+
def info(*args)
|
27
|
+
`#@native.info.apply(#@native, args)`
|
28
|
+
end
|
29
|
+
|
30
|
+
# Log the passed objects based on an optional initial format as warning.
|
31
|
+
def warn(*args)
|
32
|
+
`#@native.warn.apply(#@native, args)`
|
33
|
+
end
|
34
|
+
|
35
|
+
# Log the passed objects based on an optional initial format as error.
|
36
|
+
def error(*args)
|
37
|
+
`#@native.error.apply(#@native, args)`
|
38
|
+
end
|
39
|
+
|
40
|
+
# Time the given block with the given label.
|
41
|
+
def time(label, &block)
|
42
|
+
raise ArgumentError, "no block given" unless block
|
43
|
+
|
44
|
+
`#@native.time(label)`
|
45
|
+
|
46
|
+
begin
|
47
|
+
if block.arity == 0
|
48
|
+
instance_exec(&block)
|
49
|
+
else
|
50
|
+
block.call(self)
|
51
|
+
end
|
52
|
+
ensure
|
53
|
+
`#@native.timeEnd()`
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Group the given block.
|
58
|
+
def group(*args, &block)
|
59
|
+
raise ArgumentError, "no block given" unless block
|
60
|
+
|
61
|
+
`#@native.group.apply(#@native, args)`
|
62
|
+
|
63
|
+
begin
|
64
|
+
if block.arity == 0
|
65
|
+
instance_exec(&block)
|
66
|
+
else
|
67
|
+
block.call(self)
|
68
|
+
end
|
69
|
+
ensure
|
70
|
+
`#@native.groupEnd()`
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Group the given block but collapse it.
|
75
|
+
def group!(*args, &block)
|
76
|
+
return unless block_given?
|
77
|
+
|
78
|
+
`#@native.groupCollapsed.apply(#@native, args)`
|
79
|
+
|
80
|
+
begin
|
81
|
+
if block.arity == 0
|
82
|
+
instance_exec(&block)
|
83
|
+
else
|
84
|
+
block.call(self)
|
85
|
+
end
|
86
|
+
ensure
|
87
|
+
`#@native.groupEnd()`
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
if defined?(`Opal.global.console`)
|
93
|
+
$console = Console.new(`Opal.global.console`)
|
94
|
+
end
|
data/stdlib/date.rb
CHANGED
@@ -10,7 +10,7 @@ class Date
|
|
10
10
|
|
11
11
|
def parse(string)
|
12
12
|
match = `/^(\d*)-(\d*)-(\d*)/.exec(string)`
|
13
|
-
wrap `new Date(parseInt(match[1]), parseInt(match[2]) - 1, parseInt(match[3]))`
|
13
|
+
wrap `new Date(parseInt(match[1], 10), parseInt(match[2], 10) - 1, parseInt(match[3], 10))`
|
14
14
|
end
|
15
15
|
|
16
16
|
def today
|
data/stdlib/encoding.rb
CHANGED
@@ -1,170 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class Encoding
|
4
|
-
def self.register(name, options = {}, &block)
|
5
|
-
names = [name] + (options[:aliases] || [])
|
6
|
-
encoding = Class.new(self, &block).
|
7
|
-
new(name, names, options[:ascii] || false, options[:dummy] || false)
|
8
|
-
|
9
|
-
names.each {|name|
|
10
|
-
const_set name.sub('-', '_'), encoding
|
11
|
-
}
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.find(name)
|
15
|
-
upcase_name = name.upcase
|
16
|
-
return upcase_name if self === upcase_name
|
17
|
-
|
18
|
-
constants.each {|const|
|
19
|
-
encoding = const_get(const)
|
20
|
-
|
21
|
-
if encoding.name == upcase_name || encoding.names.include?(upcase_name)
|
22
|
-
return encoding
|
23
|
-
end
|
24
|
-
}
|
25
|
-
|
26
|
-
raise ArgumentError, "unknown encoding name - #{name}"
|
27
|
-
end
|
28
|
-
|
29
|
-
class << self
|
30
|
-
attr_accessor :default_external
|
31
|
-
end
|
32
|
-
|
33
|
-
attr_reader :name, :names
|
34
|
-
|
35
|
-
def initialize(name, names, ascii, dummy)
|
36
|
-
@name = name
|
37
|
-
@names = names
|
38
|
-
@ascii = ascii
|
39
|
-
@dummy = dummy
|
40
|
-
end
|
41
|
-
|
42
|
-
def ascii_compatible?
|
43
|
-
@ascii
|
44
|
-
end
|
45
|
-
|
46
|
-
def dummy?
|
47
|
-
@dummy
|
48
|
-
end
|
49
|
-
|
50
|
-
def to_s
|
51
|
-
@name
|
52
|
-
end
|
53
|
-
|
54
|
-
def inspect
|
55
|
-
"#<Encoding:#{@name}#{" (dummy)" if @dummy}>"
|
56
|
-
end
|
57
|
-
|
58
|
-
# methods to implement per encoding
|
59
|
-
def each_byte(*)
|
60
|
-
raise NotImplementedError
|
61
|
-
end
|
62
|
-
|
63
|
-
def getbyte(*)
|
64
|
-
raise NotImplementedError
|
65
|
-
end
|
66
|
-
|
67
|
-
def bytesize(*)
|
68
|
-
raise NotImplementedError
|
69
|
-
end
|
70
|
-
|
71
|
-
class EncodingError < StandardError; end
|
72
|
-
class CompatibilityError < EncodingError; end
|
73
|
-
end
|
74
|
-
|
75
|
-
Encoding.register "UTF-8", aliases: ["CP65001"], ascii: true do
|
76
|
-
def each_byte(string, &block)
|
77
|
-
%x{
|
78
|
-
for (var i = 0, length = string.length; i < length; i++) {
|
79
|
-
var code = string.charCodeAt(i);
|
80
|
-
|
81
|
-
if (code <= 0x7f) {
|
82
|
-
#{yield `code`};
|
83
|
-
}
|
84
|
-
else {
|
85
|
-
var encoded = encodeURIComponent(string.charAt(i)).substr(1).split('%');
|
86
|
-
|
87
|
-
for (var j = 0, encoded_length = encoded.length; j < encoded_length; j++) {
|
88
|
-
#{yield `parseInt(encoded[j], 16)`};
|
89
|
-
}
|
90
|
-
}
|
91
|
-
}
|
92
|
-
}
|
93
|
-
end
|
94
|
-
|
95
|
-
def bytesize
|
96
|
-
bytes.length
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
Encoding.register "UTF-16LE" do
|
101
|
-
def each_byte(string, &block)
|
102
|
-
%x{
|
103
|
-
for (var i = 0, length = string.length; i < length; i++) {
|
104
|
-
var code = string.charCodeAt(i);
|
105
|
-
|
106
|
-
#{yield `code & 0xff`};
|
107
|
-
#{yield `code >> 8`};
|
108
|
-
}
|
109
|
-
}
|
110
|
-
end
|
111
|
-
|
112
|
-
def bytesize
|
113
|
-
bytes.length
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
Encoding.register "ASCII-8BIT", aliases: ["BINARY"], ascii: true do
|
118
|
-
def each_byte(string, &block)
|
119
|
-
%x{
|
120
|
-
for (var i = 0, length = string.length; i < length; i++) {
|
121
|
-
#{yield `string.charCodeAt(i) & 0xff`};
|
122
|
-
}
|
123
|
-
}
|
124
|
-
end
|
125
|
-
|
126
|
-
def bytesize
|
127
|
-
bytes.length
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
class String
|
132
|
-
`def.encoding = #{Encoding::UTF_16LE}`
|
133
|
-
|
134
|
-
def bytes
|
135
|
-
each_byte.to_a
|
136
|
-
end
|
137
|
-
|
138
|
-
def bytesize
|
139
|
-
@encoding.bytesize(self)
|
140
|
-
end
|
141
|
-
|
142
|
-
def each_byte(&block)
|
143
|
-
return enum_for :each_byte unless block_given?
|
144
|
-
|
145
|
-
@encoding.each_byte(self, &block)
|
146
|
-
|
147
|
-
self
|
148
|
-
end
|
149
|
-
|
150
|
-
def encoding
|
151
|
-
@encoding
|
152
|
-
end
|
153
|
-
|
154
|
-
def force_encoding(encoding)
|
155
|
-
encoding = Encoding.find(encoding)
|
156
|
-
|
157
|
-
return self if encoding == @encoding
|
158
|
-
|
159
|
-
%x{
|
160
|
-
var result = new String(self);
|
161
|
-
result.encoding = encoding;
|
162
|
-
|
163
|
-
return result;
|
164
|
-
}
|
165
|
-
end
|
166
|
-
|
167
|
-
def getbyte(idx)
|
168
|
-
@encoding.getbyte(self, idx)
|
169
|
-
end
|
170
|
-
end
|
1
|
+
warn "DEPRECATION: encoding is now part of the core library, requiring it is deprecated"
|
data/stdlib/js.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# The JS module provides syntax sugar for calling native javascript
|
2
|
+
# operators (e.g. typeof, instanceof, new, delete) and global functions
|
3
|
+
# (e.g. parseFloat, parseInt).
|
4
|
+
module JS
|
5
|
+
# Use delete to remove a property from an object.
|
6
|
+
def delete(object, property)
|
7
|
+
`delete #{object}[#{property}]`
|
8
|
+
end
|
9
|
+
|
10
|
+
# The global object
|
11
|
+
def global
|
12
|
+
`Opal.global`
|
13
|
+
end
|
14
|
+
|
15
|
+
# Use in to check for a property in an object.
|
16
|
+
def in(property, object)
|
17
|
+
`#{property} in #{object}`
|
18
|
+
end
|
19
|
+
|
20
|
+
# Use instanceof to return whether value is an instance of the function.
|
21
|
+
def instanceof(value, func)
|
22
|
+
`#{value} instanceof #{func}`
|
23
|
+
end
|
24
|
+
|
25
|
+
# Use new to create a new instance of the prototype of the function.
|
26
|
+
def new(func, *args, &block)
|
27
|
+
args << block if block
|
28
|
+
f = `function(){return func.apply(this, args)}`
|
29
|
+
f.JS[:prototype] = func.JS[:prototype]
|
30
|
+
`new f()`
|
31
|
+
end
|
32
|
+
|
33
|
+
# Use typeof to return the underlying javascript type of value.
|
34
|
+
# Note that for undefined values, this will not work exactly like
|
35
|
+
# the javascript typeof operator, as the argument is evaluated before
|
36
|
+
# the function call.
|
37
|
+
def typeof(value)
|
38
|
+
`typeof #{value}`
|
39
|
+
end
|
40
|
+
|
41
|
+
# Use void to return undefined.
|
42
|
+
def void(expr)
|
43
|
+
# Could use `undefined` here, but this is closer to the intent of the method
|
44
|
+
`void #{expr}`
|
45
|
+
end
|
46
|
+
|
47
|
+
# Call the global javascript function with the given arguments.
|
48
|
+
def call(func, *args, &block)
|
49
|
+
g = global
|
50
|
+
args << block if block
|
51
|
+
g.JS[func].JS.apply(g, args)
|
52
|
+
end
|
53
|
+
alias method_missing call
|
54
|
+
|
55
|
+
extend self
|
56
|
+
end
|
data/stdlib/json.rb
CHANGED
@@ -40,7 +40,7 @@ module JSON
|
|
40
40
|
|
41
41
|
var klass;
|
42
42
|
if ((klass = #{`hash`[JSON.create_id]}) != nil) {
|
43
|
-
klass = Opal.
|
43
|
+
klass = Opal.get(klass);
|
44
44
|
return #{`klass`.json_create(`hash`)};
|
45
45
|
}
|
46
46
|
else {
|
@@ -136,27 +136,22 @@ end
|
|
136
136
|
class Hash
|
137
137
|
def to_json
|
138
138
|
%x{
|
139
|
-
var
|
140
|
-
keys = self.keys,
|
141
|
-
_map = self.map,
|
142
|
-
smap = self.smap,
|
143
|
-
map, khash;
|
139
|
+
var result = [];
|
144
140
|
|
145
|
-
for (var i = 0, length = keys.length; i < length; i++) {
|
146
|
-
|
141
|
+
for (var i = 0, keys = self.keys, length = keys.length, key, value; i < length; i++) {
|
142
|
+
key = keys[i];
|
147
143
|
|
148
144
|
if (key.$$is_string) {
|
149
|
-
|
150
|
-
khash = key;
|
145
|
+
value = self.smap[key];
|
151
146
|
} else {
|
152
|
-
|
153
|
-
|
147
|
+
value = key.value;
|
148
|
+
key = key.key;
|
154
149
|
}
|
155
150
|
|
156
|
-
|
151
|
+
result.push(#{`key`.to_s.to_json} + ':' + #{`value`.to_json});
|
157
152
|
}
|
158
153
|
|
159
|
-
return '{' +
|
154
|
+
return '{' + result.join(', ') + '}';
|
160
155
|
}
|
161
156
|
end
|
162
157
|
end
|
data/stdlib/math.rb
CHANGED
@@ -1,370 +1 @@
|
|
1
|
-
|
2
|
-
class DomainError < StandardError
|
3
|
-
def self.new(method)
|
4
|
-
super "Numerical argument is out of domain - \"#{method}\""
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
E = `Math.E`
|
9
|
-
PI = `Math.PI`
|
10
|
-
|
11
|
-
def acos(x)
|
12
|
-
%x{
|
13
|
-
if (!#{Numeric === x}) {
|
14
|
-
#{raise Opal.type_error(x, Float)};
|
15
|
-
}
|
16
|
-
|
17
|
-
x = #{x.to_f};
|
18
|
-
|
19
|
-
if (x < -1 || x > 1) {
|
20
|
-
#{raise DomainError, :acos};
|
21
|
-
}
|
22
|
-
|
23
|
-
return Math.acos(x);
|
24
|
-
}
|
25
|
-
end
|
26
|
-
|
27
|
-
unless defined?(`Math.acosh`)
|
28
|
-
%x{
|
29
|
-
Math.acosh = function(x) {
|
30
|
-
return Math.log(x + Math.sqrt(x * x - 1));
|
31
|
-
}
|
32
|
-
}
|
33
|
-
end
|
34
|
-
|
35
|
-
def acosh(x)
|
36
|
-
%x{
|
37
|
-
if (!#{Numeric === x}) {
|
38
|
-
#{raise Opal.type_error(x, Float)};
|
39
|
-
}
|
40
|
-
|
41
|
-
return Math.acosh(#{x.to_f});
|
42
|
-
}
|
43
|
-
end
|
44
|
-
|
45
|
-
def asin(x)
|
46
|
-
%x{
|
47
|
-
if (!#{Numeric === x}) {
|
48
|
-
#{raise Opal.type_error(x, Float)};
|
49
|
-
}
|
50
|
-
|
51
|
-
x = #{x.to_f};
|
52
|
-
|
53
|
-
if (x < -1 || x > 1) {
|
54
|
-
#{raise DomainError, :asin};
|
55
|
-
}
|
56
|
-
|
57
|
-
return Math.asin(x);
|
58
|
-
}
|
59
|
-
end
|
60
|
-
|
61
|
-
unless defined?(`Math.asinh`)
|
62
|
-
%x{
|
63
|
-
Math.asinh = function(x) {
|
64
|
-
return Math.log(x + Math.sqrt(x * x + 1))
|
65
|
-
}
|
66
|
-
}
|
67
|
-
end
|
68
|
-
|
69
|
-
def asinh(x)
|
70
|
-
%x{
|
71
|
-
if (!#{Numeric === x}) {
|
72
|
-
#{raise Opal.type_error(x, Float)};
|
73
|
-
}
|
74
|
-
|
75
|
-
return Math.asinh(#{x.to_f});
|
76
|
-
}
|
77
|
-
end
|
78
|
-
|
79
|
-
def atan(x)
|
80
|
-
%x{
|
81
|
-
if (!#{Numeric === x}) {
|
82
|
-
#{raise Opal.type_error(x, Float)};
|
83
|
-
}
|
84
|
-
|
85
|
-
return Math.atan(#{x.to_f});
|
86
|
-
}
|
87
|
-
end
|
88
|
-
|
89
|
-
def atan2(x, y)
|
90
|
-
%x{
|
91
|
-
if (!#{Numeric === x}) {
|
92
|
-
#{raise Opal.type_error(x, Float)};
|
93
|
-
}
|
94
|
-
|
95
|
-
if (!#{Numeric === y}) {
|
96
|
-
#{raise Opal.type_error(y, Float)};
|
97
|
-
}
|
98
|
-
|
99
|
-
return Math.atan2(#{x.to_f}, #{y.to_f});
|
100
|
-
}
|
101
|
-
end
|
102
|
-
|
103
|
-
unless defined?(`Math.atanh`)
|
104
|
-
%x{
|
105
|
-
Math.atanh = function(x) {
|
106
|
-
return 0.5 * Math.log((1 + x) / (1 - x));
|
107
|
-
}
|
108
|
-
}
|
109
|
-
end
|
110
|
-
|
111
|
-
def atanh(x)
|
112
|
-
%x{
|
113
|
-
if (!#{Numeric === x}) {
|
114
|
-
#{raise Opal.type_error(x, Float)};
|
115
|
-
}
|
116
|
-
|
117
|
-
x = #{x.to_f};
|
118
|
-
|
119
|
-
if (x < -1 || x > 1) {
|
120
|
-
#{raise DomainError, :atanh};
|
121
|
-
}
|
122
|
-
|
123
|
-
return Math.atanh(x);
|
124
|
-
}
|
125
|
-
end
|
126
|
-
|
127
|
-
# TODO: reimplement this when unavailable
|
128
|
-
def cbrt(x)
|
129
|
-
`Math.cbrt(x)`
|
130
|
-
end
|
131
|
-
|
132
|
-
def cos(x)
|
133
|
-
%x{
|
134
|
-
if (!#{Numeric === x}) {
|
135
|
-
#{raise Opal.type_error(x, Float)};
|
136
|
-
}
|
137
|
-
|
138
|
-
return Math.cos(#{x.to_f});
|
139
|
-
}
|
140
|
-
end
|
141
|
-
|
142
|
-
unless defined?(`Math.cosh`)
|
143
|
-
%x{
|
144
|
-
Math.cosh = function(x) {
|
145
|
-
return (Math.exp(x) + Math.exp(-x)) / 2;
|
146
|
-
}
|
147
|
-
}
|
148
|
-
end
|
149
|
-
|
150
|
-
def cosh(x)
|
151
|
-
%x{
|
152
|
-
if (!#{Numeric === x}) {
|
153
|
-
#{raise Opal.type_error(x, Float)};
|
154
|
-
}
|
155
|
-
|
156
|
-
return Math.cosh(#{x.to_f});
|
157
|
-
}
|
158
|
-
end
|
159
|
-
|
160
|
-
def erf(x)
|
161
|
-
raise NotImplementedError
|
162
|
-
end
|
163
|
-
|
164
|
-
def erfc(x)
|
165
|
-
raise NotImplementedError
|
166
|
-
end
|
167
|
-
|
168
|
-
def exp(x)
|
169
|
-
%x{
|
170
|
-
if (!#{Numeric === x}) {
|
171
|
-
#{raise Opal.type_error(x, Float)};
|
172
|
-
}
|
173
|
-
|
174
|
-
return Math.exp(#{x.to_f});
|
175
|
-
}
|
176
|
-
end
|
177
|
-
|
178
|
-
def frexp(x)
|
179
|
-
raise NotImplementedError
|
180
|
-
end
|
181
|
-
|
182
|
-
def gamma(x)
|
183
|
-
raise NotImplementedError
|
184
|
-
end
|
185
|
-
|
186
|
-
unless defined?(`Math.hypot`)
|
187
|
-
%x{
|
188
|
-
Math.hypot = function(x, y) {
|
189
|
-
return Math.sqrt(x * x + y * y)
|
190
|
-
}
|
191
|
-
}
|
192
|
-
end
|
193
|
-
|
194
|
-
def hypot(x, y)
|
195
|
-
%x{
|
196
|
-
if (!#{Numeric === x}) {
|
197
|
-
#{raise Opal.type_error(x, Float)};
|
198
|
-
}
|
199
|
-
|
200
|
-
if (!#{Numeric === y}) {
|
201
|
-
#{raise Opal.type_error(y, Float)};
|
202
|
-
}
|
203
|
-
|
204
|
-
return Math.hypot(#{x.to_f}, #{y.to_f});
|
205
|
-
}
|
206
|
-
end
|
207
|
-
|
208
|
-
def ldexp(flt, int)
|
209
|
-
raise NotImplementedError
|
210
|
-
end
|
211
|
-
|
212
|
-
def lgamma(x)
|
213
|
-
raise NotImplementedError
|
214
|
-
end
|
215
|
-
|
216
|
-
def log(num, base = E, method = nil)
|
217
|
-
%x{
|
218
|
-
if (!#{Numeric === num}) {
|
219
|
-
#{raise Opal.type_error(num, Float)};
|
220
|
-
}
|
221
|
-
|
222
|
-
if (!#{Numeric === base}) {
|
223
|
-
#{raise Opal.type_error(base, Float)};
|
224
|
-
}
|
225
|
-
|
226
|
-
num = #{num.to_f};
|
227
|
-
base = #{base.to_f};
|
228
|
-
|
229
|
-
if (num < 0) {
|
230
|
-
#{raise DomainError, method || :log};
|
231
|
-
}
|
232
|
-
|
233
|
-
num = Math.log(num);
|
234
|
-
|
235
|
-
if (base != Math.E) {
|
236
|
-
num /= Math.log(base);
|
237
|
-
}
|
238
|
-
|
239
|
-
return num
|
240
|
-
}
|
241
|
-
end
|
242
|
-
|
243
|
-
if defined?(`Math.log10`)
|
244
|
-
def log10(num)
|
245
|
-
%x{
|
246
|
-
if (!#{Numeric === num}) {
|
247
|
-
#{raise Opal.type_error(num, Float)};
|
248
|
-
}
|
249
|
-
|
250
|
-
num = #{num.to_f};
|
251
|
-
|
252
|
-
if (num < 0) {
|
253
|
-
#{raise DomainError, :log2};
|
254
|
-
}
|
255
|
-
|
256
|
-
return Math.log10(num);
|
257
|
-
}
|
258
|
-
end
|
259
|
-
else
|
260
|
-
def log10(num)
|
261
|
-
log(num, 10, :log10)
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
if defined?(`Math.log2`)
|
266
|
-
def log2(num)
|
267
|
-
%x{
|
268
|
-
if (!#{Numeric === num}) {
|
269
|
-
#{raise Opal.type_error(num, Float)};
|
270
|
-
}
|
271
|
-
|
272
|
-
num = #{num.to_f};
|
273
|
-
|
274
|
-
if (num < 0) {
|
275
|
-
#{raise DomainError, :log2};
|
276
|
-
}
|
277
|
-
|
278
|
-
return Math.log2(num);
|
279
|
-
}
|
280
|
-
end
|
281
|
-
else
|
282
|
-
def log2(num)
|
283
|
-
log(num, 2, :log2)
|
284
|
-
end
|
285
|
-
end
|
286
|
-
|
287
|
-
def sin(x)
|
288
|
-
%x{
|
289
|
-
if (!#{Numeric === x}) {
|
290
|
-
#{raise Opal.type_error(x, Float)};
|
291
|
-
}
|
292
|
-
|
293
|
-
return Math.sin(#{x.to_f});
|
294
|
-
}
|
295
|
-
end
|
296
|
-
|
297
|
-
unless defined?(`Math.sinh`)
|
298
|
-
%x{
|
299
|
-
Math.sinh = function(x) {
|
300
|
-
return (Math.exp(x) - Math.exp(-x)) / 2;
|
301
|
-
}
|
302
|
-
}
|
303
|
-
end
|
304
|
-
|
305
|
-
def sinh(x)
|
306
|
-
%x{
|
307
|
-
if (!#{Numeric === x}) {
|
308
|
-
#{raise Opal.type_error(x, Float)};
|
309
|
-
}
|
310
|
-
|
311
|
-
return Math.sinh(#{x.to_f});
|
312
|
-
}
|
313
|
-
end
|
314
|
-
|
315
|
-
def sqrt(x)
|
316
|
-
%x{
|
317
|
-
if (!#{Numeric === x}) {
|
318
|
-
#{raise Opal.type_error(x, Float)};
|
319
|
-
}
|
320
|
-
|
321
|
-
x = #{x.to_f};
|
322
|
-
|
323
|
-
if (x < 0) {
|
324
|
-
#{raise DomainError, :log2};
|
325
|
-
}
|
326
|
-
|
327
|
-
return Math.sqrt(x);
|
328
|
-
}
|
329
|
-
end
|
330
|
-
|
331
|
-
def tan(x)
|
332
|
-
%x{
|
333
|
-
if (!#{Numeric === x}) {
|
334
|
-
#{raise Opal.type_error(x, Float)};
|
335
|
-
}
|
336
|
-
|
337
|
-
return Math.tan(#{x.to_f});
|
338
|
-
}
|
339
|
-
end
|
340
|
-
|
341
|
-
unless defined?(`Math.tanh`)
|
342
|
-
%x{
|
343
|
-
Math.tanh = function(x) {
|
344
|
-
if (x == Infinity) {
|
345
|
-
return 1;
|
346
|
-
}
|
347
|
-
else if (x == -Infinity) {
|
348
|
-
return -1;
|
349
|
-
}
|
350
|
-
else {
|
351
|
-
return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
|
352
|
-
}
|
353
|
-
}
|
354
|
-
}
|
355
|
-
end
|
356
|
-
|
357
|
-
def tanh(x)
|
358
|
-
%x{
|
359
|
-
if (!#{Numeric === x}) {
|
360
|
-
#{raise Opal.type_error(x, Float)};
|
361
|
-
}
|
362
|
-
|
363
|
-
return Math.tanh(#{x.to_f});
|
364
|
-
}
|
365
|
-
end
|
366
|
-
|
367
|
-
class << self
|
368
|
-
include Math
|
369
|
-
end
|
370
|
-
end
|
1
|
+
warn "DEPRECATION: math is now part of the core library, requiring it is deprecated"
|