Header: mruby/proc.h

Overview

env object (for internal used)

  • don’t create multiple envs on one ci.

  • don’t share a env to different ci.

  • don’t attach a closed env to any ci.

Function Summary collapse

Define Summary

#define MRUBY_PROC_H
#define MRB_ENV_SET_LEN

flags (21bits): 5(ZERO):8(cioff/bidx):8(stack_len)

#define MRB_ENV_LEN
#define MRB_ENV_CLOSE
#define MRB_ENV_ONSTACK_P
#define MRB_ENV_BIDX
#define MRB_ENV_SET_BIDX
#define MRB_ASPEC_REQ

aspec access

#define MRB_ASPEC_OPT
#define MRB_ASPEC_REST
#define MRB_ASPEC_POST
#define MRB_ASPEC_KEY
#define MRB_ASPEC_KDICT
#define MRB_ASPEC_BLOCK
#define MRB_PROC_CFUNC_FL
#define MRB_PROC_CFUNC_P
#define MRB_PROC_CFUNC
#define MRB_PROC_STRICT
#define MRB_PROC_STRICT_P
#define MRB_PROC_ORPHAN
#define MRB_PROC_ORPHAN_P
#define MRB_PROC_ENVSET
#define MRB_PROC_ENV_P
#define MRB_PROC_ENV
#define MRB_PROC_TARGET_CLASS
#define MRB_PROC_SET_TARGET_CLASS
#define MRB_PROC_SCOPE
#define MRB_PROC_SCOPE_P
#define MRB_PROC_NOARG
#define MRB_PROC_NOARG_P
#define MRB_PROC_ALIAS
#define MRB_PROC_ALIAS_P
#define mrb_proc_ptr
#define mrb_cfunc_env_get

old name

#define MRB_METHOD_FUNC_FL
#define MRB_METHOD_NOARG_FL
#define MRB_METHOD_PUBLIC_FL
#define MRB_METHOD_PRIVATE_FL
#define MRB_METHOD_PROTECTED_FL
#define MRB_METHOD_VDEFAULT_FL
#define MRB_METHOD_VISIBILITY_MASK
#define MRB_METHOD_FUNC_P
#define MRB_METHOD_NOARG_P
#define MRB_METHOD_FUNC
#define MRB_METHOD_NOARG_SET
#define MRB_METHOD_FROM_FUNC
#define MRB_METHOD_FROM_PROC
#define MRB_METHOD_PROC_P
#define MRB_METHOD_PROC
#define MRB_METHOD_UNDEF_P
#define MRB_METHOD_VISIBILITY
#define MRB_SET_VISIBILITY
#define MRB_METHOD_SET_VISIBILITY
#define MRB_METHOD_CFUNC_P
#define MRB_METHOD_CFUNC

use MRB_METHOD_CFUNC(m) only when MRB_METHOD_CFUNC_P(m) is true

Function Details

struct RProc * mrb_proc_new_cfunc(mrb_state* , mrb_func_t )

struct RProc * mrb_closure_new_cfunc(mrb_state * mrb, mrb_func_t func, int nlocals)

struct RProc * mrb_proc_new_cfunc_with_env(mrb_state * mrb, mrb_func_t func, mrb_int argc, const mrb_value * argv)

following functions are defined in mruby-proc-ext so please include it when using

mrb_value mrb_proc_cfunc_env_get(mrb_state * mrb, mrb_int idx)

mrb_value mrb_load_proc(mrb_state * mrb, const struct RProc * proc)

void mrb_vm_ci_env_clear(mrb_state * mrb, mrb_callinfo * ci)

It can be used to isolate top-level scopes referenced by blocks generated by mrb_load_string_cxt() or similar called before entering the mruby VM (e.g. from main()). In that case, the ci parameter should be mrb->c->cibase.

#include <mruby.h>
#include <mruby/compile.h>
#include <mruby/proc.h>

int
main(int argc, char **argv)
{
  mrb_state *mrb;
  mrb_ccontext *cxt;
  mrb_value blk, ret;

  mrb = mrb_open();
  cxt = mrb_ccontext_new(mrb);
  blk = mrb_load_string_cxt(mrb, "x, y, z = 1, 2, 3; proc { [x, y, z] }", cxt);
  mrb_vm_ci_env_clear(mrb, mrb->c->cibase);
  mrb_load_string_cxt(mrb, "x, y, z = 4, 5, 6", cxt);
  ret = mrb_funcall(mrb, blk, "call", 0);
  mrb_p(mrb, ret);  // => [1, 2, 3]
                    // => [4, 5, 6] if `mrb_vm_ci_env_clear()` is commented out
  mrb_ccontext_free(mrb, cxt);
  mrb_close(mrb);

  return 0;
}

The top-level local variable names stored in mrb_ccontext are retained. Use also mrb_ccontext_cleanup_local_variables() at the same time, if necessary.