Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

498 lines (327 sloc) 12.844 kb
* 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
}()
Jump to Line
Something went wrong with that request. Please try again.