Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 183 lines (149 sloc) 5.197 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#include "builtin/sendsite.hpp"
#include "builtin/class.hpp"
#include "builtin/lookuptable.hpp"
#include "builtin/selector.hpp"
#include "builtin/symbol.hpp"

#include "resolver.hpp"
#include "global_cache.hpp"
#include "objectmemory.hpp"

namespace rubinius {
  void SendSite::init(STATE) {
    GO(send_site).set(state->new_class("SendSite"));
    G(send_site)->set_object_type(SendSiteType);
  }

  SendSite* SendSite::create(STATE, OBJECT name) {
    SendSite* ss = (SendSite*)state->new_struct(G(send_site), sizeof(SendSite));
    SET(ss, name, name);
    SET(ss, sender, Qnil);
    SET(ss, selector, Selector::lookup(state, name));
    ss->hits = ss->misses = 0;
    ss->resolver = NULL;

    ss->initialize(state);
    ss->selector->associate(state, ss);

    return ss;
  }

  void SendSite::initialize(STATE) {
    if(resolver) delete resolver;
    specialized = false;
    resolver = new GlobalCacheResolver;
  }

  OBJECT SendSite::set_sender(STATE, CompiledMethod* cm) {
    SET(this, sender, cm);
    return Qnil;
  }

  /* Indicates that the object hasn't been specialized in anyway */
  bool SendSite::basic_p(STATE) {
    return !specialized;
  }

  /* Use the information within +this+ to populate +msg+. Returns
* true if +msg+ was populated. */

  bool SendSite::locate(STATE, Message& msg) {
    if(!resolver->resolve(state, msg)) {
      msg.unshift_argument(state, msg.name);
      msg.name = G(sym_method_missing);
      msg.priv = true; // lets us look for method_missing anywhere
      if(!resolver->resolve(state, msg)) {
        return false;
      }
    }

    return true;
  }

  /* Fill in details about +msg+ by looking up the class heirarchy
* and in method tables. Returns true if lookup was successful
* and +msg+ is now filled in. */

  bool HierarchyResolver::resolve(STATE, Message& msg) {
    Module* module = msg.lookup_from;
    OBJECT entry;
    MethodVisibility* vis;

    do {
      entry = module->method_table->fetch(state, msg.name);

      /* Nothing, there? Ok, keep looking. */
      if(entry->nil_p()) goto keep_looking;

      /* A 'false' method means to terminate method lookup.
* (eg. undef_method) */
      if(entry == Qfalse) return false;

      vis = try_as<MethodVisibility>(entry);

      /* If this was a private send, then we can handle use
* any method seen. */
      if(msg.priv) {
        /* nil means that the actual method object is 'up' from here */
        if(vis && vis->method->nil_p()) goto keep_looking;

        msg.method = as<Executable>(vis ? vis->method : entry);
        msg.module = module;
        break;
      } else if(vis) {
        /* The method is private, but this wasn't a private send. */
        if(vis->private_p(state)) {
          return false;
        } else if(vis->protected_p(state)) {
          /* The method is protected, but it's not being called from
* the same module */
          if(!msg.current_self->kind_of_p(state, module)) {
            return false;
          }
        }

        /* The method was callable, but we need to keep looking
* for the implementation, so make the invocation bypass all further
* visibility checks */
        if(vis->method->nil_p()) {
          msg.priv = true;
          goto keep_looking;
        }

        msg.method = as<Executable>(vis->method);
        msg.module = module;
        break;
      } else {
        msg.method = as<Executable>(entry);
        msg.module = module;
        break;
      }

keep_looking:
      module = module->superclass;

      /* No more places to look, we couldn't find it. */
      if(module->nil_p()) return false;
    } while(1);

    return true;
  }

  bool GlobalCacheResolver::resolve(STATE, Message& msg) {
    struct GlobalCache::cache_entry* entry;

    entry = state->global_cache->lookup(msg.lookup_from, msg.name);
    if(entry) {
      if(msg.priv || entry->is_public) {
        msg.method = entry->method;
        msg.module = entry->module;
        return true;
      }
    }

    if(HierarchyResolver::resolve(state, msg)) {
      state->global_cache->retain(state, msg.lookup_from, msg.name,
          msg.module, msg.method);
      return true;
    }

    return false;
  }

  bool SpecializedResolver::resolve(STATE, Message& msg) {
    if(msg.lookup_from == klass) {
      msg.module = mod;
      msg.method = method;
      return true;
    }

    if(GlobalCacheResolver::resolve(state, msg)) {
      mod = msg.module;
      method = msg.method;
      klass = msg.lookup_from;
      return true;
    }

    return false;
  }

  void SendSite::Info::show(STATE, OBJECT self, int level) {
    SendSite* ss = as<SendSite>(self);

    class_header(state, self);
    indent_attribute(++level, "name"); ss->name->show(state, level);
    indent_attribute(level, "sender"); class_info(state, ss->sender, true);
    indent_attribute(level, "selector"); class_info(state, ss->selector, true);
    indent_attribute(level, "hits"); std::cout << ss->hits << std::endl;
    indent_attribute(level, "misses"); std::cout << ss->misses << std::endl;
    indent_attribute(level, "specialized"); std::cout << ss->specialized << std::endl;
    close_body(level);
  }
};
Something went wrong with that request. Please try again.