-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPerl6Asset.js
191 lines (144 loc) · 5.25 KB
/
Perl6Asset.js
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
'use strict';
const Asset = require('parcel-bundler/src/Asset');
const SourceMap = require('parcel-bundler/src/SourceMap');
const lineCounter = require('parcel-bundler/src/utils/lineCounter');
const fs = require('fs');
const path = require('path');
const nqpRuntimePath = 'nqp-browser-runtime';
const rakudoLibrary = require('rakudo/rakudo-library.js');
function insertAfter(whole, where, what) {
return whole.replace(where, (match, offset, string) => where + what);
}
function stripSourceMappingUrl(js) {
return js.substring(0, js.lastIndexOf('//# sourceMappingURL'));
}
module.exports = class Perl6Asset extends Asset {
async getDependencies() {
this.addDependency(nqpRuntimePath + '/runtime.nqp-raw-runtime');
}
constructor(name, options) {
super(name, options);
this.type = 'js';
}
requireNqp() {
return `require('${nqpRuntimePath}/runtime.nqp-raw-runtime')`;
}
fixRuntime(js) {
js = js.replace(/require\("nqp-runtime"\)/g, this.requireNqp());
return js;
}
async generate() {
const config = await this.getConfig(['.rakudorc']);
const options = {};
options.sourceMap = this.options.sourceMaps;
let libs = [];
if (config && config.lib) {
const dirs = config.lib.map(dir => path.isAbsolute(dir)
? dir
: path.resolve(this.options.rootDir, dir));
libs = libs.concat(dirs, libs);
}
if (config && config['use-lib-hack']) {
const base = path.dirname(path.dirname(this.name));
const regex = /### USE LIB \$\?FILE\.IO\.parent\(2\)\.(?:add|child)[(:]\s*["']([^"']+)/;
const matches = this.contents.match(new RegExp(regex, 'g'));
if (matches) {
for (const use of matches) {
libs.unshift(path.join(base, use.match(regex)[1]));
}
}
}
if (libs.length) {
options.rakudoPrecompWith = libs.map(dir => 'filerecording#' + dir).join(',');
}
let sourceMap;
let compiled;
try {
compiled = rakudoLibrary.compile(this.name, options);
sourceMap = compiled.sourceMap;
} catch (e) {
console.log('error', e);
throw e;
}
let js = compiled.js;
if (sourceMap) {
sourceMap.sources = [this.relativeName];
sourceMap.sourcesContent = [this.contents];
js = stripSourceMappingUrl(js);
}
js = this.fixRuntime(js);
/* HACKS */
js = insertAfter(
js,
'var ctxWithPath = new nqp.Ctx(null, null, null);',
'(/*await*/ nqp.op.loadbytecode(ctxWithPath,"Perl6-World"));'
+ 'nqp.op.bindhllsym("perl6","progname", new nqp.NQPStr(' + JSON.stringify(this.name) + '));'
+ '(/*await*/ nqp.op.loadbytecode(ctxWithPath,"load-compiler"));'
+ 'nqp.afterRun = {hll: "perl6", sym: "&THE_END"};'
+ 'nqp.op.bindhllsym("perl6","@END_PHASERS",nqp.list(nqp.getHLL("nqp"),[]));'
+ '/*await*/ loadedDuringCompile(nqp, ctxWithPath);'
);
const loadedJS = [];
const paths = [];
const loaded = {};
for (const dep of compiled.loaded) {
loaded[dep.id] = dep;
}
const seen = {};
function dfs(id) {
if (seen[id]) return;
seen[id] = true;
for (const dep of loaded[id].deps) {
if (!loaded[dep]) {
console.log('missing dep', dep);
} else {
dfs(dep);
}
}
paths.push(loaded[id].path);
}
for (const id in loaded) {
dfs(id);
}
for (const path of paths) {
const compUnitWithJS = stripSourceMappingUrl(fs.readFileSync(path, 'utf8')).split(/\n/);
let skip = 0;
while (compUnitWithJS[skip] != '' && skip < compUnitWithJS.length) {
skip++;
}
compUnitWithJS.splice(0, skip+1);
const sourceMap = JSON.parse(fs.readFileSync(path + '.bc.map', 'utf8'));
if (!sourceMap.sourcesContent) sourceMap.sourcesContent = [];
for (let i = 0; i < sourceMap.sources.length; i++) {
sourceMap.sourcesContent[i] = fs.readFileSync(sourceMap.sources[i].replace(/ \([^()]*\)$/, ''), 'utf8');
}
loadedJS.push({
sourceMap: sourceMap,
js: '/*await*/ nqp.loadCompileTimeDependency(function(module) {' + this.fixRuntime(compUnitWithJS.join('\n')) + '});'
});
}
const prelude =
'{' +
'const nqp = ' + this.requireNqp() + ';' +
'nqp.extraRuntime("perl6", "nqp-browser-runtime/perl6-runtime.nqp-raw-runtime");' +
'};';
js = 'require.main = module;' + js;
const combinedSourceMap = new SourceMap();
let loadAllDeps = prelude + '/*async*/ function loadedDuringCompile(nqp, $$outer) {\n';
for (const dependency of loadedJS) {
combinedSourceMap.addMap(
dependency.sourceMap,
lineCounter(loadAllDeps) - 1
);
loadAllDeps = loadAllDeps + dependency.js;
}
loadAllDeps = loadAllDeps + '}\n';
combinedSourceMap.addMap(
sourceMap,
lineCounter(loadAllDeps) - 1
);
js = loadAllDeps + js;
return {js: js, map: combinedSourceMap};
}
};
module.exports.type = 'nqp';