Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

166 lines (135 sloc) 4.895 kb
#ifndef RBX_VM_TYPE_INFO_HPP
#define RBX_VM_TYPE_INFO_HPP
#include <map>
#include <stdexcept>
#include "vm/gc_object_mark.hpp"
#include "vm/object_types.hpp"
#include "vm/prelude.hpp"
namespace rubinius {
class Class;
class Object;
class ObjectMark;
/**
* Static type information for the VM.
*
* Due to memory layout restrictions, virtual methods cannot exist
* on builtin types. This class abstracts those operations out of
* the classes themselves. Using virtual dispatch here allows the
* correct method implementation to be invoked, based on the object's
* obj_type field.
*
* @see doc/builtin_internal_resource_releasing.txt
* @see vm/object_types.hpp
* @see builtin/object.hpp
*/
class TypeInfo {
public:
typedef std::map<native_int, long> Slots;
static void init(STATE);
static void auto_init(STATE);
virtual void auto_mark(OBJECT obj, ObjectMark& mark);
public: /* Ctors */
/**
* Make a new TypeInfo.
*
* To support TypeInfo hierarchies where some classes may
* and some may not need e.g. a cleanup, the instantiation
* will set the cleanup flag if _any_ of the classes in
* the chain request it.
*/
TypeInfo(object_type type, bool cleanup = false);
virtual ~TypeInfo();
public: /* Interface */
/**
* Internal resource cleanup.
*
* This method is called with an object being collected if it is
* marked as needing cleanup.
*
* There are two things of note:
*
* 1. Cleanup methods in subclasses MUST be marked virtual; and
* 2. Each cleanup method MUST call its "super". In C++, this
* means explicitly qualifying the method name as, e.g.
*
* MySuperTypeInfo::cleanup(obj);
*/
virtual void cleanup(OBJECT obj);
virtual void mark(OBJECT obj, ObjectMark& mark);
virtual void set_field(STATE, OBJECT target, size_t index, OBJECT val);
virtual OBJECT get_field(STATE, OBJECT target, size_t index);
/**
* Default output for any object. Prints just the class name
* and address. It is expected that classes will define their own
* show output.
*/
virtual void show(STATE, OBJECT self, int level);
/**
* Currently prints the same output as show. Is specialized by
* complex classes to e.g. limit the recursion into nested
* objects to make the output more manageable. See e.g. Tuple
* and CompiledMethod. Immediates and numeric classes print
* their value for both show and show_simple.
*/
virtual void show_simple(STATE, OBJECT self, int level);
/**
* Prints spaces to indent the following text to the requested
* level. Levels are specified as 0, 1, 2, ... and the multiplier
* is 2. So, text at level 2 will have 2 * 2 = 4 spaces in front.
*/
virtual void indent(int level);
/**
* Indents the attribute name to the requested level. @see indent.
*/
virtual void indent_attribute(int level, const char* name);
/**
* Prints out the class name and address. Used for simple classes
* that may append other info on the same line.
*
* #<SomeClass:0x346882
*/
virtual void class_info(STATE, OBJECT self, bool newline = false);
/**
* Prints out the class name and address followed by a newline. Used
* for complex classes that will print additional member info.
*
* #<SomeClass:0x3287648\n
*/
virtual void class_header(STATE, OBJECT self);
/**
* Prints "..." + endl at the requested indent level.
*/
virtual void ellipsis(int level);
/**
* Indents to level-1 and prints ">" + endl.
*/
virtual void close_body(int level);
public: /* Instance vars */
bool instances_need_cleanup;
Slots slots;
VM* state;
object_type type;
std::string type_name;
};
}
#define BASIC_TYPEINFO(super) \
Info(object_type type, bool cleanup = false) : super(type, cleanup) { } \
virtual void auto_mark(OBJECT obj, ObjectMark& mark); \
virtual void set_field(STATE, OBJECT target, size_t index, OBJECT val); \
virtual OBJECT get_field(STATE, OBJECT target, size_t index);
/**
* Generate TypeInfo declaration contents.
*
* This version marks any object that has this type info in
* its type info hierarchy as needing some internal resource
* cleanup.
*
* @see doc/builtin_internal_resource_releasing.txt
*/
#define BASIC_TYPEINFO_WITH_CLEANUP(super) \
Info(object_type type, bool cleanup = true) : super(type, true) { } \
virtual void auto_mark(OBJECT obj, ObjectMark& mark); \
virtual void cleanup(Object* obj); \
virtual void set_field(STATE, OBJECT target, size_t index, OBJECT val); \
virtual OBJECT get_field(STATE, OBJECT target, size_t index);
#endif
Jump to Line
Something went wrong with that request. Please try again.