Граф коммитов

498 Коммитов

Автор SHA1 Сообщение Дата
卜部昌平 5aacb30455 refactor assign variables
For readability.  Requested by ko1.

See: 356e203a3a (r35661401)
2019-10-26 15:35:18 +09:00
卜部昌平 356e203a3a more on struct rb_call_data
Replacing adjacent struct rb_call_info and struct rb_call_cache
into a struct rb_call_data.
2019-10-25 12:24:22 +09:00
Yusuke Endoh d0e30fc955 vm_eval.c (rb_adjust_argv_kw_splat): avoid memcpy with zero length
A method call is often with `argc = 1` and `argv = &v` where v is a
VALUE, and some functions shift the arguments by `argc-1` and `argv+1`
(for example, rb_sym_proc_call).  I'm unsure whether it is safe or not
to pass a pointer `argv+1` to memcpy with zero length, but Coverity Scan
complains it.  So this attempts to suppress the warning by explicit
check of the length.
2019-10-09 14:03:04 +09:00
Yusuke Endoh b43afa0a8f Make parser_params have parent_iseq instead of base_block
The parser needs to determine whether a local varaiable is defined or
not in outer scope.  For the sake, "base_block" field has kept the outer
block.

However, the whole block was actually unneeded; the parser used only
base_block->iseq.

So, this change lets parser_params have the iseq directly, instead of
the whole block.
2019-10-04 02:30:36 +09:00
Jeremy Evans a8c436d9a1 Issue keyword flag warning even with no arguments
If the keyword flag is set, there should be at least one argument,
if there isn't, that is a sign the keyword flag was passed when it
should not have been.
2019-09-30 07:06:42 -07:00
Jeremy Evans 649a64ae29 Add three more C-API functions for handling keywords
This adds rb_funcall_passing_block_kw, rb_funcallv_public_kw,
and rb_yield_splat_kw.  This functions are necessary to easily
handle cases where rb_funcall_passing_block, rb_funcallv_public,
and rb_yield_splat are currently used and a keyword argument
separation warning is raised.
2019-09-29 18:31:08 -07:00
Jeremy Evans 6fdd701472 Remove VM_NO_KEYWORDS, replace with RB_NO_KEYWORDS
VM_NO_KEYWORDS was introduced first in vm_core.h, but it is best
to only use a single definition for this.
2019-09-29 16:41:00 -07:00
Jeremy Evans 58dfea0c8d Make direct calls to rb_{obj_instance,mod_module}_{eval,exec} not pass keywords
In general RB_PASS_CALLED_KEYWORDS should only be set if we are
sure the arguments passed come directly from Ruby.  For direct calls
to these C functions, we should not assume that keywords are passed.
Add static *_internal versions of these functions that
Kernel#instance_{eval,exec} and Module#{class,module}_{eval,exec}
call that set RB_PASS_CALLED_KEYWORDS.

Also, change struct.c back to calling rb_mod_module_eval, now that
the call is safe.
2019-09-29 16:28:52 -07:00
Jeremy Evans 660c7e050f Fix more keyword separation issues
This fixes instance_exec and similar methods. It also fixes
Enumerator::Yielder#yield, rb_yield_block, and a couple of cases
with Proc#{<<,>>}.

This support requires the addition of rb_yield_values_kw, similar to
rb_yield_values2, for passing the keyword flag.

Unlike earlier attempts at this, this does not modify the rb_block_call_func
type or add a separate function type.  The functions of type
rb_block_call_func are called by Ruby with a separate VM frame, and we can
get the keyword flag information from the VM frame flags, so it doesn't need
to be passed as a function argument.

These changes require the following VM functions accept a keyword flag:

* vm_yield_with_cref
* vm_yield
* vm_yield_with_block
2019-09-26 19:24:58 -07:00
Jeremy Evans 74e33662fe Make public_send and rb_f_send handle keyword argument separation
Kernel#send takes a different optimized code path that was already
handled.
2019-09-23 09:28:27 -07:00
Jeremy Evans e47b111627 Call rb_vm_call_kw insted of rb_vm_call0 in a few cases
rb_vm_call_kw handles the tmp buffer for you.

Also, change method_missing so it also calls rb_vm_call_kw to
handle the kw_splat flag, instead of requiring callers to handle
kw_splat flag before calling method_missing.  This may fix other
cases where method_missing is currently called without the kw_splat
being handled.
2019-09-20 07:45:29 -07:00
Jeremy Evans c9f2b790ad Handle keyword argument separation for Enumerator#size
When Object#to_enum is passed a block, the block is called to get
a size with the arguments given to to_enum.  This calls the block
with the same keyword flag as to_enum is called with.

This requires adding rb_check_funcall_kw and
rb_check_funcall_default_kw to handle keyword flags.
2019-09-20 07:45:18 -07:00
Jeremy Evans 27b6746872 Make passing empty keywords to dig pass empty keywords to next dig method
If defined in Ruby, dig would be defined as def dig(arg, *rest) end,
it would not use keywords.  If the last dig argument was an empty
hash, it could be treated as keyword arguments by the next dig
method.  Allow dig to pass along the empty keyword flag if called
with an empty keyword, to suppress the previous behavior and force
treating the hash as a positional argument and not keywords.

Also handle the case where dig calls method_missing, passing the
empty keyword flag to that as well.

This requires adding rb_check_funcall_with_hook_kw functions, so
that dig can specify how arguments are treated.  It also adds
kw_splat arguments to a couple static functions.
2019-09-20 07:45:11 -07:00
卜部昌平 66c644da5e refactor reuse existing on-stack structs
rb_vm_call0 allocates its own struct call_info etc.  But they are
already there in case of rb_funcallv_with_cc.  Let's just pass the
existing ones, instead of re-creation.
2019-09-19 12:18:18 +09:00
Jeremy Evans 775365cbd2 Fix keyword argument separation issues with sym procs when using refinements
Make sure that vm_yield_with_cfunc can correctly set the empty keyword
flag by passing 2 as the kw_splat value when calling it in
vm_invoke_ifunc_block.  Make sure calling.kw_splat is set to 1 and not
128 in vm_sendish, so we can safely check for different kw_splat values.

vm_args.c needs to call add_empty_keyword, and to make JIT happy, the
function needs to be exported.  Rename the function to
rb_adjust_argv_kw_splat to more accurately reflect what it does, and
mark it as MJIT exported.
2019-09-17 16:22:44 -07:00
Jeremy Evans 9b35dc3864 Pass keyword argument flag when rb_call_super_kw calls method_missing
This makes method_missing take a flag for whether keyword arguments
were passed.

Adds tests both for rb_call_super_kw usage as well as general usage
of super calling method_missing in Ruby methods.
2019-09-17 16:22:44 -07:00
Jeremy Evans 1edcfd6107 Issue a warning if invalid kw_splat is passed to *_kw function
This should only happen if the API is misused.  It's much better
to warn here and fix the problem, versus to try to debug TypeErrors
or segfaults later.
2019-09-14 13:57:39 -07:00
Takashi Kokubun 8263459627
Keep the reference of imemo while argv may be used
To prevent the `v` reference from being eliminated before argv is used,
calling `rb_free_tmp_buffer` against `v` explicitly.
2019-09-14 21:38:41 +09:00
Nobuyoshi Nakada f41cd4ba43
Fixed one-off error
Needs another room to append an empty hash.
2019-09-14 21:09:39 +09:00
Jeremy Evans 39c37acf86 Fix memory leak when adding empty keyword hashes
nagachika pointed out that ALLOC_N is actually just malloc, so
this memory wasn't being freed.  This shouldn't be a performance
sensitive code path, and will be going away after 2.7, so just
allocate a temp buffer that will be freed later by Ruby GC.
2019-09-14 02:21:37 -07:00
Jeremy Evans b78a345bd6 Only set RB_PASS_CALLED_KEYWORDS in C functions called directly from Ruby
It is not safe to set this in C functions that can be called from
other C functions, as in the non argument-delegation case, you
can end up calling a Ruby method with a flag indicating keywords
are set without passing keywords.

Introduce some new *_kw functions that take a kw_splat flag and
use these functions to set RB_PASS_CALLED_KEYWORDS in places where
we know we are delegating methods (e.g. Class#new, Method#call)
2019-09-14 01:49:33 -07:00
git b2c29bbab6 * remove trailing spaces. [ci skip] 2019-09-14 08:54:08 +09:00
Jeremy Evans 3cfbfa9628 Consolidate empty keyword handling
Remove rb_add_empty_keyword, and instead of calling that every
place you need to add empty keyword hashes, run that code in
a single static function in vm_eval.c.

Add 4 defines to include/ruby/ruby.h, these are to be used as
int kw_splat values when calling the various rb_*_kw functions:

RB_NO_KEYWORDS :: Do not pass keywords
RB_PASS_KEYWORDS :: Pass final argument (which should be hash) as keywords
RB_PASS_EMPTY_KEYWORDS :: Add an empty hash to arguments and pass as keywords
RB_PASS_CALLED_KEYWORDS :: Passes same keyword type as current method was
                           called with (for method delegation)

rb_empty_keyword_given_p needs to stay.  It is required if argument
delegation is done but delayed to a later point, which Enumerator
does.

Use RB_PASS_CALLED_KEYWORDS in rb_call_super to correctly
delegate keyword arguments to super method.
2019-09-13 16:42:27 -07:00
Jeremy Evans 24b1b33975 Correctly handle keywords for Method#call for cfuncs, send, and attr_*
This sets the correct VM frame flags when using Method#call to
call funcs, and handles empty keyword hashes for cfuncs,
attr_reader, and attr_writer. It also fixes calls to send through
Method#call.  It adds tests for all of those, as well as tests for
using Method#call to call define_method, lambda, and sym_procs
(which didn't require code changes).
2019-09-13 09:53:54 -07:00
Jeremy Evans 80e679ef2d Fix keyword argument separation warnings for enumerators
This makes objects created via #to_enum and related methods pass
keyword arguments as keywords.

To implement this, add a kw_splat member of struct enumerator and
struct iter_method_arg, and add rb_block_call_kw, which is the
same as rb_block_call_kw with a flag for whether the last argument
is keyword options.
2019-09-06 19:41:23 -07:00
Jeremy Evans 37a2c660aa Convert keyword argument to required positional hash argument for Class#new, Method#call, UnboundMethod#bind_call
Also add keyword argument separation warnings for Class#new and Method#call.

To allow for keyword argument to required positional hash converstion in
cfuncs, add a vm frame flag indicating the cfunc was called with an empty
keyword hash (which was removed before calling the cfunc).  The cfunc can
check this frame flag and add back an empty hash if it is passing its
arguments to another Ruby method.  Add rb_empty_keyword_given_p function
for checking if called with an empty keyword hash, and
rb_add_empty_keyword for adding back an empty hash to argv.

All of this empty keyword argument support is only for 2.7.  It will be
removed in 3.0 as Ruby 3 will not convert empty keyword arguments to
required positional hash arguments.  Comment all of the relevent code
to make it obvious this is expected to be removed.

Add rb_funcallv_kw as an public C-API function, just like rb_funcallv
but with a keyword flag.  This is used by rb_obj_call_init (internals
of Class#new).  This also required expected call_type enum with
CALL_FCALL_KW, similar to the recent addition of CALL_PUBLIC_KW.

Add rb_vm_call_kw as a internal function, used by call_method_data
(internals of Method#call and UnboundMethod#bind_call). Add tests
for UnboundMethod#bind_call keyword handling.
2019-09-06 19:41:23 -07:00
卜部昌平 d6a94cffda doxygen update [ci skip] 2019-09-06 15:50:58 +09:00
Jeremy Evans 7fc874bf4c Add rb_funcall_with_block_kw
This is needed for C functions to call methods with keyword arguments.
This is a copy of rb_funcall_with_block with an extra argument for
the keyword flag.

There isn't a clean way to implement this that doesn't involve
changing a lot of function signatures, because rb_call doesn't
support a way to mark that the call has keyword arguments.  So hack
this in using a CALL_PUBLIC_KW call_type, which we switch for
CALL_PUBLIC later in the call stack.

We do need to modify rm_vm_call0 to take an argument for whether
keyword arguments are used, since the call_type is no longer
available at that point.  Use the passed in value to set the
appropriate keyword flag in both calling and ci_entry.
2019-09-05 17:47:12 -07:00
Yusuke Endoh ce04392d8d Propagate kw_splat information
The kw_splat flag is whether the original call passes keyword or not.
Some types of methods (e.g., bmethod and sym_proc) drops the
information.  This change tries to propagate the flag to the final
callee, as far as I can.
2019-09-05 17:47:12 -07:00
卜部昌平 dd2b9d4a96 hide rb_funcallv_with_cc from public
Requested by ko1.  Also, because now that this function is internal
use only, why not just directly use struct rb_call_cache to purge
the ZALLOC.
2019-09-05 12:13:07 +09:00
卜部昌平 b005d7c2e2 use existing vm_search_method()
Ko1 plans to implement Guild.  That can interface the caching
mechanism introduced here.  To prevent future breakage we would
better avoid rolling our own code here.  Instead use the existing
vm_search_method() which would be modified by him.

This commit deletes some asserions, but they are in fact checked
inside of vm_search_method().
2019-09-05 11:58:39 +09:00
Urabe, Shyouhei 2a6457b5b7 add rb_funcallv_with_cc()
Why not cache the method entry at each caller site.  The void**
is in fact a method entry, but this struct is hidden from ruby.h
so intentionally left opaque.
2019-09-04 14:08:40 +09:00
卜部昌平 e913fa94d3 delete ruby_eval_string_from_file_protect
Not used from anywhere.
2019-09-03 14:43:08 +09:00
卜部昌平 7bcfd9189a drop-in type check for rb_define_global_function
We can check the function pointer passed to rb_define_global_function
like we do so in rb_define_method.  It turns out that almost anybody
is misunderstanding the API.
2019-08-29 18:34:09 +09:00
卜部昌平 af5e256640 rb_catch now free from ANYARGS
After 5e86b005c0, I now think ANYARGS is
dangerous and should be extinct.  This commit deletes ANYARGS from
rb_catch, and fixes some bugs revealed by that.
2019-08-27 15:52:26 +09:00
卜部昌平 5c7c2d9951 rb_rescue / rb_rescue2 now free from ANYARGS
After 5e86b005c0, I now think ANYARGS is
dangerous and should be extinct.  This commit deletes ANYARGS from
rb_rescue / rb_rescue2, which revealed many arity / type mismatches.
2019-08-27 15:52:26 +09:00
卜部昌平 3cae73133c rb_iterate now takes rb_block_call_func_t
After 5e86b005c0, I now think ANYARGS is
dangerous and should be extinct.  This commit makes rb_iterate free
from ANYARGS.
2019-08-27 15:52:26 +09:00
卜部昌平 0c8592b9af fix function prototype mismatch of rb_block_call
Nobu missed it in f0e73fc986.
2019-08-27 15:52:26 +09:00
OKURA Masafumi 042be439d9 Improve the doc example of `method_missing`
Improvements are:
* Use `symbol` instead of `methId`, described in doc
* Add `*args` following method signature
* Rescue error in `roman_to_int` and calls `super`, recommended in doc
* Call invalid `foo` method to Roman object to raise NoMethodError
2019-08-17 12:19:13 +09:00
Takashi Kokubun a3188f43a8
Revert "Revert "Fix dangling path name from fstring""
This reverts commit 326c00b6f8.

We also confirmed that test_gced_eval_location fails without the changes:
https://travis-ci.org/ruby/ruby/builds/567417818
https://rubyci.org/logs/rubyci.s3.amazonaws.com/arch/ruby-master/log/20190804T000003Z.fail.html.gz
https://rubyci.org/logs/rubyci.s3.amazonaws.com/ubuntu1604/ruby-master/log/20190804T003005Z.fail.html.gz
https://rubyci.org/logs/rubyci.s3.amazonaws.com/icc-x64/ruby-master/log/20190804T000007Z.fail.html.gz
https://rubyci.org/logs/rubyci.s3.amazonaws.com/unstable10x/ruby-master/log/20190804T001806Z.fail.html.gz
https://rubyci.org/logs/rubyci.s3.amazonaws.com/debian9/ruby-master/log/20190804T003005Z.fail.html.gz
2019-08-04 10:08:17 +09:00
Takashi Kokubun 326c00b6f8
Revert "Fix dangling path name from fstring"
This reverts commit 5931857281 temporarily,
leaving `TestEval#test_gced_eval_location` to see the impact for missing
the changes.

That's because too many CIs are failing for `require` behaviors:
http://rubyci.s3.amazonaws.com/freebsd11zfs/ruby-master/log/20190803T063004Z.fail.html.gz
http://rubyci.s3.amazonaws.com/unstable10x/ruby-master/log/20190803T051806Z.fail.html.gz
http://rubyci.s3.amazonaws.com/unstable11x/ruby-master/log/20190803T052406Z.fail.html.gz
http://rubyci.s3.amazonaws.com/unstable10s/ruby-master/log/20190803T111909Z.fail.html.gz
http://rubyci.s3.amazonaws.com/unstable11s/ruby-master/log/20190803T062506Z.fail.html.gz
http://rubyci.s3.amazonaws.com/solaris11s-sunc/ruby-master/log/20190803T052505Z.fail.html.gz
https://app.wercker.com/ruby/ruby/runs/mjit-test1/5d4512c921ca08000857936a?step=5d451305c2809c0008a3da76
https://app.wercker.com/ruby/ruby/runs/mjit-test2/5d4512c921ca080008579371?step=5d4513000421020007ca122d
http://ci.rvm.jp/results/trunk_gcc4@silicon-docker/2177591
http://ci.rvm.jp/results/trunk_gcc6@silicon-docker/2177596
http://ci.rvm.jp/results/trunk_clang_60@silicon-docker/2178802
http://ci.rvm.jp/results/trunk-theap-asserts@silicon-docker/2177555
http://ci.rvm.jp/results/trunk-mjit-wait@silicon-docker/2178747

Mostly `TestRequire#test_race_exception` failures, but in ci.rvm.jp
`require` inside rubyspec hangs very often.
2019-08-04 08:52:41 +09:00
git cee394eb22 * expand tabs. 2019-08-03 13:51:23 +09:00
Nobuyoshi Nakada 5931857281
Fix dangling path name from fstring
* parse.y (yycompile): make sure in advance that the `__FILE__`
  object shares a fstring, to get rid of dangling path name.
  Fixed up 53e9908d8a.  [Bug #16041]

* vm_eval.c (eval_make_iseq): ditto.
2019-08-03 13:48:29 +09:00
Jeremy Evans 01995df645 Document BasicObject does not implement #object_id and #send [ci skip]
Fixes [Bug #10422]
2019-07-22 15:07:22 -07:00
Nobuyoshi Nakada bdddaa9f56
Use rb_ident_hash_new instead of rb_hash_new_compare_by_id
The latter is same as the former, removed the duplicate function.
2019-07-03 02:09:01 +09:00
Nobuyoshi Nakada b1aecef873
Use UNALIGNED_MEMBER_PTR
* internal.h (UNALIGNED_MEMBER_ACCESS, UNALIGNED_MEMBER_PTR):
  moved from eval_intern.h.

* compile.c iseq.c, vm.c: use UNALIGNED_MEMBER_PTR for `entries`
  in `struct iseq_catch_table`.

* vm_eval.c, vm_insnhelper.c: use UNALIGNED_MEMBER_PTR for `body`
  in `rb_method_definition_t`.
2019-05-31 16:04:16 +09:00
Urabe, Shyouhei e7b18ca6d9 glibc says memcpy cannot take NULL
At least since 2004, glibc's <string.h> annotates memcpy as
__attribute__((__nonnull__)).  On the other hand the argv here,
which is passed from rb_funcallv, may be NULL.  Practically this
should never be a serious problem but for maximum safety, let's
avoid passing NULL here.
2019-04-29 21:52:44 +09:00
tenderlove 91793b8967 Add `GC.compact` again.
🙏

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-20 01:19:47 +00:00
tenderlove 744e5df715 Reverting compaction for now
For some reason symbols (or classes) are being overridden in trunk

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-17 09:41:41 +00:00
tenderlove 3c55b643ae Adding `GC.compact` and compacting GC support.
This commit adds the new method `GC.compact` and compacting GC support.
Please see this issue for caveats:

  https://bugs.ruby-lang.org/issues/15626

[Feature #15626]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-17 03:17:25 +00:00