Esempio n. 1
0
  /* This is the execute implementation used by normal Ruby code,
   * as opposed to Primitives or FFI functions.
   * It prepares a Ruby method for execution.
   * Here, +exec+ is a VMMethod instance accessed via the +vmm+ slot on
   * CompiledMethod.
   */
  ExecuteStatus VMMethod::execute(STATE, Task* task, Message& msg) {
    CompiledMethod* cm = as<CompiledMethod>(msg.method);

    MethodContext* ctx = MethodContext::create(state, msg.recv, cm);

    VMMethod* vmm = cm->backend_method_;

    // Copy in things we all need.
    ctx->module(state, msg.module);
    ctx->name(state, msg.name);

    ctx->block(state, msg.block);
    ctx->args = msg.args();

    // If argument handling fails..
    GenericArguments args;
    if(args.call(state, vmm, ctx, msg) == false) {
      // Clear the values from the caller
      task->active()->clear_stack(msg.stack);

      // TODO we've got full control here, we should just raise the exception
      // in the runtime here rather than throwing a C++ exception and raising
      // it later.

      Exception::argument_error(state, vmm->required_args, msg.args());
      // never reached!
    }

#if 0
    if(!probe_->nil_p()) {
      probe_->start_method(this, msg);
    }
#endif

    // Clear the values from the caller
    task->active()->clear_stack(msg.stack);

    task->make_active(ctx);

    if(unlikely(task->profiler)) {
      profiler::Method* prof_meth;
      if(MetaClass* mc = try_as<MetaClass>(msg.module)) {
        Object* attached = mc->attached_instance();
        if(Module* mod = try_as<Module>(attached)) {
          prof_meth = task->profiler->enter_method(msg.name, mod->name(), profiler::kNormal);
        } else {
          prof_meth = task->profiler->enter_method(msg.name, attached->id(state), profiler::kNormal);
        }
      } else {
        prof_meth = task->profiler->enter_method(msg.name, msg.module->name(), profiler::kSingleton);
      }

      if(!prof_meth->file()) {
        prof_meth->set_position(cm->file(), cm->start_line(state));
      }
    }

    return cExecuteRestart;
  }