Added spec:commit task to commit changes to spec/ruby sources.
[rbx.git] / shotgun / lib / rubinius.c
blobdcbd77e4271bfea53bc101ae9dd0f11af55242bd
1 #include <string.h>
3 #include "shotgun/lib/shotgun.h"
4 #include "shotgun/lib/string.h"
5 #include "shotgun/lib/symbol.h"
6 #include "shotgun/lib/module.h"
7 #include "shotgun/lib/class.h"
9 OBJECT rbs_const_set(STATE, OBJECT module, const char *name, OBJECT obj) {
10 OBJECT sym, str;
12 str = string_new(state, name);
13 sym = string_to_sym(state, str);
15 module_const_set(state, module, sym, obj);
16 return obj;
19 OBJECT rbs_const_get(STATE, OBJECT module, const char *name) {
20 OBJECT sym, str;
22 str = string_new(state, name);
23 sym = string_to_sym(state, str);
25 return module_const_get(state, module, sym);
28 OBJECT rbs_class_new(STATE, const char *name, int fields, OBJECT sup) {
29 return class_new(state, name, fields, sup, BASIC_CLASS(object));
32 OBJECT rbs_class_new_with_namespace(STATE, const char *name, int fields, OBJECT sup, OBJECT ns) {
33 return class_new(state, name, fields, sup, ns);
36 OBJECT rbs_module_new(STATE, const char *name, OBJECT ns) {
37 OBJECT mod;
39 mod = rbs_const_get(state, ns, name);
41 if (!NIL_P(mod)) /* HACK check for module */
42 return mod;
44 mod = module_allocate_mature(state, 0);
45 module_setup_with_namespace(state, mod, name, ns);
47 return mod;
50 OBJECT rbs_symbol_to_string(STATE, OBJECT sym) {
51 OBJECT str;
52 str = symtbl_find_string(state, state->global->symbols, sym);
53 return str;
56 const char* rbs_symbol_to_cstring(STATE, OBJECT sym) {
57 return rbx_string_as_cstr(state, rbs_symbol_to_string(state, sym));
60 OBJECT rbs_get_field_raw(OBJECT obj, int fel) {
61 return NTH_FIELD_DIRECT(obj, fel);
64 int rbs_num_fields(OBJECT obj) {
65 return NUM_FIELDS(obj);
68 OBJECT rbs_raw_class(OBJECT obj) {
69 if(REFERENCE_P(obj)) {
70 return obj->klass;
72 return Qnil;
75 OBJECT rbs_class_new_instance(STATE, OBJECT cls) {
76 OBJECT t1;
77 t1 = class_get_instance_fields(cls);
78 return NEW_OBJECT(cls, N2I(t1));
81 const char *rbs_inspect(STATE, OBJECT obj) {
82 OBJECT kls;
83 static char buf[1024];
85 kls = object_class(state, obj);
87 if(NIL_P(kls)) {
88 assert(RTEST(kls) && "class is nil");
89 snprintf(buf, sizeof(buf), "<(NilClass!!):%p>", (void*)obj);
90 } else if(kls == state->global->class) {
91 snprintf(buf, sizeof(buf), "%s", rbs_symbol_to_cstring(state, module_get_name(obj)));
92 } else {
93 snprintf(buf, sizeof(buf), "<%s:%p>", rbs_symbol_to_cstring(state, module_get_name(kls)), (void*)obj);
95 return buf;
98 const char *rbs_inspect_verbose(STATE, OBJECT obj) {
99 OBJECT kls;
100 static char buf[1024];
102 kls = object_class(state, obj);
104 if(NIL_P(kls)) {
105 assert(RTEST(kls) && "class is nil");
106 snprintf(buf, sizeof(buf), "<(NilClass!!):%p>", (void*)obj);
107 } else if(kls == state->global->class) {
108 snprintf(buf, sizeof(buf), "<Class:%s>", rbs_symbol_to_cstring(state, module_get_name(obj)));
109 } else if(kls == state->global->module) {
110 snprintf(buf, sizeof(buf), "<Module:%s>", rbs_symbol_to_cstring(state, module_get_name(obj)));
111 } else if(kls == state->global->cmethod) {
112 snprintf(buf, sizeof(buf), "<CompiledMethod:%p %s>", (void*)obj, rbs_symbol_to_cstring(state, cmethod_get_name(obj)));
113 } else if(kls == state->global->symbol || kls == state->global->string) {
114 const char *s = NULL;
115 snprintf(buf, sizeof(buf), "<%s:%p '", rbs_symbol_to_cstring(state, module_get_name(kls)), (void*)obj);
117 if (kls == state->global->symbol) {
118 s = rbs_symbol_to_cstring(state, obj);
121 if (kls == state->global->string) {
122 s = rbx_string_as_cstr(state, obj);
125 if (s) {
126 int l;
127 if ((l = strlen(s)) > 40) {
128 strncat(buf, s, 20);
129 snprintf(buf, sizeof(buf), "%s...", buf);
130 strncat(buf, s+l-20, 20);
131 } else
132 strncat(buf, s, 40);
134 snprintf(buf, sizeof(buf), "%s'>", buf);
135 } else {
136 snprintf(buf, sizeof(buf), "<%s:%p>", rbs_symbol_to_cstring(state, module_get_name(kls)), (void*)obj);
138 return buf;
141 /* Debugging functions */
142 void _print_stack(int cnt, int start) {
143 OBJECT *stk;
144 int i;
146 stk = current_machine->c->sp_ptr;
147 printf("Current Stack: %p\n", stk);
149 if(start < 0) {
150 stk -= start;
151 for(i = start; i < 0; i++) {
152 printf(" %3d:\t%s\t\t\t%p\n", i, _inspect(*stk), stk);
153 stk--;
157 stk = current_machine->c->sp_ptr;
159 printf("> 0:\t%s\n", _inspect(*stk--));
161 for(i = 1; i <= cnt; i++) {
162 printf(" %3d:\t%s\n", i, _inspect(*stk));
163 stk--;
167 const char *_inspect(OBJECT obj) {
168 if(SYMBOL_P(obj)) {
169 return rbs_symbol_to_cstring(current_machine->s, obj);
171 return rbs_inspect_verbose(current_machine->s, obj);
174 void _source_location(int *line_number, const char **filename) {
175 struct fast_context *fc = FASTCTX(current_machine->c->active_context);
176 *line_number = cpu_ip2line(current_machine->s, fc->method, fc->ip);
177 *filename = rbs_symbol_to_cstring(current_machine->s, cmethod_get_file(fc->method));