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
) {
12 str
= string_new(state
, name
);
13 sym
= string_to_sym(state
, str
);
15 module_const_set(state
, module
, sym
, obj
);
19 OBJECT
rbs_const_get(STATE
, OBJECT module
, const char *name
) {
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
) {
39 mod
= rbs_const_get(state
, ns
, name
);
41 if (!NIL_P(mod
)) /* HACK check for module */
44 mod
= module_allocate_mature(state
, 0);
45 module_setup_with_namespace(state
, mod
, name
, ns
);
50 OBJECT
rbs_symbol_to_string(STATE
, OBJECT sym
) {
52 str
= symtbl_find_string(state
, state
->global
->symbols
, sym
);
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
)) {
75 OBJECT
rbs_class_new_instance(STATE
, OBJECT cls
) {
77 t1
= class_get_instance_fields(cls
);
78 return NEW_OBJECT(cls
, N2I(t1
));
81 const char *rbs_inspect(STATE
, OBJECT obj
) {
83 static char buf
[1024];
85 kls
= object_class(state
, obj
);
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
)));
93 snprintf(buf
, sizeof(buf
), "<%s:%p>", rbs_symbol_to_cstring(state
, module_get_name(kls
)), (void*)obj
);
98 const char *rbs_inspect_verbose(STATE
, OBJECT obj
) {
100 static char buf
[1024];
102 kls
= object_class(state
, obj
);
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
);
127 if ((l
= strlen(s
)) > 40) {
129 snprintf(buf
, sizeof(buf
), "%s...", buf
);
130 strncat(buf
, s
+l
-20, 20);
134 snprintf(buf
, sizeof(buf
), "%s'>", buf
);
136 snprintf(buf
, sizeof(buf
), "<%s:%p>", rbs_symbol_to_cstring(state
, module_get_name(kls
)), (void*)obj
);
141 /* Debugging functions */
142 void _print_stack(int cnt
, int start
) {
146 stk
= current_machine
->c
->sp_ptr
;
147 printf("Current Stack: %p\n", stk
);
151 for(i
= start
; i
< 0; i
++) {
152 printf(" %3d:\t%s\t\t\t%p\n", i
, _inspect(*stk
), 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
));
167 const char *_inspect(OBJECT 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
));