opal 1.0.3 → 1.1.1.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +13 -12
- data/.gitattributes +1 -1
- data/.github/FUNDING.yml +1 -0
- data/.github/workflows/build.yml +90 -0
- data/.jshintrc +1 -1
- data/.overcommit.yml +35 -0
- data/.rubocop.yml +44 -7
- data/{.rubocop_todo.yml → .rubocop/todo.yml} +4 -5
- data/CHANGELOG.md +152 -1
- data/Gemfile +2 -3
- data/HACKING.md +2 -9
- data/LICENSE +1 -1
- data/README.md +21 -18
- data/UNRELEASED.md +4 -2
- data/benchmark-ips/bm_array_pop_1.rb +8 -0
- data/benchmark-ips/bm_array_shift.rb +7 -0
- data/benchmark-ips/bm_js_symbols_vs_strings.rb +7 -2
- data/benchmark-ips/class_shovel_vs_singleton_class.rb +16 -0
- data/bin/build-browser-source-map-support +4 -0
- data/bin/format-filters +54 -0
- data/bin/git-submodule-fast-install +49 -0
- data/bin/remove-filters +39 -0
- data/bin/setup +4 -0
- data/bin/yarn +11 -0
- data/docs/releasing.md +18 -0
- data/docs/roda-sprockets.md +86 -0
- data/docs/sinatra.md +5 -12
- data/examples/rack/Gemfile +1 -0
- data/examples/rack/app/application.rb +3 -0
- data/examples/rack/config.ru +5 -4
- data/exe/opal-repl +2 -83
- data/lib/opal/ast/builder.rb +1 -1
- data/lib/opal/builder.rb +2 -10
- data/lib/opal/builder_processors.rb +1 -1
- data/lib/opal/cli.rb +4 -1
- data/lib/opal/cli_options.rb +11 -3
- data/lib/opal/cli_runners.rb +27 -37
- data/lib/opal/cli_runners/applescript.rb +5 -44
- data/lib/opal/cli_runners/chrome.js +7 -35
- data/lib/opal/cli_runners/chrome.rb +75 -17
- data/lib/opal/cli_runners/compiler.rb +17 -0
- data/lib/opal/cli_runners/nashorn.rb +9 -43
- data/lib/opal/cli_runners/nodejs.rb +18 -47
- data/lib/opal/cli_runners/server.rb +18 -6
- data/lib/opal/cli_runners/source-map-support-browser.js +6449 -0
- data/lib/opal/cli_runners/source-map-support-node.js +3704 -0
- data/lib/opal/cli_runners/source-map-support.js +639 -0
- data/lib/opal/cli_runners/system_runner.rb +45 -0
- data/lib/opal/compiler.rb +57 -29
- data/lib/opal/config.rb +0 -5
- data/lib/opal/erb.rb +1 -1
- data/lib/opal/magic_comments.rb +34 -0
- data/lib/opal/nodes/args/arity_check.rb +2 -1
- data/lib/opal/nodes/base.rb +1 -1
- data/lib/opal/nodes/call.rb +1 -4
- data/lib/opal/nodes/def.rb +2 -0
- data/lib/opal/nodes/iter.rb +1 -1
- data/lib/opal/nodes/literal.rb +19 -14
- data/lib/opal/nodes/logic.rb +5 -80
- data/lib/opal/nodes/masgn.rb +2 -0
- data/lib/opal/nodes/rescue.rb +1 -1
- data/lib/opal/nodes/super.rb +24 -10
- data/lib/opal/nodes/top.rb +5 -4
- data/lib/opal/parser/with_c_lexer.rb +2 -1
- data/lib/opal/parser/with_ruby_lexer.rb +1 -1
- data/lib/opal/path_reader.rb +2 -2
- data/lib/opal/paths.rb +8 -5
- data/lib/opal/repl.rb +103 -0
- data/lib/opal/rewriter.rb +4 -0
- data/lib/opal/rewriters/for_rewriter.rb +1 -1
- data/lib/opal/rewriters/js_reserved_words.rb +7 -7
- data/lib/opal/rewriters/numblocks.rb +31 -0
- data/lib/opal/rewriters/returnable_logic.rb +33 -0
- data/lib/opal/util.rb +11 -48
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +26 -26
- data/opal/corelib/array.rb +160 -118
- data/opal/corelib/array/pack.rb +5 -3
- data/opal/corelib/basic_object.rb +4 -4
- data/opal/corelib/class.rb +2 -1
- data/opal/corelib/comparable.rb +49 -31
- data/opal/corelib/constants.rb +5 -5
- data/opal/corelib/enumerable.rb +108 -46
- data/opal/corelib/enumerator.rb +27 -12
- data/opal/corelib/file.rb +3 -1
- data/opal/corelib/hash.rb +6 -1
- data/opal/corelib/helpers.rb +8 -28
- data/opal/corelib/io.rb +12 -7
- data/opal/corelib/kernel.rb +45 -14
- data/opal/corelib/kernel/format.rb +3 -1
- data/opal/corelib/math.rb +8 -6
- data/opal/corelib/method.rb +8 -0
- data/opal/corelib/module.rb +17 -2
- data/opal/corelib/number.rb +3 -12
- data/opal/corelib/proc.rb +16 -0
- data/opal/corelib/random/mersenne_twister.rb +147 -0
- data/opal/corelib/range.rb +3 -13
- data/opal/corelib/regexp.rb +10 -6
- data/opal/corelib/runtime.js +208 -81
- data/opal/corelib/string.rb +55 -49
- data/opal/corelib/string/encoding.rb +109 -32
- data/opal/corelib/string/unpack.rb +2 -17
- data/opal/corelib/struct.rb +14 -1
- data/opal/corelib/time.rb +4 -0
- data/opal/opal.rb +1 -1
- data/opal/opal/mini.rb +1 -1
- data/package.json +16 -0
- data/spec/README.md +10 -0
- data/spec/filters/bugs/array.rb +76 -0
- data/spec/filters/bugs/base64.rb +10 -0
- data/spec/filters/bugs/basicobject.rb +12 -0
- data/spec/filters/bugs/bigdecimal.rb +248 -0
- data/spec/filters/bugs/class.rb +12 -0
- data/spec/filters/bugs/complex.rb +7 -0
- data/spec/filters/bugs/date.rb +104 -0
- data/spec/filters/bugs/encoding.rb +259 -0
- data/spec/filters/bugs/enumerable.rb +26 -0
- data/spec/filters/bugs/enumerator.rb +48 -0
- data/spec/filters/bugs/exception.rb +120 -0
- data/spec/filters/bugs/file.rb +48 -0
- data/spec/filters/bugs/float.rb +74 -0
- data/spec/filters/bugs/hash.rb +60 -0
- data/spec/filters/bugs/integer.rb +78 -0
- data/spec/filters/bugs/io.rb +9 -0
- data/spec/filters/bugs/kernel.rb +401 -0
- data/spec/filters/bugs/language.rb +451 -0
- data/spec/filters/bugs/marshal.rb +50 -0
- data/spec/filters/bugs/math.rb +4 -0
- data/spec/filters/bugs/method.rb +79 -0
- data/spec/filters/bugs/module.rb +281 -0
- data/spec/filters/bugs/nilclass.rb +4 -0
- data/spec/filters/bugs/numeric.rb +27 -0
- data/spec/filters/bugs/openstruct.rb +8 -0
- data/spec/filters/bugs/pack_unpack.rb +138 -0
- data/spec/filters/bugs/pathname.rb +9 -0
- data/spec/filters/bugs/proc.rb +80 -0
- data/spec/filters/bugs/random.rb +20 -0
- data/spec/filters/bugs/range.rb +139 -0
- data/spec/filters/bugs/rational.rb +12 -0
- data/spec/filters/bugs/regexp.rb +91 -0
- data/spec/filters/bugs/set.rb +51 -0
- data/spec/filters/bugs/singleton.rb +7 -0
- data/spec/filters/bugs/string.rb +341 -0
- data/spec/filters/bugs/stringscanner.rb +78 -0
- data/spec/filters/bugs/struct.rb +26 -0
- data/spec/filters/bugs/symbol.rb +7 -0
- data/spec/filters/bugs/time.rb +109 -0
- data/spec/filters/bugs/unboundmethod.rb +33 -0
- data/spec/filters/bugs/warnings.rb +30 -0
- data/spec/filters/unsupported/array.rb +168 -0
- data/spec/filters/unsupported/basicobject.rb +15 -0
- data/spec/filters/unsupported/bignum.rb +55 -0
- data/spec/filters/unsupported/class.rb +5 -0
- data/spec/filters/unsupported/delegator.rb +6 -0
- data/spec/filters/unsupported/enumerable.rb +12 -0
- data/spec/filters/unsupported/enumerator.rb +14 -0
- data/spec/filters/unsupported/file.rb +4 -0
- data/spec/filters/unsupported/fixnum.rb +15 -0
- data/spec/filters/unsupported/float.rb +47 -0
- data/spec/filters/unsupported/freeze.rb +259 -0
- data/spec/filters/unsupported/hash.rb +44 -0
- data/spec/filters/unsupported/integer.rb +101 -0
- data/spec/filters/unsupported/kernel.rb +35 -0
- data/spec/filters/unsupported/language.rb +25 -0
- data/spec/filters/unsupported/marshal.rb +44 -0
- data/spec/filters/unsupported/matchdata.rb +63 -0
- data/spec/filters/unsupported/math.rb +4 -0
- data/spec/filters/unsupported/pathname.rb +4 -0
- data/spec/filters/unsupported/privacy.rb +287 -0
- data/spec/filters/unsupported/proc.rb +4 -0
- data/spec/filters/unsupported/random.rb +5 -0
- data/spec/filters/unsupported/range.rb +8 -0
- data/spec/filters/unsupported/regexp.rb +70 -0
- data/spec/filters/unsupported/set.rb +5 -0
- data/spec/filters/unsupported/singleton.rb +7 -0
- data/spec/filters/unsupported/string.rb +687 -0
- data/spec/filters/unsupported/struct.rb +7 -0
- data/spec/filters/unsupported/symbol.rb +21 -0
- data/spec/filters/unsupported/taint.rb +162 -0
- data/spec/filters/unsupported/thread.rb +10 -0
- data/spec/filters/unsupported/time.rb +204 -0
- data/spec/filters/unsupported/usage_of_files.rb +262 -0
- data/spec/lib/builder_processors_spec.rb +44 -0
- data/spec/lib/builder_spec.rb +133 -0
- data/spec/lib/cli_runners/server_spec.rb +25 -0
- data/spec/lib/cli_runners_spec.rb +16 -0
- data/spec/lib/cli_spec.rb +256 -0
- data/spec/lib/compiler_spec.rb +693 -0
- data/spec/lib/config_spec.rb +112 -0
- data/spec/lib/dependency_resolver_spec.rb +43 -0
- data/spec/lib/deprecations_spec.rb +17 -0
- data/spec/lib/fixtures/complex_sprockets.js.rb.erb +4 -0
- data/spec/lib/fixtures/file_with_directives.js +2 -0
- data/spec/lib/fixtures/jst_file.js.jst +1 -0
- data/spec/lib/fixtures/no_requires.rb +1 -0
- data/spec/lib/fixtures/opal_file.rb +2 -0
- data/spec/lib/fixtures/require_tree_test.rb +3 -0
- data/spec/lib/fixtures/required_file.js +1 -0
- data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
- data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
- data/spec/lib/fixtures/requires.rb +7 -0
- data/spec/lib/fixtures/source_location_test.rb +7 -0
- data/spec/lib/fixtures/source_map.rb +1 -0
- data/spec/lib/fixtures/source_map/subfolder/other_file.rb +1 -0
- data/spec/lib/fixtures/sprockets_file.js.rb +3 -0
- data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
- data/spec/lib/path_reader_spec.rb +47 -0
- data/spec/lib/paths_spec.rb +18 -0
- data/spec/lib/repl_spec.rb +28 -0
- data/spec/lib/rewriters/base_spec.rb +68 -0
- data/spec/lib/rewriters/binary_operator_assignment_spec.rb +153 -0
- data/spec/lib/rewriters/block_to_iter_spec.rb +28 -0
- data/spec/lib/rewriters/dot_js_syntax_spec.rb +108 -0
- data/spec/lib/rewriters/explicit_writer_return_spec.rb +186 -0
- data/spec/lib/rewriters/for_rewriter_spec.rb +92 -0
- data/spec/lib/rewriters/hashes/key_duplicates_rewriter_spec.rb +47 -0
- data/spec/lib/rewriters/js_reserved_words_spec.rb +119 -0
- data/spec/lib/rewriters/logical_operator_assignment_spec.rb +202 -0
- data/spec/lib/rewriters/opal_engine_check_spec.rb +84 -0
- data/spec/lib/rewriters/returnable_logic_spec.rb +52 -0
- data/spec/lib/rewriters/rubyspec/filters_rewriter_spec.rb +59 -0
- data/spec/lib/simple_server_spec.rb +56 -0
- data/spec/lib/source_map/file_spec.rb +67 -0
- data/spec/lib/source_map/index_spec.rb +80 -0
- data/spec/lib/spec_helper.rb +105 -0
- data/spec/mspec-opal/formatters.rb +197 -0
- data/spec/mspec-opal/runner.rb +172 -0
- data/spec/opal/compiler/irb_spec.rb +44 -0
- data/spec/opal/compiler/unicode_spec.rb +10 -0
- data/spec/opal/core/array/dup_spec.rb +23 -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/class/inherited_spec.rb +18 -0
- data/spec/opal/core/enumerable/all_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/any_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/collect_break_spec.rb +13 -0
- data/spec/opal/core/enumerable/count_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/detect_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/drop_while_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/each_slice_break_spec.rb +6 -0
- data/spec/opal/core/enumerable/each_with_index_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/each_with_object_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/find_all_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/find_index_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/grep_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/max_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/max_by_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/min_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/min_by_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/none_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/one_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/reduce_break_spec.rb +5 -0
- data/spec/opal/core/enumerable/take_while_break_spec.rb +5 -0
- data/spec/opal/core/enumerator/with_index_spec.rb +6 -0
- data/spec/opal/core/exception_spec.rb +8 -0
- data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/nested/nested 1.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/nested/nested 2.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/other/other 1.rb +1 -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 +339 -0
- data/spec/opal/core/helpers_spec.rb +14 -0
- data/spec/opal/core/iterable_props_spec.rb +53 -0
- data/spec/opal/core/kernel/at_exit_spec.rb +70 -0
- data/spec/opal/core/kernel/freeze_spec.rb +15 -0
- data/spec/opal/core/kernel/instance_variables_spec.rb +110 -0
- data/spec/opal/core/kernel/methods_spec.rb +25 -0
- data/spec/opal/core/kernel/public_methods_spec.rb +25 -0
- data/spec/opal/core/kernel/require_tree_spec.rb +18 -0
- data/spec/opal/core/kernel/respond_to_spec.rb +15 -0
- data/spec/opal/core/language/DATA/characters_support_spec.rb +9 -0
- data/spec/opal/core/language/DATA/empty___END___spec.rb +7 -0
- data/spec/opal/core/language/DATA/multiple___END___spec.rb +10 -0
- data/spec/opal/core/language/arguments/mlhs_arg_spec.rb +18 -0
- data/spec/opal/core/language/keyword_arguments_spec.rb +9 -0
- data/spec/opal/core/language/numblocks_spec.rb +16 -0
- data/spec/opal/core/language/safe_navigator_spec.rb +7 -0
- data/spec/opal/core/language/while_spec.rb +31 -0
- data/spec/opal/core/language_spec.rb +29 -0
- data/spec/opal/core/marshal/dump_spec.rb +81 -0
- data/spec/opal/core/marshal/load_spec.rb +13 -0
- data/spec/opal/core/module_spec.rb +27 -0
- data/spec/opal/core/object_id_spec.rb +56 -0
- data/spec/opal/core/regexp/interpolation_spec.rb +40 -0
- data/spec/opal/core/regexp/match_spec.rb +78 -0
- data/spec/opal/core/runtime/bridged_classes_spec.rb +123 -0
- data/spec/opal/core/runtime/constants_spec.rb +16 -0
- data/spec/opal/core/runtime/eval_spec.rb +5 -0
- data/spec/opal/core/runtime/exit_spec.rb +29 -0
- data/spec/opal/core/runtime/is_a_spec.rb +48 -0
- data/spec/opal/core/runtime/loaded_spec.rb +20 -0
- data/spec/opal/core/runtime/main_methods_spec.rb +39 -0
- data/spec/opal/core/runtime/method_missing_spec.rb +68 -0
- data/spec/opal/core/runtime/rescue_spec.rb +37 -0
- data/spec/opal/core/runtime/string_spec.rb +25 -0
- data/spec/opal/core/runtime/truthy_spec.rb +61 -0
- data/spec/opal/core/runtime_spec.rb +58 -0
- data/spec/opal/core/string/each_byte_spec.rb +19 -0
- data/spec/opal/core/string/gsub_spec.rb +35 -0
- data/spec/opal/core/string/to_sym_spec.rb +9 -0
- data/spec/opal/core/string_spec.rb +28 -0
- data/spec/opal/core/struct/dup_spec.rb +11 -0
- data/spec/opal/core/time_spec.rb +68 -0
- data/spec/opal/stdlib/erb/erb_spec.rb +30 -0
- data/spec/opal/stdlib/erb/inline_block.opalerb +3 -0
- data/spec/opal/stdlib/erb/quoted.opalerb +1 -0
- data/spec/opal/stdlib/erb/simple.opalerb +1 -0
- data/spec/opal/stdlib/js_spec.rb +72 -0
- data/spec/opal/stdlib/json/ext_spec.rb +55 -0
- data/spec/opal/stdlib/json/parse_spec.rb +37 -0
- data/spec/opal/stdlib/logger/logger_spec.rb +308 -0
- data/spec/opal/stdlib/native/alias_native_spec.rb +27 -0
- data/spec/opal/stdlib/native/array_spec.rb +11 -0
- data/spec/opal/stdlib/native/date_spec.rb +12 -0
- data/spec/opal/stdlib/native/deprecated_include_spec.rb +8 -0
- data/spec/opal/stdlib/native/each_spec.rb +13 -0
- data/spec/opal/stdlib/native/element_reference_spec.rb +16 -0
- data/spec/opal/stdlib/native/exposure_spec.rb +33 -0
- data/spec/opal/stdlib/native/ext_spec.rb +19 -0
- data/spec/opal/stdlib/native/hash_spec.rb +67 -0
- data/spec/opal/stdlib/native/initialize_spec.rb +17 -0
- data/spec/opal/stdlib/native/method_missing_spec.rb +51 -0
- data/spec/opal/stdlib/native/native_alias_spec.rb +26 -0
- data/spec/opal/stdlib/native/native_class_spec.rb +18 -0
- data/spec/opal/stdlib/native/native_module_spec.rb +13 -0
- data/spec/opal/stdlib/native/native_reader_spec.rb +22 -0
- data/spec/opal/stdlib/native/native_writer_spec.rb +30 -0
- data/spec/opal/stdlib/native/new_spec.rb +92 -0
- data/spec/opal/stdlib/native/struct_spec.rb +12 -0
- data/spec/opal/stdlib/pp_spec.rb +5 -0
- data/spec/opal/stdlib/promise/always_spec.rb +49 -0
- data/spec/opal/stdlib/promise/error_spec.rb +15 -0
- data/spec/opal/stdlib/promise/rescue_spec.rb +53 -0
- data/spec/opal/stdlib/promise/then_spec.rb +79 -0
- data/spec/opal/stdlib/promise/trace_spec.rb +51 -0
- data/spec/opal/stdlib/promise/value_spec.rb +15 -0
- data/spec/opal/stdlib/promise/when_spec.rb +34 -0
- data/spec/opal/stdlib/source_map_spec.rb +8 -0
- data/spec/opal/stdlib/strscan/scan_spec.rb +11 -0
- data/spec/opal/stdlib/template/paths_spec.rb +10 -0
- data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
- data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
- data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
- data/spec/ruby_specs +183 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/support/guard_platform.rb +4 -0
- data/spec/support/match_helpers.rb +57 -0
- data/spec/support/mspec_rspec_adapter.rb +33 -0
- data/spec/support/rewriters_helper.rb +54 -0
- data/spec/support/source_map_helper.rb +190 -0
- data/stdlib/base64.rb +2 -2
- data/stdlib/bigdecimal.rb +15 -3
- data/stdlib/bigdecimal/bignumber.js.rb +1 -1
- data/stdlib/bigdecimal/util.rb +148 -0
- data/stdlib/delegate.rb +8 -0
- data/stdlib/nodejs/fileutils.rb +1 -1
- data/stdlib/nodejs/kernel.rb +0 -13
- data/stdlib/nodejs/stacktrace.rb +4 -179
- data/stdlib/ostruct.rb +5 -0
- data/stdlib/pp.rb +586 -19
- data/stdlib/prettyprint.rb +556 -0
- data/stdlib/rbconfig/sizeof.rb +2 -0
- data/stdlib/securerandom.rb +32 -0
- data/stdlib/set.rb +36 -0
- data/stdlib/strscan.rb +15 -0
- data/tasks/benchmarking.rake +1 -1
- data/tasks/linting.rake +3 -5
- data/tasks/releasing.rake +2 -5
- data/tasks/testing.rake +16 -11
- data/tasks/testing/mspec_special_calls.rb +1 -19
- data/test/nodejs/fixtures/cat.png +0 -0
- data/test/nodejs/fixtures/hello.rb +1 -0
- data/test/nodejs/fixtures/iso88591.txt +1 -0
- data/test/nodejs/fixtures/utf8.txt +1 -0
- data/test/nodejs/fixtures/win1258.txt +1 -0
- data/test/nodejs/test_dir.rb +39 -0
- data/test/nodejs/test_env.rb +62 -0
- data/test/nodejs/test_error.rb +29 -0
- data/test/nodejs/test_file.rb +206 -0
- data/test/nodejs/test_file_encoding.rb +20 -0
- data/test/nodejs/test_io.rb +18 -0
- data/test/nodejs/test_opal_builder.rb +12 -0
- data/test/nodejs/test_pathname.rb +16 -0
- data/test/opal/cat.png +0 -0
- data/test/opal/http_server.rb +52 -0
- data/test/opal/test_base64.rb +115 -0
- data/test/opal/test_keyword.rb +590 -0
- data/test/opal/test_matrix.rb +661 -0
- data/test/opal/test_openuri.rb +53 -0
- data/test/opal/unsupported_and_bugs.rb +39 -0
- data/yarn.lock +1355 -0
- metadata +905 -40
- data/.travis.yml +0 -109
- data/appveyor.yml +0 -35
- data/lib/opal/nodes/runtime_helpers.rb +0 -51
- data/opal/corelib/random/MersenneTwister.js +0 -137
- data/opal/corelib/random/mersenne_twister.js.rb +0 -13
- data/stdlib/bigdecimal/kernel.rb +0 -5
data/opal/corelib/string.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# helpers: coerce_to, respond_to
|
2
|
+
|
1
3
|
require 'corelib/comparable'
|
2
4
|
require 'corelib/regexp'
|
3
5
|
|
@@ -28,7 +30,7 @@ class String < `String`
|
|
28
30
|
end
|
29
31
|
|
30
32
|
def self.new(str = '')
|
31
|
-
str =
|
33
|
+
str = `$coerce_to(str, #{String}, 'to_str')`
|
32
34
|
`new self.$$constructor(str)`
|
33
35
|
end
|
34
36
|
|
@@ -51,7 +53,7 @@ class String < `String`
|
|
51
53
|
|
52
54
|
def *(count)
|
53
55
|
%x{
|
54
|
-
count =
|
56
|
+
count = $coerce_to(count, #{Integer}, 'to_int');
|
55
57
|
|
56
58
|
if (count < 0) {
|
57
59
|
#{raise ArgumentError, 'negative argument'}
|
@@ -88,7 +90,7 @@ class String < `String`
|
|
88
90
|
end
|
89
91
|
|
90
92
|
def +(other)
|
91
|
-
other =
|
93
|
+
other = `$coerce_to(#{other}, #{String}, 'to_str')`
|
92
94
|
|
93
95
|
`self + #{other.to_s}`
|
94
96
|
end
|
@@ -117,7 +119,7 @@ class String < `String`
|
|
117
119
|
if (other.$$is_string) {
|
118
120
|
return self.toString() === other.toString();
|
119
121
|
}
|
120
|
-
if (
|
122
|
+
if ($respond_to(other, '$to_str')) {
|
121
123
|
return #{other == self};
|
122
124
|
}
|
123
125
|
return false;
|
@@ -143,8 +145,8 @@ class String < `String`
|
|
143
145
|
|
144
146
|
if (index.$$is_range) {
|
145
147
|
exclude = index.excl;
|
146
|
-
length =
|
147
|
-
index =
|
148
|
+
length = $coerce_to(index.end, #{Integer}, 'to_int');
|
149
|
+
index = $coerce_to(index.begin, #{Integer}, 'to_int');
|
148
150
|
|
149
151
|
if (Math.abs(index) > size) {
|
150
152
|
return nil;
|
@@ -194,7 +196,7 @@ class String < `String`
|
|
194
196
|
return self.$$cast(match[0]);
|
195
197
|
}
|
196
198
|
|
197
|
-
length =
|
199
|
+
length = $coerce_to(length, #{Integer}, 'to_int');
|
198
200
|
|
199
201
|
if (length < 0 && -length < match.length) {
|
200
202
|
return self.$$cast(match[length += match.length]);
|
@@ -208,7 +210,7 @@ class String < `String`
|
|
208
210
|
}
|
209
211
|
|
210
212
|
|
211
|
-
index =
|
213
|
+
index = $coerce_to(index, #{Integer}, 'to_int');
|
212
214
|
|
213
215
|
if (index < 0) {
|
214
216
|
index += size;
|
@@ -221,7 +223,7 @@ class String < `String`
|
|
221
223
|
return self.$$cast(self.substr(index, 1));
|
222
224
|
}
|
223
225
|
|
224
|
-
length =
|
226
|
+
length = $coerce_to(length, #{Integer}, 'to_int');
|
225
227
|
|
226
228
|
if (length < 0) {
|
227
229
|
return nil;
|
@@ -247,7 +249,7 @@ class String < `String`
|
|
247
249
|
|
248
250
|
def casecmp(other)
|
249
251
|
return nil unless other.respond_to?(:to_str)
|
250
|
-
other =
|
252
|
+
other = `$coerce_to(other, #{String}, 'to_str')`.to_s
|
251
253
|
%x{
|
252
254
|
var ascii_only = /^[\x00-\x7F]*$/;
|
253
255
|
if (ascii_only.test(self) && ascii_only.test(other)) {
|
@@ -270,8 +272,8 @@ class String < `String`
|
|
270
272
|
end
|
271
273
|
|
272
274
|
def center(width, padstr = ' ')
|
273
|
-
width =
|
274
|
-
padstr =
|
275
|
+
width = `$coerce_to(#{width}, #{Integer}, 'to_int')`
|
276
|
+
padstr = `$coerce_to(#{padstr}, #{String}, 'to_str')`.to_s
|
275
277
|
|
276
278
|
if padstr.empty?
|
277
279
|
raise ArgumentError, 'zero width padding'
|
@@ -344,14 +346,14 @@ class String < `String`
|
|
344
346
|
end
|
345
347
|
|
346
348
|
def clone
|
347
|
-
copy = `self
|
349
|
+
copy = `new String(self)`
|
348
350
|
copy.copy_singleton_methods(self)
|
349
351
|
copy.initialize_clone(self)
|
350
352
|
copy
|
351
353
|
end
|
352
354
|
|
353
355
|
def dup
|
354
|
-
copy = `self
|
356
|
+
copy = `new String(self)`
|
355
357
|
copy.initialize_dup(self)
|
356
358
|
copy
|
357
359
|
end
|
@@ -385,7 +387,7 @@ class String < `String`
|
|
385
387
|
def delete_prefix(prefix)
|
386
388
|
%x{
|
387
389
|
if (!prefix.$$is_string) {
|
388
|
-
|
390
|
+
prefix = $coerce_to(prefix, #{String}, 'to_str');
|
389
391
|
}
|
390
392
|
|
391
393
|
if (self.slice(0, prefix.length) === prefix) {
|
@@ -399,7 +401,7 @@ class String < `String`
|
|
399
401
|
def delete_suffix(suffix)
|
400
402
|
%x{
|
401
403
|
if (!suffix.$$is_string) {
|
402
|
-
|
404
|
+
suffix = $coerce_to(suffix, #{String}, 'to_str');
|
403
405
|
}
|
404
406
|
|
405
407
|
if (self.slice(self.length - suffix.length) === suffix) {
|
@@ -436,7 +438,7 @@ class String < `String`
|
|
436
438
|
return self;
|
437
439
|
}
|
438
440
|
|
439
|
-
separator =
|
441
|
+
separator = $coerce_to(separator, #{String}, 'to_str')
|
440
442
|
|
441
443
|
var a, i, n, length, chomped, trailing, splitted;
|
442
444
|
|
@@ -475,7 +477,7 @@ class String < `String`
|
|
475
477
|
def end_with?(*suffixes)
|
476
478
|
%x{
|
477
479
|
for (var i = 0, length = suffixes.length; i < length; i++) {
|
478
|
-
var suffix =
|
480
|
+
var suffix = $coerce_to(suffixes[i], #{String}, 'to_str').$to_s();
|
479
481
|
|
480
482
|
if (self.length >= suffix.length &&
|
481
483
|
self.substr(self.length - suffix.length, suffix.length) == suffix) {
|
@@ -500,7 +502,7 @@ class String < `String`
|
|
500
502
|
if (pattern.$$is_regexp) {
|
501
503
|
pattern = Opal.global_multiline_regexp(pattern);
|
502
504
|
} else {
|
503
|
-
pattern =
|
505
|
+
pattern = $coerce_to(pattern, #{String}, 'to_str');
|
504
506
|
pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm');
|
505
507
|
}
|
506
508
|
|
@@ -526,7 +528,7 @@ class String < `String`
|
|
526
528
|
}
|
527
529
|
else {
|
528
530
|
if (!replacement.$$is_string) {
|
529
|
-
replacement =
|
531
|
+
replacement = $coerce_to(replacement, #{String}, 'to_str');
|
530
532
|
}
|
531
533
|
_replacement = replacement.replace(/([\\]+)([0-9+&`'])/g, function (original, slashes, command) {
|
532
534
|
if (slashes.length % 2 === 0) {
|
@@ -549,7 +551,7 @@ class String < `String`
|
|
549
551
|
}
|
550
552
|
|
551
553
|
if (pattern.lastIndex === match.index) {
|
552
|
-
result += (
|
554
|
+
result += (self.slice(index, match.index) + _replacement + (self[match.index] || ""));
|
553
555
|
pattern.lastIndex += 1;
|
554
556
|
}
|
555
557
|
else {
|
@@ -574,7 +576,7 @@ class String < `String`
|
|
574
576
|
def include?(other)
|
575
577
|
%x{
|
576
578
|
if (!other.$$is_string) {
|
577
|
-
|
579
|
+
other = $coerce_to(other, #{String}, 'to_str');
|
578
580
|
}
|
579
581
|
return self.indexOf(other) !== -1;
|
580
582
|
}
|
@@ -589,7 +591,7 @@ class String < `String`
|
|
589
591
|
if (offset === undefined) {
|
590
592
|
offset = 0;
|
591
593
|
} else {
|
592
|
-
offset =
|
594
|
+
offset = $coerce_to(offset, #{Integer}, 'to_int');
|
593
595
|
if (offset < 0) {
|
594
596
|
offset += self.length;
|
595
597
|
if (offset < 0) {
|
@@ -615,7 +617,7 @@ class String < `String`
|
|
615
617
|
regex.lastIndex = match.index + 1;
|
616
618
|
}
|
617
619
|
} else {
|
618
|
-
search =
|
620
|
+
search = $coerce_to(search, #{String}, 'to_str');
|
619
621
|
if (search.length === 0 && offset > self.length) {
|
620
622
|
index = -1;
|
621
623
|
} else {
|
@@ -663,8 +665,8 @@ class String < `String`
|
|
663
665
|
end
|
664
666
|
|
665
667
|
def ljust(width, padstr = ' ')
|
666
|
-
width =
|
667
|
-
padstr =
|
668
|
+
width = `$coerce_to(#{width}, #{Integer}, 'to_int')`
|
669
|
+
padstr = `$coerce_to(#{padstr}, #{String}, 'to_str')`.to_s
|
668
670
|
|
669
671
|
if padstr.empty?
|
670
672
|
raise ArgumentError, 'zero width padding'
|
@@ -860,7 +862,7 @@ class String < `String`
|
|
860
862
|
i = m.index;
|
861
863
|
}
|
862
864
|
} else {
|
863
|
-
sep =
|
865
|
+
sep = $coerce_to(sep, #{String}, 'to_str');
|
864
866
|
i = self.indexOf(sep);
|
865
867
|
}
|
866
868
|
|
@@ -887,7 +889,7 @@ class String < `String`
|
|
887
889
|
if (offset === undefined) {
|
888
890
|
offset = self.length;
|
889
891
|
} else {
|
890
|
-
offset =
|
892
|
+
offset = $coerce_to(offset, #{Integer}, 'to_int');
|
891
893
|
if (offset < 0) {
|
892
894
|
offset += self.length;
|
893
895
|
if (offset < 0) {
|
@@ -915,7 +917,7 @@ class String < `String`
|
|
915
917
|
i = m.index;
|
916
918
|
}
|
917
919
|
} else {
|
918
|
-
search =
|
920
|
+
search = $coerce_to(search, #{String}, 'to_str');
|
919
921
|
i = self.lastIndexOf(search, offset);
|
920
922
|
}
|
921
923
|
|
@@ -924,8 +926,8 @@ class String < `String`
|
|
924
926
|
end
|
925
927
|
|
926
928
|
def rjust(width, padstr = ' ')
|
927
|
-
width =
|
928
|
-
padstr =
|
929
|
+
width = `$coerce_to(#{width}, #{Integer}, 'to_int')`
|
930
|
+
padstr = `$coerce_to(#{padstr}, #{String}, 'to_str')`.to_s
|
929
931
|
|
930
932
|
if padstr.empty?
|
931
933
|
raise ArgumentError, 'zero width padding'
|
@@ -969,7 +971,7 @@ class String < `String`
|
|
969
971
|
}
|
970
972
|
|
971
973
|
} else {
|
972
|
-
sep =
|
974
|
+
sep = $coerce_to(sep, #{String}, 'to_str');
|
973
975
|
i = self.lastIndexOf(sep);
|
974
976
|
}
|
975
977
|
|
@@ -998,7 +1000,7 @@ class String < `String`
|
|
998
1000
|
if (pattern.$$is_regexp) {
|
999
1001
|
pattern = Opal.global_multiline_regexp(pattern);
|
1000
1002
|
} else {
|
1001
|
-
pattern =
|
1003
|
+
pattern = $coerce_to(pattern, #{String}, 'to_str');
|
1002
1004
|
pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm');
|
1003
1005
|
}
|
1004
1006
|
|
@@ -1052,7 +1054,7 @@ class String < `String`
|
|
1052
1054
|
if (pattern.$$is_regexp) {
|
1053
1055
|
pattern = Opal.global_multiline_regexp(pattern);
|
1054
1056
|
} else {
|
1055
|
-
pattern =
|
1057
|
+
pattern = $coerce_to(pattern, #{String}, 'to_str').$to_s();
|
1056
1058
|
if (pattern === ' ') {
|
1057
1059
|
pattern = /\s+/gm;
|
1058
1060
|
string = string.replace(/^\s+/, '');
|
@@ -1139,7 +1141,7 @@ class String < `String`
|
|
1139
1141
|
def start_with?(*prefixes)
|
1140
1142
|
%x{
|
1141
1143
|
for (var i = 0, length = prefixes.length; i < length; i++) {
|
1142
|
-
var prefix =
|
1144
|
+
var prefix = $coerce_to(prefixes[i], #{String}, 'to_str').$to_s();
|
1143
1145
|
|
1144
1146
|
if (self.indexOf(prefix) === 0) {
|
1145
1147
|
return true;
|
@@ -1157,7 +1159,7 @@ class String < `String`
|
|
1157
1159
|
def sub(pattern, replacement = undefined, &block)
|
1158
1160
|
%x{
|
1159
1161
|
if (!pattern.$$is_regexp) {
|
1160
|
-
pattern =
|
1162
|
+
pattern = $coerce_to(pattern, #{String}, 'to_str');
|
1161
1163
|
pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
1162
1164
|
}
|
1163
1165
|
|
@@ -1182,7 +1184,7 @@ class String < `String`
|
|
1182
1184
|
|
1183
1185
|
} else {
|
1184
1186
|
|
1185
|
-
replacement =
|
1187
|
+
replacement = $coerce_to(replacement, #{String}, 'to_str');
|
1186
1188
|
|
1187
1189
|
replacement = replacement.replace(/([\\]+)([0-9+&`'])/g, function (original, slashes, command) {
|
1188
1190
|
if (slashes.length % 2 === 0) {
|
@@ -1215,7 +1217,7 @@ class String < `String`
|
|
1215
1217
|
|
1216
1218
|
def sum(n = 16)
|
1217
1219
|
%x{
|
1218
|
-
n =
|
1220
|
+
n = $coerce_to(n, #{Integer}, 'to_int');
|
1219
1221
|
|
1220
1222
|
var result = 0,
|
1221
1223
|
length = self.length,
|
@@ -1268,7 +1270,7 @@ class String < `String`
|
|
1268
1270
|
%x{
|
1269
1271
|
var result,
|
1270
1272
|
string = self.toLowerCase(),
|
1271
|
-
radix =
|
1273
|
+
radix = $coerce_to(base, #{Integer}, 'to_int');
|
1272
1274
|
|
1273
1275
|
if (radix === 1 || radix < 0 || radix > 36) {
|
1274
1276
|
#{raise ArgumentError, "invalid radix #{`radix`}"}
|
@@ -1364,9 +1366,10 @@ class String < `String`
|
|
1364
1366
|
alias to_sym intern
|
1365
1367
|
|
1366
1368
|
def tr(from, to)
|
1367
|
-
from = Opal.coerce_to(from, String, :to_str).to_s
|
1368
|
-
to = Opal.coerce_to(to, String, :to_str).to_s
|
1369
1369
|
%x{
|
1370
|
+
from = $coerce_to(from, #{String}, 'to_str').$to_s();
|
1371
|
+
to = $coerce_to(to, #{String}, 'to_str').$to_s();
|
1372
|
+
|
1370
1373
|
if (from.length == 0 || from === to) {
|
1371
1374
|
return self;
|
1372
1375
|
}
|
@@ -1508,9 +1511,10 @@ class String < `String`
|
|
1508
1511
|
end
|
1509
1512
|
|
1510
1513
|
def tr_s(from, to)
|
1511
|
-
from = Opal.coerce_to(from, String, :to_str).to_s
|
1512
|
-
to = Opal.coerce_to(to, String, :to_str).to_s
|
1513
1514
|
%x{
|
1515
|
+
from = $coerce_to(from, #{String}, 'to_str').$to_s();
|
1516
|
+
to = $coerce_to(to, #{String}, 'to_str').$to_s();
|
1517
|
+
|
1514
1518
|
if (from.length == 0) {
|
1515
1519
|
return self;
|
1516
1520
|
}
|
@@ -1675,10 +1679,11 @@ class String < `String`
|
|
1675
1679
|
|
1676
1680
|
def upto(stop, excl = false, &block)
|
1677
1681
|
return enum_for :upto, stop, excl unless block_given?
|
1678
|
-
stop = Opal.coerce_to(stop, String, :to_str)
|
1679
1682
|
%x{
|
1680
1683
|
var a, b, s = self.toString();
|
1681
1684
|
|
1685
|
+
stop = $coerce_to(stop, #{String}, 'to_str');
|
1686
|
+
|
1682
1687
|
if (s.length === 1 && stop.length === 1) {
|
1683
1688
|
|
1684
1689
|
a = s.charCodeAt(0);
|
@@ -1778,7 +1783,7 @@ class String < `String`
|
|
1778
1783
|
neg_intersection = '';
|
1779
1784
|
|
1780
1785
|
for (i = 0, len = sets.length; i < len; i++) {
|
1781
|
-
set =
|
1786
|
+
set = $coerce_to(sets[i], #{String}, 'to_str');
|
1782
1787
|
neg = (set.charAt(0) === '^' && set.length > 1);
|
1783
1788
|
set = explode_sequences_in_character_set(neg ? set.slice(1) : set);
|
1784
1789
|
if (neg) {
|
@@ -1820,12 +1825,13 @@ class String < `String`
|
|
1820
1825
|
new(*args)
|
1821
1826
|
end
|
1822
1827
|
|
1823
|
-
def unicode_normalize(form =
|
1824
|
-
|
1828
|
+
def unicode_normalize(form = :nfc)
|
1829
|
+
raise ArgumentError, "Invalid normalization form #{form}" unless %i[nfc nfd nfkc nfkd].include?(form)
|
1830
|
+
`self.normalize(#{form.upcase})`
|
1825
1831
|
end
|
1826
1832
|
|
1827
|
-
def unicode_normalized?(form =
|
1828
|
-
|
1833
|
+
def unicode_normalized?(form = :nfc)
|
1834
|
+
unicode_normalize(form) == self
|
1829
1835
|
end
|
1830
1836
|
|
1831
1837
|
def unpack(format)
|
@@ -1,31 +1,30 @@
|
|
1
1
|
require 'corelib/string'
|
2
2
|
|
3
3
|
class Encoding
|
4
|
-
`Opal.defineProperty(self, '$$register', {})`
|
5
|
-
|
6
4
|
def self.register(name, options = {}, &block)
|
7
|
-
names
|
8
|
-
|
9
|
-
|
5
|
+
names = [name] + (options[:aliases] || [])
|
6
|
+
ascii = options[:ascii] || false
|
7
|
+
dummy = options[:dummy] || false
|
8
|
+
|
9
|
+
encoding = new(name, names, ascii, dummy)
|
10
|
+
encoding.instance_eval(&block)
|
10
11
|
|
11
|
-
register =
|
12
|
+
register = `Opal.encodings`
|
12
13
|
names.each do |encoding_name|
|
13
14
|
const_set encoding_name.sub('-', '_'), encoding
|
14
|
-
register.JS[
|
15
|
+
register.JS[encoding_name] = encoding
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
19
|
def self.find(name)
|
19
20
|
return default_external if name == :default_external
|
20
|
-
register =
|
21
|
-
encoding = register.JS[
|
21
|
+
register = `Opal.encodings`
|
22
|
+
encoding = register.JS[name] || register.JS[name.upcase]
|
22
23
|
raise ArgumentError, "unknown encoding name - #{name}" unless encoding
|
23
24
|
encoding
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
|
-
attr_accessor :default_external
|
28
|
-
end
|
27
|
+
singleton_class.attr_accessor :default_external
|
29
28
|
|
30
29
|
attr_reader :name, :names
|
31
30
|
|
@@ -57,10 +56,6 @@ class Encoding
|
|
57
56
|
raise NotImplementedError
|
58
57
|
end
|
59
58
|
|
60
|
-
def getbyte(*)
|
61
|
-
raise NotImplementedError
|
62
|
-
end
|
63
|
-
|
64
59
|
def bytesize(*)
|
65
60
|
raise NotImplementedError
|
66
61
|
end
|
@@ -72,19 +67,90 @@ end
|
|
72
67
|
Encoding.register 'UTF-8', aliases: ['CP65001'], ascii: true do
|
73
68
|
def each_byte(string, &block)
|
74
69
|
%x{
|
75
|
-
|
76
|
-
|
70
|
+
// Taken from: https://github.com/feross/buffer/blob/f52dffd9df0445b93c0c9065c2f8f0f46b2c729a/index.js#L1954-L2032
|
71
|
+
var units = Infinity
|
72
|
+
var codePoint
|
73
|
+
var length = string.length
|
74
|
+
var leadSurrogate = null
|
75
|
+
|
76
|
+
for (var i = 0; i < length; ++i) {
|
77
|
+
codePoint = string.charCodeAt(i)
|
78
|
+
|
79
|
+
// is surrogate component
|
80
|
+
if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
81
|
+
// last char was a lead
|
82
|
+
if (!leadSurrogate) {
|
83
|
+
// no lead yet
|
84
|
+
if (codePoint > 0xDBFF) {
|
85
|
+
// unexpected trail
|
86
|
+
if ((units -= 3) > -1) {
|
87
|
+
#{yield `0xEF`};
|
88
|
+
#{yield `0xBF`};
|
89
|
+
#{yield `0xBD`};
|
90
|
+
}
|
91
|
+
continue
|
92
|
+
} else if (i + 1 === length) {
|
93
|
+
// unpaired lead
|
94
|
+
if ((units -= 3) > -1) {
|
95
|
+
#{yield `0xEF`};
|
96
|
+
#{yield `0xBF`};
|
97
|
+
#{yield `0xBD`};
|
98
|
+
}
|
99
|
+
continue
|
100
|
+
}
|
101
|
+
|
102
|
+
// valid lead
|
103
|
+
leadSurrogate = codePoint
|
104
|
+
|
105
|
+
continue
|
106
|
+
}
|
77
107
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
108
|
+
// 2 leads in a row
|
109
|
+
if (codePoint < 0xDC00) {
|
110
|
+
if ((units -= 3) > -1) {
|
111
|
+
#{yield `0xEF`};
|
112
|
+
#{yield `0xBF`};
|
113
|
+
#{yield `0xBD`};
|
114
|
+
}
|
115
|
+
leadSurrogate = codePoint
|
116
|
+
continue
|
117
|
+
}
|
83
118
|
|
84
|
-
|
85
|
-
|
119
|
+
// valid surrogate pair
|
120
|
+
codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
|
121
|
+
} else if (leadSurrogate) {
|
122
|
+
// valid bmp char, but last char was a lead
|
123
|
+
if ((units -= 3) > -1) {
|
124
|
+
#{yield `0xEF`};
|
125
|
+
#{yield `0xBF`};
|
126
|
+
#{yield `0xBD`};
|
86
127
|
}
|
87
128
|
}
|
129
|
+
|
130
|
+
leadSurrogate = null
|
131
|
+
|
132
|
+
// encode utf8
|
133
|
+
if (codePoint < 0x80) {
|
134
|
+
if ((units -= 1) < 0) break
|
135
|
+
#{yield `codePoint`};
|
136
|
+
} else if (codePoint < 0x800) {
|
137
|
+
if ((units -= 2) < 0) break
|
138
|
+
#{yield `codePoint >> 0x6 | 0xC0`};
|
139
|
+
#{yield `codePoint & 0x3F | 0x80`};
|
140
|
+
} else if (codePoint < 0x10000) {
|
141
|
+
if ((units -= 3) < 0) break
|
142
|
+
#{yield `codePoint >> 0xC | 0xE0`};
|
143
|
+
#{yield `codePoint >> 0x6 & 0x3F | 0x80`};
|
144
|
+
#{yield `codePoint & 0x3F | 0x80`};
|
145
|
+
} else if (codePoint < 0x110000) {
|
146
|
+
if ((units -= 4) < 0) break
|
147
|
+
#{yield `codePoint >> 0x12 | 0xF0`};
|
148
|
+
#{yield `codePoint >> 0xC & 0x3F | 0x80`};
|
149
|
+
#{yield `codePoint >> 0x6 & 0x3F | 0x80`};
|
150
|
+
#{yield `codePoint & 0x3F | 0x80`};
|
151
|
+
} else {
|
152
|
+
// Invalid code point
|
153
|
+
}
|
88
154
|
}
|
89
155
|
}
|
90
156
|
end
|
@@ -163,20 +229,24 @@ end
|
|
163
229
|
|
164
230
|
class String
|
165
231
|
attr_reader :encoding
|
166
|
-
|
232
|
+
attr_reader :internal_encoding
|
233
|
+
`Opal.defineProperty(String.prototype, 'bytes', nil)`
|
234
|
+
`Opal.defineProperty(String.prototype, 'encoding', #{Encoding::UTF_8})`
|
235
|
+
`Opal.defineProperty(String.prototype, 'internal_encoding', #{Encoding::UTF_8})`
|
167
236
|
|
168
237
|
def bytes
|
169
|
-
each_byte.to_a
|
238
|
+
@bytes ||= each_byte.to_a
|
239
|
+
@bytes.dup
|
170
240
|
end
|
171
241
|
|
172
242
|
def bytesize
|
173
|
-
@
|
243
|
+
@internal_encoding.bytesize(self)
|
174
244
|
end
|
175
245
|
|
176
246
|
def each_byte(&block)
|
177
247
|
return enum_for :each_byte unless block_given?
|
178
248
|
|
179
|
-
@
|
249
|
+
@internal_encoding.each_byte(self, &block)
|
180
250
|
|
181
251
|
self
|
182
252
|
end
|
@@ -198,7 +268,7 @@ class String
|
|
198
268
|
end
|
199
269
|
|
200
270
|
def encode(encoding)
|
201
|
-
|
271
|
+
`Opal.enc(self, encoding)`
|
202
272
|
end
|
203
273
|
|
204
274
|
def force_encoding(encoding)
|
@@ -210,13 +280,18 @@ class String
|
|
210
280
|
|
211
281
|
if (encoding === self.encoding) { return self; }
|
212
282
|
|
213
|
-
self
|
283
|
+
Opal.set_encoding(self, encoding);
|
284
|
+
|
214
285
|
return self;
|
215
286
|
}
|
216
287
|
end
|
217
288
|
|
218
289
|
def getbyte(idx)
|
219
|
-
|
290
|
+
string_bytes = bytes
|
291
|
+
idx = Opal.coerce_to!(idx, Integer, :to_int)
|
292
|
+
return if string_bytes.length < idx
|
293
|
+
|
294
|
+
string_bytes[idx]
|
220
295
|
end
|
221
296
|
|
222
297
|
# stub
|
@@ -224,3 +299,5 @@ class String
|
|
224
299
|
true
|
225
300
|
end
|
226
301
|
end
|
302
|
+
|
303
|
+
Encoding.default_external = __ENCODING__
|