/
deepclone.winxed
179 lines (150 loc) · 3.91 KB
/
deepclone.winxed
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#! winxed
$include_const 'hash_key_type.pasm';
const int VISIT_FREEZE_NORMAL = 0x04; // 0x00 | 0x04;
const int VISIT_THAW_NORMAL = 0x05; // 0x01 | 0x04;
// static var nci = {};
function nci[anon, immediate] () {
return {};
}
function onload[anon, load] () {
using static nci;
var core_nci_funcs = {
"Parrot_pmc_new_noinit" : "PJi",
"Parrot_PMC_visit" : "vJPP",
"Parrot_PMC_freeze" : "vJPP",
"Parrot_PMC_thaw" : "vJPP",
"Parrot_PMC_thawfinish" : "vJPP"
};
var lib = null;
for (string name in core_nci_funcs) {
string sig = core_nci_funcs[name];
var func = dlfunc(lib, name, sig);
nci[name] = func;
}
/*
* Use a helper library to get at data Parrot wisely
* encapsulates away from the programmer.
*
* XXX Don't do this at home!
*/
var my_lib = loadlib("libdeepclonehelper");
var my_nci_funcs = {
"pmc_addr" : "iJP",
"pmc_get_meta" : "PJP",
"pmc_set_meta" : "vJPP",
"pmc_typenum" : "iJP"
};
for (string name in my_nci_funcs) {
string sig = my_nci_funcs[name];
var func = dlfunc(my_lib, name, sig);
nci[name] = func;
}
}
class DeepClone {
var todo;
var seen;
var state;
var int_queue;
var num_queue;
var str_queue;
var pmc_queue;
function set_pmc[vtable] (var orig) {
self.todo = new 'ResizablePMCArray'();
self.seen = new 'Hash'();
// XXX can't create (ptr => pmc) hashes in HLLs
// self.seen.set_key_type(Hash_key_type_ptr);
self.seen.set_key_type(Hash_key_type_int);
self.state = new 'Integer'();
self.int_queue = new 'ResizableIntegerArray'();
self.num_queue = new 'ResizableFloatArray'();
self.str_queue = new 'ResizableStringArray'();
self.pmc_queue = new 'ResizablePMCArray'();
var todo = self.todo;
${ push todo, orig };
}
function get_pmc[vtable] () {
using static nci;
for (int i = 0; i < self.todo; i++) {
var orig = self.todo[i];
var meta = nci['pmc_get_meta'](orig);
self.state = VISIT_FREEZE_NORMAL;
nci['Parrot_PMC_visit'](orig, self);
nci['Parrot_PMC_freeze'](orig, self);
self.push_pmc(meta);
int addr = nci['pmc_addr'](orig);
var dup = self.seen[addr];
self.state = VISIT_THAW_NORMAL;
nci['Parrot_PMC_visit'](dup, self);
nci['Parrot_PMC_thaw'](dup, self);
nci['pmc_set_meta'](dup, self.shift_pmc());
}
for (int i = 0; i < self.todo; i++) {
var dup = self.seen[nci['pmc_addr'](self.todo[i])];
nci['Parrot_PMC_thawfinish'](dup, self);
}
return self.seen[nci['pmc_addr'](self.todo[0])];
}
function get_integer[vtable] () {
int i = self.state;
return i;
}
function push_pmc[vtable] (var v) {
using static nci;
int addr = nci['pmc_addr'](v);
var dup;
int nullp;
${ isnull nullp, v };
if (nullp) {
dup = null;
}
else {
dup = self.seen[addr];
${ isnull nullp, dup };
if (nullp) {
int i = nci['pmc_typenum'](v);
dup = nci['Parrot_pmc_new_noinit'](i);
self.seen[addr] = dup;
var todo = self.todo;
${ push todo, v };
}
}
var pmc_queue = self.pmc_queue;
${ push pmc_queue, dup };
}
function shift_pmc[vtable] () {
var v;
var pmc_queue = self.pmc_queue;
${ shift v, pmc_queue };
return v;
}
function push_integer[vtable] (int v) {
var int_queue = self.int_queue;
${ push int_queue, v };
}
function shift_integer[vtable] () {
int v;
var int_queue = self.int_queue;
${ shift v, int_queue };
return v;
}
function push_float[vtable] (float v) {
var num_queue = self.num_queue;
${ push num_queue, v };
}
function shift_float[vtable] () {
float v;
var num_queue = self.num_queue;
${ shift v, num_queue };
return v;
}
function push_string[vtable] (string v) {
var str_queue = self.str_queue;
${ push str_queue, v };
}
function shift_string[vtable] () {
string v;
var str_queue = self.str_queue;
${ shift v, str_queue };
return v;
}
}