forked from fglock/Perlito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README-perlito5-js
497 lines (327 loc) · 12.5 KB
/
README-perlito5-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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
* Perlito5-in-Javascript - what works
- Subroutines, anonymous subroutines, prototypes, namespaces.
"main", "CORE" and "CORE::GLOBAL".
- Scalar, Hash, Array, Code should work the same as in perl.
- Objects should work the same as in perl.
- Scalar vs. List context, wantarray().
- Variables.
"my", "our", "local".
Most special variables.
Most variable declarations.
- Most control structures.
"next", "last", "redo", "continue" and labels.
- Most string, numeric, array, hash operators.
- Some I/O operators.
- Some regex operators.
- Tie array.
- AUTOLOAD.
* Perlito5-in-Javascript differences from "perl"
- "~~" smartmatch is not implemented
- "sleep" is not implemented
- "state" declaration is not implemented yet.
- Perlito5 in the browser doesn't implement many of the perl I/O operators;
Perlito5 in node.js should implement most of the I/O operators.
- Javascript doesn't implement reference counting;
DESTROY will not work;
weaken() will be a no-op.
- Overload is not implemented yet.
- XS is not supported.
- Variable aliasing is not implemented yet;
modifying $_ or $_[0] doesn't change the original variable.
- Variable redeclaration is not implemented yet.
- Unicode is not implemented yet.
- Regex is missing some features, such as /e and /x modifiers.
- Control structures are partially implemented;
"goto LABEL" is not supported.
"given" is not implemented.
"goto &sub" works, but tail call is not implemented yet.
- String pos() is not implemented yet.
- Signals are not implemented yet.
- BEGIN blocks are partially implemented.
- Typeglobs are partially implemented.
- tie() is partially implemented.
- String increment partially implemented - array and hash lookups; not in scalars.
* Perlito5 compiler globals
- context: system-wide; shared by all modules
$Perlito5::CORE_PROTO = { 'CORE::join' => '$@' }
- hashref with subroutine-full-name to prototype mappings in the CORE package only
$Perlito5::PROTO = { 'main::mysub' => undef }
- hashref with subroutine-full-name to prototype mappings
- context: module-wide
$Perlito5::PKG_NAME = "main"
- the current package name (unescaped string)
- context: subroutine-wide
$Perlito5::THROW = 1
- boolean value; tracks if the current subroutine needs to catch a javascript "throw" as a return-value
- context: lexical block
$Perlito5::VAR = [ { '$_' => { decl => 'our', namespace => 'main' } ]
- arrayref-of-hashes with variable-short-names to (declaration type, namespace) mappings
* Perlito5 Javascript Data model Overview
method call
- lookup the method in the _class_ attribute; follow the class hierarchy through @ISA until the UNIVERSAL base class.
- do a native call on the method.
- the argument list is the named array "List__" (that is, "@_").
- additional arguments can be used to pass out-of-band data, such as: caller(), wantarray()
- the invocant is the first argument. "this" is not used.
subroutine call
- lookup the subroutine in the current namespace; follow the hierarchy until the CORE base class.
- do a native method call.
- the argument list is the named array "List__" (that is, "@_").
- additional arguments can be used to pass out-of-band data, such as: caller(), wantarray()
- "this" is not used.
Hash
- native {} with perl5-specific getters and setters
Array
- native [] with perl5-specific getters and setters
Scalar
- native value
Tied containers
- javascript object with perl5-specific getters and setters
HashRef
- native {} wrapped in a "HashRef" object
ArrayRef
- native [] wrapped in a "ArrayRef" object
ScalarRef
- native value wrapped in a "ScalarRef" object
CodeRef
- native value
Object
- one of the reference types, with a _class_ attribute
Class
- classes are stored in the CLASS global hash
- the _class_ attribute points to the class itself
- classes inherit from UNIVERSAL
Namespace
- namespaces are stored in the p5pkg global hash
- current namespace object is in the PKG variable
- Namespace inherits from from CORE::GLOBAL, which inherits from CORE
- Namespace is a copy of the Class, but with a different inheritance
- Namespace and Class are updated in parallel, both when a sub is declared or when using typeglob assignment
Calling context ("wantarray", caller)
- TODO
- these are the possible compile-time contexts:
'scalar'
'list'
'void'
'runtime' (unknown)
'str'
'bool'
'num'
- at run-time the contexts are in wantarray ("p5want" variable):
1
0
undef
Alias
- TODO
- using String/Boolean/Number (boxed types) as SCALARs doesn't seem to work
List (eg. subroutine return value)
- native []
eval string
- the compiler gets the current namespace as an argument
- javascript eval() happens at the current runtime lexical context
eval block
- ...
do block
- ...
AUTOLOAD
- ...
my
- ...
local
- ...
our
- ...
* Javascript resources
https://github.com/eriwen/javascript-stacktrace
- how to get a stacktrace in browsers
https://github.com/audreyt/pugs/tree/master/perl5/PIL2JS
- Pugs Perl6 in javascript
* Regex
- regex extension library: http://xregexp.com
- modifiers: g i m
- slashes must be escaped
- From http://www.regular-expressions.info/javascript.html
No \A or \Z anchors to match the start or end of the string. Use a caret or dollar instead.
Lookbehind is not supported at all. Lookahead is fully supported.
No atomic grouping or possessive quantifiers
No Unicode support, except for matching single characters with
No named capturing groups. Use numbered capturing groups instead.
No mode modifiers to set matching options within the regular expression.
No conditionals.
No regular expression comments.
* Compile-time / Run-time interleaving (TODO)
- See perlref:
named subroutines are created at compile time so their lexical variables
get assigned to the parent lexicals from the first execution of the parent
block.
If a parent scope is entered a second time, its lexicals are created again,
while the nested subs still reference the old ones.
---
$ perl -e ' use strict; { my $x = 3; sub z { 123 } BEGIN { print "$x ", z, "\n" } INIT { $x = 4 } print "$x\n" } '
123
3
---
open anonymous block in the compiling environment
add incomplete block to the AST
add variable my $x to the AST
add variable my $x to the compiling environment
open named sub in the compiling environment
add incomplete sub to the AST
close named sub in the compiling environment
add sub to the AST
compile and run BEGIN block in the compiling environment
# add BEGIN side-effects to the AST
compile INIT block
add INIT block to the AST
compile print
add print to the AST
close anonymous block
add block to the AST
(sub {
my $x = 3;
$NAMESPACE::z = sub { 123 }; # named sub
push @COMPILING::RUN, sub { 1 }; # BEGIN block result
push @COMPILING::INIT, sub { $x = 4 }; # INIT block
push @COMPILING::RUN, sub { print "$x\n" };
})->();
$_->() for @COMPILING::INIT;
$_->() for @COMPILING::RUN;
---
$ perl -e ' use strict; my $y = 123; sub x { my $x = 3; sub z { $y } BEGIN { print "$x ", z, "\n" } INIT { $x = 4 } print "$x\n" } '
---
(sub {
my $y = 123;
(sub {
my $x = 3;
$NAMESPACE::z = sub { 123 }; # named sub
1; # BEGIN block result
push @COMPILING::INIT, sub { $x = 4 }; # INIT block
})->();
$NAMESPACE::x = sub {
my $x = 3;
1; # BEGIN block result
print "$x\n";
};
})->();
$_->() for @COMPILING::INIT;
$_->() for @COMPILING::RUN;
- disambiguation between block and hash should not backtrack, because any internal special blocks would be compiled/run twice
- anonymous blocks, named subroutines and variables must be instantiated at compile-time
* Threads (TODO)
* Reference counting (TODO)
- add a new attribute _cnt_ to all references
- lexicals, locals decrement the count when going out of scope
- call DESTROY when count reaches zero
* Cell-based aliasing (TODO)
- slow
- allows aliasing (rw parameters to functions)
- allows "tie", because collection access is done through methods
- simplifies autovivification
- allows lvalue subroutines, such as chop(), chomp(), keys(), pos(), substr(), undef()
- allows "our"
- examples:
v = new Cell();
v.set(5);
f(v); // f gets a copy of the cell; v.set() inside f() modifies the original variable.
1 + v; // calls v.valueOf()
x = v; // alias (copies the cell); v.set() modifies x.valueOf()
x.set( v.valueOf() ); // copies the value (doesn't alias)
h.lookup("x"); // looks up h["x"] for a cell; autovivifies if needed
v.lookup("x"); // error if the cell in v contains something else than undef or an arrayref
- see mp6_Scalar class in src6/lib/Perlito/Python/Runtime.py
* Tail call (TODO)
- a tail call can be transformed into a loop at the caller:
# sub mysub { goto &other }
can be called:
ret = new TailCall(mysub);
do {
ret = ret.f();
}
while (ret instanceof TailCall);
- alternately, the loop can be run at the subroutine itself, but this creates other problems
* "js3" virtual machine
- "js3" milestones (TODO list)
- timely destruction (depends on reference counting); weaken()
- tie()
- Overload
- variable aliasing ($_[0], for-loop, map)
- variable redeclaration
- make perlito usable for CPAN smoke tests:
node perlito5.js Makefile.PL
make test
- possible implementation of lexical variables
This allows better control over memory allocation (for example, to implement destructors and aliasing)
// lexical variables
function p5env_001 () {};
var p5env = p5env_001;
p5env.a = 3;
function myfun () {
var p5env_002 = function () {};
p5env_002.prototype = p5env_001;
var p5env = new p5env_002();
p5env.b = 4;
process.stdout.write( "" + p5env.a + " " + p5env.b + "\n" );
p5env.a = 5;
process.stdout.write( "" + p5env.a + " " + p5env.b + "\n" );
}
myfun();
process.stdout.write( "" + p5env.a + " " + p5env.b + "\n" );
@_ is special:
$_[n] lvalue can be represented by
at_env[n][at_var[n]] = ...
subroutine call:
mysub( [
env, "var", // a variable
env.arr, 0, // a subscript
[ 123 ], 0 // a value
], context );
lvalue subroutine call:
??? - maybe use 'context' to force return of a settable object
tied containers:
Tie::Scalar magic can be implemented with getters/setters in env
p5env.b = 4; // call b setter if there is a setter
Tie::Array, Tie::Hash
p5env.list_b[0] = 4; // ??? - but this works if we use a method to get/set the variable
problem: tie'ing a variable in the outer scope (p5env_001) using defineProperty() would be
inherited by the inner scope (p5env_002 in the example above).
workaround: access variables from the outer scope directly, without using inheritance.
This can be complicated by statements like { my $v = 0 if $x }
which create lexicals dynamically - but the behaviour in this case is undefined anyway.
defineProperty() can be used to provide accessors to non-tied containers:
Object.defineProperty( Array.prototype, "p5aget", {
enumerable : false,
value : function (i) { return this[i] }
});
Object.defineProperty( Array.prototype, "p5aset", {
enumerable : false,
value : function (i, v) { this[i] = v; return this[i] }
});
Object.defineProperty( Object.prototype, "p5hget", {
enumerable : false,
value : function (i) { return this[i] }
});
Object.defineProperty( Object.prototype, "p5hset", {
enumerable : false,
value : function (i, v) { this[i] = v; return this[i] }
});
b = [5,6,8];
b.p5aset(2, 13);
process.stdout.write( " " + b.p5aget(2) + "\n" );
h = { x : 4, y : 7 };
h.p5hset("x", 13);
process.stdout.write( " " + h.p5hget("x") + "\n" );
- Alternative implementation for lvalue @_ and tail calls
// calling function x()
// the variables (a,b,c) are lexicals aliased to $_[0], $_[1], $_[2]
// .mod signals that @_ was modified
// .at returns @_
// .res is the funtion result
// .tail is a tail-call result flag
function(){
r = x([a,b,c], want);
if (r.mod) { a=r.at[0]; b=r.at[1]; c=r.at[2] };
while (r.tail) {
// do tail calls
}
return r.res
}()