/
rakudo-library.js
245 lines (181 loc) Β· 5.21 KB
/
rakudo-library.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
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
const nqp = require('nqp-runtime');
let passedArgs;
let oldArgs;
function fakeArgs(isMain) {
return isMain ? passedArgs.map(arg => new nqp.NativeStrArg(arg)) : [];
};
const code = /*async*/ function() {
/*await*/ require('./rakudo.js')(nqp, true);
};
const core = require('nqp-runtime/core.js');
module.exports.compile = function(source, options = {}) {
const oldGlobalContext = nqp.freshGlobalContext();
const oldArgs = nqp.args;
nqp.args = fakeArgs;
const tmp = require('tmp');
const tmpFile = tmp.tmpNameSync();
passedArgs = ['perl6-js', '--output', tmpFile, '--target=js', source];
if (options.sourceMap) {
passedArgs.splice(1, 0, '--source-map');
}
if (Object.prototype.hasOwnProperty.call(nqp.op.getstdout(), '$$writefh')) {
throw `Can't overwrite $$writefh on stdout, it's already set`;
}
const output = [];
nqp.op.getstdout().$$writefh = function(buf) {
output.push(core.toRawBuffer(buf));
}
if (options.rakudoPrecompWith) {
const oldValue = options.rakudoPrecompWith;
process.env.RAKUDO_PRECOMP_WITH = options.rakudoPrecompWith;
code();
process.env.RAKUDO_PRECOMP_WITH = oldValue;
} else {
code();
}
delete nqp.op.getstdout().$$writefh;
const lines = Buffer.concat(output).toString().split(/\n/);
const loaded = [];
for (const line of lines) {
let match;
if (/^[A-Z0-9]{40}\0/.test(line)) {
} else if (match = line.match(/^LOAD-UNIT ID:(.*?) DEPS:(.*?) PATH:(.*)/)) {
const deps = match[2] == '' ? [] : match[2].split(',');
loaded.push({id: match[1], deps: deps, path: match[3]});
} else {
// console.warn('extra line', line);
}
}
nqp.args = oldArgs;
nqp.setGlobalContext(oldGlobalContext);
const fs = require('fs');
const returnValue = {js: fs.readFileSync(tmpFile, 'utf8'), loaded: loaded};
if (options.sourceMap) {
returnValue.sourceMap = JSON.parse(fs.readFileSync(tmpFile + '.map', 'utf8'));
}
return returnValue;
};
module.exports.capturedRun = /*async*/ function(source, input, compileArgs, args, passedEnv) {
const oldGlobalContext = nqp.freshGlobalContext();
const env = nqp.hash();
const pid = core.randomInt()[0];
const oldGetpid = nqp.op.getpid;
nqp.op.getpid = function() {
return pid;
};
passedEnv.content.forEach((value, key, map) => {
env.content.set(key, new nqp.NQPStr(value.$$getStr()));
});
const oldArgs = nqp.args;
nqp.args = fakeArgs;
const oldGetEnvHash = nqp.op.getenvhash;
nqp.op.getenvhash = function() {
return env;
};
const out = [];
const err = [];
passedArgs = ['perl6-js'].concat(compileArgs, ['*SOURCE*'], args);
const oldExit = nqp.op.exit;
class Exit {
constructor(status) {
this.status = status;
}
}
nqp.op.exit = function(status) {
throw new Exit(status);
};
if (Object.prototype.hasOwnProperty.call(nqp.op.getstdout(), '$$writefh')) {
throw `Can't overwrite $$writefh on stdout, it's already set`;
}
nqp.op.getstdout().$$writefh = function(buf) {
out.push(core.toRawBuffer(buf));
};
if (Object.prototype.hasOwnProperty.call(nqp.op.getstderr(), '$$writefh')) {
throw `Can't overwrite $$writefh on stderr, it's already set`;
}
nqp.op.getstderr().$$writefh = function(buf) {
err.push(core.toRawBuffer(buf));
};
const oldOpen = nqp.op.open;
class ReadFromStringHandle {
constructor(source, flag) {
this.source = Buffer.from(source, 'utf8');
}
$$readfh(buf, bytes) {
if (this.flag) console.log('$$readfh');
core.writeBuffer(buf, 0, this.source.slice(0, bytes));
this.source = this.source.slice(bytes);
return buf;
}
$$eoffh() {
if (this.flag) console.log('$$eoffh');
return (this.source.length === 0 ? 1 : 0);
}
$$closefh() {
if (this.flag) console.log('$$closefh');
}
$$decont(ctx) {
return this;
}
$$isttyfh() {
return 0;
}
$$setbuffersizefh(size) {
return this;
}
$$can(ctx, name) {
return 0;
}
$$toBool(ctx) {
return 1;
}
};
const oldGetstdin = nqp.op.getstdin;
const fakeStdin = new ReadFromStringHandle(input, true);
nqp.op.getstdin = function() {
return fakeStdin;
};
nqp.op.open = function(name, mode) {
if (name === '*SOURCE*') {
return new ReadFromStringHandle(source);
} else {
return oldOpen(name, mode);
}
};
const oldStat = nqp.op.stat;
const EXISTS = 0;
const ISDIR = 2;
nqp.op.stat = function(file, code) {
if (file === '*SOURCE*' && code === EXISTS) {
return 1;
} else if (file === '*SOURCE*' && code === ISDIR) {
return 0;
} else {
return oldStat(file, code);
}
};
let status = 0;
try {
/*await*/ code();
} catch (e) {
if (e instanceof Exit) {
status = e.status;
} else {
throw e;
}
}
delete nqp.op.getstdout().$$writefh;
delete nqp.op.getstderr().$$writefh;
nqp.op.exit = oldExit;
nqp.op.open = oldOpen;
nqp.args = oldArgs;
nqp.op.getenvhash = oldGetEnvHash;
nqp.op.getpid = oldGetpid;
nqp.op.getstdin = oldGetstdin;
nqp.setGlobalContext(oldGlobalContext);
return {
status: status,
out: Buffer.concat(out).toString(),
err: Buffer.concat(err).toString()
};
};