forked from rubinius/rubinius
/
module.cpp
115 lines (86 loc) · 3.04 KB
/
module.cpp
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
#include "vm.hpp"
#include "vm/object_utils.hpp"
#include "objectmemory.hpp"
#include "builtin/class.hpp"
#include "builtin/module.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/lookuptable.hpp"
#include "builtin/methodtable.hpp"
#include "builtin/symbol.hpp"
#include "builtin/string.hpp"
namespace rubinius {
Module* Module::create(STATE) {
Module* mod = state->new_object<Module>(G(module));
mod->name(state, (Symbol*)Qnil);
mod->superclass(state, (Module*)Qnil);
mod->setup(state);
return mod;
}
Module* Module::allocate(STATE, Object* self) {
Module* module = Module::create(state);
module->klass(state, (Class*)self);
return module;
}
void Module::setup(STATE) {
constants(state, LookupTable::create(state));
method_table(state, MethodTable::create(state));
}
void Module::setup(STATE, const char* str, Module* under) {
setup(state, state->symbol(str), under);
}
void Module::setup(STATE, Symbol* name, Module* under) {
if(!under) under = G(object);
this->constants(state, LookupTable::create(state));
this->method_table(state, MethodTable::create(state));
this->name(state, name);
under->set_const(state, name, this);
}
void Module::set_name(STATE, Module* under, Symbol* name) {
if(under == G(object)) {
this->name(state, name);
} else {
String* cur = under->name()->to_str(state);
String* str_name = cur->add(state, "::")->append(state,
name->to_str(state));
this->name(state, str_name->to_sym(state));
}
}
void Module::set_const(STATE, Object* sym, Object* val) {
constants_->store(state, sym, val);
state->shared.inc_global_serial();
}
void Module::set_const(STATE, const char* name, Object* val) {
set_const(state, state->symbol(name), val);
}
Object* Module::get_const(STATE, Symbol* sym, bool* found) {
return constants_->fetch(state, sym, found);
}
Object* Module::get_const(STATE, Symbol* sym) {
bool found;
return get_const(state, sym, &found);
}
Object* Module::get_const(STATE, const char* sym) {
return get_const(state, state->symbol(sym));
}
void Module::Info::show(STATE, Object* self, int level) {
Module* mod = as<Module>(self);
class_header(state, self);
indent_attribute(++level, "name"); mod->name()->show(state, level);
indent_attribute(level, "superclass"); class_info(state, mod->superclass(), true);
indent_attribute(level, "constants"); mod->constants()->show(state, level);
indent_attribute(level, "method_table"); mod->method_table()->show(state, level);
close_body(level);
}
IncludedModule* IncludedModule::create(STATE) {
IncludedModule* imod;
imod = state->new_object<IncludedModule>(G(included_module));
imod->name(state, state->symbol("<included module>"));
imod->superclass(state, (Module*)Qnil);
return imod;
}
IncludedModule* IncludedModule::allocate(STATE, Object* self) {
IncludedModule* imod = IncludedModule::create(state);
imod->klass(state, (Class*)self);
return imod;
}
}