-
Notifications
You must be signed in to change notification settings - Fork 130
/
ModuleLoader.nqp
430 lines (387 loc) · 19.5 KB
/
ModuleLoader.nqp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Coercive dispatchers come here, because we use them during module
# loading, and so can't have them with the others that are in the setting,
# as the module loader is used to load the setting.
my $get-exception-message := -> $ex { nqp::getmessage($ex) };
nqp::dispatch('boot-syscall', 'dispatcher-register', 'nqp-stringify', -> $capture {
# Guard on the type of the argument for stringification.
my $track-arg := nqp::dispatch('boot-syscall', 'dispatcher-track-arg', $capture, 0);
nqp::dispatch('boot-syscall', 'dispatcher-guard-type', $track-arg);
# A null argument always maps to the empty string.
my $arg := nqp::captureposarg($capture, 0);
if nqp::isnull($arg) {
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, '');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Anything that can unbox as a string will simply just be handed back;
# we rely on the argument handling to do the unbox.
elsif nqp::isconcrete($arg) && nqp::dispatch('boot-syscall', 'can-unbox-to-str', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-value', $capture);
}
# If there's a Str method, invoke that
elsif nqp::isconcrete(my $meth := $arg.HOW.find_method($arg, 'Str')) {
my $with-name := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'Str');
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg',
$with-name, 0, $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'lang-meth-call', $delegate);
}
# If it's undefined, then the empty string.
elsif !nqp::isconcrete($arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, '');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Boxed int/num.
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-int', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-int-to-str');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-num', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-num-to-str');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
# Exception stringifies to its message
elsif nqp::isconcrete(try { nqp::getmessage($arg) }) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-obj',
$capture, 0, $get-exception-message);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-code-constant', $delegate);
}
# Otherwise, don't know what to do.
else {
nqp::die('Cannot stringify object of type ' ~ $arg.HOW.name($arg));
}
});
nqp::dispatch('boot-syscall', 'dispatcher-register', 'nqp-intify', -> $capture {
# Guard on the type of the argument for intification.
my $track-arg := nqp::dispatch('boot-syscall', 'dispatcher-track-arg', $capture, 0);
nqp::dispatch('boot-syscall', 'dispatcher-guard-type', $track-arg);
# A null argument always maps to 0.
my $arg := nqp::captureposarg($capture, 0);
if nqp::isnull($arg) {
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-int',
$capture, 0, 0);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Anything that can unbox as an int will simply just be handed back;
# we rely on the argument handling to do the unbox.
elsif nqp::isconcrete($arg) && nqp::dispatch('boot-syscall', 'can-unbox-to-int', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-value', $capture);
}
# If there's an Int method, invoke that
elsif nqp::isconcrete(try { my $meth := $arg.HOW.find_method($arg, 'Int') }) {
my $with-name := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'Int');
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg',
$with-name, 0, $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'lang-meth-call', $delegate);
}
# If it's undefined, then 0.
elsif !nqp::isconcrete($arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-int',
$capture, 0, 0);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Array or hash intifies to elems
elsif nqp::islist($arg) || nqp::ishash($arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'elems');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
# Boxed str/num
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-str', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-str-to-int');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-num', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-num-to-int');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
# Otherwise, don't know what to do.
else {
nqp::die('Cannot intify object of type ' ~ $arg.HOW.name($arg));
}
});
nqp::dispatch('boot-syscall', 'dispatcher-register', 'nqp-uintify', -> $capture {
# Guard on the type of the argument for intification.
my $track-arg := nqp::dispatch('boot-syscall', 'dispatcher-track-arg', $capture, 0);
nqp::dispatch('boot-syscall', 'dispatcher-guard-type', $track-arg);
# A null argument always maps to 0.
my $arg := nqp::captureposarg($capture, 0);
if nqp::isnull($arg) {
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-int',
$capture, 0, 0);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Anything that can unbox as an int will simply just be handed back;
# we rely on the argument handling to do the unbox.
elsif nqp::isconcrete($arg) && nqp::dispatch('boot-syscall', 'can-unbox-to-int', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-value', $capture);
}
# If there's an Int method, invoke that
elsif nqp::isconcrete(try { my $meth := $arg.HOW.find_method($arg, 'UInt') }) {
my $with-name := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'Int');
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg',
$with-name, 0, $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'lang-meth-call', $delegate);
}
# If it's undefined, then 0.
elsif !nqp::isconcrete($arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-int',
$capture, 0, 0);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Array or hash intifies to elems
elsif nqp::islist($arg) || nqp::ishash($arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'elems');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
# Boxed str/num
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-str', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-str-to-int');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-num', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-num-to-int');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
# Otherwise, don't know what to do.
else {
nqp::die('Cannot intify object of type ' ~ $arg.HOW.name($arg));
}
});
nqp::dispatch('boot-syscall', 'dispatcher-register', 'nqp-numify', -> $capture {
# Guard on the type of the argument for numification.
my $track-arg := nqp::dispatch('boot-syscall', 'dispatcher-track-arg', $capture, 0);
nqp::dispatch('boot-syscall', 'dispatcher-guard-type', $track-arg);
# A null argument always maps to 0e0.
my $arg := nqp::captureposarg($capture, 0);
if nqp::isnull($arg) {
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-num',
$capture, 0, 0e0);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Anything that can unbox as a num will simply just be handed back;
# we rely on the argument handling to do the unbox.
elsif nqp::isconcrete($arg) && nqp::dispatch('boot-syscall', 'can-unbox-to-num', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-value', $capture);
}
# If there's a Num method, invoke that
elsif nqp::isconcrete(try { my $meth := $arg.HOW.find_method($arg, 'Num') }) {
my $with-name := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'Num');
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg',
$with-name, 0, $track-arg);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'lang-meth-call', $delegate);
}
# If it's undefined, then 0e0.
elsif !nqp::isconcrete($arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-num',
$capture, 0, 0e0);
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-constant', $delegate);
}
# Array or hash numifies to elems (we can leave implicit return argument
# conversion to take care of making it into a num).
elsif nqp::islist($arg) || nqp::ishash($arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'elems');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
# Boxed str/int
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-str', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-str-to-num');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
elsif nqp::dispatch('boot-syscall', 'can-unbox-to-int', $arg) {
nqp::dispatch('boot-syscall', 'dispatcher-guard-concreteness', $track-arg);
my $delegate := nqp::dispatch('boot-syscall', 'dispatcher-insert-arg-literal-str',
$capture, 0, 'coerce-boxed-int-to-num');
nqp::dispatch('boot-syscall', 'dispatcher-delegate', 'boot-syscall', $delegate);
}
# Otherwise, don't know what to do.
else {
nqp::die('Cannot numify object of type ' ~ $arg.HOW.name($arg));
}
});
knowhow ModuleLoader {
my %modules_loaded;
my %settings_loaded;
method search_path($explicit_path) {
my @search_paths;
# Put any explicitly specified path on the start of the list.
my $explicit := nqp::ifnull(nqp::ifnull(%*COMPILING, {})<%?OPTIONS>, {}){$explicit_path};
if !nqp::isnull($explicit) && nqp::defined($explicit) {
nqp::push(@search_paths, $explicit);
}
my %env := nqp::getenvhash();
if nqp::existskey(%env, 'NQP_LIB') {
nqp::push(@search_paths, %env<NQP_LIB>);
}
@search_paths
}
method ctxsave() {
$*MAIN_CTX := nqp::ctxcaller(nqp::ctx());
$*CTXSAVE := 0;
}
method load_module($module_name, *@global_merge_target) {
# If we didn't already do so, load the module and capture
# its mainline. Otherwise, we already loaded it so go on
# with what we already have.
my $module_ctx;
my $path := nqp::join('/', nqp::split('::', $module_name)) ~ '.moarvm';
my @prefixes := self.search_path('module-path');
for @prefixes -> $prefix {
if nqp::stat("$prefix/$path", 0) {
$path := "$prefix/$path";
last;
}
}
if nqp::existskey(%modules_loaded, $path) {
$module_ctx := %modules_loaded{$path};
}
else {
my $*CTXSAVE := self;
my $*MAIN_CTX := ModuleLoader;
my $boot_mode := nqp::ifnull(nqp::ifnull(%*COMPILING, {})<%?OPTIONS>, {})<bootstrap>;
$boot_mode := !nqp::isnull($boot_mode) && $boot_mode;
my $preserve_global := nqp::getcurhllsym('GLOBAL');
nqp::usecompileehllconfig() if $boot_mode;
nqp::loadbytecode($path);
nqp::usecompilerhllconfig() if $boot_mode;
nqp::bindcurhllsym('GLOBAL', $preserve_global);
%modules_loaded{$path} := $module_ctx := $*MAIN_CTX;
}
# Provided we have a mainline...
if nqp::defined($module_ctx) {
# Merge any globals.
my $UNIT := nqp::ctxlexpad($module_ctx);
unless nqp::isnull($UNIT<GLOBALish>) {
if nqp::elems(@global_merge_target) {
merge_globals(@global_merge_target[0], $UNIT<GLOBALish>);
}
}
}
$module_ctx;
}
# XXX This is a really dumb and minimalistic GLOBAL merger.
# For a much more complete one, see sorear++'s work in
# Niecza. This one will likely evolve towards that, but for
# now I just need something that's just good enough for the
# immediate needs of NQP bootstrapping.
my $stub_how := 'KnowHOW';
sub merge_globals($target, $source) {
# XXX For now just merge the top level symbols;
# if there's a conflict then don't dig any deeper.
# Obviously, just a first cut at this. :-)
my %known_symbols;
for $target.WHO {
%known_symbols{nqp::iterkey_s($_)} := 1;
}
for $source.WHO {
my $sym := nqp::iterkey_s($_);
my $val := nqp::iterval($_);
if !nqp::existskey(%known_symbols, $sym) {
my $source_is_stub := 0;
# XXX TODO: Exceptions.
#try {
my $source_mo := $val.HOW;
$source_is_stub := $source_mo.WHAT.HOW.name($source_mo) eq $stub_how &&
!nqp::isnull(nqp::who($val)) && nqp::who($val);
#}
if $source_is_stub {
my $source := $val;
my $source_clone := $source.HOW.new_type(:name($source.HOW.name($source)));
$source_clone.HOW.compose($source_clone);
my %WHO_clone;
for nqp::who($source) {
%WHO_clone{nqp::iterkey_s($_)} := nqp::iterval($_);
}
nqp::setwho($source_clone, %WHO_clone);
($target.WHO){$sym} := $source_clone;
}
else {
($target.WHO){$sym} := $val;
}
}
elsif ($target.WHO){$sym} =:= $val {
# No problemo; a symbol can't conflict with itself.
}
else {
my $source_mo := $val.HOW;
my $source_is_stub := $source_mo.WHAT.HOW.name($source_mo) eq $stub_how;
my $target_mo := ($target.WHO){$sym}.HOW;
my $target_is_stub := $target_mo.WHAT.HOW.name($target_mo) eq $stub_how;
if $source_is_stub && $target_is_stub {
# Leave target as is, and merge the nested symbols.
merge_globals(($target.WHO){$sym}, $val);
}
else {
nqp::die("Merging GLOBAL symbols failed: duplicate definition of symbol $sym");
}
}
}
}
method load_setting($setting_name) {
my $setting;
if $setting_name ne 'NULL' {
# Add path prefix and .setting suffix.
my $path := "$setting_name.setting.moarvm";
my @prefixes := self.search_path('setting-path');
for @prefixes -> $prefix {
if nqp::stat("$prefix/$path", 0) {
$path := "$prefix/$path";
last;
}
}
# Unless we already did so, load the setting.
unless nqp::existskey(%settings_loaded, $path) {
my $*CTXSAVE := self;
my $*MAIN_CTX := ModuleLoader;
my $boot_mode := nqp::ifnull(nqp::ifnull(%*COMPILING, {})<%?OPTIONS>, {})<bootstrap>;
$boot_mode := !nqp::isnull($boot_mode) && $boot_mode;
my $preserve_global := nqp::getcurhllsym('GLOBAL');
nqp::usecompileehllconfig() if $boot_mode;
nqp::loadbytecode($path);
nqp::usecompilerhllconfig() if $boot_mode;
nqp::bindcurhllsym('GLOBAL', $preserve_global);
unless nqp::defined($*MAIN_CTX) {
nqp::die("Unable to load setting $setting_name; maybe it is missing a YOU_ARE_HERE?");
}
%settings_loaded{$path} := $*MAIN_CTX;
}
$setting := %settings_loaded{$path};
}
$setting;
}
}
# Since this *is* the module loader, we can't locate it the normal way by
# GLOBAL merging. So instead we stash it away in the current HLL directly.
nqp::bindcurhllsym('ModuleLoader', ModuleLoader);