-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
runtime.js
433 lines (353 loc) Β· 12 KB
/
runtime.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
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
431
432
433
module.exports.load = function(nqp, CodeRef, Capture, containerSpecs) {
const Null = nqp.Null;
let op = {};
let Scalar, True, False, Int, Num, Str, Code, Mu, Any, ContainerDescriptor, Routine;
let Iterable;
let defaultContainerDescriptor;
op.p6settypes = function(types) {
Scalar = types.content.get('Scalar');
True = types.content.get('True');
False = types.content.get('False');
Int = types.content.get('Int');
Num = types.content.get('Num');
Str = types.content.get('Str');
Code = types.content.get('Code');
Mu = types.content.get('Mu');
Any = types.content.get('Any');
Nil = types.content.get('Nil');
Routine = types.content.get('Routine');
ContainerDescriptor = types.content.get('ContainerDescriptor');
Signature = types.content.get('Signature');
defaultContainerDescriptor = ContainerDescriptor._STable.REPR.allocate(ContainerDescriptor._STable);
defaultContainerDescriptor.$$bindattr(ContainerDescriptor, '$!of', Mu);
defaultContainerDescriptor.$$bindattr_s(ContainerDescriptor, '$!name', "<element>");
defaultContainerDescriptor.$$bindattr_i(ContainerDescriptor, '$!rw', 1);
defaultContainerDescriptor.$$bindattr(ContainerDescriptor, '$!default', Any);
return types;
};
op.p6bool = function(value) {
return value ? True : False;
};
op.p6definite = function(obj) {
return (obj === Null || obj.typeObject_) ? False : True;
};
op.p6typecheckrv = function(ctx, rv, routine, bypassType) {
const sig = routine.$$getattr(Code, '$!signature');
const rtype = sig.$$getattr(Signature, '$!returns');
if (rtype !== Null && nqp.op.objprimspec(rtype) === 0) {
const decontValue = rv.$$decont(ctx);
if (decontValue.$$istype(ctx, rtype) === 0 && decontValue.$$istype(ctx, bypassType) === 0) {
const thrower = getThrower("X::TypeCheck::Return");
if (thrower === Null) {
ctx.die("Type check failed for return value");
} else {
thrower.$$call(ctx, null, decontValue, rtype);
}
}
}
return rv;
};
op.p6setfirstflag = function(codeObj) {
firstPhaserCodeBlock = codeObj.$$getattr(Code, "$!do");
return codeObj;
};
op.p6takefirstflag = function(ctx) {
const matches = firstPhaserCodeBlock === ctx.codeRef();
firstPhaserCodeBlock = Null;
return matches ? 1 : 0;
};
const prePhaserFrames = [];
op.p6setpre = function(ctx) {
prePhaserFrames.push(ctx);
return Null;
};
op.p6clearpre = function(ctx) {
const index = prePhaserFrames.indexOf(ctx);
if (index !== -1) {
prePhaserFrames.splice(index, 1);
}
return Null;
};
op.p6inpre = function(ctx) {
const index = prePhaserFrames.indexOf(ctx.$$caller);
if (index !== -1) {
prePhaserFrames.splice(index, 1);
return 1;
} else {
return 0;
}
};
function isRWScalar(check) {
if (check._STable === Scalar._STable && !check.typeObject_) {
let desc = check.$$getattr(Scalar, '$!descriptor');
if (desc === Null) {
return false;
}
return desc.$$getattr_i(ContainerDescriptor, '$!rw') !== 0;
}
return false;
}
op.p6decontrv = function(routine, cont) {
if (isRWScalar(cont)) {
let isRW = routine.$$getattr_i(Routine, '$!rw');
if (isRW === 0) {
let roCont = Scalar._STable.REPR.allocate(Scalar._STable);
roCont.$$bindattr(Scalar, '$!value', cont.$$decont(null));
return roCont;
}
} else if (cont._STable && cont._STable.REPR instanceof nqp.NativeRef) {
let isRW = routine.$$getattr_i(Routine, '$!rw');
if (isRW === 0) {
return cont.$$decont();
}
}
return cont;
};
op.p6box_i = function(int) {
var repr = Int._STable.REPR;
var boxed = repr.allocate(Int._STable);
boxed.$$setInt(int);
return boxed;
};
op.p6box_n = function(num) {
var repr = Num._STable.REPR;
var boxed = repr.allocate(Num._STable);
boxed.$$setNum(num);
return boxed;
};
op.p6box_s = function(str) {
var repr = Str._STable.REPR;
var boxed = repr.allocate(Str._STable);
boxed.$$setStr(str);
return boxed;
};
op.p6captureouters2 = function(ctx, capList, target) {
var cf = target.outerCtx;
if (cf === Null) {
return capList;
}
var elems = capList.$$elems();
for (var i = 0; i < elems; i++) {
var codeObj = capList.$$atpos(i);
var closure = codeObj.$$getattr(Code, "$!do");
var ctxToDiddle = closure.outerCtx;
if (ctxToDiddle) {
ctxToDiddle.$$outer = cf;
} else {
console.log("can't diddle", closure);
}
}
return capList;
};
op.p6capturelex = function(ctx, codeObj) {
var closure = codeObj.$$getattr(Code, "$!do");
var wantedStaticInfo = closure.staticCode.outerCodeRef;
if (ctx.codeRef().staticCode === wantedStaticInfo) {
closure.outerCtx = ctx;
closure.capture(closure.staticCode.freshBlock());
} else {
/* HACK - workaround for rakudo bugs */
//console.log("HORRIBLE hack - p6capturelex will do nothing");
}
return codeObj;
};
op.p6capturelexwhere = function(ctx, codeObj) {
var closure = codeObj.$$getattr(Code, "$!do");
var wantedStaticInfo = closure.staticCode.outerCodeRef;
var find = ctx;
while (find) {
if (find.codeRef().staticCode === wantedStaticInfo) {
closure.outerCtx = find;
closure.capture(closure.staticCode.freshBlock());
return codeObj;
}
find = find.$$caller;
}
// console.log("HORRIBLE hack - p6capturelexwhere will do nothing");
return codeObj;
};
op.p6var = function(cont) {
if (cont.$$iscont && cont.$$iscont()) {
var wrapper = Scalar._STable.REPR.allocate(Scalar._STable);
wrapper.$$bindattr(Scalar, '$!value', cont);
return wrapper;
} else {
return cont;
}
}
op.p6bindassert = function(ctx, value, type) {
if (type !== Mu) {
if (value.$$decont(ctx).$$istype(ctx, type) == 0) {
const thrower = getThrower("X::TypeCheck::Binding");
if (thrower === null) {
ctx.die("Type check failed in binding");
} else {
thrower.$$call(ctx, null, value, type);
}
}
}
return value;
};
op.p6setitertype = function(type) {
Iterable = type;
return type;
};
op.p6store = function(ctx, cont, value) {
if (cont.$$assign) {
cont.$$assign(ctx, value.$$decont(ctx));
} else {
if (!cont.STORE) {
// TODO throw typed exception X::Assignment::RO
ctx.die("Cannot assign to a non-container");
} else {
cont.STORE(ctx, null, cont, value);
}
}
return cont;
};
const p6HLL = nqp.getHLL('perl6');
op.p6argvmarray = function(ctx, args) {
var array = [];
for (var i=2; i < args.length; i++) {
array[i-2] = nqp.op.hllizefor(ctx, nqp.arg(p6HLL, args[i]), 'perl6');
}
return nqp.createArray(array);
};
op.p6recont_ro = function(cont) {
if (isRWScalar(cont)) {
let roCont = Scalar._STable.REPR.allocate(Scalar._STable);
roCont.$$bindattr(Scalar, '$!value', cont.$$decont(null));
return roCont;
}
return cont;
};
op.p6scalarfromdesc = function(desc) {
if (desc === Null || desc.typeObject_)
desc = defaultContainerDescriptor;
let defVal = desc.$$getattr(ContainerDescriptor, '$!default');
let cont = Scalar._STable.REPR.allocate(Scalar._STable);
cont.$$bindattr(Scalar, '$!descriptor', desc);
cont.$$bindattr(Scalar, '$!value', defVal);
return cont;
};
op.p6decodelocaltime = function(sinceEpoch) {
let date = new Date(sinceEpoch * 1000);
return nqp.createArray([
date.getSeconds(),
date.getMinutes(),
date.getHours(),
date.getDate(),
date.getMonth()+1,
date.getFullYear()
]);
}
op.p6finddispatcher = function(ctx, usage) {
let dispatcher;
let search = ctx.$$caller;
while (search) {
/* Do we have a dispatcher here? */
if (search.hasOwnProperty(["$*DISPATCHER"])) {
dispatcher = search["$*DISPATCHER"];
if (dispatcher.typeObject_) {
dispatcher = dispatcher.vivify_for(ctx, null, dispatcher, search.codeRef().codeObj, search, new Capture(search.$$args[1], Array.prototype.slice.call(search.$$args, 2)));
search["$*DISPATCHER"] = dispatcher;
}
return dispatcher;
}
search = search.$$caller;
}
/* TODO - throw X::NoDispatcher */
throw usage + ' is not in the dynamic scope of a dispatcher';
};
op.p6argsfordispatcher = function(ctx, dispatcher) {
let search = ctx;
while (search) {
/* Do we have the dispatcher we're looking for? */
if (search['$*DISPATCHER'] === dispatcher) {
return new Capture(search.$$args[1], Array.prototype.slice.call(search.$$args, 2));
}
/* Follow dynamic chain. */
search = search.$$caller;
}
throw 'Could not find arguments for dispatcher';
};
op.p6sink = function(ctx, obj) {
if (obj.typeObject_ || obj === Null) return;
if (obj.$$can(ctx, 'sink')) {
obj.sink(ctx, null, obj);
}
};
op.p6staticouter = function(ctx, codeRef) {
if (!(codeRef instanceof CodeRef)) throw new nqp.NQPException("p6staticouter must be used on a CodeRef");
return codeRef.staticCode.outerCodeRef;
};
function RakudoScalar(STable) {
this.STable = STable;
}
RakudoScalar.prototype.configure = function(conf) {
this.setupSTable();
};
function getThrower(type) {
let exHash = nqp.op.gethllsym("perl6", "P6EX");
return (exHash === Null ? Null : exHash.$$atkey(type));
}
RakudoScalar.prototype.setupSTable = function() {
this.STable.addInternalMethod('$$assignunchecked', function(ctx, value) {
let whence = this.$$getattr(Scalar, '$!whence');
if (whence !== Null) whence.$$call(ctx, null);
this.$$bindattr(Scalar, '$!value', value);
});
this.STable.addInternalMethod('$$assign', function(ctx, maybeValue) {
/* TODO - checking */
let desc = this.$$getattr(Scalar, '$!descriptor');
let rw = desc === Null ? 0 : desc.$$getattr_i(ContainerDescriptor, '$!rw');
if (!rw) {
if (desc === Null) {
let name = desc.$$getattr_s(ContainerDescriptor, '$!name');
ctx.die("Cannot assign to a readonly variable (" + name + ") or a value");
} else {
ctx.die("Cannot assign to a readonly variable or a value");
}
}
let value;
if (maybeValue._STable && maybeValue._STable.WHAT === Nil) {
value = desc.$$getattr(ContainerDescriptor, '$!default');
} else {
value = maybeValue;
let of = desc.$$getattr(ContainerDescriptor, '$!of');
if (of !== Mu && value.$$istype(ctx, of)=== 0) {
let name = desc.$$getattr_s(ContainerDescriptor, '$!name');
let thrower = getThrower("X::TypeCheck::Assignment");
if (thrower === null) {
ctx.die("Type check failed in assignment to '" + name + "'");
} else {
thrower.$$call(ctx, null, name, value, of);
}
}
}
let whence = this.$$getattr(Scalar, '$!whence');
if (whence !== Null) whence.$$call(ctx, null);
this.$$bindattr(Scalar, '$!value', value);
});
this.STable.addInternalMethod('$$decont', function(ctx) {
return this.$$getattr(Scalar, '$!value');
});
this.STable.addInternalMethod('$$iscont', function() {
return 1;
});
this.STable.addInternalMethod('$$isrwcont', function() {
if (this.typeObject_) return 0;
let desc = this.$$getattr(Scalar, '$!descriptor');
return desc === Null ? 0 : desc.$$getattr_i(ContainerDescriptor, '$!rw');
});
};
RakudoScalar.prototype.serialize = function(cursor) {
/* No data to serialize. */
};
RakudoScalar.prototype.deserialize = function(cursor) {
/* No data to deserialize. */
};
RakudoScalar.prototype.name = 'rakudo_scalar';
containerSpecs.rakudo_scalar = RakudoScalar;
nqp.loadOps({op: op});
};