-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
callsite_hash.c
67 lines (58 loc) · 1.42 KB
/
callsite_hash.c
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
/* Author: Lourens Naudé */
#include "ruby.h"
#include "node.h"
#include "env.h"
static int strhash(register const char *string) {
register int c;
register int val = 0;
while ((c = *string++) != '\0') {
val = val*997 + c;
}
return val + (val>>5);
}
static VALUE rb_f_callsite( VALUE obj ) {
struct FRAME *frame = ruby_frame;
NODE *n;
int lev = -1;
int csite = 0;
if (frame->last_func == ID_ALLOCATOR) {
frame = frame->prev;
}
if (lev < 0) {
ruby_set_current_source();
if (frame->last_func) {
csite += strhash(ruby_sourcefile) + ruby_sourceline + frame->last_func;
}
else if (ruby_sourceline == 0) {
csite += strhash(ruby_sourcefile);
}
else {
csite += strhash(ruby_sourcefile) + ruby_sourceline;
}
if (lev < -1) return INT2FIX(csite);
}
else {
while (lev-- > 0) {
frame = frame->prev;
if (!frame) {
csite = 0;
break;
}
}
}
for (; frame && (n = frame->node); frame = frame->prev) {
if (frame->prev && frame->prev->last_func) {
if (frame->prev->node == n) {
if (frame->prev->last_func == frame->last_func) continue;
}
csite += strhash(n->nd_file) + nd_line(n) + frame->prev->last_func;
}
else {
csite += strhash(n->nd_file) + nd_line(n);
}
}
return INT2FIX(csite);
}
void Init_callsite_hash() {
rb_define_global_function("callsite_hash", rb_f_callsite, 0);
}