2008-05-24 21:12:12 -04:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
vm_eval.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Sat May 24 16:02:32 JST 2008
|
|
|
|
|
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2014-05-07 00:26:49 -04:00
|
|
|
struct local_var_list {
|
|
|
|
VALUE tbl;
|
|
|
|
};
|
|
|
|
|
2017-08-15 17:39:38 -04:00
|
|
|
static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status);
|
2017-10-27 02:06:31 -04:00
|
|
|
static inline VALUE vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, const rb_cref_t *cref, int is_lambda);
|
|
|
|
static inline VALUE vm_yield(rb_execution_context_t *ec, int argc, const VALUE *argv);
|
|
|
|
static inline VALUE vm_yield_with_block(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE block_handler);
|
|
|
|
static inline VALUE vm_yield_force_blockarg(rb_execution_context_t *ec, VALUE args);
|
2018-03-02 23:07:02 -05:00
|
|
|
VALUE vm_exec(rb_execution_context_t *ec, int mjit_enable_p);
|
2017-10-28 06:23:58 -04:00
|
|
|
static void vm_set_eval_stack(rb_execution_context_t * th, const rb_iseq_t *iseq, const rb_cref_t *cref, const struct rb_block *base_block);
|
2017-10-26 20:46:11 -04:00
|
|
|
static int vm_collect_local_variables_in_heap(const VALUE *dfp, const struct local_var_list *vars);
|
2009-07-15 10:59:41 -04:00
|
|
|
|
2014-11-15 02:28:08 -05:00
|
|
|
static VALUE rb_eUncaughtThrow;
|
2015-10-22 05:58:01 -04:00
|
|
|
static ID id_result, id_tag, id_value;
|
2014-11-16 03:33:35 -05:00
|
|
|
#define id_mesg idMesg
|
2014-11-15 02:28:08 -05:00
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
typedef enum call_type {
|
|
|
|
CALL_PUBLIC,
|
|
|
|
CALL_FCALL,
|
|
|
|
CALL_VCALL,
|
2009-10-30 04:01:48 -04:00
|
|
|
CALL_TYPE_MAX
|
2009-07-15 10:59:41 -04:00
|
|
|
} call_type;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2009-10-30 03:57:21 -04:00
|
|
|
static VALUE send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope);
|
2017-10-26 22:49:30 -04:00
|
|
|
static VALUE vm_call0_body(rb_execution_context_t* ec, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, const VALUE *argv);
|
2012-10-14 13:54:21 -04:00
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
#ifndef MJIT_HEADER
|
|
|
|
|
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
2018-07-24 11:17:08 -04:00
|
|
|
rb_vm_call0(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const rb_callable_method_entry_t *me)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2015-09-19 13:59:58 -04:00
|
|
|
struct rb_calling_info calling_entry, *calling;
|
|
|
|
struct rb_call_info ci_entry;
|
|
|
|
struct rb_call_cache cc_entry;
|
* insns.def (send, invokesuper, invokeblock, opt_*), vm_core.h:
use only a `ci' (rb_call_info_t) parameter instead of using
parameters such as `op_id', 'op_argc', `blockiseq' and flag.
These information are stored in rb_call_info_t at the compile
time.
This technique simplifies parameter passings at related
function calls (~10% speedups for simple mehtod invocation at
my machine).
`rb_call_info_t' also has new function pointer variable `call'.
This `call' variable enables to customize method (block)
invocation process for each place. However, it always call
`vm_call_general()' at this changes.
`rb_call_info_t' also has temporary variables for method
(block) invocation.
* vm_core.h, compile.c, insns.def: introduce VM_CALL_ARGS_SKIP_SETUP
VM_CALL macro. This flag indicates that this call can skip
caller_setup (block arg and splat arg).
* compile.c: catch up above changes.
* iseq.c: catch up above changes (especially for TS_CALLINFO).
* tool/instruction.rb: catch up above chagnes.
* vm_insnhelper.c, vm_insnhelper.h: ditto. Macros and functions
parameters are changed.
* vm_eval.c (vm_call0): ditto (it will be rewriten soon).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37180 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-14 12:59:05 -04:00
|
|
|
|
2015-09-19 13:59:58 -04:00
|
|
|
calling = &calling_entry;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-09-19 13:59:58 -04:00
|
|
|
ci_entry.flag = 0;
|
|
|
|
ci_entry.mid = id;
|
|
|
|
|
|
|
|
cc_entry.me = me;
|
|
|
|
|
|
|
|
calling->recv = recv;
|
|
|
|
calling->argc = argc;
|
|
|
|
|
2017-10-26 22:49:30 -04:00
|
|
|
return vm_call0_body(ec, calling, &ci_entry, &cc_entry, argv);
|
2012-10-14 13:54:21 -04:00
|
|
|
}
|
|
|
|
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
static VALUE
|
2017-10-26 22:49:30 -04:00
|
|
|
vm_call0_cfunc_with_frame(rb_execution_context_t* ec, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, const VALUE *argv)
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
{
|
|
|
|
VALUE val;
|
2015-09-19 13:59:58 -04:00
|
|
|
const rb_callable_method_entry_t *me = cc->me;
|
2012-11-13 04:48:08 -05:00
|
|
|
const rb_method_cfunc_t *cfunc = &me->def->body.cfunc;
|
|
|
|
int len = cfunc->argc;
|
2015-09-19 13:59:58 -04:00
|
|
|
VALUE recv = calling->recv;
|
|
|
|
int argc = calling->argc;
|
2012-11-13 04:48:08 -05:00
|
|
|
ID mid = ci->mid;
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE block_handler = calling->block_handler;
|
2012-11-13 04:48:08 -05:00
|
|
|
|
2017-11-07 03:19:25 -05:00
|
|
|
RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, me->owner, me->def->original_id);
|
2017-10-29 09:19:14 -04:00
|
|
|
EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, recv, me->def->original_id, mid, me->owner, Qnil);
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
{
|
2017-10-26 22:49:30 -04:00
|
|
|
rb_control_frame_t *reg_cfp = ec->cfp;
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
|
2017-10-26 22:49:30 -04:00
|
|
|
vm_push_frame(ec, 0, VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL, recv,
|
2016-07-28 07:02:30 -04:00
|
|
|
block_handler, (VALUE)me,
|
|
|
|
0, reg_cfp->sp, 0, 0);
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
|
2012-11-13 04:48:08 -05:00
|
|
|
if (len >= 0) rb_check_arity(argc, len, len);
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
|
2015-10-29 02:03:17 -04:00
|
|
|
VM_PROFILE_UP(C2C_CALL);
|
2012-11-13 04:48:08 -05:00
|
|
|
val = (*cfunc->invoker)(cfunc->func, recv, argc, argv);
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
|
2017-04-11 00:17:45 -04:00
|
|
|
CHECK_CFP_CONSISTENCY("vm_call0_cfunc_with_frame");
|
2015-10-29 02:03:17 -04:00
|
|
|
VM_PROFILE_UP(C2C_POPF);
|
2017-10-26 22:49:30 -04:00
|
|
|
rb_vm_pop_frame(ec);
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
}
|
2017-10-29 09:19:14 -04:00
|
|
|
EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, recv, me->def->original_id, mid, me->owner, val);
|
2017-11-07 03:19:25 -05:00
|
|
|
RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2017-10-26 22:49:30 -04:00
|
|
|
vm_call0_cfunc(rb_execution_context_t *ec, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, const VALUE *argv)
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
{
|
2017-10-26 22:49:30 -04:00
|
|
|
return vm_call0_cfunc_with_frame(ec, calling, ci, cc, argv);
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
}
|
|
|
|
|
2012-10-15 08:29:42 -04:00
|
|
|
/* `ci' should point temporal value (on stack value) */
|
2012-10-14 13:54:21 -04:00
|
|
|
static VALUE
|
2017-10-26 22:49:30 -04:00
|
|
|
vm_call0_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, const VALUE *argv)
|
2012-10-14 13:54:21 -04:00
|
|
|
{
|
2013-02-18 18:53:41 -05:00
|
|
|
VALUE ret;
|
|
|
|
|
2017-10-28 06:01:54 -04:00
|
|
|
calling->block_handler = vm_passed_block_handler(ec);
|
2009-07-15 10:59:41 -04:00
|
|
|
|
2008-12-15 00:39:39 -05:00
|
|
|
again:
|
2015-09-19 13:59:58 -04:00
|
|
|
switch (cc->me->def->type) {
|
2012-10-23 16:34:25 -04:00
|
|
|
case VM_METHOD_TYPE_ISEQ:
|
|
|
|
{
|
2017-10-26 22:49:30 -04:00
|
|
|
rb_control_frame_t *reg_cfp = ec->cfp;
|
2012-10-23 16:34:25 -04:00
|
|
|
int i;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-09-19 13:59:58 -04:00
|
|
|
CHECK_VM_STACK_OVERFLOW(reg_cfp, calling->argc + 1);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-09-19 13:59:58 -04:00
|
|
|
*reg_cfp->sp++ = calling->recv;
|
|
|
|
for (i = 0; i < calling->argc; i++) {
|
2012-10-23 16:34:25 -04:00
|
|
|
*reg_cfp->sp++ = argv[i];
|
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-10-26 22:49:30 -04:00
|
|
|
vm_call_iseq_setup(ec, reg_cfp, calling, ci, cc);
|
|
|
|
VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
|
2018-03-02 23:07:02 -05:00
|
|
|
return vm_exec(ec, TRUE); /* CHECK_INTS in this function */
|
2012-10-23 16:34:25 -04:00
|
|
|
}
|
2010-08-07 00:33:33 -04:00
|
|
|
case VM_METHOD_TYPE_NOTIMPLEMENTED:
|
* vm_core.h, vm_insnhelper.c, vm_eval.c (OPT_CALL_CFUNC_WITHOUT_FRAME):
add a new otpimization and its macro `OPT_CALL_CFUNC_WITHOUT_FRAME'.
This optimization makes all cfunc method calls `frameless', which
is fster than ordinal cfunc method call.
If `frame' is needed (for example, it calls another method with
`rb_funcall()'), then build a frame. In other words, this
optimization delays frame building.
However, to delay the frame building, we need additional overheads:
(1) Store the last call information.
(2) Check the delayed frame buidling before the frame is needed.
(3) Overhead to build a delayed frame.
rb_thread_t::passed_ci is storage of delayed cfunc call information.
(1) is lightweight because it is only 1 assignment to `passed_ci'.
To achieve (2), we modify GET_THREAD() to check `passed_ci' every
time. It causes 10% overhead on my envrionment.
This optimization only works for cfunc methods which do not need
their `frame'.
After evaluation on my environment, this optimization does not
effective every time. Because of this evaluation results, this
optimization is disabled at default.
* vm_insnhelper.c, vm.c: add VM_PROFILE* macros to measure behaviour
of VM internals. I will extend this feature.
* vm_method.c, method.h: change parameters of the `invoker' function.
Receive `func' pointer as the first parameter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-10-23 00:22:31 -04:00
|
|
|
case VM_METHOD_TYPE_CFUNC:
|
2017-10-26 22:49:30 -04:00
|
|
|
ret = vm_call0_cfunc(ec, calling, ci, cc, argv);
|
2013-02-18 18:53:41 -05:00
|
|
|
goto success;
|
2012-10-23 16:34:25 -04:00
|
|
|
case VM_METHOD_TYPE_ATTRSET:
|
2015-09-19 13:59:58 -04:00
|
|
|
rb_check_arity(calling->argc, 1, 1);
|
|
|
|
ret = rb_ivar_set(calling->recv, cc->me->def->body.attr.id, argv[0]);
|
2013-02-18 18:53:41 -05:00
|
|
|
goto success;
|
2012-10-23 16:34:25 -04:00
|
|
|
case VM_METHOD_TYPE_IVAR:
|
2015-09-19 13:59:58 -04:00
|
|
|
rb_check_arity(calling->argc, 0, 0);
|
|
|
|
ret = rb_attr_get(calling->recv, cc->me->def->body.attr.id);
|
2013-02-18 18:53:41 -05:00
|
|
|
goto success;
|
2012-10-23 16:34:25 -04:00
|
|
|
case VM_METHOD_TYPE_BMETHOD:
|
2017-10-26 22:49:30 -04:00
|
|
|
ret = vm_call_bmethod_body(ec, calling, ci, cc, argv);
|
2013-02-18 18:53:41 -05:00
|
|
|
goto success;
|
2012-10-14 13:54:21 -04:00
|
|
|
case VM_METHOD_TYPE_ZSUPER:
|
* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
entry with VM_METHOD_TYPE_REFINED holds only the original method
definition, so ci->me is set to a method entry allocated in the
stack, and it causes SEGV/ILL. In this commit, a method entry
with VM_METHOD_TYPE_REFINED holds the whole original method entry.
Furthermore, rb_thread_mark() is changed to mark cfp->klass to
avoid GC for iclasses created by copy_refinement_iclass().
* vm_method.c (rb_method_entry_make): add a method entry with
VM_METHOD_TYPE_REFINED to the class refined by the refinement if
the target module is a refinement. When a method entry with
VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
the same name is searched in refinements. If such a method is
found, the method is invoked. Otherwise, the original method in
the refined class (rb_method_definition_t::body.orig_me) is
invoked. This change is made to simplify the normal method lookup
and to improve the performance of normal method calls.
* vm_method.c (EXPR1, search_method, rb_method_entry),
vm_eval.c (rb_call0, rb_search_method_entry): do not use
refinements for method lookup.
* vm_insnhelper.c (vm_call_method): search methods in refinements if
ci->me is VM_METHOD_TYPE_REFINED. If the method is called by
super (i.e., ci->call == vm_call_super_method), skip the same
method entry as the current method to avoid infinite call of the
same method.
* class.c (include_modules_at): add a refined method entry for each
method defined in a module included in a refinement.
* class.c (rb_prepend_module): set an empty table to
RCLASS_M_TBL(klass) to add refined method entries, because
refinements should have priority over prepended modules.
* proc.c (mnew): use rb_method_entry_with_refinements() to get
a refined method.
* vm.c (rb_thread_mark): mark cfp->klass for iclasses created by
copy_refinement_iclass().
* vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass.
* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
the test because it should pass successfully.
* test/ruby/test_refinement.rb (test_redefine_refined_method): new
test for the case a refined method is redefined.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-06 08:08:41 -05:00
|
|
|
case VM_METHOD_TYPE_REFINED:
|
2012-10-14 13:54:21 -04:00
|
|
|
{
|
2015-09-19 13:59:58 -04:00
|
|
|
const rb_method_type_t type = cc->me->def->type;
|
2016-11-10 09:18:52 -05:00
|
|
|
VALUE super_class = cc->me->defined_class;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
|
2016-11-10 19:08:41 -05:00
|
|
|
if (type == VM_METHOD_TYPE_ZSUPER) {
|
2016-11-10 09:18:52 -05:00
|
|
|
super_class = RCLASS_ORIGIN(super_class);
|
* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
entry with VM_METHOD_TYPE_REFINED holds only the original method
definition, so ci->me is set to a method entry allocated in the
stack, and it causes SEGV/ILL. In this commit, a method entry
with VM_METHOD_TYPE_REFINED holds the whole original method entry.
Furthermore, rb_thread_mark() is changed to mark cfp->klass to
avoid GC for iclasses created by copy_refinement_iclass().
* vm_method.c (rb_method_entry_make): add a method entry with
VM_METHOD_TYPE_REFINED to the class refined by the refinement if
the target module is a refinement. When a method entry with
VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
the same name is searched in refinements. If such a method is
found, the method is invoked. Otherwise, the original method in
the refined class (rb_method_definition_t::body.orig_me) is
invoked. This change is made to simplify the normal method lookup
and to improve the performance of normal method calls.
* vm_method.c (EXPR1, search_method, rb_method_entry),
vm_eval.c (rb_call0, rb_search_method_entry): do not use
refinements for method lookup.
* vm_insnhelper.c (vm_call_method): search methods in refinements if
ci->me is VM_METHOD_TYPE_REFINED. If the method is called by
super (i.e., ci->call == vm_call_super_method), skip the same
method entry as the current method to avoid infinite call of the
same method.
* class.c (include_modules_at): add a refined method entry for each
method defined in a module included in a refinement.
* class.c (rb_prepend_module): set an empty table to
RCLASS_M_TBL(klass) to add refined method entries, because
refinements should have priority over prepended modules.
* proc.c (mnew): use rb_method_entry_with_refinements() to get
a refined method.
* vm.c (rb_thread_mark): mark cfp->klass for iclasses created by
copy_refinement_iclass().
* vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass.
* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
the test because it should pass successfully.
* test/ruby/test_refinement.rb (test_redefine_refined_method): new
test for the case a refined method is redefined.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-06 08:08:41 -05:00
|
|
|
}
|
2016-11-10 19:08:41 -05:00
|
|
|
else if (cc->me->def->body.refined.orig_me) {
|
|
|
|
cc->me = refined_method_callable_without_refinement(cc->me);
|
|
|
|
goto again;
|
|
|
|
}
|
* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
entry with VM_METHOD_TYPE_REFINED holds only the original method
definition, so ci->me is set to a method entry allocated in the
stack, and it causes SEGV/ILL. In this commit, a method entry
with VM_METHOD_TYPE_REFINED holds the whole original method entry.
Furthermore, rb_thread_mark() is changed to mark cfp->klass to
avoid GC for iclasses created by copy_refinement_iclass().
* vm_method.c (rb_method_entry_make): add a method entry with
VM_METHOD_TYPE_REFINED to the class refined by the refinement if
the target module is a refinement. When a method entry with
VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
the same name is searched in refinements. If such a method is
found, the method is invoked. Otherwise, the original method in
the refined class (rb_method_definition_t::body.orig_me) is
invoked. This change is made to simplify the normal method lookup
and to improve the performance of normal method calls.
* vm_method.c (EXPR1, search_method, rb_method_entry),
vm_eval.c (rb_call0, rb_search_method_entry): do not use
refinements for method lookup.
* vm_insnhelper.c (vm_call_method): search methods in refinements if
ci->me is VM_METHOD_TYPE_REFINED. If the method is called by
super (i.e., ci->call == vm_call_super_method), skip the same
method entry as the current method to avoid infinite call of the
same method.
* class.c (include_modules_at): add a refined method entry for each
method defined in a module included in a refinement.
* class.c (rb_prepend_module): set an empty table to
RCLASS_M_TBL(klass) to add refined method entries, because
refinements should have priority over prepended modules.
* proc.c (mnew): use rb_method_entry_with_refinements() to get
a refined method.
* vm.c (rb_thread_mark): mark cfp->klass for iclasses created by
copy_refinement_iclass().
* vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass.
* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
the test because it should pass successfully.
* test/ruby/test_refinement.rb (test_redefine_refined_method): new
test for the case a refined method is redefined.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-06 08:08:41 -05:00
|
|
|
|
2016-11-10 09:18:52 -05:00
|
|
|
super_class = RCLASS_SUPER(super_class);
|
2012-10-14 13:54:21 -04:00
|
|
|
|
2015-09-19 13:59:58 -04:00
|
|
|
if (!super_class || !(cc->me = rb_callable_method_entry(super_class, ci->mid))) {
|
2015-06-03 06:42:18 -04:00
|
|
|
enum method_missing_reason ex = (type == VM_METHOD_TYPE_ZSUPER) ? MISSING_SUPER : 0;
|
2015-09-19 13:59:58 -04:00
|
|
|
ret = method_missing(calling->recv, ci->mid, calling->argc, argv, ex);
|
2013-02-18 18:53:41 -05:00
|
|
|
goto success;
|
2012-10-14 13:54:21 -04:00
|
|
|
}
|
2017-11-06 02:44:28 -05:00
|
|
|
RUBY_VM_CHECK_INTS(ec);
|
2012-10-14 13:54:21 -04:00
|
|
|
goto again;
|
2009-07-15 20:38:07 -04:00
|
|
|
}
|
2015-05-30 14:45:28 -04:00
|
|
|
case VM_METHOD_TYPE_ALIAS:
|
2015-09-19 13:59:58 -04:00
|
|
|
cc->me = aliased_callable_method_entry(cc->me);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
goto again;
|
2012-10-23 16:34:25 -04:00
|
|
|
case VM_METHOD_TYPE_MISSING:
|
|
|
|
{
|
2017-10-28 06:01:54 -04:00
|
|
|
vm_passed_block_handler_set(ec, calling->block_handler);
|
2016-03-02 02:18:31 -05:00
|
|
|
return method_missing(calling->recv, ci->mid, calling->argc,
|
|
|
|
argv, MISSING_NOENTRY);
|
2012-10-23 16:34:25 -04:00
|
|
|
}
|
|
|
|
case VM_METHOD_TYPE_OPTIMIZED:
|
2015-09-19 13:59:58 -04:00
|
|
|
switch (cc->me->def->body.optimize_type) {
|
2009-07-15 20:38:07 -04:00
|
|
|
case OPTIMIZED_METHOD_TYPE_SEND:
|
2015-09-19 13:59:58 -04:00
|
|
|
ret = send_internal(calling->argc, argv, calling->recv, CALL_FCALL);
|
2013-02-18 18:53:41 -05:00
|
|
|
goto success;
|
2012-10-23 16:34:25 -04:00
|
|
|
case OPTIMIZED_METHOD_TYPE_CALL:
|
|
|
|
{
|
|
|
|
rb_proc_t *proc;
|
2015-09-19 13:59:58 -04:00
|
|
|
GetProcPtr(calling->recv, proc);
|
2017-10-27 02:06:31 -04:00
|
|
|
ret = rb_vm_invoke_proc(ec, proc, calling->argc, argv, calling->block_handler);
|
2013-02-18 18:53:41 -05:00
|
|
|
goto success;
|
2012-10-23 16:34:25 -04:00
|
|
|
}
|
2009-07-15 20:38:07 -04:00
|
|
|
default:
|
2015-09-19 13:59:58 -04:00
|
|
|
rb_bug("vm_call0: unsupported optimized method type (%d)", cc->me->def->body.optimize_type);
|
2009-07-15 20:38:07 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-10-23 16:34:25 -04:00
|
|
|
case VM_METHOD_TYPE_UNDEF:
|
|
|
|
break;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2015-09-19 13:59:58 -04:00
|
|
|
rb_bug("vm_call0: unsupported method type (%d)", cc->me->def->type);
|
2012-10-23 16:34:25 -04:00
|
|
|
return Qundef;
|
2013-02-18 18:53:41 -05:00
|
|
|
|
|
|
|
success:
|
2017-11-06 02:44:28 -05:00
|
|
|
RUBY_VM_CHECK_INTS(ec);
|
2013-02-18 18:53:41 -05:00
|
|
|
return ret;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2017-10-28 07:52:56 -04:00
|
|
|
rb_vm_call(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc, const VALUE *argv, const rb_callable_method_entry_t *me)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2018-07-24 11:17:08 -04:00
|
|
|
return rb_vm_call0(ec, recv, id, argc, argv, me);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
2017-10-29 10:38:25 -04:00
|
|
|
vm_call_super(rb_execution_context_t *ec, int argc, const VALUE *argv)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-10-29 10:38:25 -04:00
|
|
|
VALUE recv = ec->cfp->self;
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE klass;
|
|
|
|
ID id;
|
2017-10-29 10:38:25 -04:00
|
|
|
rb_control_frame_t *cfp = ec->cfp;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2016-08-02 21:50:50 -04:00
|
|
|
if (VM_FRAME_RUBYFRAME_P(cfp)) {
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_bug("vm_call_super: should not be reached");
|
|
|
|
}
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
klass = RCLASS_ORIGIN(me->defined_class);
|
2015-03-05 20:31:03 -05:00
|
|
|
klass = RCLASS_SUPER(klass);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
id = me->def->original_id;
|
|
|
|
me = rb_callable_method_entry(klass, id);
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
if (!me) {
|
2015-06-02 21:39:16 -04:00
|
|
|
return method_missing(recv, id, argc, argv, MISSING_SUPER);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
else {
|
2018-07-24 11:17:08 -04:00
|
|
|
return rb_vm_call0(ec, recv, id, argc, argv, me);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_call_super(int argc, const VALUE *argv)
|
|
|
|
{
|
2017-10-29 10:38:25 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
PASS_PASSED_BLOCK_HANDLER_EC(ec);
|
|
|
|
return vm_call_super(ec, argc, argv);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2014-11-16 05:38:15 -05:00
|
|
|
VALUE
|
|
|
|
rb_current_receiver(void)
|
|
|
|
{
|
2017-10-29 10:39:51 -04:00
|
|
|
const rb_execution_context_t *ec = GET_EC();
|
2014-11-16 05:38:15 -05:00
|
|
|
rb_control_frame_t *cfp;
|
2017-10-29 10:39:51 -04:00
|
|
|
if (!ec || !(cfp = ec->cfp)) {
|
2014-11-16 05:38:15 -05:00
|
|
|
rb_raise(rb_eRuntimeError, "no self, no life");
|
2017-10-29 10:39:51 -04:00
|
|
|
}
|
2014-11-16 05:38:15 -05:00
|
|
|
return cfp->self;
|
|
|
|
}
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
#endif /* #ifndef MJIT_HEADER */
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
static inline void
|
2017-10-28 08:23:51 -04:00
|
|
|
stack_check(rb_execution_context_t *ec)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-11-07 00:22:09 -05:00
|
|
|
if (!rb_ec_raised_p(ec, RAISED_STACKOVERFLOW) &&
|
2017-10-29 09:49:45 -04:00
|
|
|
rb_ec_stack_check(ec)) {
|
2017-11-07 00:22:09 -05:00
|
|
|
rb_ec_raised_set(ec, RAISED_STACKOVERFLOW);
|
|
|
|
rb_ec_stack_overflow(ec, FALSE);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
#ifndef MJIT_HEADER
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
static inline const rb_callable_method_entry_t *rb_search_method_entry(VALUE recv, ID mid);
|
2017-10-28 08:23:51 -04:00
|
|
|
static inline enum method_missing_reason rb_method_call_status(rb_execution_context_t *ec, const rb_callable_method_entry_t *me, call_type scope, VALUE self);
|
2009-10-30 03:42:04 -04:00
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* \internal
|
|
|
|
* calls the specified method.
|
|
|
|
*
|
|
|
|
* This function is called by functions in rb_call* family.
|
2017-10-29 10:45:16 -04:00
|
|
|
* \param ec current execution context
|
2009-08-29 09:39:44 -04:00
|
|
|
* \param recv receiver of the method
|
|
|
|
* \param mid an ID that represents the name of the method
|
|
|
|
* \param argc the number of method arguments
|
|
|
|
* \param argv a pointer to an array of method arguments
|
2010-05-29 14:51:39 -04:00
|
|
|
* \param scope
|
2012-12-05 11:10:41 -05:00
|
|
|
* \param self self in the caller. Qundef means no self is considered and
|
|
|
|
* protected methods cannot be called
|
2009-08-29 09:39:44 -04:00
|
|
|
*
|
|
|
|
* \note \a self is used in order to controlling access to protected methods.
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
static inline VALUE
|
2017-10-29 10:45:16 -04:00
|
|
|
rb_call0(rb_execution_context_t *ec,
|
|
|
|
VALUE recv, ID mid, int argc, const VALUE *argv,
|
* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
entry with VM_METHOD_TYPE_REFINED holds only the original method
definition, so ci->me is set to a method entry allocated in the
stack, and it causes SEGV/ILL. In this commit, a method entry
with VM_METHOD_TYPE_REFINED holds the whole original method entry.
Furthermore, rb_thread_mark() is changed to mark cfp->klass to
avoid GC for iclasses created by copy_refinement_iclass().
* vm_method.c (rb_method_entry_make): add a method entry with
VM_METHOD_TYPE_REFINED to the class refined by the refinement if
the target module is a refinement. When a method entry with
VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
the same name is searched in refinements. If such a method is
found, the method is invoked. Otherwise, the original method in
the refined class (rb_method_definition_t::body.orig_me) is
invoked. This change is made to simplify the normal method lookup
and to improve the performance of normal method calls.
* vm_method.c (EXPR1, search_method, rb_method_entry),
vm_eval.c (rb_call0, rb_search_method_entry): do not use
refinements for method lookup.
* vm_insnhelper.c (vm_call_method): search methods in refinements if
ci->me is VM_METHOD_TYPE_REFINED. If the method is called by
super (i.e., ci->call == vm_call_super_method), skip the same
method entry as the current method to avoid infinite call of the
same method.
* class.c (include_modules_at): add a refined method entry for each
method defined in a module included in a refinement.
* class.c (rb_prepend_module): set an empty table to
RCLASS_M_TBL(klass) to add refined method entries, because
refinements should have priority over prepended modules.
* proc.c (mnew): use rb_method_entry_with_refinements() to get
a refined method.
* vm.c (rb_thread_mark): mark cfp->klass for iclasses created by
copy_refinement_iclass().
* vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass.
* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
the test because it should pass successfully.
* test/ruby/test_refinement.rb (test_redefine_refined_method): new
test for the case a refined method is redefined.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-06 08:08:41 -05:00
|
|
|
call_type scope, VALUE self)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
const rb_callable_method_entry_t *me = rb_search_method_entry(recv, mid);
|
2017-10-28 08:23:51 -04:00
|
|
|
enum method_missing_reason call_status = rb_method_call_status(ec, me, scope, self);
|
2009-10-30 03:42:04 -04:00
|
|
|
|
2015-06-02 21:39:16 -04:00
|
|
|
if (call_status != MISSING_NONE) {
|
2009-10-30 03:42:04 -04:00
|
|
|
return method_missing(recv, mid, argc, argv, call_status);
|
|
|
|
}
|
2017-10-28 08:23:51 -04:00
|
|
|
stack_check(ec);
|
2018-07-24 11:17:08 -04:00
|
|
|
return rb_vm_call0(ec, recv, mid, argc, argv, me);
|
2009-10-30 03:42:04 -04:00
|
|
|
}
|
|
|
|
|
2009-10-31 11:32:22 -04:00
|
|
|
struct rescue_funcall_args {
|
2015-08-21 22:05:57 -04:00
|
|
|
VALUE defined_class;
|
2009-10-31 11:32:22 -04:00
|
|
|
VALUE recv;
|
2015-02-05 21:33:38 -05:00
|
|
|
ID mid;
|
2017-11-07 00:39:02 -05:00
|
|
|
rb_execution_context_t *ec;
|
2015-08-21 22:05:57 -04:00
|
|
|
const rb_method_entry_t *me;
|
2015-08-22 02:43:14 -04:00
|
|
|
unsigned int respond: 1;
|
|
|
|
unsigned int respond_to_missing: 1;
|
2009-10-31 11:32:22 -04:00
|
|
|
int argc;
|
2013-08-27 03:08:32 -04:00
|
|
|
const VALUE *argv;
|
2009-10-31 11:32:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
check_funcall_exec(struct rescue_funcall_args *args)
|
|
|
|
{
|
2017-11-07 00:39:02 -05:00
|
|
|
return call_method_entry(args->ec, args->defined_class,
|
2015-08-21 22:05:57 -04:00
|
|
|
args->recv, idMethodMissing,
|
|
|
|
args->me, args->argc, args->argv);
|
2009-10-31 11:32:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
check_funcall_failed(struct rescue_funcall_args *args, VALUE e)
|
|
|
|
{
|
2015-08-22 02:43:14 -04:00
|
|
|
int ret = args->respond;
|
|
|
|
if (!ret) {
|
|
|
|
switch (rb_method_boundp(args->defined_class, args->mid,
|
|
|
|
BOUND_PRIVATE|BOUND_RESPONDS)) {
|
|
|
|
case 2:
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
ret = args->respond_to_missing;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret) {
|
2009-10-31 11:32:22 -04:00
|
|
|
rb_exc_raise(e);
|
2009-11-27 19:26:27 -05:00
|
|
|
}
|
2009-10-31 11:32:22 -04:00
|
|
|
return Qundef;
|
|
|
|
}
|
|
|
|
|
2012-12-23 01:05:50 -05:00
|
|
|
static int
|
2017-10-28 08:23:51 -04:00
|
|
|
check_funcall_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mid)
|
2009-10-30 03:42:04 -04:00
|
|
|
{
|
2017-10-28 08:23:51 -04:00
|
|
|
return vm_respond_to(ec, klass, recv, mid, TRUE);
|
2012-12-23 01:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-10-28 08:23:51 -04:00
|
|
|
check_funcall_callable(rb_execution_context_t *ec, const rb_callable_method_entry_t *me)
|
2012-12-23 01:05:50 -05:00
|
|
|
{
|
2017-10-28 08:23:51 -04:00
|
|
|
return rb_method_call_status(ec, me, CALL_FCALL, ec->cfp->self) == MISSING_NONE;
|
2012-12-23 01:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2017-10-28 08:23:51 -04:00
|
|
|
check_funcall_missing(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mid, int argc, const VALUE *argv, int respond, VALUE def)
|
2012-12-23 01:05:50 -05:00
|
|
|
{
|
2015-08-21 22:05:57 -04:00
|
|
|
struct rescue_funcall_args args;
|
2015-08-22 02:43:14 -04:00
|
|
|
const rb_method_entry_t *me;
|
2015-08-21 22:05:57 -04:00
|
|
|
VALUE ret = Qundef;
|
2015-08-22 02:43:14 -04:00
|
|
|
|
2017-10-28 08:23:51 -04:00
|
|
|
ret = basic_obj_respond_to_missing(ec, klass, recv,
|
2017-09-20 18:02:10 -04:00
|
|
|
ID2SYM(mid), Qtrue);
|
2017-06-01 04:02:41 -04:00
|
|
|
if (!RTEST(ret)) return def;
|
2015-08-22 02:43:14 -04:00
|
|
|
args.respond = respond > 0;
|
|
|
|
args.respond_to_missing = (ret != Qundef);
|
2015-11-09 07:48:20 -05:00
|
|
|
ret = def;
|
2015-08-22 02:43:14 -04:00
|
|
|
me = method_entry_get(klass, idMethodMissing, &args.defined_class);
|
2015-08-21 22:05:57 -04:00
|
|
|
if (me && !METHOD_ENTRY_BASIC(me)) {
|
|
|
|
VALUE argbuf, *new_args = ALLOCV_N(VALUE, argbuf, argc+1);
|
|
|
|
|
|
|
|
new_args[0] = ID2SYM(mid);
|
|
|
|
MEMCPY(new_args+1, argv, VALUE, argc);
|
2017-11-07 00:01:51 -05:00
|
|
|
ec->method_missing_reason = MISSING_NOENTRY;
|
2017-11-07 00:39:02 -05:00
|
|
|
args.ec = ec;
|
2012-12-23 01:05:50 -05:00
|
|
|
args.recv = recv;
|
2015-08-21 22:05:57 -04:00
|
|
|
args.me = me;
|
2015-02-05 21:33:38 -05:00
|
|
|
args.mid = mid;
|
2015-08-21 22:05:57 -04:00
|
|
|
args.argc = argc + 1;
|
|
|
|
args.argv = new_args;
|
|
|
|
ret = rb_rescue2(check_funcall_exec, (VALUE)&args,
|
|
|
|
check_funcall_failed, (VALUE)&args,
|
|
|
|
rb_eNoMethodError, (VALUE)0);
|
|
|
|
ALLOCV_END(argbuf);
|
2012-12-23 01:05:50 -05:00
|
|
|
}
|
2015-08-21 22:05:57 -04:00
|
|
|
return ret;
|
2012-12-23 01:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2013-08-27 03:08:32 -04:00
|
|
|
rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
|
2015-11-09 07:48:20 -05:00
|
|
|
{
|
|
|
|
return rb_check_funcall_default(recv, mid, argc, argv, Qundef);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def)
|
2012-12-23 01:05:50 -05:00
|
|
|
{
|
|
|
|
VALUE klass = CLASS_OF(recv);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
const rb_callable_method_entry_t *me;
|
2017-10-28 08:23:51 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
int respond = check_funcall_respond_to(ec, klass, recv, mid);
|
2012-12-23 01:05:50 -05:00
|
|
|
|
2015-08-22 02:43:14 -04:00
|
|
|
if (!respond)
|
2015-11-09 07:48:20 -05:00
|
|
|
return def;
|
2009-10-30 03:42:04 -04:00
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
me = rb_search_method_entry(recv, mid);
|
2017-10-28 08:23:51 -04:00
|
|
|
if (!check_funcall_callable(ec, me)) {
|
|
|
|
VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
|
2017-06-01 04:02:41 -04:00
|
|
|
respond, def);
|
|
|
|
if (ret == Qundef) ret = def;
|
|
|
|
return ret;
|
2009-10-30 03:42:04 -04:00
|
|
|
}
|
2017-10-28 08:23:51 -04:00
|
|
|
stack_check(ec);
|
2018-07-24 11:17:08 -04:00
|
|
|
return rb_vm_call0(ec, recv, mid, argc, argv, me);
|
2009-10-30 03:42:04 -04:00
|
|
|
}
|
|
|
|
|
2009-10-31 11:32:22 -04:00
|
|
|
VALUE
|
2013-08-27 03:08:32 -04:00
|
|
|
rb_check_funcall_with_hook(VALUE recv, ID mid, int argc, const VALUE *argv,
|
2012-12-23 01:05:50 -05:00
|
|
|
rb_check_funcall_hook *hook, VALUE arg)
|
2009-10-31 11:32:22 -04:00
|
|
|
{
|
2012-12-23 01:05:50 -05:00
|
|
|
VALUE klass = CLASS_OF(recv);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
const rb_callable_method_entry_t *me;
|
2017-10-28 08:23:51 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
int respond = check_funcall_respond_to(ec, klass, recv, mid);
|
2012-12-23 01:05:50 -05:00
|
|
|
|
2016-01-30 01:19:13 -05:00
|
|
|
if (!respond) {
|
|
|
|
(*hook)(FALSE, recv, mid, argc, argv, arg);
|
2012-12-23 01:05:50 -05:00
|
|
|
return Qundef;
|
2016-01-30 01:19:13 -05:00
|
|
|
}
|
2012-12-23 01:05:50 -05:00
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
me = rb_search_method_entry(recv, mid);
|
2017-10-28 08:23:51 -04:00
|
|
|
if (!check_funcall_callable(ec, me)) {
|
|
|
|
VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
|
2015-12-12 04:51:30 -05:00
|
|
|
respond, Qundef);
|
|
|
|
(*hook)(ret != Qundef, recv, mid, argc, argv, arg);
|
|
|
|
return ret;
|
2012-12-23 01:05:50 -05:00
|
|
|
}
|
2017-10-28 08:23:51 -04:00
|
|
|
stack_check(ec);
|
2012-12-23 01:05:50 -05:00
|
|
|
(*hook)(TRUE, recv, mid, argc, argv, arg);
|
2018-07-24 11:17:08 -04:00
|
|
|
return rb_vm_call0(ec, recv, mid, argc, argv, me);
|
2009-10-30 03:42:04 -04:00
|
|
|
}
|
|
|
|
|
iseq.c: dump type of branchiftype on disasm
This makes easier to debug scripts related to r59950.
* before
$ ./ruby --dump=insns -e '"#{a}"'
== disasm: #<ISeq:<main>@-e>============================================
0000 putobject "" ( 1)[Li]
0002 putself
0003 opt_send_without_block <callinfo!mid:a, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0006 dup
0007 branchiftype 5, 15
0010 dup
0011 opt_send_without_block <callinfo!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>, <callcache>
0014 tostring
0015 concatstrings 2
0017 leave
* after
$ ./ruby --dump=insns -e '"#{a}"'
== disasm: #<ISeq:<main>@-e>============================================
0000 putobject "" ( 1)[Li]
0002 putself
0003 opt_send_without_block <callinfo!mid:a, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0006 dup
0007 branchiftype T_STRING, 15
0010 dup
0011 opt_send_without_block <callinfo!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>, <callcache>
0014 tostring
0015 concatstrings 2
0017 leave
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61217 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-13 11:07:52 -05:00
|
|
|
const char *
|
2009-11-18 12:52:12 -05:00
|
|
|
rb_type_str(enum ruby_value_type type)
|
|
|
|
{
|
2018-01-18 02:59:03 -05:00
|
|
|
#define type_case(t) t: return #t
|
2009-11-18 12:52:12 -05:00
|
|
|
switch (type) {
|
2018-01-18 02:59:03 -05:00
|
|
|
case type_case(T_NONE);
|
|
|
|
case type_case(T_OBJECT);
|
|
|
|
case type_case(T_CLASS);
|
|
|
|
case type_case(T_MODULE);
|
|
|
|
case type_case(T_FLOAT);
|
|
|
|
case type_case(T_STRING);
|
|
|
|
case type_case(T_REGEXP);
|
|
|
|
case type_case(T_ARRAY);
|
|
|
|
case type_case(T_HASH);
|
|
|
|
case type_case(T_STRUCT);
|
|
|
|
case type_case(T_BIGNUM);
|
|
|
|
case type_case(T_FILE);
|
|
|
|
case type_case(T_DATA);
|
|
|
|
case type_case(T_MATCH);
|
|
|
|
case type_case(T_COMPLEX);
|
|
|
|
case type_case(T_RATIONAL);
|
|
|
|
case type_case(T_NIL);
|
|
|
|
case type_case(T_TRUE);
|
|
|
|
case type_case(T_FALSE);
|
|
|
|
case type_case(T_SYMBOL);
|
|
|
|
case type_case(T_FIXNUM);
|
|
|
|
case type_case(T_IMEMO);
|
|
|
|
case type_case(T_UNDEF);
|
|
|
|
case type_case(T_NODE);
|
|
|
|
case type_case(T_ICLASS);
|
|
|
|
case type_case(T_ZOMBIE);
|
2017-10-24 04:13:13 -04:00
|
|
|
case T_MASK: break;
|
2009-11-18 12:52:12 -05:00
|
|
|
}
|
|
|
|
#undef type_case
|
2017-10-24 04:13:13 -04:00
|
|
|
return NULL;
|
2009-11-18 12:52:12 -05:00
|
|
|
}
|
|
|
|
|
2017-10-24 03:17:36 -04:00
|
|
|
NORETURN(static void uncallable_object(VALUE recv, ID mid));
|
|
|
|
static void
|
|
|
|
uncallable_object(VALUE recv, ID mid)
|
|
|
|
{
|
|
|
|
VALUE flags;
|
|
|
|
int type;
|
|
|
|
const char *typestr;
|
|
|
|
VALUE mname = rb_id2str(mid);
|
|
|
|
|
|
|
|
if (SPECIAL_CONST_P(recv)) {
|
|
|
|
rb_raise(rb_eNotImpError,
|
|
|
|
"method `%"PRIsVALUE"' called on unexpected immediate object (%p)",
|
|
|
|
mname, (void *)recv);
|
|
|
|
}
|
|
|
|
else if ((flags = RBASIC(recv)->flags) == 0) {
|
|
|
|
rb_raise(rb_eNotImpError,
|
|
|
|
"method `%"PRIsVALUE"' called on terminated object (%p)",
|
|
|
|
mname, (void *)recv);
|
|
|
|
}
|
|
|
|
else if (!(typestr = rb_type_str(type = BUILTIN_TYPE(recv)))) {
|
|
|
|
rb_raise(rb_eNotImpError,
|
|
|
|
"method `%"PRIsVALUE"' called on broken T_?""?""?(0x%02x) object"
|
|
|
|
" (%p flags=0x%"PRIxVALUE")",
|
|
|
|
mname, type, (void *)recv, flags);
|
|
|
|
}
|
|
|
|
else if (T_OBJECT <= type && type < T_NIL) {
|
|
|
|
rb_raise(rb_eNotImpError,
|
|
|
|
"method `%"PRIsVALUE"' called on hidden %s object"
|
|
|
|
" (%p flags=0x%"PRIxVALUE")",
|
|
|
|
mname, typestr, (void *)recv, flags);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eNotImpError,
|
|
|
|
"method `%"PRIsVALUE"' called on unexpected %s object"
|
|
|
|
" (%p flags=0x%"PRIxVALUE")",
|
|
|
|
mname, typestr, (void *)recv, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
static inline const rb_callable_method_entry_t *
|
|
|
|
rb_search_method_entry(VALUE recv, ID mid)
|
2009-10-30 03:42:04 -04:00
|
|
|
{
|
|
|
|
VALUE klass = CLASS_OF(recv);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-10-24 03:17:36 -04:00
|
|
|
if (!klass) uncallable_object(recv, mid);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
return rb_callable_method_entry(klass, mid);
|
2009-10-30 03:42:04 -04:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
2015-06-03 06:42:18 -04:00
|
|
|
static inline enum method_missing_reason
|
2017-10-28 08:23:51 -04:00
|
|
|
rb_method_call_status(rb_execution_context_t *ec, const rb_callable_method_entry_t *me, call_type scope, VALUE self)
|
2009-10-30 03:42:04 -04:00
|
|
|
{
|
|
|
|
VALUE klass;
|
|
|
|
ID oid;
|
2015-06-02 21:39:16 -04:00
|
|
|
rb_method_visibility_t visi;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-05-07 23:11:35 -04:00
|
|
|
if (UNDEFINED_METHOD_ENTRY_P(me)) {
|
|
|
|
undefined:
|
2015-06-02 21:39:16 -04:00
|
|
|
return scope == CALL_VCALL ? MISSING_VCALL : MISSING_NOENTRY;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2015-05-07 23:11:35 -04:00
|
|
|
if (me->def->type == VM_METHOD_TYPE_REFINED) {
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
me = rb_resolve_refined_method_callable(Qnil, me);
|
2015-05-07 23:11:35 -04:00
|
|
|
if (UNDEFINED_METHOD_ENTRY_P(me)) goto undefined;
|
|
|
|
}
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
|
|
|
|
klass = me->owner;
|
2009-08-27 22:45:41 -04:00
|
|
|
oid = me->def->original_id;
|
2015-06-06 06:19:48 -04:00
|
|
|
visi = METHOD_ENTRY_VISI(me);
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
if (oid != idMethodMissing) {
|
2008-05-24 21:12:12 -04:00
|
|
|
/* receiver specified form for private method */
|
2015-06-02 21:39:16 -04:00
|
|
|
if (UNLIKELY(visi != METHOD_VISI_PUBLIC)) {
|
|
|
|
if (visi == METHOD_VISI_PRIVATE && scope == CALL_PUBLIC) {
|
|
|
|
return MISSING_PRIVATE;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* self must be kind of a specified form for protected method */
|
2015-06-02 21:39:16 -04:00
|
|
|
if (visi == METHOD_VISI_PROTECTED && scope == CALL_PUBLIC) {
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE defined_class = klass;
|
2009-02-22 09:23:33 -05:00
|
|
|
|
2011-09-29 07:07:45 -04:00
|
|
|
if (RB_TYPE_P(defined_class, T_ICLASS)) {
|
2008-05-24 21:12:12 -04:00
|
|
|
defined_class = RBASIC(defined_class)->klass;
|
|
|
|
}
|
|
|
|
|
2012-12-05 11:10:41 -05:00
|
|
|
if (self == Qundef || !rb_obj_is_kind_of(self, defined_class)) {
|
2015-06-02 21:39:16 -04:00
|
|
|
return MISSING_PROTECTED;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 21:39:16 -04:00
|
|
|
|
|
|
|
return MISSING_NONE;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \internal
|
|
|
|
* calls the specified method.
|
|
|
|
*
|
|
|
|
* This function is called by functions in rb_call* family.
|
|
|
|
* \param recv receiver
|
|
|
|
* \param mid an ID that represents the name of the method
|
|
|
|
* \param argc the number of method arguments
|
|
|
|
* \param argv a pointer to an array of method arguments
|
2010-05-29 14:51:39 -04:00
|
|
|
* \param scope
|
2009-08-29 09:39:44 -04:00
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
static inline VALUE
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-10-29 10:45:16 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
return rb_call0(ec, recv, mid, argc, argv, scope, ec->cfp->self);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-10-29 10:52:05 -04:00
|
|
|
NORETURN(static void raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
|
2015-06-03 06:42:18 -04:00
|
|
|
VALUE obj, enum method_missing_reason call_status));
|
2009-02-21 20:43:59 -05:00
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.method_missing(symbol [, *args] ) -> result
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
|
|
|
|
* <i>symbol</i> is the symbol for the method called, and <i>args</i>
|
|
|
|
* are any arguments that were passed to it. By default, the interpreter
|
|
|
|
* raises an error when this method is called. However, it is possible
|
|
|
|
* to override the method to provide more dynamic behavior.
|
|
|
|
* If it is decided that a particular method should not be handled, then
|
|
|
|
* <i>super</i> should be called, so that ancestors can pick up the
|
|
|
|
* missing method.
|
|
|
|
* The example below creates
|
|
|
|
* a class <code>Roman</code>, which responds to methods with names
|
|
|
|
* consisting of roman numerals, returning the corresponding integer
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
* class Roman
|
2011-03-07 03:44:45 -05:00
|
|
|
* def roman_to_int(str)
|
2008-05-24 21:12:12 -04:00
|
|
|
* # ...
|
|
|
|
* end
|
|
|
|
* def method_missing(methId)
|
|
|
|
* str = methId.id2name
|
2011-03-07 03:44:45 -05:00
|
|
|
* roman_to_int(str)
|
2008-05-24 21:12:12 -04:00
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* r = Roman.new
|
|
|
|
* r.iv #=> 4
|
|
|
|
* r.xxiii #=> 23
|
|
|
|
* r.mm #=> 2000
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_method_missing(int argc, const VALUE *argv, VALUE obj)
|
2009-02-21 20:43:59 -05:00
|
|
|
{
|
2017-11-07 00:01:51 -05:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
raise_method_missing(ec, argc, argv, obj, ec->method_missing_reason);
|
2018-07-24 01:38:07 -04:00
|
|
|
UNREACHABLE_RETURN(Qnil);
|
2009-02-21 20:43:59 -05:00
|
|
|
}
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
2018-02-16 20:30:05 -05:00
|
|
|
rb_make_no_method_exception(VALUE exc, VALUE format, VALUE obj,
|
|
|
|
int argc, const VALUE *argv, int priv)
|
2011-10-06 03:29:33 -04:00
|
|
|
{
|
2018-04-11 23:48:48 -04:00
|
|
|
VALUE name = argv[0];
|
2011-10-06 03:29:33 -04:00
|
|
|
|
|
|
|
if (!format) {
|
2016-01-13 03:05:07 -05:00
|
|
|
format = rb_fstring_cstr("undefined method `%s' for %s%s%s");
|
2011-10-06 03:29:33 -04:00
|
|
|
}
|
|
|
|
if (exc == rb_eNoMethodError) {
|
2018-04-11 23:48:48 -04:00
|
|
|
VALUE args = rb_ary_new4(argc - 1, argv + 1);
|
|
|
|
return rb_nomethod_err_new(format, obj, name, args, priv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_name_err_new(format, obj, name);
|
2011-10-06 03:29:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
#endif /* #ifndef MJIT_HEADER */
|
|
|
|
|
2009-02-21 20:43:59 -05:00
|
|
|
static void
|
2017-10-29 10:52:05 -04:00
|
|
|
raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE obj,
|
2015-06-03 06:42:18 -04:00
|
|
|
enum method_missing_reason last_call_status)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
|
|
|
VALUE exc = rb_eNoMethodError;
|
2016-01-13 03:05:07 -05:00
|
|
|
VALUE format = 0;
|
2009-02-21 20:43:59 -05:00
|
|
|
|
2015-09-02 21:07:52 -04:00
|
|
|
if (UNLIKELY(argc == 0)) {
|
2015-09-03 04:57:47 -04:00
|
|
|
rb_raise(rb_eArgError, "no method name given");
|
2015-09-02 21:07:52 -04:00
|
|
|
}
|
|
|
|
else if (UNLIKELY(!SYMBOL_P(argv[0]))) {
|
|
|
|
const VALUE e = rb_eArgError; /* TODO: TypeError? */
|
|
|
|
rb_raise(e, "method name must be a Symbol but %"PRIsVALUE" is given",
|
|
|
|
rb_obj_class(argv[0]));
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-10-29 10:52:05 -04:00
|
|
|
stack_check(ec);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-06-02 21:39:16 -04:00
|
|
|
if (last_call_status & MISSING_PRIVATE) {
|
2016-01-13 03:05:07 -05:00
|
|
|
format = rb_fstring_cstr("private method `%s' called for %s%s%s");
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2015-06-02 21:39:16 -04:00
|
|
|
else if (last_call_status & MISSING_PROTECTED) {
|
2016-01-13 03:05:07 -05:00
|
|
|
format = rb_fstring_cstr("protected method `%s' called for %s%s%s");
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2015-06-02 21:39:16 -04:00
|
|
|
else if (last_call_status & MISSING_VCALL) {
|
2016-01-13 03:05:07 -05:00
|
|
|
format = rb_fstring_cstr("undefined local variable or method `%s' for %s%s%s");
|
2008-05-24 21:12:12 -04:00
|
|
|
exc = rb_eNameError;
|
|
|
|
}
|
2015-06-02 21:39:16 -04:00
|
|
|
else if (last_call_status & MISSING_SUPER) {
|
2016-01-13 03:05:07 -05:00
|
|
|
format = rb_fstring_cstr("super: no superclass method `%s' for %s%s%s");
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-02-16 20:30:05 -05:00
|
|
|
exc = rb_make_no_method_exception(exc, format, obj, argc, argv,
|
|
|
|
last_call_status & (MISSING_FCALL|MISSING_VCALL));
|
2015-06-02 21:39:16 -04:00
|
|
|
if (!(last_call_status & MISSING_MISSING)) {
|
2014-01-09 05:12:59 -05:00
|
|
|
rb_vm_pop_cfunc_frame();
|
2009-02-21 20:43:59 -05:00
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:38:41 -05:00
|
|
|
static void
|
|
|
|
vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
|
|
|
|
VALUE obj, int call_status)
|
|
|
|
{
|
|
|
|
vm_passed_block_handler_set(ec, VM_BLOCK_HANDLER_NONE);
|
|
|
|
raise_method_missing(ec, argc, argv, obj, call_status | MISSING_MISSING);
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
static inline VALUE
|
2015-06-03 06:42:18 -04:00
|
|
|
method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2016-03-02 02:28:50 -05:00
|
|
|
VALUE *nargv, result, work, klass;
|
2017-10-28 06:01:54 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
VALUE block_handler = vm_passed_block_handler(ec);
|
2016-03-02 02:28:50 -05:00
|
|
|
const rb_callable_method_entry_t *me;
|
2008-12-15 00:15:26 -05:00
|
|
|
|
2017-11-07 00:01:51 -05:00
|
|
|
ec->method_missing_reason = call_status;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2008-12-15 00:15:26 -05:00
|
|
|
if (id == idMethodMissing) {
|
2016-03-02 02:28:50 -05:00
|
|
|
missing:
|
2017-10-29 10:52:05 -04:00
|
|
|
raise_method_missing(ec, argc, argv, obj, call_status | MISSING_MISSING);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2014-01-01 11:40:11 -05:00
|
|
|
nargv = ALLOCV_N(VALUE, work, argc + 1);
|
2008-05-24 21:12:12 -04:00
|
|
|
nargv[0] = ID2SYM(id);
|
|
|
|
MEMCPY(nargv + 1, argv, VALUE, argc);
|
2016-03-02 02:28:50 -05:00
|
|
|
++argc;
|
|
|
|
argv = nargv;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2016-03-02 02:28:50 -05:00
|
|
|
klass = CLASS_OF(obj);
|
|
|
|
if (!klass) goto missing;
|
|
|
|
me = rb_callable_method_entry(klass, idMethodMissing);
|
|
|
|
if (!me || METHOD_ENTRY_BASIC(me)) goto missing;
|
2017-10-28 06:01:54 -04:00
|
|
|
vm_passed_block_handler_set(ec, block_handler);
|
2018-07-24 11:17:08 -04:00
|
|
|
result = rb_vm_call0(ec, obj, idMethodMissing, argc, argv, me);
|
2014-01-01 11:40:11 -05:00
|
|
|
if (work) ALLOCV_END(work);
|
2008-12-15 00:15:26 -05:00
|
|
|
return result;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
#ifndef MJIT_HEADER
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* Calls a method
|
|
|
|
* \param recv receiver of the method
|
|
|
|
* \param mid an ID that represents the name of the method
|
|
|
|
* \param args an Array object which contains method arguments
|
|
|
|
*
|
|
|
|
* \pre \a args must refer an Array object.
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_apply(VALUE recv, ID mid, VALUE args)
|
|
|
|
{
|
|
|
|
int argc;
|
2011-07-29 10:53:47 -04:00
|
|
|
VALUE *argv, ret;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2009-05-20 12:43:41 -04:00
|
|
|
argc = RARRAY_LENINT(args);
|
2011-07-29 10:53:47 -04:00
|
|
|
if (argc >= 0x100) {
|
|
|
|
args = rb_ary_subseq(args, 0, argc);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
RBASIC_CLEAR_CLASS(args);
|
2011-07-29 10:53:47 -04:00
|
|
|
OBJ_FREEZE(args);
|
2014-03-17 00:20:16 -04:00
|
|
|
ret = rb_call(recv, mid, argc, RARRAY_CONST_PTR(args), CALL_FCALL);
|
2011-07-29 10:53:47 -04:00
|
|
|
RB_GC_GUARD(args);
|
|
|
|
return ret;
|
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
argv = ALLOCA_N(VALUE, argc);
|
2013-11-07 21:37:47 -05:00
|
|
|
MEMCPY(argv, RARRAY_CONST_PTR(args), VALUE, argc);
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_call(recv, mid, argc, argv, CALL_FCALL);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-04-14 22:09:27 -04:00
|
|
|
#undef rb_funcall
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* Calls a method
|
|
|
|
* \param recv receiver of the method
|
|
|
|
* \param mid an ID that represents the name of the method
|
|
|
|
* \param n the number of arguments
|
|
|
|
* \param ... arbitrary number of method arguments
|
|
|
|
*
|
|
|
|
* \pre each of arguments after \a n must be a VALUE.
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_funcall(VALUE recv, ID mid, int n, ...)
|
|
|
|
{
|
|
|
|
VALUE *argv;
|
|
|
|
va_list ar;
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
long i;
|
|
|
|
|
2010-11-15 08:48:37 -05:00
|
|
|
va_init_list(ar, n);
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
argv = ALLOCA_N(VALUE, n);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
argv[i] = va_arg(ar, VALUE);
|
|
|
|
}
|
|
|
|
va_end(ar);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
argv = 0;
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_call(recv, mid, n, argv, CALL_FCALL);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* Calls a method
|
|
|
|
* \param recv receiver of the method
|
|
|
|
* \param mid an ID that represents the name of the method
|
|
|
|
* \param argc the number of arguments
|
|
|
|
* \param argv pointer to an array of method arguments
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
2013-05-31 04:27:06 -04:00
|
|
|
rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_call(recv, mid, argc, argv, CALL_FCALL);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* Calls a method.
|
|
|
|
*
|
2016-07-29 07:57:14 -04:00
|
|
|
* Same as rb_funcallv but this function can call only public methods.
|
2009-08-29 09:39:44 -04:00
|
|
|
* \param recv receiver of the method
|
|
|
|
* \param mid an ID that represents the name of the method
|
|
|
|
* \param argc the number of arguments
|
|
|
|
* \param argv pointer to an array of method arguments
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
2013-05-31 04:27:06 -04:00
|
|
|
rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_call(recv, mid, argc, argv, CALL_PUBLIC);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2010-09-24 10:45:19 -04:00
|
|
|
VALUE
|
|
|
|
rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
|
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
PASS_PASSED_BLOCK_HANDLER();
|
2010-09-24 10:45:19 -04:00
|
|
|
return rb_call(recv, mid, argc, argv, CALL_PUBLIC);
|
|
|
|
}
|
|
|
|
|
2013-06-17 08:47:26 -04:00
|
|
|
VALUE
|
2016-07-28 07:02:30 -04:00
|
|
|
rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE passed_procval)
|
2013-06-17 08:47:26 -04:00
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
if (!NIL_P(passed_procval)) {
|
2017-10-28 06:01:54 -04:00
|
|
|
vm_passed_block_handler_set(GET_EC(), passed_procval);
|
2013-06-17 08:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rb_call(recv, mid, argc, argv, CALL_PUBLIC);
|
|
|
|
}
|
|
|
|
|
2015-02-04 23:41:05 -05:00
|
|
|
static VALUE *
|
2017-10-29 11:31:47 -04:00
|
|
|
current_vm_stack_arg(const rb_execution_context_t *ec, const VALUE *argv)
|
2015-02-04 23:41:05 -05:00
|
|
|
{
|
2017-10-29 11:31:47 -04:00
|
|
|
rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
|
|
|
|
if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, prev_cfp)) return NULL;
|
2015-02-04 23:41:05 -05:00
|
|
|
if (prev_cfp->sp + 1 != argv) return NULL;
|
|
|
|
return prev_cfp->sp + 1;
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
static VALUE
|
2009-10-30 03:57:21 -04:00
|
|
|
send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2011-10-06 03:29:33 -04:00
|
|
|
ID id;
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE vid;
|
2012-12-05 11:10:41 -05:00
|
|
|
VALUE self;
|
2015-02-04 23:41:05 -05:00
|
|
|
VALUE ret, vargv = 0;
|
2017-10-29 10:45:16 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2012-12-05 11:10:41 -05:00
|
|
|
if (scope == CALL_PUBLIC) {
|
|
|
|
self = Qundef;
|
|
|
|
}
|
|
|
|
else {
|
2017-10-29 10:45:16 -04:00
|
|
|
self = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp)->self;
|
2012-12-05 11:10:41 -05:00
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
if (argc == 0) {
|
|
|
|
rb_raise(rb_eArgError, "no method name given");
|
|
|
|
}
|
|
|
|
|
2015-02-04 14:10:03 -05:00
|
|
|
vid = *argv;
|
* vm.c, eval_intern.h (PASS_PASSED_BLOCK):
set a VM_FRAME_FLAG_PASSED flag to skip this frame when
searching ruby-level-cfp.
* eval.c, eval_intern.h, proc.c: fix to check cfp. if there is
no valid ruby-level-cfp, cause RuntimeError exception.
[ruby-dev:34128]
* vm_core.h, vm_evalbody.c, vm.c, vm_dump.c, vm_insnhelper.c,
insns.def: rename FRAME_MAGIC_* to VM_FRAME_MAGIC_*.
* KNOWNBUGS.rb, bootstraptest/test*.rb: move solved bugs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17084 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-10 17:46:43 -04:00
|
|
|
|
2011-10-06 03:29:33 -04:00
|
|
|
id = rb_check_id(&vid);
|
|
|
|
if (!id) {
|
|
|
|
if (rb_method_basic_definition_p(CLASS_OF(recv), idMethodMissing)) {
|
2018-02-16 20:30:05 -05:00
|
|
|
VALUE exc = rb_make_no_method_exception(rb_eNoMethodError, 0,
|
|
|
|
recv, argc, argv,
|
|
|
|
scope != CALL_PUBLIC);
|
2011-10-06 03:29:33 -04:00
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
2015-02-04 23:41:05 -05:00
|
|
|
if (!SYMBOL_P(*argv)) {
|
2017-10-29 11:31:47 -04:00
|
|
|
VALUE *tmp_argv = current_vm_stack_arg(ec, argv);
|
2015-02-04 23:41:05 -05:00
|
|
|
vid = rb_str_intern(vid);
|
|
|
|
if (tmp_argv) {
|
|
|
|
tmp_argv[0] = vid;
|
|
|
|
}
|
|
|
|
else if (argc > 1) {
|
|
|
|
tmp_argv = ALLOCV_N(VALUE, vargv, argc);
|
|
|
|
tmp_argv[0] = vid;
|
|
|
|
MEMCPY(tmp_argv+1, argv+1, VALUE, argc-1);
|
|
|
|
argv = tmp_argv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
argv = &vid;
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 14:10:03 -05:00
|
|
|
id = idMethodMissing;
|
2017-11-07 00:01:51 -05:00
|
|
|
ec->method_missing_reason = MISSING_NOENTRY;
|
2015-02-04 23:41:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2015-02-04 14:10:03 -05:00
|
|
|
argv++; argc--;
|
2011-10-06 03:29:33 -04:00
|
|
|
}
|
2017-10-29 10:45:16 -04:00
|
|
|
PASS_PASSED_BLOCK_HANDLER_EC(ec);
|
|
|
|
ret = rb_call0(ec, recv, id, argc, argv, scope, self);
|
2015-02-04 23:41:05 -05:00
|
|
|
ALLOCV_END(vargv);
|
|
|
|
return ret;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-02-23 23:24:52 -05:00
|
|
|
* call-seq:
|
|
|
|
* foo.send(symbol [, args...]) -> obj
|
|
|
|
* foo.__send__(symbol [, args...]) -> obj
|
|
|
|
* foo.send(string [, args...]) -> obj
|
|
|
|
* foo.__send__(string [, args...]) -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Invokes the method identified by _symbol_, passing it any
|
|
|
|
* arguments specified. You can use <code>__send__</code> if the name
|
|
|
|
* +send+ clashes with an existing method in _obj_.
|
2013-02-23 23:24:52 -05:00
|
|
|
* When the method is identified by a string, the string is converted
|
|
|
|
* to a symbol.
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* class Klass
|
|
|
|
* def hello(*args)
|
|
|
|
* "Hello " + args.join(' ')
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* k = Klass.new
|
|
|
|
* k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_f_send(int argc, VALUE *argv, VALUE recv)
|
|
|
|
{
|
2009-10-30 03:57:21 -04:00
|
|
|
return send_internal(argc, argv, recv, CALL_FCALL);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.public_send(symbol [, args...]) -> obj
|
2013-02-23 23:24:52 -05:00
|
|
|
* obj.public_send(string [, args...]) -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Invokes the method identified by _symbol_, passing it any
|
|
|
|
* arguments specified. Unlike send, public_send calls public
|
|
|
|
* methods only.
|
2013-02-23 23:24:52 -05:00
|
|
|
* When the method is identified by a string, the string is converted
|
|
|
|
* to a symbol.
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* 1.public_send(:puts, "hello") # causes NoMethodError
|
|
|
|
*/
|
|
|
|
|
2017-11-16 02:47:33 -05:00
|
|
|
static VALUE
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_f_public_send(int argc, VALUE *argv, VALUE recv)
|
|
|
|
{
|
2009-10-30 03:57:21 -04:00
|
|
|
return send_internal(argc, argv, recv, CALL_PUBLIC);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* yield */
|
|
|
|
|
2017-08-15 17:39:38 -04:00
|
|
|
static inline VALUE
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_yield_0(int argc, const VALUE * argv)
|
|
|
|
{
|
2017-10-27 02:06:31 -04:00
|
|
|
return vm_yield(GET_EC(), argc, argv);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2015-10-10 17:22:54 -04:00
|
|
|
VALUE
|
|
|
|
rb_yield_1(VALUE val)
|
|
|
|
{
|
|
|
|
return rb_yield_0(1, &val);
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_yield(VALUE val)
|
|
|
|
{
|
|
|
|
if (val == Qundef) {
|
|
|
|
return rb_yield_0(0, 0);
|
|
|
|
}
|
|
|
|
else {
|
2015-10-10 17:22:54 -04:00
|
|
|
return rb_yield_1(val);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 04:33:08 -04:00
|
|
|
#undef rb_yield_values
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_yield_values(int n, ...)
|
|
|
|
{
|
|
|
|
if (n == 0) {
|
|
|
|
return rb_yield_0(0, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
VALUE *argv;
|
|
|
|
va_list args;
|
|
|
|
argv = ALLOCA_N(VALUE, n);
|
|
|
|
|
|
|
|
va_init_list(args, n);
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
argv[i] = va_arg(args, VALUE);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return rb_yield_0(n, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_yield_values2(int argc, const VALUE *argv)
|
|
|
|
{
|
|
|
|
return rb_yield_0(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_yield_splat(VALUE values)
|
|
|
|
{
|
|
|
|
VALUE tmp = rb_check_array_type(values);
|
2016-06-17 21:50:46 -04:00
|
|
|
VALUE v;
|
2008-05-24 21:12:12 -04:00
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eArgError, "not an array");
|
|
|
|
}
|
2013-11-07 21:37:47 -05:00
|
|
|
v = rb_yield_0(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp));
|
2014-11-13 19:26:26 -05:00
|
|
|
RB_GC_GUARD(tmp);
|
2008-05-24 21:12:12 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:11:12 -04:00
|
|
|
VALUE
|
2017-06-05 02:15:28 -04:00
|
|
|
rb_yield_force_blockarg(VALUE values)
|
2017-03-18 21:11:12 -04:00
|
|
|
{
|
2017-10-27 02:06:31 -04:00
|
|
|
return vm_yield_force_blockarg(GET_EC(), values);
|
2017-03-18 21:11:12 -04:00
|
|
|
}
|
|
|
|
|
2013-11-29 03:06:19 -05:00
|
|
|
VALUE
|
|
|
|
rb_yield_block(VALUE val, VALUE arg, int argc, const VALUE *argv, VALUE blockarg)
|
|
|
|
{
|
2017-10-27 02:06:31 -04:00
|
|
|
return vm_yield_with_block(GET_EC(), argc, argv,
|
2016-07-28 07:02:30 -04:00
|
|
|
NIL_P(blockarg) ? VM_BLOCK_HANDLER_NONE : blockarg);
|
2013-11-29 03:06:19 -05:00
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
static VALUE
|
|
|
|
loop_i(void)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
rb_yield_0(0, 0);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2015-10-22 05:58:01 -04:00
|
|
|
static VALUE
|
|
|
|
loop_stop(VALUE dummy, VALUE exc)
|
|
|
|
{
|
|
|
|
return rb_attr_get(exc, id_result);
|
|
|
|
}
|
|
|
|
|
2012-11-06 12:12:20 -05:00
|
|
|
static VALUE
|
2013-06-26 09:43:22 -04:00
|
|
|
rb_f_loop_size(VALUE self, VALUE args, VALUE eobj)
|
2012-11-06 17:50:30 -05:00
|
|
|
{
|
2018-01-18 20:45:36 -05:00
|
|
|
return DBL2NUM(HUGE_VAL);
|
2012-11-06 12:12:20 -05:00
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-13 01:49:55 -04:00
|
|
|
* loop { block }
|
|
|
|
* loop -> an_enumerator
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Repeatedly executes the block.
|
|
|
|
*
|
2010-05-13 01:49:55 -04:00
|
|
|
* If no block is given, an enumerator is returned instead.
|
|
|
|
*
|
2008-05-24 21:12:12 -04:00
|
|
|
* loop do
|
|
|
|
* print "Input: "
|
|
|
|
* line = gets
|
|
|
|
* break if !line or line =~ /^qQ/
|
|
|
|
* # ...
|
|
|
|
* end
|
|
|
|
*
|
2015-10-22 05:58:01 -04:00
|
|
|
* StopIteration raised in the block breaks the loop. In this case,
|
|
|
|
* loop returns the "result" value stored in the exception.
|
|
|
|
*
|
|
|
|
* enum = Enumerator.new { |y|
|
|
|
|
* y << "one"
|
|
|
|
* y << "two"
|
|
|
|
* :ok
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* result = loop {
|
|
|
|
* puts enum.next
|
|
|
|
* } #=> :ok
|
2008-05-24 21:12:12 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-12-27 04:28:26 -05:00
|
|
|
rb_f_loop(VALUE self)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2012-11-06 12:12:20 -05:00
|
|
|
RETURN_SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size);
|
2015-10-22 05:58:01 -04:00
|
|
|
return rb_rescue2(loop_i, (VALUE)0, loop_stop, (VALUE)0, rb_eStopIteration, (VALUE)0);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2010-02-17 03:05:42 -05:00
|
|
|
#if VMDEBUG
|
2010-01-24 08:52:32 -05:00
|
|
|
static const char *
|
|
|
|
vm_frametype_name(const rb_control_frame_t *cfp);
|
2010-02-17 03:05:42 -05:00
|
|
|
#endif
|
2010-01-24 08:52:32 -05:00
|
|
|
|
2015-03-28 01:22:12 -04:00
|
|
|
static VALUE
|
|
|
|
rb_iterate0(VALUE (* it_proc) (VALUE), VALUE data1,
|
|
|
|
const struct vm_ifunc *const ifunc,
|
2017-10-29 10:55:19 -04:00
|
|
|
rb_execution_context_t *ec)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-06-23 03:25:52 -04:00
|
|
|
enum ruby_tag_type state;
|
2008-05-24 21:12:12 -04:00
|
|
|
volatile VALUE retval = Qnil;
|
2017-10-29 10:55:19 -04:00
|
|
|
rb_control_frame_t *const cfp = ec->cfp;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-10-29 10:55:19 -04:00
|
|
|
EC_PUSH_TAG(ec);
|
2017-10-26 07:02:13 -04:00
|
|
|
state = EC_EXEC_TAG();
|
2008-05-24 21:12:12 -04:00
|
|
|
if (state == 0) {
|
|
|
|
iter_retry:
|
2009-12-20 09:20:46 -05:00
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE block_handler;
|
|
|
|
|
2015-03-28 01:22:12 -04:00
|
|
|
if (ifunc) {
|
2016-07-28 07:02:30 -04:00
|
|
|
struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
|
|
|
|
captured->code.ifunc = ifunc;
|
|
|
|
block_handler = VM_BH_FROM_IFUNC_BLOCK(captured);
|
2009-12-20 09:20:46 -05:00
|
|
|
}
|
|
|
|
else {
|
2016-07-28 07:02:30 -04:00
|
|
|
block_handler = VM_CF_BLOCK_HANDLER(cfp);
|
2009-12-20 09:20:46 -05:00
|
|
|
}
|
2017-10-29 10:55:19 -04:00
|
|
|
vm_passed_block_handler_set(ec, block_handler);
|
2009-12-20 09:20:46 -05:00
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
retval = (*it_proc) (data1);
|
|
|
|
}
|
2015-03-28 01:22:04 -04:00
|
|
|
else if (state == TAG_BREAK || state == TAG_RETRY) {
|
2017-10-29 10:55:19 -04:00
|
|
|
const struct vm_throw_data *const err = (struct vm_throw_data *)ec->errinfo;
|
2015-03-28 01:22:04 -04:00
|
|
|
const rb_control_frame_t *const escape_cfp = THROW_DATA_CATCH_FRAME(err);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-03-28 01:22:04 -04:00
|
|
|
if (cfp == escape_cfp) {
|
2017-10-29 10:55:19 -04:00
|
|
|
rb_vm_rewind_cfp(ec, cfp);
|
2010-01-24 08:52:32 -05:00
|
|
|
|
2015-03-28 01:22:04 -04:00
|
|
|
state = 0;
|
2017-10-29 10:55:19 -04:00
|
|
|
ec->tag->state = TAG_NONE;
|
|
|
|
ec->errinfo = Qnil;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-03-28 01:22:04 -04:00
|
|
|
if (state == TAG_RETRY) goto iter_retry;
|
|
|
|
retval = THROW_DATA_VAL(err);
|
|
|
|
}
|
|
|
|
else if (0) {
|
2018-01-02 01:41:40 -05:00
|
|
|
SDR(); fprintf(stderr, "%p, %p\n", (void *)cfp, (void *)escape_cfp);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-26 07:02:13 -04:00
|
|
|
EC_POP_TAG();
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2015-03-27 19:51:18 -04:00
|
|
|
if (state) {
|
2017-10-29 10:55:19 -04:00
|
|
|
EC_JUMP_TAG(ec, state);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-03-28 01:22:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_iterate(VALUE (* it_proc)(VALUE), VALUE data1,
|
|
|
|
VALUE (* bl_proc)(ANYARGS), VALUE data2)
|
|
|
|
{
|
|
|
|
return rb_iterate0(it_proc, data1,
|
2017-07-18 04:31:02 -04:00
|
|
|
bl_proc ? rb_vm_ifunc_proc_new(bl_proc, (void *)data2) : 0,
|
2017-10-29 10:55:19 -04:00
|
|
|
GET_EC());
|
2015-03-28 01:22:12 -04:00
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
struct iter_method_arg {
|
|
|
|
VALUE obj;
|
|
|
|
ID mid;
|
|
|
|
int argc;
|
2013-11-29 03:02:51 -05:00
|
|
|
const VALUE *argv;
|
2008-05-24 21:12:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
iterate_method(VALUE obj)
|
|
|
|
{
|
|
|
|
const struct iter_method_arg * arg =
|
|
|
|
(struct iter_method_arg *) obj;
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_call(arg->obj, arg->mid, arg->argc, arg->argv, CALL_FCALL);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2013-11-29 03:02:51 -05:00
|
|
|
rb_block_call(VALUE obj, ID mid, int argc, const VALUE * argv,
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE (*bl_proc) (ANYARGS), VALUE data2)
|
|
|
|
{
|
|
|
|
struct iter_method_arg arg;
|
|
|
|
|
|
|
|
arg.obj = obj;
|
|
|
|
arg.mid = mid;
|
|
|
|
arg.argc = argc;
|
|
|
|
arg.argv = argv;
|
|
|
|
return rb_iterate(iterate_method, (VALUE)&arg, bl_proc, data2);
|
|
|
|
}
|
|
|
|
|
2017-07-18 04:31:02 -04:00
|
|
|
VALUE
|
|
|
|
rb_lambda_call(VALUE obj, ID mid, int argc, const VALUE *argv,
|
|
|
|
rb_block_call_func_t bl_proc, int min_argc, int max_argc,
|
|
|
|
VALUE data2)
|
|
|
|
{
|
|
|
|
struct iter_method_arg arg;
|
|
|
|
struct vm_ifunc *block;
|
|
|
|
|
|
|
|
if (!bl_proc) rb_raise(rb_eArgError, "NULL lambda function");
|
|
|
|
arg.obj = obj;
|
|
|
|
arg.mid = mid;
|
|
|
|
arg.argc = argc;
|
|
|
|
arg.argv = argv;
|
|
|
|
block = rb_vm_ifunc_new(bl_proc, (void *)data2, min_argc, max_argc);
|
2017-10-29 10:55:19 -04:00
|
|
|
return rb_iterate0(iterate_method, (VALUE)&arg, block, GET_EC());
|
2017-07-18 04:31:02 -04:00
|
|
|
}
|
|
|
|
|
2012-09-19 11:42:26 -04:00
|
|
|
static VALUE
|
|
|
|
iterate_check_method(VALUE obj)
|
|
|
|
{
|
|
|
|
const struct iter_method_arg * arg =
|
|
|
|
(struct iter_method_arg *) obj;
|
|
|
|
|
|
|
|
return rb_check_funcall(arg->obj, arg->mid, arg->argc, arg->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2013-11-29 03:02:51 -05:00
|
|
|
rb_check_block_call(VALUE obj, ID mid, int argc, const VALUE *argv,
|
2012-09-19 11:42:26 -04:00
|
|
|
VALUE (*bl_proc) (ANYARGS), VALUE data2)
|
|
|
|
{
|
|
|
|
struct iter_method_arg arg;
|
|
|
|
|
|
|
|
arg.obj = obj;
|
|
|
|
arg.mid = mid;
|
|
|
|
arg.argc = argc;
|
|
|
|
arg.argv = argv;
|
|
|
|
return rb_iterate(iterate_check_method, (VALUE)&arg, bl_proc, data2);
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_each(VALUE obj)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_call(obj, idEach, 0, 0, CALL_FCALL);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-12-26 05:10:41 -05:00
|
|
|
void rb_parser_warn_location(VALUE, int);
|
|
|
|
static const rb_iseq_t *
|
2017-12-26 21:55:15 -05:00
|
|
|
eval_make_iseq(VALUE src, VALUE fname, int line, const rb_binding_t *bind,
|
|
|
|
const struct rb_block *base_block)
|
2017-12-26 05:10:41 -05:00
|
|
|
{
|
|
|
|
const VALUE parser = rb_parser_new();
|
|
|
|
const rb_iseq_t *const parent = vm_block_iseq(base_block);
|
2017-12-26 21:55:15 -05:00
|
|
|
VALUE realpath = Qnil;
|
2017-12-26 05:10:41 -05:00
|
|
|
rb_iseq_t *iseq = 0;
|
|
|
|
rb_ast_t *ast;
|
|
|
|
|
2017-12-26 21:55:15 -05:00
|
|
|
if (!fname) {
|
|
|
|
fname = rb_source_location(&line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fname != Qundef) {
|
|
|
|
realpath = fname;
|
|
|
|
}
|
|
|
|
else if (bind) {
|
|
|
|
fname = pathobj_path(bind->pathobj);
|
|
|
|
realpath = pathobj_realpath(bind->pathobj);
|
|
|
|
line = bind->first_lineno;
|
|
|
|
rb_parser_warn_location(parser, TRUE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fname = rb_usascii_str_new_cstr("(eval)");
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:10:41 -05:00
|
|
|
rb_parser_set_context(parser, base_block, FALSE);
|
|
|
|
ast = rb_parser_compile_string_path(parser, fname, src, line);
|
2018-01-05 03:59:20 -05:00
|
|
|
if (ast->body.root) {
|
2018-01-05 03:59:22 -05:00
|
|
|
iseq = rb_iseq_new_with_opt(&ast->body,
|
2017-12-26 05:10:41 -05:00
|
|
|
parent->body->location.label,
|
|
|
|
fname, realpath, INT2FIX(line),
|
|
|
|
parent, ISEQ_TYPE_EVAL, NULL);
|
|
|
|
}
|
|
|
|
rb_ast_dispose(ast);
|
2017-12-26 21:55:15 -05:00
|
|
|
|
|
|
|
if (0 && iseq) { /* for debug */
|
|
|
|
VALUE disasm = rb_iseq_disasm(iseq);
|
|
|
|
printf("%s\n", StringValuePtr(disasm));
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:10:41 -05:00
|
|
|
return iseq;
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
static VALUE
|
2017-12-26 21:55:16 -05:00
|
|
|
eval_string_with_cref(VALUE self, VALUE src, rb_cref_t *cref, VALUE file, int line)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-10-28 06:23:58 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
2016-07-28 07:02:30 -04:00
|
|
|
struct rb_block block;
|
2017-12-26 21:55:16 -05:00
|
|
|
const rb_iseq_t *iseq;
|
|
|
|
rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
|
|
|
|
if (!cfp) {
|
|
|
|
rb_raise(rb_eRuntimeError, "Can't eval on top of Fiber or Thread");
|
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-12-26 21:55:16 -05:00
|
|
|
block.as.captured = *VM_CFP_TO_CAPTURED_BLOCK(cfp);
|
|
|
|
block.as.captured.self = self;
|
|
|
|
block.as.captured.code.iseq = cfp->iseq;
|
|
|
|
block.type = block_type_iseq;
|
2014-03-18 10:06:34 -04:00
|
|
|
|
2017-12-26 21:55:16 -05:00
|
|
|
iseq = eval_make_iseq(src, file, line, NULL, &block);
|
|
|
|
if (!iseq) {
|
|
|
|
rb_exc_raise(ec->errinfo);
|
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
|
2017-12-26 21:55:16 -05:00
|
|
|
/* TODO: what the code checking? */
|
|
|
|
if (!cref && block.as.captured.code.val) {
|
|
|
|
rb_cref_t *orig_cref = rb_vm_get_cref(vm_block_ep(&block));
|
|
|
|
cref = vm_cref_dup(orig_cref);
|
|
|
|
}
|
|
|
|
vm_set_eval_stack(ec, iseq, cref, &block);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-12-26 21:55:16 -05:00
|
|
|
/* kick */
|
2018-03-02 23:07:02 -05:00
|
|
|
return vm_exec(ec, TRUE);
|
2017-12-26 21:55:16 -05:00
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-12-26 21:55:16 -05:00
|
|
|
static VALUE
|
|
|
|
eval_string_with_scope(VALUE scope, VALUE src, VALUE file, int line)
|
|
|
|
{
|
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
rb_binding_t *bind = Check_TypedStruct(scope, &ruby_binding_data_type);
|
|
|
|
const rb_iseq_t *iseq = eval_make_iseq(src, file, line, bind, &bind->block);
|
|
|
|
if (!iseq) {
|
|
|
|
rb_exc_raise(ec->errinfo);
|
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-12-26 21:55:16 -05:00
|
|
|
vm_set_eval_stack(ec, iseq, NULL, &bind->block);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-12-26 21:55:16 -05:00
|
|
|
/* save new env */
|
|
|
|
if (iseq->body->local_table_size > 0) {
|
|
|
|
vm_bind_update_env(scope, bind, vm_make_env_object(ec, ec->cfp));
|
2016-03-28 17:39:24 -04:00
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-12-26 04:05:28 -05:00
|
|
|
/* kick */
|
2018-03-02 23:07:02 -05:00
|
|
|
return vm_exec(ec, TRUE);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* eval(string [, binding [, filename [,lineno]]]) -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Evaluates the Ruby expression(s) in <em>string</em>. If
|
2009-10-14 10:08:26 -04:00
|
|
|
* <em>binding</em> is given, which must be a <code>Binding</code>
|
|
|
|
* object, the evaluation is performed in its context. If the
|
|
|
|
* optional <em>filename</em> and <em>lineno</em> parameters are
|
|
|
|
* present, they will be used when reporting syntax errors.
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2011-03-07 03:44:45 -05:00
|
|
|
* def get_binding(str)
|
2008-05-24 21:12:12 -04:00
|
|
|
* return binding
|
|
|
|
* end
|
|
|
|
* str = "hello"
|
|
|
|
* eval "str + ' Fred'" #=> "hello Fred"
|
2011-03-07 03:44:45 -05:00
|
|
|
* eval "str + ' Fred'", get_binding("bye") #=> "bye Fred"
|
2008-05-24 21:12:12 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2014-06-18 02:16:39 -04:00
|
|
|
rb_f_eval(int argc, const VALUE *argv, VALUE self)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
|
|
|
VALUE src, scope, vfile, vline;
|
2013-07-29 04:00:34 -04:00
|
|
|
VALUE file = Qundef;
|
2008-05-24 21:12:12 -04:00
|
|
|
int line = 1;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
|
2013-08-18 06:36:51 -04:00
|
|
|
SafeStringValue(src);
|
2008-05-24 21:12:12 -04:00
|
|
|
if (argc >= 3) {
|
|
|
|
StringValue(vfile);
|
|
|
|
}
|
|
|
|
if (argc >= 4) {
|
|
|
|
line = NUM2INT(vline);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(vfile))
|
2013-07-29 04:00:34 -04:00
|
|
|
file = vfile;
|
2017-12-26 22:00:30 -05:00
|
|
|
|
|
|
|
if (NIL_P(scope))
|
|
|
|
return eval_string_with_cref(self, src, NULL, file, line);
|
|
|
|
else
|
|
|
|
return eval_string_with_scope(scope, src, file, line);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2012-07-10 23:25:26 -04:00
|
|
|
/** @note This function name is not stable. */
|
|
|
|
VALUE
|
2012-09-03 22:52:44 -04:00
|
|
|
ruby_eval_string_from_file(const char *str, const char *filename)
|
|
|
|
{
|
2013-07-29 04:00:34 -04:00
|
|
|
VALUE file = filename ? rb_str_new_cstr(filename) : 0;
|
2017-12-26 22:00:30 -05:00
|
|
|
return eval_string_with_cref(rb_vm_top_self(), rb_str_new2(str), NULL, file, 1);
|
2012-07-10 23:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct eval_string_from_file_arg {
|
2013-07-29 04:00:34 -04:00
|
|
|
VALUE str;
|
|
|
|
VALUE filename;
|
2012-07-10 23:25:26 -04:00
|
|
|
};
|
2012-09-03 22:52:44 -04:00
|
|
|
|
2012-07-10 23:25:26 -04:00
|
|
|
static VALUE
|
2013-07-29 01:52:50 -04:00
|
|
|
eval_string_from_file_helper(VALUE data)
|
2012-09-03 22:52:44 -04:00
|
|
|
{
|
2012-07-10 23:25:26 -04:00
|
|
|
const struct eval_string_from_file_arg *const arg = (struct eval_string_from_file_arg*)data;
|
2017-12-26 22:00:30 -05:00
|
|
|
return eval_string_with_cref(rb_vm_top_self(), arg->str, NULL, arg->filename, 1);
|
2012-07-10 23:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2012-09-03 22:52:44 -04:00
|
|
|
ruby_eval_string_from_file_protect(const char *str, const char *filename, int *state)
|
|
|
|
{
|
2012-09-03 22:35:47 -04:00
|
|
|
struct eval_string_from_file_arg arg;
|
2013-07-29 04:00:34 -04:00
|
|
|
arg.str = rb_str_new_cstr(str);
|
|
|
|
arg.filename = filename ? rb_str_new_cstr(filename) : 0;
|
2013-07-29 01:52:50 -04:00
|
|
|
return rb_protect(eval_string_from_file_helper, (VALUE)&arg, state);
|
2012-07-10 23:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the given string in an isolated binding.
|
|
|
|
*
|
|
|
|
* Here "isolated" means the binding does not inherit any other binding. This
|
|
|
|
* behaves same as the binding for required libraries.
|
|
|
|
*
|
|
|
|
* __FILE__ will be "(eval)", and __LINE__ starts from 1 in the evaluation.
|
|
|
|
*
|
|
|
|
* @param str Ruby code to evaluate.
|
|
|
|
* @return The evaluated result.
|
|
|
|
* @throw Exception Raises an exception on error.
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_eval_string(const char *str)
|
|
|
|
{
|
2012-07-10 23:25:26 -04:00
|
|
|
return ruby_eval_string_from_file(str, "eval");
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2018-05-08 06:58:56 -04:00
|
|
|
static VALUE
|
|
|
|
eval_string_protect(VALUE str)
|
|
|
|
{
|
|
|
|
return rb_eval_string((char *)str);
|
|
|
|
}
|
|
|
|
|
2012-07-10 23:25:26 -04:00
|
|
|
/**
|
|
|
|
* Evaluates the given string in an isolated binding.
|
|
|
|
*
|
|
|
|
* __FILE__ will be "(eval)", and __LINE__ starts from 1 in the evaluation.
|
|
|
|
*
|
|
|
|
* @sa rb_eval_string
|
|
|
|
* @param str Ruby code to evaluate.
|
|
|
|
* @param state Being set to zero if succeeded. Nonzero if an error occurred.
|
|
|
|
* @return The evaluated result if succeeded, an undefined value if otherwise.
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
2017-06-23 03:25:52 -04:00
|
|
|
rb_eval_string_protect(const char *str, int *pstate)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2018-05-08 06:58:56 -04:00
|
|
|
return rb_protect(eval_string_protect, (VALUE)str, pstate);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2012-07-10 23:25:26 -04:00
|
|
|
/**
|
|
|
|
* Evaluates the given string under a module binding in an isolated binding.
|
2014-12-08 22:48:35 -05:00
|
|
|
* This is same as the binding for loaded libraries on "load('foo', true)".
|
2012-07-10 23:25:26 -04:00
|
|
|
*
|
|
|
|
* __FILE__ will be "(eval)", and __LINE__ starts from 1 in the evaluation.
|
|
|
|
*
|
|
|
|
* @sa rb_eval_string
|
|
|
|
* @param str Ruby code to evaluate.
|
|
|
|
* @param state Being set to zero if succeeded. Nonzero if an error occurred.
|
|
|
|
* @return The evaluated result if succeeded, an undefined value if otherwise.
|
|
|
|
*/
|
2008-05-24 21:12:12 -04:00
|
|
|
VALUE
|
2017-06-23 03:25:52 -04:00
|
|
|
rb_eval_string_wrap(const char *str, int *pstate)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-06-23 03:25:52 -04:00
|
|
|
int state;
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
VALUE self = th->top_self;
|
|
|
|
VALUE wrapper = th->top_wrapper;
|
|
|
|
VALUE val;
|
|
|
|
|
|
|
|
th->top_wrapper = rb_module_new();
|
|
|
|
th->top_self = rb_obj_clone(rb_vm_top_self());
|
|
|
|
rb_extend_object(th->top_self, th->top_wrapper);
|
|
|
|
|
2017-06-23 03:25:52 -04:00
|
|
|
val = rb_eval_string_protect(str, &state);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
|
|
|
th->top_self = self;
|
|
|
|
th->top_wrapper = wrapper;
|
|
|
|
|
2017-06-23 03:25:52 -04:00
|
|
|
if (pstate) {
|
|
|
|
*pstate = state;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2017-06-23 03:25:52 -04:00
|
|
|
else if (state != TAG_NONE) {
|
2017-10-26 07:02:13 -04:00
|
|
|
EC_JUMP_TAG(th->ec, state);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_eval_cmd(VALUE cmd, VALUE arg, int level)
|
|
|
|
{
|
2017-06-23 03:25:52 -04:00
|
|
|
enum ruby_tag_type state;
|
2013-05-15 04:06:10 -04:00
|
|
|
volatile VALUE val = Qnil; /* OK */
|
2017-12-28 15:09:24 -05:00
|
|
|
const int VAR_NOCLOBBERED(current_safe_level) = rb_safe_level();
|
2017-10-29 10:59:04 -04:00
|
|
|
rb_execution_context_t * volatile ec = GET_EC();
|
2008-05-24 21:12:12 -04:00
|
|
|
|
|
|
|
if (OBJ_TAINTED(cmd)) {
|
2015-07-17 10:51:34 -04:00
|
|
|
level = RUBY_SAFE_LEVEL_MAX;
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-10-29 10:59:04 -04:00
|
|
|
EC_PUSH_TAG(ec);
|
2016-06-17 21:50:49 -04:00
|
|
|
rb_set_safe_level_force(level);
|
2017-10-26 07:02:13 -04:00
|
|
|
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
|
2016-06-17 21:50:49 -04:00
|
|
|
if (!RB_TYPE_P(cmd, T_STRING)) {
|
2016-07-29 07:57:14 -04:00
|
|
|
val = rb_funcallv(cmd, idCall, RARRAY_LENINT(arg),
|
2014-03-17 00:20:16 -04:00
|
|
|
RARRAY_CONST_PTR(arg));
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2016-06-17 21:50:49 -04:00
|
|
|
else {
|
2017-12-26 22:00:30 -05:00
|
|
|
val = eval_string_with_cref(rb_vm_top_self(), cmd, NULL, 0, 0);
|
2016-06-17 21:50:49 -04:00
|
|
|
}
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2017-10-26 07:02:13 -04:00
|
|
|
EC_POP_TAG();
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-12-28 15:09:24 -05:00
|
|
|
rb_set_safe_level_force(current_safe_level);
|
2017-10-29 10:59:04 -04:00
|
|
|
if (state) EC_JUMP_TAG(ec, state);
|
2008-05-24 21:12:12 -04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* block eval under the class/module context */
|
|
|
|
|
|
|
|
static VALUE
|
2016-07-07 11:49:17 -04:00
|
|
|
yield_under(VALUE under, VALUE self, int argc, const VALUE *argv)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-10-27 02:06:31 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
rb_control_frame_t *cfp = ec->cfp;
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE block_handler = VM_CF_BLOCK_HANDLER(cfp);
|
|
|
|
VALUE new_block_handler = 0;
|
|
|
|
const struct rb_captured_block *captured = NULL;
|
|
|
|
struct rb_captured_block new_captured;
|
|
|
|
const VALUE *ep = NULL;
|
2015-03-08 17:22:43 -04:00
|
|
|
rb_cref_t *cref;
|
2017-06-05 03:03:27 -04:00
|
|
|
int is_lambda = FALSE;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
if (block_handler != VM_BLOCK_HANDLER_NONE) {
|
|
|
|
again:
|
|
|
|
switch (vm_block_handler_type(block_handler)) {
|
|
|
|
case block_handler_type_iseq:
|
|
|
|
captured = VM_BH_TO_CAPT_BLOCK(block_handler);
|
|
|
|
new_captured = *captured;
|
|
|
|
new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
|
|
|
|
break;
|
|
|
|
case block_handler_type_ifunc:
|
|
|
|
captured = VM_BH_TO_CAPT_BLOCK(block_handler);
|
|
|
|
new_captured = *captured;
|
|
|
|
new_block_handler = VM_BH_FROM_IFUNC_BLOCK(&new_captured);
|
|
|
|
break;
|
|
|
|
case block_handler_type_proc:
|
2017-06-05 03:03:27 -04:00
|
|
|
is_lambda = rb_proc_lambda_p(block_handler) != Qfalse;
|
2016-07-28 07:02:30 -04:00
|
|
|
block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
|
|
|
|
goto again;
|
|
|
|
case block_handler_type_symbol:
|
2016-12-26 05:00:36 -05:00
|
|
|
return rb_sym_proc_call(SYM2ID(VM_BH_TO_SYMBOL(block_handler)),
|
|
|
|
argc, argv, VM_BLOCK_HANDLER_NONE);
|
2016-07-28 07:02:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
new_captured.self = self;
|
|
|
|
ep = captured->ep;
|
|
|
|
|
2017-10-27 02:06:31 -04:00
|
|
|
VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-10-27 02:06:31 -04:00
|
|
|
cref = vm_cref_push(ec, under, ep, TRUE);
|
|
|
|
return vm_yield_with_cref(ec, argc, argv, cref, is_lambda);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2012-12-07 21:37:16 -05:00
|
|
|
VALUE
|
|
|
|
rb_yield_refine_block(VALUE refinement, VALUE refinements)
|
|
|
|
{
|
2017-10-27 02:06:31 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
|
2012-12-07 21:37:16 -05:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
|
|
|
|
rb_bug("rb_yield_refine_block: an iseq block is required");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
|
|
|
|
struct rb_captured_block new_captured = *captured;
|
|
|
|
VALUE new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
|
|
|
|
const VALUE *ep = captured->ep;
|
2017-10-27 02:06:31 -04:00
|
|
|
rb_cref_t *cref = vm_cref_push(ec, refinement, ep, TRUE);
|
2016-07-28 07:02:30 -04:00
|
|
|
CREF_REFINEMENTS_SET(cref, refinements);
|
2017-10-27 02:06:31 -04:00
|
|
|
VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
|
2016-07-28 07:02:30 -04:00
|
|
|
new_captured.self = refinement;
|
2017-10-27 02:06:31 -04:00
|
|
|
return vm_yield_with_cref(ec, 0, NULL, cref, FALSE);
|
2012-12-07 21:37:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
/* string eval under the class/module context */
|
|
|
|
static VALUE
|
2013-07-29 04:00:34 -04:00
|
|
|
eval_under(VALUE under, VALUE self, VALUE src, VALUE file, int line)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2017-10-26 21:31:15 -04:00
|
|
|
rb_cref_t *cref = vm_cref_push(GET_EC(), under, NULL, SPECIAL_CONST_P(self) && !NIL_P(under));
|
2013-08-18 06:36:51 -04:00
|
|
|
SafeStringValue(src);
|
2017-12-26 21:55:16 -05:00
|
|
|
return eval_string_with_cref(self, src, cref, file, line);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2014-06-18 02:16:39 -04:00
|
|
|
specific_eval(int argc, const VALUE *argv, VALUE klass, VALUE self)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
|
|
|
if (rb_block_given_p()) {
|
2012-03-14 17:10:34 -04:00
|
|
|
rb_check_arity(argc, 0, 0);
|
2016-07-07 11:49:17 -04:00
|
|
|
return yield_under(klass, self, 1, &self);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
else {
|
2013-07-29 04:00:34 -04:00
|
|
|
VALUE file = Qundef;
|
2008-05-24 21:12:12 -04:00
|
|
|
int line = 1;
|
2014-06-18 02:16:39 -04:00
|
|
|
VALUE code;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2012-03-14 17:10:34 -04:00
|
|
|
rb_check_arity(argc, 1, 3);
|
2014-06-18 02:16:39 -04:00
|
|
|
code = argv[0];
|
|
|
|
SafeStringValue(code);
|
2012-03-14 17:10:34 -04:00
|
|
|
if (argc > 2)
|
|
|
|
line = NUM2INT(argv[2]);
|
|
|
|
if (argc > 1) {
|
2013-07-29 04:00:34 -04:00
|
|
|
file = argv[1];
|
|
|
|
if (!NIL_P(file)) StringValue(file);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2014-06-18 02:16:39 -04:00
|
|
|
return eval_under(klass, self, code, file, line);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 22:35:58 -04:00
|
|
|
static VALUE
|
|
|
|
singleton_class_for_eval(VALUE self)
|
|
|
|
{
|
|
|
|
if (SPECIAL_CONST_P(self)) {
|
|
|
|
return rb_special_singleton_class(self);
|
|
|
|
}
|
|
|
|
switch (BUILTIN_TYPE(self)) {
|
|
|
|
case T_FLOAT: case T_BIGNUM: case T_SYMBOL:
|
|
|
|
return Qnil;
|
2016-11-14 00:17:55 -05:00
|
|
|
case T_STRING:
|
|
|
|
if (FL_TEST_RAW(self, RSTRING_FSTR)) return Qnil;
|
2015-04-22 22:35:58 -04:00
|
|
|
default:
|
|
|
|
return rb_singleton_class(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.instance_eval(string [, filename [, lineno]] ) -> obj
|
2014-05-24 17:52:31 -04:00
|
|
|
* obj.instance_eval {|obj| block } -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Evaluates a string containing Ruby source code, or the given block,
|
|
|
|
* within the context of the receiver (_obj_). In order to set the
|
|
|
|
* context, the variable +self+ is set to _obj_ while
|
|
|
|
* the code is executing, giving the code access to _obj_'s
|
2014-05-24 17:52:31 -04:00
|
|
|
* instance variables and private methods.
|
|
|
|
*
|
|
|
|
* When <code>instance_eval</code> is given a block, _obj_ is also
|
|
|
|
* passed in as the block's only argument.
|
|
|
|
*
|
|
|
|
* When <code>instance_eval</code> is given a +String+, the optional
|
|
|
|
* second and third parameters supply a filename and starting line number
|
|
|
|
* that are used when reporting compilation errors.
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* class KlassWithSecret
|
|
|
|
* def initialize
|
|
|
|
* @secret = 99
|
|
|
|
* end
|
2014-05-24 17:52:31 -04:00
|
|
|
* private
|
|
|
|
* def the_secret
|
|
|
|
* "Ssssh! The secret is #{@secret}."
|
|
|
|
* end
|
2008-05-24 21:12:12 -04:00
|
|
|
* end
|
|
|
|
* k = KlassWithSecret.new
|
2014-05-24 17:52:31 -04:00
|
|
|
* k.instance_eval { @secret } #=> 99
|
|
|
|
* k.instance_eval { the_secret } #=> "Ssssh! The secret is 99."
|
|
|
|
* k.instance_eval {|obj| obj == self } #=> true
|
2008-05-24 21:12:12 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2014-06-18 02:16:39 -04:00
|
|
|
rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2015-04-22 22:35:58 -04:00
|
|
|
VALUE klass = singleton_class_for_eval(self);
|
2008-05-24 21:12:12 -04:00
|
|
|
return specific_eval(argc, argv, klass, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.instance_exec(arg...) {|var...| block } -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Executes the given block within the context of the receiver
|
|
|
|
* (_obj_). In order to set the context, the variable +self+ is set
|
|
|
|
* to _obj_ while the code is executing, giving the code access to
|
|
|
|
* _obj_'s instance variables. Arguments are passed as block parameters.
|
|
|
|
*
|
|
|
|
* class KlassWithSecret
|
|
|
|
* def initialize
|
|
|
|
* @secret = 99
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* k = KlassWithSecret.new
|
|
|
|
* k.instance_exec(5) {|x| @secret+x } #=> 104
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2014-06-18 02:16:39 -04:00
|
|
|
rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2015-04-22 22:35:58 -04:00
|
|
|
VALUE klass = singleton_class_for_eval(self);
|
2016-07-07 11:49:17 -04:00
|
|
|
return yield_under(klass, self, argc, argv);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.class_eval(string [, filename [, lineno]]) -> obj
|
2016-02-20 00:44:52 -05:00
|
|
|
* mod.class_eval {|mod| block } -> obj
|
|
|
|
* mod.module_eval(string [, filename [, lineno]]) -> obj
|
|
|
|
* mod.module_eval {|mod| block } -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2012-03-16 01:28:55 -04:00
|
|
|
* Evaluates the string or block in the context of _mod_, except that when
|
2012-03-16 01:38:52 -04:00
|
|
|
* a block is given, constant/class variable lookup is not affected. This
|
|
|
|
* can be used to add methods to a class. <code>module_eval</code> returns
|
|
|
|
* the result of evaluating its argument. The optional _filename_ and
|
|
|
|
* _lineno_ parameters set the text for error messages.
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* class Thing
|
|
|
|
* end
|
|
|
|
* a = %q{def hello() "Hello there!" end}
|
|
|
|
* Thing.module_eval(a)
|
|
|
|
* puts Thing.new.hello()
|
|
|
|
* Thing.module_eval("invalid code", "dummy", 123)
|
|
|
|
*
|
|
|
|
* <em>produces:</em>
|
|
|
|
*
|
|
|
|
* Hello there!
|
|
|
|
* dummy:123:in `module_eval': undefined local variable
|
|
|
|
* or method `code' for Thing:Class
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2014-06-18 02:16:39 -04:00
|
|
|
rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
|
|
|
return specific_eval(argc, argv, mod, mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.module_exec(arg...) {|var...| block } -> obj
|
|
|
|
* mod.class_exec(arg...) {|var...| block } -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Evaluates the given block in the context of the class/module.
|
|
|
|
* The method defined in the block will belong to the receiver.
|
2013-06-01 10:17:14 -04:00
|
|
|
* Any arguments passed to the method will be passed to the block.
|
|
|
|
* This can be used if the block needs to access instance variables.
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* class Thing
|
|
|
|
* end
|
|
|
|
* Thing.class_exec{
|
|
|
|
* def hello() "Hello there!" end
|
|
|
|
* }
|
|
|
|
* puts Thing.new.hello()
|
|
|
|
*
|
|
|
|
* <em>produces:</em>
|
|
|
|
*
|
|
|
|
* Hello there!
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2014-06-18 02:16:39 -04:00
|
|
|
rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
|
2008-05-24 21:12:12 -04:00
|
|
|
{
|
2016-07-07 11:49:17 -04:00
|
|
|
return yield_under(mod, mod, argc, argv);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2014-11-15 02:28:08 -05:00
|
|
|
/*
|
|
|
|
* Document-class: UncaughtThrowError
|
|
|
|
*
|
|
|
|
* Raised when +throw+ is called with a _tag_ which does not have
|
|
|
|
* corresponding +catch+ block.
|
|
|
|
*
|
|
|
|
* throw "foo", "bar"
|
|
|
|
*
|
|
|
|
* <em>raises the exception:</em>
|
|
|
|
*
|
|
|
|
* UncaughtThrowError: uncaught throw "foo"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
uncaught_throw_init(int argc, const VALUE *argv, VALUE exc)
|
|
|
|
{
|
|
|
|
rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
|
|
|
|
rb_call_super(argc - 2, argv + 2);
|
2014-11-16 03:33:49 -05:00
|
|
|
rb_ivar_set(exc, id_tag, argv[0]);
|
|
|
|
rb_ivar_set(exc, id_value, argv[1]);
|
2014-11-15 02:28:08 -05:00
|
|
|
return exc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* uncaught_throw.tag -> obj
|
|
|
|
*
|
|
|
|
* Return the tag object which was called for.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
uncaught_throw_tag(VALUE exc)
|
|
|
|
{
|
2014-11-16 03:33:49 -05:00
|
|
|
return rb_ivar_get(exc, id_tag);
|
2014-11-15 02:28:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* uncaught_throw.value -> obj
|
|
|
|
*
|
|
|
|
* Return the return value which was called for.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
uncaught_throw_value(VALUE exc)
|
|
|
|
{
|
2014-11-16 03:33:49 -05:00
|
|
|
return rb_ivar_get(exc, id_value);
|
2014-11-15 02:28:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* uncaught_throw.to_s -> string
|
|
|
|
*
|
|
|
|
* Returns formatted message with the inspected tag.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
uncaught_throw_to_s(VALUE exc)
|
|
|
|
{
|
2014-11-16 03:33:35 -05:00
|
|
|
VALUE mesg = rb_attr_get(exc, id_mesg);
|
2014-11-15 02:28:08 -05:00
|
|
|
VALUE tag = uncaught_throw_tag(exc);
|
|
|
|
return rb_str_format(1, &tag, mesg);
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-12 18:06:59 -04:00
|
|
|
* throw(tag [, obj])
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
|
|
|
* Transfers control to the end of the active +catch+ block
|
2014-11-15 02:28:08 -05:00
|
|
|
* waiting for _tag_. Raises +UncaughtThrowError+ if there
|
2009-06-12 18:06:59 -04:00
|
|
|
* is no +catch+ block for the _tag_. The optional second
|
2008-05-24 21:12:12 -04:00
|
|
|
* parameter supplies a return value for the +catch+ block,
|
|
|
|
* which otherwise defaults to +nil+. For examples, see
|
|
|
|
* <code>Kernel::catch</code>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_throw(int argc, VALUE *argv)
|
|
|
|
{
|
|
|
|
VALUE tag, value;
|
2009-03-13 05:10:07 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &tag, &value);
|
|
|
|
rb_throw_obj(tag, value);
|
2018-07-24 01:38:07 -04:00
|
|
|
UNREACHABLE_RETURN(Qnil);
|
2009-03-13 05:10:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_throw_obj(VALUE tag, VALUE value)
|
|
|
|
{
|
2017-10-29 11:00:23 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
struct rb_vm_tag *tt = ec->tag;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
|
|
|
while (tt) {
|
|
|
|
if (tt->tag == tag) {
|
|
|
|
tt->retval = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tt = tt->prev;
|
|
|
|
}
|
|
|
|
if (!tt) {
|
2014-11-15 02:28:08 -05:00
|
|
|
VALUE desc[3];
|
|
|
|
desc[0] = tag;
|
|
|
|
desc[1] = value;
|
|
|
|
desc[2] = rb_str_new_cstr("uncaught throw %p");
|
|
|
|
rb_exc_raise(rb_class_new_instance(numberof(desc), desc, rb_eUncaughtThrow));
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-10-29 11:00:23 -04:00
|
|
|
ec->errinfo = (VALUE)THROW_DATA_NEW(tag, NULL, TAG_THROW);
|
|
|
|
EC_JUMP_TAG(ec, TAG_THROW);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_throw(const char *tag, VALUE val)
|
|
|
|
{
|
2015-11-29 23:44:41 -05:00
|
|
|
rb_throw_obj(rb_sym_intern_ascii_cstr(tag), val);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
|
|
|
|
2009-09-15 17:18:04 -04:00
|
|
|
static VALUE
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-21 16:58:26 -04:00
|
|
|
catch_i(VALUE tag, VALUE data)
|
|
|
|
{
|
2009-09-15 17:18:04 -04:00
|
|
|
return rb_yield_0(1, &tag);
|
|
|
|
}
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-21 16:58:26 -04:00
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2014-07-27 17:35:47 -04:00
|
|
|
* catch([tag]) {|tag| block } -> obj
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2014-07-27 17:38:23 -04:00
|
|
|
* +catch+ executes its block. If +throw+ is not called, the block executes
|
|
|
|
* normally, and +catch+ returns the value of the last expression evaluated.
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2014-07-27 17:35:47 -04:00
|
|
|
* catch(1) { 123 } # => 123
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2016-03-11 19:50:29 -05:00
|
|
|
* If <code>throw(tag2, val)</code> is called, Ruby searches up its stack for
|
|
|
|
* a +catch+ block whose +tag+ has the same +object_id+ as _tag2_. When found,
|
|
|
|
* the block stops executing and returns _val_ (or +nil+ if no second argument
|
|
|
|
* was given to +throw+).
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2014-07-27 17:35:47 -04:00
|
|
|
* catch(1) { throw(1, 456) } # => 456
|
|
|
|
* catch(1) { throw(1) } # => nil
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2014-07-27 17:38:23 -04:00
|
|
|
* When +tag+ is passed as the first argument, +catch+ yields it as the
|
|
|
|
* parameter of the block.
|
2014-07-27 17:35:47 -04:00
|
|
|
*
|
|
|
|
* catch(1) {|x| x + 2 } # => 3
|
2008-05-24 21:12:12 -04:00
|
|
|
*
|
2014-07-27 17:38:23 -04:00
|
|
|
* When no +tag+ is given, +catch+ yields a new unique object (as from
|
|
|
|
* +Object.new+) as the block parameter. This object can then be used as the
|
|
|
|
* argument to +throw+, and will match the correct +catch+ block.
|
2009-06-12 18:06:59 -04:00
|
|
|
*
|
2014-07-27 17:35:47 -04:00
|
|
|
* catch do |obj_A|
|
|
|
|
* catch do |obj_B|
|
|
|
|
* throw(obj_B, 123)
|
|
|
|
* puts "This puts is not reached"
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* puts "This puts is displayed"
|
|
|
|
* 456
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* # => 456
|
|
|
|
*
|
|
|
|
* catch do |obj_A|
|
|
|
|
* catch do |obj_B|
|
|
|
|
* throw(obj_A, 123)
|
|
|
|
* puts "This puts is still not reached"
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* puts "Now this puts is also not reached"
|
|
|
|
* 456
|
|
|
|
* end
|
2009-06-12 18:06:59 -04:00
|
|
|
*
|
2014-07-27 17:35:47 -04:00
|
|
|
* # => 123
|
2008-05-24 21:12:12 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_catch(int argc, VALUE *argv)
|
|
|
|
{
|
|
|
|
VALUE tag;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
tag = rb_obj_alloc(rb_cObject);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "01", &tag);
|
|
|
|
}
|
2009-09-15 17:18:04 -04:00
|
|
|
return rb_catch_obj(tag, catch_i, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_catch(const char *tag, VALUE (*func)(), VALUE data)
|
|
|
|
{
|
2015-11-29 23:44:41 -05:00
|
|
|
VALUE vtag = tag ? rb_sym_intern_ascii_cstr(tag) : rb_obj_alloc(rb_cObject);
|
2009-09-15 17:18:04 -04:00
|
|
|
return rb_catch_obj(vtag, func, data);
|
|
|
|
}
|
|
|
|
|
2016-05-04 23:22:20 -04:00
|
|
|
static VALUE
|
|
|
|
vm_catch_protect(VALUE tag, rb_block_call_func *func, VALUE data,
|
2017-10-29 11:03:33 -04:00
|
|
|
enum ruby_tag_type *stateptr, rb_execution_context_t *volatile ec)
|
2009-09-15 17:18:04 -04:00
|
|
|
{
|
2017-06-23 03:25:52 -04:00
|
|
|
enum ruby_tag_type state;
|
2016-05-04 23:22:20 -04:00
|
|
|
VALUE val = Qnil; /* OK */
|
2017-10-29 11:03:33 -04:00
|
|
|
rb_control_frame_t *volatile saved_cfp = ec->cfp;
|
2009-09-15 17:18:04 -04:00
|
|
|
|
2017-10-29 11:03:33 -04:00
|
|
|
EC_PUSH_TAG(ec);
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2013-11-18 08:47:56 -05:00
|
|
|
_tag.tag = tag;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
2017-10-26 07:02:13 -04:00
|
|
|
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
|
2009-09-15 17:18:04 -04:00
|
|
|
/* call with argc=1, argv = [tag], block = Qnil to insure compatibility */
|
2013-12-03 07:53:18 -05:00
|
|
|
val = (*func)(tag, data, 1, (const VALUE *)&tag, Qnil);
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
2017-10-29 11:03:33 -04:00
|
|
|
else if (state == TAG_THROW && THROW_DATA_VAL((struct vm_throw_data *)ec->errinfo) == tag) {
|
|
|
|
rb_vm_rewind_cfp(ec, saved_cfp);
|
|
|
|
val = ec->tag->retval;
|
|
|
|
ec->errinfo = Qnil;
|
2008-05-24 21:12:12 -04:00
|
|
|
state = 0;
|
|
|
|
}
|
2017-10-26 07:02:13 -04:00
|
|
|
EC_POP_TAG();
|
2013-12-03 07:53:18 -05:00
|
|
|
if (stateptr)
|
|
|
|
*stateptr = state;
|
2008-05-24 21:12:12 -04:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-06-23 04:24:54 -04:00
|
|
|
VALUE
|
|
|
|
rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr)
|
|
|
|
{
|
2017-10-29 11:03:33 -04:00
|
|
|
return vm_catch_protect(t, func, data, stateptr, GET_EC());
|
2017-06-23 04:24:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_catch_obj(VALUE t, VALUE (*func)(), VALUE data)
|
|
|
|
{
|
|
|
|
enum ruby_tag_type state;
|
2017-10-29 11:03:33 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
VALUE val = vm_catch_protect(t, (rb_block_call_func *)func, data, &state, ec);
|
|
|
|
if (state) EC_JUMP_TAG(ec, state);
|
2017-06-23 04:24:54 -04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:57:37 -04:00
|
|
|
static void
|
|
|
|
local_var_list_init(struct local_var_list *vars)
|
|
|
|
{
|
|
|
|
vars->tbl = rb_hash_new();
|
|
|
|
RHASH(vars->tbl)->ntbl = st_init_numtable(); /* compare_by_identity */
|
|
|
|
RBASIC_CLEAR_CLASS(vars->tbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
local_var_list_finish(struct local_var_list *vars)
|
|
|
|
{
|
|
|
|
/* TODO: not to depend on the order of st_table */
|
|
|
|
VALUE ary = rb_hash_keys(vars->tbl);
|
|
|
|
rb_hash_clear(vars->tbl);
|
|
|
|
vars->tbl = 0;
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:26:51 -04:00
|
|
|
static int
|
|
|
|
local_var_list_update(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
|
|
|
|
{
|
|
|
|
if (existing) return ST_STOP;
|
|
|
|
*value = (st_data_t)Qtrue; /* INT2FIX(arg) */
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:26:49 -04:00
|
|
|
static void
|
|
|
|
local_var_list_add(const struct local_var_list *vars, ID lid)
|
|
|
|
{
|
2015-11-11 03:38:27 -05:00
|
|
|
if (lid && rb_is_local_id(lid)) {
|
2014-05-07 00:26:49 -04:00
|
|
|
/* should skip temporary variable */
|
2014-05-07 00:26:51 -04:00
|
|
|
st_table *tbl = RHASH_TBL_RAW(vars->tbl);
|
|
|
|
st_data_t idx = 0; /* tbl->num_entries */
|
|
|
|
st_update(tbl, ID2SYM(lid), local_var_list_update, idx);
|
2014-05-07 00:26:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-18 19:13:44 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* local_variables -> array
|
2009-01-18 19:13:44 -05:00
|
|
|
*
|
|
|
|
* Returns the names of the current local variables.
|
|
|
|
*
|
|
|
|
* fred = 1
|
|
|
|
* for i in 1..10
|
|
|
|
* # ...
|
|
|
|
* end
|
2009-06-12 18:08:46 -04:00
|
|
|
* local_variables #=> [:fred, :i]
|
2009-01-18 19:13:44 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_local_variables(void)
|
|
|
|
{
|
2014-05-07 00:26:49 -04:00
|
|
|
struct local_var_list vars;
|
2017-10-26 20:46:11 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp));
|
2015-07-24 17:44:14 -04:00
|
|
|
unsigned int i;
|
2009-01-18 19:13:44 -05:00
|
|
|
|
2014-07-01 13:57:37 -04:00
|
|
|
local_var_list_init(&vars);
|
2009-01-18 19:13:44 -05:00
|
|
|
while (cfp) {
|
|
|
|
if (cfp->iseq) {
|
2015-07-21 18:52:59 -04:00
|
|
|
for (i = 0; i < cfp->iseq->body->local_table_size; i++) {
|
|
|
|
local_var_list_add(&vars, cfp->iseq->body->local_table[i]);
|
2009-01-18 19:13:44 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
if (!VM_ENV_LOCAL_P(cfp->ep)) {
|
2009-01-18 19:13:44 -05:00
|
|
|
/* block */
|
2016-07-28 07:02:30 -04:00
|
|
|
const VALUE *ep = VM_CF_PREV_EP(cfp);
|
2009-01-18 19:13:44 -05:00
|
|
|
|
2017-10-26 20:46:11 -04:00
|
|
|
if (vm_collect_local_variables_in_heap(ep, &vars)) {
|
2009-01-18 19:13:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2012-06-10 23:14:59 -04:00
|
|
|
while (cfp->ep != ep) {
|
2009-01-18 19:13:44 -05:00
|
|
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-07-01 13:57:37 -04:00
|
|
|
return local_var_list_finish(&vars);
|
2009-01-18 19:13:44 -05:00
|
|
|
}
|
|
|
|
|
2009-01-18 22:03:09 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* block_given? -> true or false
|
|
|
|
* iterator? -> true or false
|
2009-01-18 22:03:09 -05:00
|
|
|
*
|
|
|
|
* Returns <code>true</code> if <code>yield</code> would execute a
|
|
|
|
* block in the current context. The <code>iterator?</code> form
|
|
|
|
* is mildly deprecated.
|
|
|
|
*
|
|
|
|
* def try
|
|
|
|
* if block_given?
|
|
|
|
* yield
|
|
|
|
* else
|
|
|
|
* "no block"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* try #=> "no block"
|
|
|
|
* try { "hello" } #=> "hello"
|
|
|
|
* try do "hello" end #=> "hello"
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-11-16 02:49:43 -05:00
|
|
|
static VALUE
|
2009-01-18 22:03:09 -05:00
|
|
|
rb_f_block_given_p(void)
|
|
|
|
{
|
2017-10-26 20:46:11 -04:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
rb_control_frame_t *cfp = ec->cfp;
|
|
|
|
cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
|
2009-01-18 22:03:09 -05:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
if (cfp != NULL && VM_CF_BLOCK_HANDLER(cfp) != VM_BLOCK_HANDLER_NONE) {
|
2009-01-18 22:03:09 -05:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-16 13:40:00 -04:00
|
|
|
VALUE
|
|
|
|
rb_current_realfilepath(void)
|
|
|
|
{
|
2017-10-26 20:46:11 -04:00
|
|
|
const rb_execution_context_t *ec = GET_EC();
|
|
|
|
rb_control_frame_t *cfp = ec->cfp;
|
|
|
|
cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
|
2017-05-31 20:05:33 -04:00
|
|
|
if (cfp != 0) return rb_iseq_realpath(cfp->iseq);
|
2010-03-16 13:40:00 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
void
|
|
|
|
Init_vm_eval(void)
|
|
|
|
{
|
2008-09-25 14:04:34 -04:00
|
|
|
rb_define_global_function("eval", rb_f_eval, -1);
|
2009-01-18 19:13:44 -05:00
|
|
|
rb_define_global_function("local_variables", rb_f_local_variables, 0);
|
2009-01-18 22:03:09 -05:00
|
|
|
rb_define_global_function("iterator?", rb_f_block_given_p, 0);
|
|
|
|
rb_define_global_function("block_given?", rb_f_block_given_p, 0);
|
2009-01-18 19:13:44 -05:00
|
|
|
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_define_global_function("catch", rb_f_catch, -1);
|
|
|
|
rb_define_global_function("throw", rb_f_throw, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("loop", rb_f_loop, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cBasicObject, "instance_eval", rb_obj_instance_eval, -1);
|
|
|
|
rb_define_method(rb_cBasicObject, "instance_exec", rb_obj_instance_exec, -1);
|
|
|
|
rb_define_private_method(rb_cBasicObject, "method_missing", rb_method_missing, -1);
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
#if 1
|
2018-01-22 08:09:37 -05:00
|
|
|
rb_add_method(rb_cBasicObject, id__send__,
|
2015-06-02 21:39:16 -04:00
|
|
|
VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
|
2018-01-22 08:09:37 -05:00
|
|
|
rb_add_method(rb_mKernel, idSend,
|
2015-06-02 21:39:16 -04:00
|
|
|
VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
|
2009-07-15 10:59:41 -04:00
|
|
|
#else
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
|
|
|
|
rb_define_method(rb_mKernel, "send", rb_f_send, -1);
|
2009-07-15 10:59:41 -04:00
|
|
|
#endif
|
2008-05-24 21:12:12 -04:00
|
|
|
rb_define_method(rb_mKernel, "public_send", rb_f_public_send, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cModule, "module_exec", rb_mod_module_exec, -1);
|
|
|
|
rb_define_method(rb_cModule, "class_exec", rb_mod_module_exec, -1);
|
2008-09-25 14:04:34 -04:00
|
|
|
rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval, -1);
|
|
|
|
rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval, -1);
|
2014-11-15 02:28:08 -05:00
|
|
|
|
|
|
|
rb_eUncaughtThrow = rb_define_class("UncaughtThrowError", rb_eArgError);
|
|
|
|
rb_define_method(rb_eUncaughtThrow, "initialize", uncaught_throw_init, -1);
|
|
|
|
rb_define_method(rb_eUncaughtThrow, "tag", uncaught_throw_tag, 0);
|
|
|
|
rb_define_method(rb_eUncaughtThrow, "value", uncaught_throw_value, 0);
|
|
|
|
rb_define_method(rb_eUncaughtThrow, "to_s", uncaught_throw_to_s, 0);
|
2014-11-16 03:33:49 -05:00
|
|
|
|
2015-10-22 05:58:01 -04:00
|
|
|
id_result = rb_intern_const("result");
|
2014-11-16 03:33:49 -05:00
|
|
|
id_tag = rb_intern_const("tag");
|
|
|
|
id_value = rb_intern_const("value");
|
2008-05-24 21:12:12 -04:00
|
|
|
}
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
|
|
|
|
#endif /* #ifndef MJIT_HEADER */
|