Files
mruby-mruby/mrbgems/mruby-binding/src/binding.c
T
Yukihiro "Matz" Matsumoto dccd66f9ef Support Ruby3.0 keyword arguments.
The Difference

Since Ruby1.9, the keyword arguments were emulated by Ruby using the hash
object at the bottom of the arguments. But we have gradually moved toward
keyword arguments separated from normal (positinal) arguments.

At the same time, we value compatibility, so that Ruby3.0 keyword
arguments are somewhat compromise. Basically, keyword arguments are
separated from positional arguments, except when the method does not
take any formal keyword arguments, given keyword arguments (packed
in the hash object) are considered as the last argument.

And we also allow non symbol keys in the keyword arguments. In that
case, those keys are just passed in the `**` hash (or raise
`ArgumentError` for unknown keys).

The Instruction Changes

We have changed `OP_SEND` instruction. `OP_SEND` instruction used to
take 3 operands, the register, the symbol, the number of (positional)
arguments. The meaning of the third operand has been changed. It is now
considered as `n|(nk<<4)`, where `n` is the number of positional
arguments, and `nk` is the number of keyword arguments, both occupies
4 bits in the operand.

The number `15` in both `n` and `nk` means variable sized arguments are
packed in the object. Positional arguments will be packed in the array,
and keyword arguments will be packed in the hash object. That means
arguments more than 14 values are always packed in the object.

Arguments information for other instructions (`OP_SENDB` and `OP_SUPER`)
are also changed. It works as the third operand of `OP_SEND`. the
difference between `OP_SEND` and `OP_SENDB` is just trivial. It assigns
`nil` to the block hidden arguments (right after arguments).

The instruction `OP_SENDV` and `OP_SENDVB` are removed. Those
instructions are replaced by `OP_SEND` and `OP_SENDB` respectively with
the `15` (variable sized) argument information.

Calling Convention

When calling a method, the stack elements shall be in the order of the
receiver of the method, positional arguments, keyword arguments and the
block argument. If the number of positional or keyword arugument (`n` or
`nk`) is zero, corresponding arguments will be empty. So when `n=0` and
`nk=0` the stack layout (from bottom to top) will be:

+-----------------------+
| recv | block (or nil) |
+-----------------------+

The last elements `block` should be explicitly filled before `OP_SEND`
or assigned to `nil` by `OP_SENDB` internally. In other words, the
following have exactly same behavior:

OP_SENDB clears `block` implicitly:

```
OP_SENDB reg sym 0
```

OP_SEND clears `block` implicitly:

```
OP_LOADNIL  R2
OP_SEND     R2 sym 0
```

When calling a method with only positional arguments (n=0..14) without
keyword arguments, the stack layout will be like following:

+--------------------------------------------+
| recv | arg1 | ... | arg_n | block (or nil) |
+--------------------------------------------+

When calling a method with arguments packed in the array (n=15) which
means argument splat (*) is used in the actual arguments, or more than
14 arguments are passed the stack layout will be like following:

+-------------------------------+
| recv | array | block (or nil) |
+-------------------------------+

The number of the actual arguments is determined by the length of the
argument array.

When keyword arguments are given (nk>0), keyword arguments are passed
between positional arguments and the block argument. For example, when
we pass one positional argument `1` and one keyword argument `a: 2`,
the stack layout will be like:

+------------------------------------+
| recv | 1 | :a | 2 | block (or nil) |
+------------------------------------+

Note that keyword arguments consume `2*nk` elements in the stack when
`nk=0..14` (unpacked).

When calling a method with keyword arguments packed in the hash object
(nk=15) which means keyword argument splat (**) is used or more than
14 keyword arguments in the actual arguments, the stack layout will
be like:

+------------------------------+
| recv | hash | block (or nil) |
+------------------------------+

Note for mruby/c

When mruby/c authors try to support new keyword arguments, they need
to handle the new meaning of the argument information operand. If they
choose not to support keyword arguments in mruby/c, it just raise
error when `nk` (taken by `(c>>4)&0xf`) is not zero. And combine
`OP_SENDV` behavior with `OP_SEND` when `n` is `15`.

If they want to support keyword arguments seriously, contact me at
<matz@ruby.or.jp> or `@yukihiro_matz`. I can help you.
2021-10-12 20:16:36 +09:00

171 lines
4.9 KiB
C

#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/compile.h>
#include <mruby/error.h>
#include <mruby/proc.h>
#include <mruby/presym.h>
#include <mruby/string.h>
mrb_noreturn void mrb_method_missing(mrb_state *mrb, mrb_sym name, mrb_value self, mrb_value args);
void mrb_proc_merge_lvar(mrb_state *mrb, mrb_irep *irep, struct REnv *env, int num, const mrb_sym *lv, const mrb_value *stack);
mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p);
const struct RProc *mrb_binding_extract_proc(mrb_state *mrb, mrb_value binding);
struct REnv *mrb_binding_extract_env(mrb_state *mrb, mrb_value binding);
typedef mrb_bool mrb_parser_foreach_top_variable_func(mrb_state *mrb, mrb_sym sym, void *user);
void mrb_parser_foreach_top_variable(mrb_state *mrb, struct mrb_parser_state *p, mrb_parser_foreach_top_variable_func *func, void *user);
static void
binding_eval_error_check(mrb_state *mrb, struct mrb_parser_state *p, const char *file)
{
if (!p) {
mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state (out of memory)");
}
if (0 < p->nerr) {
mrb_value str;
if (file) {
str = mrb_format(mrb, "file %s line %d: %s",
file,
p->error_buffer[0].lineno,
p->error_buffer[0].message);
}
else {
str = mrb_format(mrb, "line %d: %s",
p->error_buffer[0].lineno,
p->error_buffer[0].message);
}
mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
}
}
#define LV_BUFFERS 8
struct expand_lvspace {
mrb_irep *irep;
struct REnv *env;
size_t numvar;
mrb_sym syms[LV_BUFFERS];
};
static mrb_bool
expand_lvspace(mrb_state *mrb, mrb_sym sym, void *user)
{
struct expand_lvspace *p = (struct expand_lvspace*)user;
mrb_int symlen;
const char *symname = mrb_sym_name_len(mrb, sym, &symlen);
if (symname && symlen > 0) {
if (symname[0] != '&' && symname[0] != '*') {
p->syms[p->numvar++] = sym;
if (p->numvar >= LV_BUFFERS) {
mrb_proc_merge_lvar(mrb, p->irep, p->env, p->numvar, p->syms, NULL);
p->numvar = 0;
}
}
}
return TRUE;
}
struct binding_eval_prepare_body {
mrb_value binding;
const char *file;
const char *expr;
mrb_int exprlen;
mrbc_context *mrbc;
struct mrb_parser_state *pstate;
};
static mrb_value
binding_eval_prepare_body(mrb_state *mrb, void *opaque)
{
struct binding_eval_prepare_body *p = (struct binding_eval_prepare_body*)opaque;
const struct RProc *proc = mrb_binding_extract_proc(mrb, p->binding);
mrb_assert(!MRB_PROC_CFUNC_P(proc));
p->mrbc = mrbc_context_new(mrb);
mrbc_filename(mrb, p->mrbc, p->file ? p->file : "(eval)");
p->mrbc->upper = proc;
p->mrbc->capture_errors = TRUE;
p->pstate = mrb_parse_nstring(mrb, p->expr, p->exprlen, p->mrbc);
binding_eval_error_check(mrb, p->pstate, p->file);
struct expand_lvspace args = {
(mrb_irep*)proc->body.irep,
mrb_binding_extract_env(mrb, p->binding),
0,
{ 0 }
};
mrb_parser_foreach_top_variable(mrb, p->pstate, expand_lvspace, &args);
if (args.numvar > 0) {
mrb_proc_merge_lvar(mrb, args.irep, args.env, args.numvar, args.syms, NULL);
}
return mrb_nil_value();
}
static void
binding_eval_prepare(mrb_state *mrb, mrb_value binding)
{
struct binding_eval_prepare_body d = { binding, NULL, NULL, 0, NULL, NULL };
mrb_int argc;
mrb_value *argv;
mrb_get_args(mrb, "s|z*!", &d.expr, &d.exprlen, &d.file, &argv, &argc);
/* `eval` should take (string[, file, line]) */
if (argc > 3) mrb_argnum_error(mrb, argc, 1, 3);
mrb_bool error;
mrb_value ret = mrb_protect_error(mrb, binding_eval_prepare_body, &d, &error);
if (d.pstate) mrb_parser_free(d.pstate);
if (d.mrbc) mrbc_context_free(mrb, d.mrbc);
if (error) mrb_exc_raise(mrb, ret);
}
static mrb_value
mrb_binding_eval(mrb_state *mrb, mrb_value binding)
{
binding_eval_prepare(mrb, binding);
struct RClass *c = mrb->kernel_module;
mrb_method_t m = mrb_method_search_vm(mrb, &c, MRB_SYM(eval));
mrb_callinfo *ci = mrb->c->ci;
int argc = ci->n;
mrb_value *argv = ci->stack + 1;
struct RProc *proc;
if (argc < 15) {
argv[0] = mrb_ary_new_from_values(mrb, argc, argv);
argv[1] = argv[argc]; /* copy block */
ci->n = 15;
}
if (MRB_METHOD_UNDEF_P(m)) {
mrb_method_missing(mrb, MRB_SYM(eval), binding, argv[0]);
}
mrb_ary_splice(mrb, argv[0], 1, 0, binding); /* insert binding as 2nd argument */
if (MRB_METHOD_FUNC_P(m)) {
proc = mrb_proc_new_cfunc(mrb, MRB_METHOD_FUNC(m));
MRB_PROC_SET_TARGET_CLASS(proc, c);
}
else {
proc = MRB_METHOD_PROC(m);
}
ci->u.target_class = c;
return mrb_exec_irep(mrb, binding, proc);
}
void
mrb_mruby_binding_gem_init(mrb_state *mrb)
{
struct RClass *binding = mrb_class_get_id(mrb, MRB_SYM(Binding));
mrb_define_method(mrb, binding, "eval", mrb_binding_eval, MRB_ARGS_ANY());
}
void
mrb_mruby_binding_gem_final(mrb_state *mrb)
{
}