Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Perlito5 - js - move Array implementation to a separate package

  • Loading branch information...
commit b93e775746c79d4a054d4d50dbda021da04e579a 1 parent 4f2b998
@fglock authored
View
524 html/perlito5.js
@@ -228,6 +228,8 @@ function p5cleanup_local(idx, value) {
return value;
}
+//-------- Reference
+
function p5HashRef(o) {
this._hash_ = o;
this._ref_ = "HASH";
@@ -252,258 +254,6 @@ function p5GlobRef(o) {
this.bool = function() { return 1 };
}
-
-//-------- Array
-
-Object.defineProperty( Array.prototype, "p5aget", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- return this[i]
- }
-});
-Object.defineProperty( Array.prototype, "p5aset", {
- enumerable : false,
- value : function (i, v) {
- if (i < 0) { i = this.length + i };
- this[i] = v;
- return this[i]
- }
-});
-
-Object.defineProperty( Array.prototype, "p5incr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- this[i] = p5incr(this[i]);
- return this[i];
- }
-});
-Object.defineProperty( Array.prototype, "p5postincr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- var v = this[i];
- this[i] = p5incr(this[i]);
- return v;
- }
-});
-Object.defineProperty( Array.prototype, "p5decr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- this[i] = p5decr(this[i]);
- return this[i];
- }
-});
-Object.defineProperty( Array.prototype, "p5postdecr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- var v = this[i];
- this[i] = p5decr(this[i]);
- return v;
- }
-});
-
-Object.defineProperty( Array.prototype, "p5aget_array", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- if (this[i] == null) { this[i] = new p5ArrayRef([]) }
- return this[i]
- }
-});
-Object.defineProperty( Array.prototype, "p5aget_hash", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- if (this[i] == null) { this[i] = new p5HashRef({}) }
- return this[i]
- }
-});
-Object.defineProperty( Array.prototype, "p5unshift", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = args.length-1; i >= 0; i--) {
- this.unshift(args[i]);
- }
- return this.length;
- }
-});
-Object.defineProperty( Array.prototype, "p5push", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = 0; i < args.length; i++) {
- this.push(args[i]);
- }
- return this.length;
- }
-});
-
-p5tie_array = function(v, List__) {
- var pkg_name = p5str(List__.shift());
-
- var res = p5call(pkg_name, 'TIEARRAY', List__, null);
-
- // TODO
-
- // A class implementing an ordinary array should have the following methods:
- // TIEARRAY pkg_name, LIST
- // FETCH this, key
- // STORE this, key, value
- // FETCHSIZE this
- // STORESIZE this, count
- // CLEAR this
- // PUSH this, LIST
- // POP this
- // SHIFT this
- // UNSHIFT this, LIST
- // SPLICE this, offset, length, LIST
- // EXTEND this, count
- // DESTROY this
- // UNTIE this
-
- Object.defineProperty( v, "p5aget", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- return p5call(res, 'FETCH', [i]);
- }
- });
- Object.defineProperty( v, "p5aset", {
- enumerable : false,
- configurable : true,
- value : function (i, value) {
- p5call(res, 'STORE', [i, value]);
- return value;
- }
- });
- Object.defineProperty( v, "p5incr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5incr(p5call(res, 'FETCH', [i]));
- p5call(res, 'STORE', [i, value]);
- return value;
- }
- });
- Object.defineProperty( v, "p5postincr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- p5call(res, 'STORE', [i, p5incr(value)]);
- return value;
- }
- });
- Object.defineProperty( v, "p5decr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5decr(p5call(res, 'FETCH', [i]));
- p5call(res, 'STORE', [i, value]);
- return value;
- }
- });
- Object.defineProperty( v, "p5postdecr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- p5call(res, 'STORE', [i, p5decr(value)]);
- return value;
- }
- });
-
- Object.defineProperty( v, "p5aget_array", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- if (value == null) {
- value = new p5ArrayRef([]);
- p5call(res, 'STORE', [i, value]);
- }
- return value;
- }
- });
- Object.defineProperty( v, "p5aget_hash", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- if (value == null) {
- value = new p5HashRef({});
- p5call(res, 'STORE', [i, value]);
- }
- return value;
- }
- });
- Object.defineProperty( v, "p5untie", {
- enumerable : false,
- configurable : true,
- value : function (i) { return p5call(res, 'UNTIE', []) }
- });
- Object.defineProperty( v, "shift", {
- enumerable : false,
- configurable : true,
- value : function () { return p5call(res, 'SHIFT', []) }
- });
- Object.defineProperty( v, "pop", {
- enumerable : false,
- configurable : true,
- value : function () { return p5call(res, 'POP', []) }
- });
- Object.defineProperty( v, "p5unshift", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = args.length-1; i >= 0; i--) {
- p5call(res, 'UNSHIFT', [args[i]]);
- }
- return p5call(res, 'FETCHSIZE', []);
- }
- });
- Object.defineProperty( v, "p5push", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = 0; i < args.length; i++) {
- p5call(res, 'PUSH', [args[i]]);
- }
- return p5call(res, 'FETCHSIZE', []);
- }
- });
-
- return res;
-};
-
-p5untie_array = function(v) {
- if (v.hasOwnProperty('p5untie')) {
- var res = v.p5untie(); // call UNTIE
- delete v.p5aget;
- delete v.p5aset;
- delete v.p5incr;
- delete v.p5postincr;
- delete v.p5decr;
- delete v.p5postdecr;
- delete v.p5aget_array;
- delete v.p5aget_hash;
- delete v.p5untie;
- delete v.shift;
- delete v.pop;
- delete v.p5unshift;
- delete v.p5push;
- return res;
- }
- else {
- return null;
- }
-};
-
//-------- Hash
Object.defineProperty( Object.prototype, "p5hget", {
@@ -971,6 +721,276 @@ perl5_to_js = function( source, namespace, var_env_js, p5want ) {
}
//
+// lib/Perlito5/Javascript2/Runtime.js
+//
+// Runtime for "Perlito" Perl5-in-Javascript2
+//
+// AUTHORS
+//
+// Flavio Soibelmann Glock fglock@gmail.com
+//
+// COPYRIGHT
+//
+// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the same terms as Perl itself.
+//
+// See http://www.perl.com/perl/misc/Artistic.html
+
+//-------- Array
+
+Object.defineProperty( Array.prototype, "p5aget", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ return this[i]
+ }
+});
+Object.defineProperty( Array.prototype, "p5aset", {
+ enumerable : false,
+ value : function (i, v) {
+ if (i < 0) { i = this.length + i };
+ this[i] = v;
+ return this[i]
+ }
+});
+
+Object.defineProperty( Array.prototype, "p5incr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ this[i] = p5incr(this[i]);
+ return this[i];
+ }
+});
+Object.defineProperty( Array.prototype, "p5postincr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ var v = this[i];
+ this[i] = p5incr(this[i]);
+ return v;
+ }
+});
+Object.defineProperty( Array.prototype, "p5decr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ this[i] = p5decr(this[i]);
+ return this[i];
+ }
+});
+Object.defineProperty( Array.prototype, "p5postdecr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ var v = this[i];
+ this[i] = p5decr(this[i]);
+ return v;
+ }
+});
+
+Object.defineProperty( Array.prototype, "p5aget_array", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ if (this[i] == null) { this[i] = new p5ArrayRef([]) }
+ return this[i]
+ }
+});
+Object.defineProperty( Array.prototype, "p5aget_hash", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ if (this[i] == null) { this[i] = new p5HashRef({}) }
+ return this[i]
+ }
+});
+Object.defineProperty( Array.prototype, "p5unshift", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = args.length-1; i >= 0; i--) {
+ this.unshift(args[i]);
+ }
+ return this.length;
+ }
+});
+Object.defineProperty( Array.prototype, "p5push", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = 0; i < args.length; i++) {
+ this.push(args[i]);
+ }
+ return this.length;
+ }
+});
+
+p5tie_array = function(v, List__) {
+ var pkg_name = p5str(List__.shift());
+
+ var res = p5call(pkg_name, 'TIEARRAY', List__, null);
+
+ // TODO
+
+ // A class implementing an ordinary array should have the following methods:
+ // TIEARRAY pkg_name, LIST
+ // FETCH this, key
+ // STORE this, key, value
+ // FETCHSIZE this
+ // STORESIZE this, count
+ // CLEAR this
+ // PUSH this, LIST
+ // POP this
+ // SHIFT this
+ // UNSHIFT this, LIST
+ // SPLICE this, offset, length, LIST
+ // EXTEND this, count
+ // DESTROY this
+ // UNTIE this
+
+ Object.defineProperty( v, "p5aget", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ return p5call(res, 'FETCH', [i]);
+ }
+ });
+ Object.defineProperty( v, "p5aset", {
+ enumerable : false,
+ configurable : true,
+ value : function (i, value) {
+ p5call(res, 'STORE', [i, value]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5incr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5incr(p5call(res, 'FETCH', [i]));
+ p5call(res, 'STORE', [i, value]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5postincr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ p5call(res, 'STORE', [i, p5incr(value)]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5decr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5decr(p5call(res, 'FETCH', [i]));
+ p5call(res, 'STORE', [i, value]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5postdecr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ p5call(res, 'STORE', [i, p5decr(value)]);
+ return value;
+ }
+ });
+
+ Object.defineProperty( v, "p5aget_array", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ if (value == null) {
+ value = new p5ArrayRef([]);
+ p5call(res, 'STORE', [i, value]);
+ }
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5aget_hash", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ if (value == null) {
+ value = new p5HashRef({});
+ p5call(res, 'STORE', [i, value]);
+ }
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5untie", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) { return p5call(res, 'UNTIE', []) }
+ });
+ Object.defineProperty( v, "shift", {
+ enumerable : false,
+ configurable : true,
+ value : function () { return p5call(res, 'SHIFT', []) }
+ });
+ Object.defineProperty( v, "pop", {
+ enumerable : false,
+ configurable : true,
+ value : function () { return p5call(res, 'POP', []) }
+ });
+ Object.defineProperty( v, "p5unshift", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = args.length-1; i >= 0; i--) {
+ p5call(res, 'UNSHIFT', [args[i]]);
+ }
+ return p5call(res, 'FETCHSIZE', []);
+ }
+ });
+ Object.defineProperty( v, "p5push", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = 0; i < args.length; i++) {
+ p5call(res, 'PUSH', [args[i]]);
+ }
+ return p5call(res, 'FETCHSIZE', []);
+ }
+ });
+
+ return res;
+};
+
+p5untie_array = function(v) {
+ if (v.hasOwnProperty('p5untie')) {
+ var res = v.p5untie(); // call UNTIE
+ delete v.p5aget;
+ delete v.p5aset;
+ delete v.p5incr;
+ delete v.p5postincr;
+ delete v.p5decr;
+ delete v.p5postdecr;
+ delete v.p5aget_array;
+ delete v.p5aget_hash;
+ delete v.p5untie;
+ delete v.shift;
+ delete v.pop;
+ delete v.p5unshift;
+ delete v.p5push;
+ return res;
+ }
+ else {
+ return null;
+ }
+};
+
+
+//
//
// lib/Perlito5/Javascript2/CORE.js
//
View
15 perlito5.pl
@@ -9222,7 +9222,7 @@ package main;
undef();
package Perlito5::Javascript2::Runtime;
sub Perlito5::Javascript2::Runtime::emit_javascript2 {
- return ((('//' . chr(10) . '// lib/Perlito5/Javascript2/Runtime.js' . chr(10) . '//' . chr(10) . '// Runtime for "Perlito" Perl5-in-Javascript2' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . 'var isNode = typeof require != "undefined";' . chr(10) . chr(10) . 'if (typeof p5pkg !== "object") {' . chr(10) . ' p5pkg = {};' . chr(10) . ' p5LOCAL = [];' . chr(10) . chr(10) . ' var universal = function () {};' . chr(10) . ' p5pkg.UNIVERSAL = new universal();' . chr(10) . ' p5pkg.UNIVERSAL._ref_ = "UNIVERSAL";' . chr(10) . ' p5pkg.UNIVERSAL.isa = function (List__) {' . chr(10) . ' // TODO - use @ISA' . chr(10) . ' return List__[0]._class_._ref_ == List__[1]' . chr(10) . ' };' . chr(10) . ' p5pkg.UNIVERSAL.can = function (List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' if ( s.indexOf("::") == -1 ) {' . chr(10) . ' return p5method_lookup(s, o._class_._ref__, {})' . chr(10) . ' }' . chr(10) . ' var c = s.split("::");' . chr(10) . ' s = c.pop(); ' . chr(10) . ' return p5method_lookup(s, c.join("::"), {});' . chr(10) . ' };' . chr(10) . ' p5pkg.UNIVERSAL.DOES = p5pkg.UNIVERSAL.can;' . chr(10) . chr(10) . ' var core = function () {};' . chr(10) . ' p5pkg["CORE"] = new core();' . chr(10) . ' p5pkg["CORE"]._ref_ = "CORE";' . chr(10) . chr(10) . ' var core_global = function () {};' . chr(10) . ' core_global.prototype = p5pkg.CORE;' . chr(10) . ' p5pkg["CORE::GLOBAL"] = new core_global();' . chr(10) . ' p5pkg["CORE::GLOBAL"]._ref_ = "CORE::GLOBAL";' . chr(10) . chr(10) . ' p5_error = function (type, v) {' . chr(10) . ' this.type = type;' . chr(10) . ' this.v = v;' . chr(10) . ' this.toString = function(){' . chr(10) . ' if (this.type == ' . chr(39) . 'break' . chr(39) . ') {' . chr(10) . ' return ' . chr(39) . 'Can' . chr(92) . chr(39) . 't "break" outside a given block' . chr(39) . chr(10) . ' }' . chr(10) . ' if (this.type == ' . chr(39) . 'next' . chr(39) . ' || this.type == ' . chr(39) . 'last' . chr(39) . ' || this.type == ' . chr(39) . 'redo' . chr(39) . ') {' . chr(10) . ' if (this.v == "") { return ' . chr(39) . 'Can' . chr(92) . chr(39) . 't "' . chr(39) . ' + this.type + ' . chr(39) . '" outside a loop block' . chr(39) . ' }' . chr(10) . ' return ' . chr(39) . 'Label not found for "' . chr(39) . ' + this.type + ' . chr(39) . ' ' . chr(39) . ' + this.v + ' . chr(39) . '"' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' return this.v;' . chr(10) . ' };' . chr(10) . ' };' . chr(10) . ' p5_error.prototype = Error.prototype;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5make_package(pkg_name) {' . chr(10) . ' if (!p5pkg.hasOwnProperty(pkg_name)) {' . chr(10) . ' var tmp = function () {};' . chr(10) . ' tmp.prototype = p5pkg["CORE::GLOBAL"];' . chr(10) . ' p5pkg[pkg_name] = new tmp();' . chr(10) . ' p5pkg[pkg_name]._ref_ = pkg_name;' . chr(10) . ' p5pkg[pkg_name]._class_ = p5pkg[pkg_name]; // XXX memory leak' . chr(10) . chr(10) . ' // TODO - add the other package global variables' . chr(10) . ' p5pkg[pkg_name]["List_ISA"] = [];' . chr(10) . ' p5pkg[pkg_name]["v_a"] = null;' . chr(10) . ' p5pkg[pkg_name]["v_b"] = null;' . chr(10) . ' p5pkg[pkg_name]["v__"] = null;' . chr(10) . ' p5pkg[pkg_name]["v_AUTOLOAD"] = null;' . chr(10) . ' }' . chr(10) . ' return p5pkg[pkg_name];' . chr(10) . '}' . chr(10) . chr(10) . 'function p5code_lookup_by_name(package_name, sub_name) {' . chr(10) . ' // sub_name can have an optional namespace' . chr(10) . ' var parts = sub_name.split(/::/);' . chr(10) . ' if (parts.length > 1) {' . chr(10) . ' sub_name = parts.pop();' . chr(10) . ' package_name = parts.join("::");' . chr(10) . ' }' . chr(10) . ' if (p5pkg.hasOwnProperty(package_name)) {' . chr(10) . ' var c = p5pkg[package_name];' . chr(10) . ' if ( c.hasOwnProperty(sub_name) ) {' . chr(10) . ' return c[sub_name]' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return null;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5get_class_for_method(method, class_name, seen) {' . chr(10) . ' // default mro' . chr(10) . ' // TODO - cache the methods that were already looked up' . chr(10) . ' if ( p5pkg[class_name].hasOwnProperty(method) ) {' . chr(10) . ' return class_name' . chr(10) . ' }' . chr(10) . ' var isa = p5pkg[class_name].List_ISA;' . chr(10) . ' for (var i = 0; i < isa.length; i++) {' . chr(10) . ' if (!seen[isa[i]]) {' . chr(10) . ' var m = p5get_class_for_method(method, isa[i]);' . chr(10) . ' if (m) {' . chr(10) . ' return m ' . chr(10) . ' }' . chr(10) . ' seen[isa[i]]++;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . '}' . chr(10) . chr(10) . 'function p5method_lookup(method, class_name, seen) {' . chr(10) . ' var c = p5get_class_for_method(method, class_name, seen);' . chr(10) . ' if (c) {' . chr(10) . ' return p5pkg[c][method]' . chr(10) . ' }' . chr(10) . ' if ( p5pkg.UNIVERSAL.hasOwnProperty(method) ) {' . chr(10) . ' return p5pkg.UNIVERSAL[method]' . chr(10) . ' }' . chr(10) . '}' . chr(10) . chr(10) . 'function p5call(invocant, method, list) {' . chr(10) . ' list.unshift(invocant);' . chr(10) . chr(10) . ' if (typeof invocant === "string") {' . chr(10) . ' invocant = p5make_package(invocant);' . chr(10) . ' }' . chr(10) . chr(10) . ' if ( invocant.hasOwnProperty("_class_") ) {' . chr(10) . chr(10) . ' if ( invocant._class_.hasOwnProperty(method) ) {' . chr(10) . ' return invocant._class_[method](list)' . chr(10) . ' }' . chr(10) . ' var m = p5method_lookup(method, invocant._class_._ref_, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list)' . chr(10) . ' }' . chr(10) . chr(10) . ' // method can have an optional namespace' . chr(10) . ' var pkg_name = method.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' var name = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' m = p5method_lookup(name, pkg_name, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list)' . chr(10) . ' }' . chr(10) . ' p5pkg.CORE.die(["method not found: ", name, " in class ", pkg_name]);' . chr(10) . ' }' . chr(10) . chr(10) . ' pkg_name = p5get_class_for_method(' . chr(39) . 'AUTOLOAD' . chr(39) . ', invocant._class_._ref_, {}) || p5get_class_for_method(' . chr(39) . 'AUTOLOAD' . chr(39) . ', "UNIVERSAL", {});' . chr(10) . ' if (pkg_name) {' . chr(10) . ' p5pkg[pkg_name]["v_AUTOLOAD"] = invocant._class_._ref_ + "::" + method;' . chr(10) . ' return p5pkg[pkg_name]["AUTOLOAD"](list);' . chr(10) . ' }' . chr(10) . chr(10) . ' p5pkg.CORE.die(["method not found: ", method, " in class ", invocant._class_._ref_]);' . chr(10) . chr(10) . ' }' . chr(10) . chr(10) . ' p5pkg.CORE.die(["Can' . chr(39) . 't call method ", method, " on unblessed reference"]);' . chr(10) . chr(10) . '}' . chr(10) . chr(10) . 'p5make_package("main");' . chr(10) . 'p5pkg["main"]["v_@"] = []; // $@' . chr(10) . 'p5pkg["main"]["v_|"] = 0; // $|' . chr(10) . 'p5pkg["main"]["List_#"] = []; // @#' . chr(10) . 'p5pkg["main"]["v_^O"] = isNode ? "node.js" : "javascript2";' . chr(10) . 'p5pkg["main"]["List_INC"] = [];' . chr(10) . 'p5pkg["main"]["Hash_INC"] = {};' . chr(10) . 'p5pkg["main"]["List_ARGV"] = [];' . chr(10) . 'p5pkg["main"]["Hash_ENV"] = {};' . chr(10) . 'if (isNode) {' . chr(10) . ' p5pkg["main"]["List_ARGV"] = process.argv.splice(2);' . chr(10) . chr(10) . ' p5pkg["main"]["Hash_ENV"] = {};' . chr(10) . ' for (e in process.env) p5pkg["main"]["Hash_ENV"][e] = process.env[e];' . chr(10) . chr(10) . ' p5pkg["main"]["v_$"] = process.pid;' . chr(10) . '} else if (typeof arguments === "object") {' . chr(10) . ' p5pkg["main"]["List_ARGV"] = arguments;' . chr(10) . '}' . chr(10) . chr(10) . 'p5make_package("Perlito5");' . chr(10) . 'p5make_package("Perlito5::IO");' . chr(10) . 'p5make_package("Perlito5::Runtime");' . chr(10) . 'p5make_package("Perlito5::Grammar");' . chr(10) . chr(10) . 'function p5make_sub(pkg_name, sub_name, func) {' . chr(10) . ' p5pkg[pkg_name][sub_name] = func;' . chr(10) . '}' . chr(10) . chr(10) . 'var sigils = { ' . chr(39) . '@' . chr(39) . ' : ' . chr(39) . 'List_' . chr(39) . ', ' . chr(39) . '%' . chr(39) . ' : ' . chr(39) . 'Hash_' . chr(39) . ', ' . chr(39) . '$' . chr(39) . ' : ' . chr(39) . 'v_' . chr(39) . ' };' . chr(10) . chr(10) . 'function p5set_local(namespace, name, sigil) {' . chr(10) . ' var vname = sigils[sigil] + name;' . chr(10) . ' p5LOCAL.push([namespace, vname, namespace[vname]]);' . chr(10) . chr(10) . ' if (sigil == ' . chr(39) . '$' . chr(39) . ') {' . chr(10) . ' namespace[vname] = null;' . chr(10) . ' }' . chr(10) . ' else if (sigil == ' . chr(39) . '@' . chr(39) . ') {' . chr(10) . ' namespace[vname] = new p5Array([]);' . chr(10) . ' }' . chr(10) . ' else if (sigil == ' . chr(39) . '%' . chr(39) . ') {' . chr(10) . ' namespace[vname] = new p5Hash({});' . chr(10) . ' }' . chr(10) . ' return namespace[vname];' . chr(10) . '}' . chr(10) . chr(10) . 'function p5cleanup_local(idx, value) {' . chr(10) . ' while (p5LOCAL.length > idx) {' . chr(10) . ' l = p5LOCAL.pop();' . chr(10) . ' l[0][l[1]] = l[2];' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5HashRef(o) {' . chr(10) . ' this._hash_ = o;' . chr(10) . ' this._ref_ = "HASH";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function p5ArrayRef(o) {' . chr(10) . ' this._array_ = o;' . chr(10) . ' this._ref_ = "ARRAY";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function p5ScalarRef(o) {' . chr(10) . ' this._scalar_ = o;' . chr(10) . ' this._ref_ = "SCALAR";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function p5GlobRef(o) {' . chr(10) . ' this._scalar_ = o;' . chr(10) . ' this._ref_ = "GLOB";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . chr(10) . '//-------- Array ' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' return this[i] ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i, v) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = v;' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5ArrayRef([]) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5HashRef({}) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' this.unshift(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' this.push(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'p5tie_array = function(v, List__) {' . chr(10) . ' var pkg_name = p5str(List__.shift());' . chr(10) . chr(10) . ' var res = p5call(pkg_name, ' . chr(39) . 'TIEARRAY' . chr(39) . ', List__, null);' . chr(10) . ' ' . chr(10) . ' // TODO' . chr(10) . ' ' . chr(10) . ' // A class implementing an ordinary array should have the following methods:' . chr(10) . ' // TIEARRAY pkg_name, LIST' . chr(10) . ' // FETCH this, key' . chr(10) . ' // STORE this, key, value' . chr(10) . ' // FETCHSIZE this' . chr(10) . ' // STORESIZE this, count' . chr(10) . ' // CLEAR this' . chr(10) . ' // PUSH this, LIST' . chr(10) . ' // POP this' . chr(10) . ' // SHIFT this' . chr(10) . ' // UNSHIFT this, LIST' . chr(10) . ' // SPLICE this, offset, length, LIST' . chr(10) . ' // EXTEND this, count' . chr(10) . ' // DESTROY this' . chr(10) . ' // UNTIE this' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i, value) {' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5incr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5incr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5decr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5decr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5ArrayRef([]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5HashRef({});' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5untie", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) { return p5call(res, ' . chr(39) . 'UNTIE' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "shift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'SHIFT' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "pop", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'POP' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' p5call(res, ' . chr(39) . 'UNSHIFT' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' p5call(res, ' . chr(39) . 'PUSH' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5untie_array = function(v) {' . chr(10) . ' if (v.hasOwnProperty(' . chr(39) . 'p5untie' . chr(39) . ')) {' . chr(10) . ' var res = v.p5untie(); // call UNTIE' . chr(10) . ' delete v.p5aget;' . chr(10) . ' delete v.p5aset;' . chr(10) . ' delete v.p5incr;' . chr(10) . ' delete v.p5postincr;' . chr(10) . ' delete v.p5decr;' . chr(10) . ' delete v.p5postdecr;' . chr(10) . ' delete v.p5aget_array;' . chr(10) . ' delete v.p5aget_hash;' . chr(10) . ' delete v.p5untie;' . chr(10) . ' delete v.shift;' . chr(10) . ' delete v.pop;' . chr(10) . ' delete v.p5unshift;' . chr(10) . ' delete v.p5push;' . chr(10) . ' return res;' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . '//-------- Hash ' . chr(10) . chr(10) . 'Object.defineProperty( Object.prototype, "p5hget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) { return this[i] }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5hset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i, v) { this[i] = v; return this[i] }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Object.prototype, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Object.prototype, "p5hget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (this[i] == null) { this[i] = new p5ArrayRef([]) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5hget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (this[i] == null) { this[i] = new p5HashRef({}) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . '//-------' . chr(10) . chr(10) . chr(10) . 'if (isNode) {' . chr(10) . ' var fs = require("fs");' . chr(10) . ' p5make_sub("Perlito5::IO", "slurp", function(List__) {' . chr(10) . ' return fs.readFileSync(List__[0],"utf8");' . chr(10) . ' });' . chr(10) . '} else {' . chr(10) . ' p5make_sub("Perlito5::IO", "slurp", function(List__) {' . chr(10) . ' var filename = List__[0];' . chr(10) . ' if (typeof readFile == "function") {' . chr(10) . ' return readFile(filename);' . chr(10) . ' }' . chr(10) . ' if (typeof read == "function") {' . chr(10) . ' // v8' . chr(10) . ' return read(filename);' . chr(10) . ' }' . chr(10) . ' p5pkg.CORE.die(["Perlito5::IO::slurp() not implemented"]);' . chr(10) . ' });' . chr(10) . '}' . chr(10) . chr(10) . 'p5context = function(List__, p5want) {' . chr(10) . ' if (p5want) {' . chr(10) . ' return p5list_to_a.apply(null, List__);' . chr(10) . ' }' . chr(10) . ' // scalar: return the last value' . chr(10) . ' var o = List__;' . chr(10) . ' while (o instanceof Array) {' . chr(10) . ' o = o.length' . chr(10) . ' ? o[o.length-1]' . chr(10) . ' : null;' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '}' . chr(10) . chr(10) . 'p5list_to_a = function() {' . chr(10) . ' var res = [];' . chr(10) . ' for (i = 0; i < arguments.length; i++) {' . chr(10) . ' var o = arguments[i];' . chr(10) . ' if ( o == null' . chr(10) . ' || o._class_ // perl5 blessed reference' . chr(10) . ' || o._ref_ // perl5 un-blessed reference' . chr(10) . ' )' . chr(10) . ' {' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' else if (o instanceof Array) {' . chr(10) . ' // perl5 array' . chr(10) . ' for (j = 0; j < o.length; j++) {' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else if (typeof o === "object") {' . chr(10) . ' // perl5 hash' . chr(10) . ' for(var j in o) {' . chr(10) . ' if (o.hasOwnProperty(j)) {' . chr(10) . ' res.push(j);' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' // non-ref' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5a_to_h = function(a) {' . chr(10) . ' var res = {};' . chr(10) . ' for (i = 0; i < a.length; i+=2) {' . chr(10) . ' res[p5str(a[i])] = a[i+1];' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5idx = function(a, i) {' . chr(10) . ' return i >= 0 ? i : a.length + i' . chr(10) . '};' . chr(10) . chr(10) . 'p5str = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return CORE.join(["", o]);' . chr(10) . ' }' . chr(10) . ' // if (typeof o.string === "function") {' . chr(10) . ' // return o.string();' . chr(10) . ' // }' . chr(10) . ' if (typeof o == "number" && Math.abs(o) < 0.0001 && o != 0) {' . chr(10) . ' return o.toExponential().replace(/e-(' . chr(92) . 'd)$/,"e-0$1");' . chr(10) . ' }' . chr(10) . ' if (typeof o === "boolean") {' . chr(10) . ' return o ? "1" : "";' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "string") {' . chr(10) . ' return "" + o;' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'p5num = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return 0;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' // if (typeof o.num === "function") {' . chr(10) . ' // return o.num();' . chr(10) . ' // }' . chr(10) . ' if (typeof o !== "number") {' . chr(10) . ' return parseFloat(p5str(o));' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'p5bool = function(o) {' . chr(10) . ' if (o) {' . chr(10) . ' if (typeof o === "boolean") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "string") {' . chr(10) . ' return o != "" && o != "0";' . chr(10) . ' }' . chr(10) . ' // if (typeof o.bool === "function") {' . chr(10) . ' // return o.bool();' . chr(10) . ' // }' . chr(10) . ' if (typeof o.length === "number") {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' if (o instanceof Error) {' . chr(10) . ' return true;' . chr(10) . ' }' . chr(10) . ' for (var i in o) {' . chr(10) . ' return true;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return false;' . chr(10) . '};' . chr(10) . chr(10) . 'p5incr = function(o) {' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o + 1;' . chr(10) . ' }' . chr(10) . ' return p5str_inc(p5str(o));' . chr(10) . '};' . chr(10) . chr(10) . 'p5decr = function(o) {' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o - 1;' . chr(10) . ' }' . chr(10) . ' return p5num(o) - 1;' . chr(10) . '};' . chr(10) . chr(10) . 'p5and = function(a, fb) {' . chr(10) . ' if (p5bool(a)) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'p5or = function(a, fb) {' . chr(10) . ' if (p5bool(a)) {' . chr(10) . ' return a;' . chr(10) . ' }' . chr(10) . ' return fb();' . chr(10) . '};' . chr(10) . chr(10) . 'p5defined_or = function(a, fb) {' . chr(10) . ' if (a == null) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'p5cmp = function(a, b) {' . chr(10) . ' return a > b ? 1 : a < b ? -1 : 0 ' . chr(10) . '};' . chr(10) . chr(10) . 'p5complement = function(a) {' . chr(10) . ' return a < 0 ? ~a : 4294967295 - a' . chr(10) . ' // return a < 0 ? ~a : 18446744073709551615 - a' . chr(10) . '};' . chr(10) . chr(10) . 'p5str_replicate = function(o, n) {' . chr(10) . ' n = p5num(n);' . chr(10) . ' return n ? Array(n + 1).join(o) : "";' . chr(10) . '};' . chr(10) . chr(10) . 'p5str_inc = function(s) {' . chr(10) . ' s = p5str(s);' . chr(10) . ' if (s.length < 2) {' . chr(10) . ' if (s.match(/[012345678ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy]/)) {' . chr(10) . ' return String.fromCharCode(s.charCodeAt(0) + 1);' . chr(10) . ' }' . chr(10) . ' if (s == "9") {' . chr(10) . ' return "10";' . chr(10) . ' }' . chr(10) . ' if (s == "Z") {' . chr(10) . ' return "AA";' . chr(10) . ' }' . chr(10) . ' if (s == "z") {' . chr(10) . ' return "aa";' . chr(10) . ' }' . chr(10) . ' return "1";' . chr(10) . ' }' . chr(10) . ' var c = p5str_inc(s.substr(s.length-1, 1));' . chr(10) . ' if (c.length == 1) {' . chr(10) . ' return s.substr(0, s.length-1) + c;' . chr(10) . ' }' . chr(10) . ' return p5str_inc(s.substr(0, s.length-1)) + c.substr(c.length-1, 1);' . chr(10) . '};' . chr(10) . chr(10) . 'p5for = function(namespace, func, args, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' var v_old = namespace["v__"];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace["v__"] = args[i];' . chr(10) . ' try {' . chr(10) . ' func()' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { i--; _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' if (cont) {' . chr(10) . ' try {' . chr(10) . ' if (!_redo) { cont() }' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' namespace["v__"] = v_old;' . chr(10) . '};' . chr(10) . chr(10) . 'p5for_lex = function(func, args, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' try {' . chr(10) . ' func(args[i])' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { i--; _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' if (cont) {' . chr(10) . ' try {' . chr(10) . ' if (!_redo) { cont() }' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'p5while = function(func, cond, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' while (_redo || p5bool(cond())) {' . chr(10) . ' _redo = false;' . chr(10) . ' try {' . chr(10) . ' func()' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' if (cont) {' . chr(10) . ' try {' . chr(10) . ' if (!_redo) { cont() }' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'p5map = function(namespace, func, args) {' . chr(10) . ' var v_old = namespace["v__"];' . chr(10) . ' var out = [];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace["v__"] = args[i];' . chr(10) . ' var o = p5list_to_a(func(1));' . chr(10) . ' for(var j = 0; j < o.length; j++) {' . chr(10) . ' out.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' namespace["v__"] = v_old;' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'p5grep = function(namespace, func, args) {' . chr(10) . ' var v_old = namespace["v__"];' . chr(10) . ' var out = [];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace["v__"] = args[i];' . chr(10) . ' if (p5bool(func(0))) {' . chr(10) . ' out.push(args[i])' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' namespace["v__"] = v_old;' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'p5sort = function(namespace, func, args) {' . chr(10) . ' var a_old = namespace["v_a"];' . chr(10) . ' var b_old = namespace["v_b"];' . chr(10) . ' var out = ' . chr(10) . ' func == null' . chr(10) . ' ? args.sort()' . chr(10) . ' : args.sort(' . chr(10) . ' function(a, b) {' . chr(10) . ' namespace["v_a"] = a;' . chr(10) . ' namespace["v_b"] = b;' . chr(10) . ' return func(0);' . chr(10) . ' }' . chr(10) . ' );' . chr(10) . ' namespace["v_a"] = a_old;' . chr(10) . ' namespace["v_b"] = b_old;' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'perl5_to_js = function( source, namespace, var_env_js, p5want ) {' . chr(10) . ' // CORE.say(["source: [" + source + "]"]);' . chr(10) . chr(10) . ' var strict_old = p5pkg["Perlito5"].v_STRICT;' . chr(10) . ' var var_env_js_old = p5pkg["Perlito5"].v_VAR;' . chr(10) . ' p5pkg["Perlito5"].v_VAR = var_env_js;' . chr(10) . chr(10) . ' var namespace_old = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' p5pkg["Perlito5"].v_PKG_NAME = namespace;' . chr(10) . chr(10) . ' match = p5call(p5pkg["Perlito5::Grammar"], "exp_stmts", [source, 0]);' . chr(10) . chr(10) . ' if ( !match || match._hash_.to != source.length ) {' . chr(10) . ' CORE.die(["Syntax error in eval near pos ", match._hash_.to]);' . chr(10) . ' }' . chr(10) . chr(10) . ' ast = p5pkg.CORE.bless([' . chr(10) . ' new p5HashRef({' . chr(10) . ' block: p5pkg.CORE.bless([' . chr(10) . ' new p5HashRef({' . chr(10) . ' stmts: p5pkg["Perlito5::Match"].flat([match])' . chr(10) . ' }),' . chr(10) . ' p5pkg["Perlito5::AST::Lit::Block"]' . chr(10) . ' ])' . chr(10) . ' }),' . chr(10) . ' p5pkg["Perlito5::AST::Do"]' . chr(10) . ' ]);' . chr(10) . chr(10) . ' // CORE.say(["ast: [" + ast + "]"]);' . chr(10) . ' js_code = p5call(ast, "emit_javascript2", [0, p5want]);' . chr(10) . ' // CORE.say(["js-source: [" + js_code + "]"]);' . chr(10) . chr(10) . ' p5pkg["Perlito5"].v_PKG_NAME = namespace_old;' . chr(10) . ' p5pkg["Perlito5"].v_VAR = var_env_js_old;' . chr(10) . ' p5pkg["Perlito5"].v_STRICT = strict_old;' . chr(10) . ' return js_code;' . chr(10) . '}' . chr(10) . chr(10))))
+ return ((('//' . chr(10) . '// lib/Perlito5/Javascript2/Runtime.js' . chr(10) . '//' . chr(10) . '// Runtime for "Perlito" Perl5-in-Javascript2' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . 'var isNode = typeof require != "undefined";' . chr(10) . chr(10) . 'if (typeof p5pkg !== "object") {' . chr(10) . ' p5pkg = {};' . chr(10) . ' p5LOCAL = [];' . chr(10) . chr(10) . ' var universal = function () {};' . chr(10) . ' p5pkg.UNIVERSAL = new universal();' . chr(10) . ' p5pkg.UNIVERSAL._ref_ = "UNIVERSAL";' . chr(10) . ' p5pkg.UNIVERSAL.isa = function (List__) {' . chr(10) . ' // TODO - use @ISA' . chr(10) . ' return List__[0]._class_._ref_ == List__[1]' . chr(10) . ' };' . chr(10) . ' p5pkg.UNIVERSAL.can = function (List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' if ( s.indexOf("::") == -1 ) {' . chr(10) . ' return p5method_lookup(s, o._class_._ref__, {})' . chr(10) . ' }' . chr(10) . ' var c = s.split("::");' . chr(10) . ' s = c.pop(); ' . chr(10) . ' return p5method_lookup(s, c.join("::"), {});' . chr(10) . ' };' . chr(10) . ' p5pkg.UNIVERSAL.DOES = p5pkg.UNIVERSAL.can;' . chr(10) . chr(10) . ' var core = function () {};' . chr(10) . ' p5pkg["CORE"] = new core();' . chr(10) . ' p5pkg["CORE"]._ref_ = "CORE";' . chr(10) . chr(10) . ' var core_global = function () {};' . chr(10) . ' core_global.prototype = p5pkg.CORE;' . chr(10) . ' p5pkg["CORE::GLOBAL"] = new core_global();' . chr(10) . ' p5pkg["CORE::GLOBAL"]._ref_ = "CORE::GLOBAL";' . chr(10) . chr(10) . ' p5_error = function (type, v) {' . chr(10) . ' this.type = type;' . chr(10) . ' this.v = v;' . chr(10) . ' this.toString = function(){' . chr(10) . ' if (this.type == ' . chr(39) . 'break' . chr(39) . ') {' . chr(10) . ' return ' . chr(39) . 'Can' . chr(92) . chr(39) . 't "break" outside a given block' . chr(39) . chr(10) . ' }' . chr(10) . ' if (this.type == ' . chr(39) . 'next' . chr(39) . ' || this.type == ' . chr(39) . 'last' . chr(39) . ' || this.type == ' . chr(39) . 'redo' . chr(39) . ') {' . chr(10) . ' if (this.v == "") { return ' . chr(39) . 'Can' . chr(92) . chr(39) . 't "' . chr(39) . ' + this.type + ' . chr(39) . '" outside a loop block' . chr(39) . ' }' . chr(10) . ' return ' . chr(39) . 'Label not found for "' . chr(39) . ' + this.type + ' . chr(39) . ' ' . chr(39) . ' + this.v + ' . chr(39) . '"' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' return this.v;' . chr(10) . ' };' . chr(10) . ' };' . chr(10) . ' p5_error.prototype = Error.prototype;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5make_package(pkg_name) {' . chr(10) . ' if (!p5pkg.hasOwnProperty(pkg_name)) {' . chr(10) . ' var tmp = function () {};' . chr(10) . ' tmp.prototype = p5pkg["CORE::GLOBAL"];' . chr(10) . ' p5pkg[pkg_name] = new tmp();' . chr(10) . ' p5pkg[pkg_name]._ref_ = pkg_name;' . chr(10) . ' p5pkg[pkg_name]._class_ = p5pkg[pkg_name]; // XXX memory leak' . chr(10) . chr(10) . ' // TODO - add the other package global variables' . chr(10) . ' p5pkg[pkg_name]["List_ISA"] = [];' . chr(10) . ' p5pkg[pkg_name]["v_a"] = null;' . chr(10) . ' p5pkg[pkg_name]["v_b"] = null;' . chr(10) . ' p5pkg[pkg_name]["v__"] = null;' . chr(10) . ' p5pkg[pkg_name]["v_AUTOLOAD"] = null;' . chr(10) . ' }' . chr(10) . ' return p5pkg[pkg_name];' . chr(10) . '}' . chr(10) . chr(10) . 'function p5code_lookup_by_name(package_name, sub_name) {' . chr(10) . ' // sub_name can have an optional namespace' . chr(10) . ' var parts = sub_name.split(/::/);' . chr(10) . ' if (parts.length > 1) {' . chr(10) . ' sub_name = parts.pop();' . chr(10) . ' package_name = parts.join("::");' . chr(10) . ' }' . chr(10) . ' if (p5pkg.hasOwnProperty(package_name)) {' . chr(10) . ' var c = p5pkg[package_name];' . chr(10) . ' if ( c.hasOwnProperty(sub_name) ) {' . chr(10) . ' return c[sub_name]' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return null;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5get_class_for_method(method, class_name, seen) {' . chr(10) . ' // default mro' . chr(10) . ' // TODO - cache the methods that were already looked up' . chr(10) . ' if ( p5pkg[class_name].hasOwnProperty(method) ) {' . chr(10) . ' return class_name' . chr(10) . ' }' . chr(10) . ' var isa = p5pkg[class_name].List_ISA;' . chr(10) . ' for (var i = 0; i < isa.length; i++) {' . chr(10) . ' if (!seen[isa[i]]) {' . chr(10) . ' var m = p5get_class_for_method(method, isa[i]);' . chr(10) . ' if (m) {' . chr(10) . ' return m ' . chr(10) . ' }' . chr(10) . ' seen[isa[i]]++;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . '}' . chr(10) . chr(10) . 'function p5method_lookup(method, class_name, seen) {' . chr(10) . ' var c = p5get_class_for_method(method, class_name, seen);' . chr(10) . ' if (c) {' . chr(10) . ' return p5pkg[c][method]' . chr(10) . ' }' . chr(10) . ' if ( p5pkg.UNIVERSAL.hasOwnProperty(method) ) {' . chr(10) . ' return p5pkg.UNIVERSAL[method]' . chr(10) . ' }' . chr(10) . '}' . chr(10) . chr(10) . 'function p5call(invocant, method, list) {' . chr(10) . ' list.unshift(invocant);' . chr(10) . chr(10) . ' if (typeof invocant === "string") {' . chr(10) . ' invocant = p5make_package(invocant);' . chr(10) . ' }' . chr(10) . chr(10) . ' if ( invocant.hasOwnProperty("_class_") ) {' . chr(10) . chr(10) . ' if ( invocant._class_.hasOwnProperty(method) ) {' . chr(10) . ' return invocant._class_[method](list)' . chr(10) . ' }' . chr(10) . ' var m = p5method_lookup(method, invocant._class_._ref_, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list)' . chr(10) . ' }' . chr(10) . chr(10) . ' // method can have an optional namespace' . chr(10) . ' var pkg_name = method.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' var name = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' m = p5method_lookup(name, pkg_name, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list)' . chr(10) . ' }' . chr(10) . ' p5pkg.CORE.die(["method not found: ", name, " in class ", pkg_name]);' . chr(10) . ' }' . chr(10) . chr(10) . ' pkg_name = p5get_class_for_method(' . chr(39) . 'AUTOLOAD' . chr(39) . ', invocant._class_._ref_, {}) || p5get_class_for_method(' . chr(39) . 'AUTOLOAD' . chr(39) . ', "UNIVERSAL", {});' . chr(10) . ' if (pkg_name) {' . chr(10) . ' p5pkg[pkg_name]["v_AUTOLOAD"] = invocant._class_._ref_ + "::" + method;' . chr(10) . ' return p5pkg[pkg_name]["AUTOLOAD"](list);' . chr(10) . ' }' . chr(10) . chr(10) . ' p5pkg.CORE.die(["method not found: ", method, " in class ", invocant._class_._ref_]);' . chr(10) . chr(10) . ' }' . chr(10) . chr(10) . ' p5pkg.CORE.die(["Can' . chr(39) . 't call method ", method, " on unblessed reference"]);' . chr(10) . chr(10) . '}' . chr(10) . chr(10) . 'p5make_package("main");' . chr(10) . 'p5pkg["main"]["v_@"] = []; // $@' . chr(10) . 'p5pkg["main"]["v_|"] = 0; // $|' . chr(10) . 'p5pkg["main"]["List_#"] = []; // @#' . chr(10) . 'p5pkg["main"]["v_^O"] = isNode ? "node.js" : "javascript2";' . chr(10) . 'p5pkg["main"]["List_INC"] = [];' . chr(10) . 'p5pkg["main"]["Hash_INC"] = {};' . chr(10) . 'p5pkg["main"]["List_ARGV"] = [];' . chr(10) . 'p5pkg["main"]["Hash_ENV"] = {};' . chr(10) . 'if (isNode) {' . chr(10) . ' p5pkg["main"]["List_ARGV"] = process.argv.splice(2);' . chr(10) . chr(10) . ' p5pkg["main"]["Hash_ENV"] = {};' . chr(10) . ' for (e in process.env) p5pkg["main"]["Hash_ENV"][e] = process.env[e];' . chr(10) . chr(10) . ' p5pkg["main"]["v_$"] = process.pid;' . chr(10) . '} else if (typeof arguments === "object") {' . chr(10) . ' p5pkg["main"]["List_ARGV"] = arguments;' . chr(10) . '}' . chr(10) . chr(10) . 'p5make_package("Perlito5");' . chr(10) . 'p5make_package("Perlito5::IO");' . chr(10) . 'p5make_package("Perlito5::Runtime");' . chr(10) . 'p5make_package("Perlito5::Grammar");' . chr(10) . chr(10) . 'function p5make_sub(pkg_name, sub_name, func) {' . chr(10) . ' p5pkg[pkg_name][sub_name] = func;' . chr(10) . '}' . chr(10) . chr(10) . 'var sigils = { ' . chr(39) . '@' . chr(39) . ' : ' . chr(39) . 'List_' . chr(39) . ', ' . chr(39) . '%' . chr(39) . ' : ' . chr(39) . 'Hash_' . chr(39) . ', ' . chr(39) . '$' . chr(39) . ' : ' . chr(39) . 'v_' . chr(39) . ' };' . chr(10) . chr(10) . 'function p5set_local(namespace, name, sigil) {' . chr(10) . ' var vname = sigils[sigil] + name;' . chr(10) . ' p5LOCAL.push([namespace, vname, namespace[vname]]);' . chr(10) . chr(10) . ' if (sigil == ' . chr(39) . '$' . chr(39) . ') {' . chr(10) . ' namespace[vname] = null;' . chr(10) . ' }' . chr(10) . ' else if (sigil == ' . chr(39) . '@' . chr(39) . ') {' . chr(10) . ' namespace[vname] = new p5Array([]);' . chr(10) . ' }' . chr(10) . ' else if (sigil == ' . chr(39) . '%' . chr(39) . ') {' . chr(10) . ' namespace[vname] = new p5Hash({});' . chr(10) . ' }' . chr(10) . ' return namespace[vname];' . chr(10) . '}' . chr(10) . chr(10) . 'function p5cleanup_local(idx, value) {' . chr(10) . ' while (p5LOCAL.length > idx) {' . chr(10) . ' l = p5LOCAL.pop();' . chr(10) . ' l[0][l[1]] = l[2];' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . '}' . chr(10) . chr(10) . '//-------- Reference' . chr(10) . chr(10) . 'function p5HashRef(o) {' . chr(10) . ' this._hash_ = o;' . chr(10) . ' this._ref_ = "HASH";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function p5ArrayRef(o) {' . chr(10) . ' this._array_ = o;' . chr(10) . ' this._ref_ = "ARRAY";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function p5ScalarRef(o) {' . chr(10) . ' this._scalar_ = o;' . chr(10) . ' this._ref_ = "SCALAR";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function p5GlobRef(o) {' . chr(10) . ' this._scalar_ = o;' . chr(10) . ' this._ref_ = "GLOB";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . '//-------- Hash ' . chr(10) . chr(10) . 'Object.defineProperty( Object.prototype, "p5hget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) { return this[i] }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5hset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i, v) { this[i] = v; return this[i] }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Object.prototype, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Object.prototype, "p5hget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (this[i] == null) { this[i] = new p5ArrayRef([]) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Object.prototype, "p5hget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (this[i] == null) { this[i] = new p5HashRef({}) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . '//-------' . chr(10) . chr(10) . chr(10) . 'if (isNode) {' . chr(10) . ' var fs = require("fs");' . chr(10) . ' p5make_sub("Perlito5::IO", "slurp", function(List__) {' . chr(10) . ' return fs.readFileSync(List__[0],"utf8");' . chr(10) . ' });' . chr(10) . '} else {' . chr(10) . ' p5make_sub("Perlito5::IO", "slurp", function(List__) {' . chr(10) . ' var filename = List__[0];' . chr(10) . ' if (typeof readFile == "function") {' . chr(10) . ' return readFile(filename);' . chr(10) . ' }' . chr(10) . ' if (typeof read == "function") {' . chr(10) . ' // v8' . chr(10) . ' return read(filename);' . chr(10) . ' }' . chr(10) . ' p5pkg.CORE.die(["Perlito5::IO::slurp() not implemented"]);' . chr(10) . ' });' . chr(10) . '}' . chr(10) . chr(10) . 'p5context = function(List__, p5want) {' . chr(10) . ' if (p5want) {' . chr(10) . ' return p5list_to_a.apply(null, List__);' . chr(10) . ' }' . chr(10) . ' // scalar: return the last value' . chr(10) . ' var o = List__;' . chr(10) . ' while (o instanceof Array) {' . chr(10) . ' o = o.length' . chr(10) . ' ? o[o.length-1]' . chr(10) . ' : null;' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '}' . chr(10) . chr(10) . 'p5list_to_a = function() {' . chr(10) . ' var res = [];' . chr(10) . ' for (i = 0; i < arguments.length; i++) {' . chr(10) . ' var o = arguments[i];' . chr(10) . ' if ( o == null' . chr(10) . ' || o._class_ // perl5 blessed reference' . chr(10) . ' || o._ref_ // perl5 un-blessed reference' . chr(10) . ' )' . chr(10) . ' {' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' else if (o instanceof Array) {' . chr(10) . ' // perl5 array' . chr(10) . ' for (j = 0; j < o.length; j++) {' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else if (typeof o === "object") {' . chr(10) . ' // perl5 hash' . chr(10) . ' for(var j in o) {' . chr(10) . ' if (o.hasOwnProperty(j)) {' . chr(10) . ' res.push(j);' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' // non-ref' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5a_to_h = function(a) {' . chr(10) . ' var res = {};' . chr(10) . ' for (i = 0; i < a.length; i+=2) {' . chr(10) . ' res[p5str(a[i])] = a[i+1];' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5idx = function(a, i) {' . chr(10) . ' return i >= 0 ? i : a.length + i' . chr(10) . '};' . chr(10) . chr(10) . 'p5str = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return CORE.join(["", o]);' . chr(10) . ' }' . chr(10) . ' // if (typeof o.string === "function") {' . chr(10) . ' // return o.string();' . chr(10) . ' // }' . chr(10) . ' if (typeof o == "number" && Math.abs(o) < 0.0001 && o != 0) {' . chr(10) . ' return o.toExponential().replace(/e-(' . chr(92) . 'd)$/,"e-0$1");' . chr(10) . ' }' . chr(10) . ' if (typeof o === "boolean") {' . chr(10) . ' return o ? "1" : "";' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "string") {' . chr(10) . ' return "" + o;' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'p5num = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return 0;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' // if (typeof o.num === "function") {' . chr(10) . ' // return o.num();' . chr(10) . ' // }' . chr(10) . ' if (typeof o !== "number") {' . chr(10) . ' return parseFloat(p5str(o));' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'p5bool = function(o) {' . chr(10) . ' if (o) {' . chr(10) . ' if (typeof o === "boolean") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "string") {' . chr(10) . ' return o != "" && o != "0";' . chr(10) . ' }' . chr(10) . ' // if (typeof o.bool === "function") {' . chr(10) . ' // return o.bool();' . chr(10) . ' // }' . chr(10) . ' if (typeof o.length === "number") {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' if (o instanceof Error) {' . chr(10) . ' return true;' . chr(10) . ' }' . chr(10) . ' for (var i in o) {' . chr(10) . ' return true;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return false;' . chr(10) . '};' . chr(10) . chr(10) . 'p5incr = function(o) {' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o + 1;' . chr(10) . ' }' . chr(10) . ' return p5str_inc(p5str(o));' . chr(10) . '};' . chr(10) . chr(10) . 'p5decr = function(o) {' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o - 1;' . chr(10) . ' }' . chr(10) . ' return p5num(o) - 1;' . chr(10) . '};' . chr(10) . chr(10) . 'p5and = function(a, fb) {' . chr(10) . ' if (p5bool(a)) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'p5or = function(a, fb) {' . chr(10) . ' if (p5bool(a)) {' . chr(10) . ' return a;' . chr(10) . ' }' . chr(10) . ' return fb();' . chr(10) . '};' . chr(10) . chr(10) . 'p5defined_or = function(a, fb) {' . chr(10) . ' if (a == null) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'p5cmp = function(a, b) {' . chr(10) . ' return a > b ? 1 : a < b ? -1 : 0 ' . chr(10) . '};' . chr(10) . chr(10) . 'p5complement = function(a) {' . chr(10) . ' return a < 0 ? ~a : 4294967295 - a' . chr(10) . ' // return a < 0 ? ~a : 18446744073709551615 - a' . chr(10) . '};' . chr(10) . chr(10) . 'p5str_replicate = function(o, n) {' . chr(10) . ' n = p5num(n);' . chr(10) . ' return n ? Array(n + 1).join(o) : "";' . chr(10) . '};' . chr(10) . chr(10) . 'p5str_inc = function(s) {' . chr(10) . ' s = p5str(s);' . chr(10) . ' if (s.length < 2) {' . chr(10) . ' if (s.match(/[012345678ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy]/)) {' . chr(10) . ' return String.fromCharCode(s.charCodeAt(0) + 1);' . chr(10) . ' }' . chr(10) . ' if (s == "9") {' . chr(10) . ' return "10";' . chr(10) . ' }' . chr(10) . ' if (s == "Z") {' . chr(10) . ' return "AA";' . chr(10) . ' }' . chr(10) . ' if (s == "z") {' . chr(10) . ' return "aa";' . chr(10) . ' }' . chr(10) . ' return "1";' . chr(10) . ' }' . chr(10) . ' var c = p5str_inc(s.substr(s.length-1, 1));' . chr(10) . ' if (c.length == 1) {' . chr(10) . ' return s.substr(0, s.length-1) + c;' . chr(10) . ' }' . chr(10) . ' return p5str_inc(s.substr(0, s.length-1)) + c.substr(c.length-1, 1);' . chr(10) . '};' . chr(10) . chr(10) . 'p5for = function(namespace, func, args, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' var v_old = namespace["v__"];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace["v__"] = args[i];' . chr(10) . ' try {' . chr(10) . ' func()' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { i--; _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' if (cont) {' . chr(10) . ' try {' . chr(10) . ' if (!_redo) { cont() }' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' namespace["v__"] = v_old;' . chr(10) . '};' . chr(10) . chr(10) . 'p5for_lex = function(func, args, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' try {' . chr(10) . ' func(args[i])' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { i--; _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' if (cont) {' . chr(10) . ' try {' . chr(10) . ' if (!_redo) { cont() }' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'p5while = function(func, cond, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' while (_redo || p5bool(cond())) {' . chr(10) . ' _redo = false;' . chr(10) . ' try {' . chr(10) . ' func()' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' if (cont) {' . chr(10) . ' try {' . chr(10) . ' if (!_redo) { cont() }' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' if (err instanceof p5_error && err.v == label) {' . chr(10) . ' if (err.type == ' . chr(39) . 'last' . chr(39) . ') { return }' . chr(10) . ' else if (err.type == ' . chr(39) . 'redo' . chr(39) . ') { _redo = true }' . chr(10) . ' else if (err.type != ' . chr(39) . 'next' . chr(39) . ') { throw(err) }' . chr(10) . ' } ' . chr(10) . ' else {' . chr(10) . ' throw(err)' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'p5map = function(namespace, func, args) {' . chr(10) . ' var v_old = namespace["v__"];' . chr(10) . ' var out = [];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace["v__"] = args[i];' . chr(10) . ' var o = p5list_to_a(func(1));' . chr(10) . ' for(var j = 0; j < o.length; j++) {' . chr(10) . ' out.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' namespace["v__"] = v_old;' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'p5grep = function(namespace, func, args) {' . chr(10) . ' var v_old = namespace["v__"];' . chr(10) . ' var out = [];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace["v__"] = args[i];' . chr(10) . ' if (p5bool(func(0))) {' . chr(10) . ' out.push(args[i])' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' namespace["v__"] = v_old;' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'p5sort = function(namespace, func, args) {' . chr(10) . ' var a_old = namespace["v_a"];' . chr(10) . ' var b_old = namespace["v_b"];' . chr(10) . ' var out = ' . chr(10) . ' func == null' . chr(10) . ' ? args.sort()' . chr(10) . ' : args.sort(' . chr(10) . ' function(a, b) {' . chr(10) . ' namespace["v_a"] = a;' . chr(10) . ' namespace["v_b"] = b;' . chr(10) . ' return func(0);' . chr(10) . ' }' . chr(10) . ' );' . chr(10) . ' namespace["v_a"] = a_old;' . chr(10) . ' namespace["v_b"] = b_old;' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'perl5_to_js = function( source, namespace, var_env_js, p5want ) {' . chr(10) . ' // CORE.say(["source: [" + source + "]"]);' . chr(10) . chr(10) . ' var strict_old = p5pkg["Perlito5"].v_STRICT;' . chr(10) . ' var var_env_js_old = p5pkg["Perlito5"].v_VAR;' . chr(10) . ' p5pkg["Perlito5"].v_VAR = var_env_js;' . chr(10) . chr(10) . ' var namespace_old = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' p5pkg["Perlito5"].v_PKG_NAME = namespace;' . chr(10) . chr(10) . ' match = p5call(p5pkg["Perlito5::Grammar"], "exp_stmts", [source, 0]);' . chr(10) . chr(10) . ' if ( !match || match._hash_.to != source.length ) {' . chr(10) . ' CORE.die(["Syntax error in eval near pos ", match._hash_.to]);' . chr(10) . ' }' . chr(10) . chr(10) . ' ast = p5pkg.CORE.bless([' . chr(10) . ' new p5HashRef({' . chr(10) . ' block: p5pkg.CORE.bless([' . chr(10) . ' new p5HashRef({' . chr(10) . ' stmts: p5pkg["Perlito5::Match"].flat([match])' . chr(10) . ' }),' . chr(10) . ' p5pkg["Perlito5::AST::Lit::Block"]' . chr(10) . ' ])' . chr(10) . ' }),' . chr(10) . ' p5pkg["Perlito5::AST::Do"]' . chr(10) . ' ]);' . chr(10) . chr(10) . ' // CORE.say(["ast: [" + ast + "]"]);' . chr(10) . ' js_code = p5call(ast, "emit_javascript2", [0, p5want]);' . chr(10) . ' // CORE.say(["js-source: [" + js_code + "]"]);' . chr(10) . chr(10) . ' p5pkg["Perlito5"].v_PKG_NAME = namespace_old;' . chr(10) . ' p5pkg["Perlito5"].v_VAR = var_env_js_old;' . chr(10) . ' p5pkg["Perlito5"].v_STRICT = strict_old;' . chr(10) . ' return js_code;' . chr(10) . '}' . chr(10) . chr(10))))
};
1;
@@ -9232,6 +9232,18 @@ sub Perlito5::Javascript2::Runtime::emit_javascript2 {
;
package main;
undef();
+package Perlito5::Javascript2::Array;
+sub Perlito5::Javascript2::Array::emit_javascript2 {
+ return ((('//' . chr(10) . '// lib/Perlito5/Javascript2/Runtime.js' . chr(10) . '//' . chr(10) . '// Runtime for "Perlito" Perl5-in-Javascript2' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . '//-------- Array ' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' return this[i] ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i, v) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = v;' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5ArrayRef([]) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5HashRef({}) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' this.unshift(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' this.push(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'p5tie_array = function(v, List__) {' . chr(10) . ' var pkg_name = p5str(List__.shift());' . chr(10) . chr(10) . ' var res = p5call(pkg_name, ' . chr(39) . 'TIEARRAY' . chr(39) . ', List__, null);' . chr(10) . ' ' . chr(10) . ' // TODO' . chr(10) . ' ' . chr(10) . ' // A class implementing an ordinary array should have the following methods:' . chr(10) . ' // TIEARRAY pkg_name, LIST' . chr(10) . ' // FETCH this, key' . chr(10) . ' // STORE this, key, value' . chr(10) . ' // FETCHSIZE this' . chr(10) . ' // STORESIZE this, count' . chr(10) . ' // CLEAR this' . chr(10) . ' // PUSH this, LIST' . chr(10) . ' // POP this' . chr(10) . ' // SHIFT this' . chr(10) . ' // UNSHIFT this, LIST' . chr(10) . ' // SPLICE this, offset, length, LIST' . chr(10) . ' // EXTEND this, count' . chr(10) . ' // DESTROY this' . chr(10) . ' // UNTIE this' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i, value) {' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5incr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5incr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5decr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5decr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5ArrayRef([]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5HashRef({});' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5untie", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) { return p5call(res, ' . chr(39) . 'UNTIE' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "shift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'SHIFT' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "pop", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'POP' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' p5call(res, ' . chr(39) . 'UNSHIFT' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' p5call(res, ' . chr(39) . 'PUSH' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5untie_array = function(v) {' . chr(10) . ' if (v.hasOwnProperty(' . chr(39) . 'p5untie' . chr(39) . ')) {' . chr(10) . ' var res = v.p5untie(); // call UNTIE' . chr(10) . ' delete v.p5aget;' . chr(10) . ' delete v.p5aset;' . chr(10) . ' delete v.p5incr;' . chr(10) . ' delete v.p5postincr;' . chr(10) . ' delete v.p5decr;' . chr(10) . ' delete v.p5postdecr;' . chr(10) . ' delete v.p5aget_array;' . chr(10) . ' delete v.p5aget_hash;' . chr(10) . ' delete v.p5untie;' . chr(10) . ' delete v.shift;' . chr(10) . ' delete v.pop;' . chr(10) . ' delete v.p5unshift;' . chr(10) . ' delete v.p5push;' . chr(10) . ' return res;' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . chr(10))))
+};
+1;
+
+;
+
+# use Perlito5::Javascript2::Array
+;
+package main;
+undef();
package Perlito5::Javascript2::CORE;
sub Perlito5::Javascript2::CORE::emit_javascript2 {
return ((('//' . chr(10) . '//' . chr(10) . '// lib/Perlito5/Javascript2/CORE.js' . chr(10) . '//' . chr(10) . '// CORE functions for "Perlito" Perl5-in-Javascript2' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . 'var CORE = p5pkg.CORE;' . chr(10) . chr(10) . 'var isNode = typeof require != "undefined";' . chr(10) . 'if (isNode) {' . chr(10) . ' CORE.print = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' process.stdout.write(p5str(List__[i]));' . chr(10) . ' }' . chr(10) . ' return 1;' . chr(10) . ' }' . chr(10) . '} else {' . chr(10) . ' CORE.print = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' write(p5str(List__[i]));' . chr(10) . ' }' . chr(10) . ' return 1;' . chr(10) . ' };' . chr(10) . '}' . chr(10) . chr(10) . 'CORE.say = function(List__) {' . chr(10) . ' CORE.print(List__);' . chr(10) . ' return CORE.print(["' . chr(92) . 'n"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.die = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' var s = "";' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' s = s + p5str(List__[i]);' . chr(10) . ' }' . chr(10) . ' try {' . chr(10) . ' s = s + "' . chr(92) . 'n" + new Error().stack;' . chr(10) . ' }' . chr(10) . ' catch(err) { }' . chr(10) . ' p5pkg["main"]["v_@"] = "Died: " + s;' . chr(10) . ' throw(new p5_error("die", "Died: " + s));' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.warn = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' var s = "";' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' s = s + p5str(List__[i]);' . chr(10) . ' }' . chr(10) . ' try {' . chr(10) . ' s = s + "' . chr(92) . 'n" + new Error().stack;' . chr(10) . ' }' . chr(10) . ' catch(err) { }' . chr(10) . ' CORE.print(["Warning: " + s + "' . chr(92) . 'n"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.bless = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var pkg_name = List__[1];' . chr(10) . ' if (typeof pkg_name === "object") {' . chr(10) . ' // bless {}, Class' . chr(10) . ' o._class_ = pkg_name;' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (!p5pkg.hasOwnProperty(pkg_name)) {' . chr(10) . ' p5make_package(pkg_name);' . chr(10) . ' }' . chr(10) . ' o._class_ = p5pkg[pkg_name];' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.chr = function(List__) {' . chr(10) . ' var v = p5num(List__[0]);' . chr(10) . ' return String.fromCharCode(v >= 0 ? v : 65533);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.ord = function(List__) {' . chr(10) . ' return p5str(List__[0]).charCodeAt(0);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.oct = function(List__) {' . chr(10) . ' var v = List__[0];' . chr(10) . ' var b = v.substr(0,2);' . chr(10) . ' v = v.replace("_", "");' . chr(10) . ' if (b == "0b" || b == "0B") { return parseInt(v.substr(2), 2) }' . chr(10) . ' if (b == "0x" || b == "0X") { return parseInt(v.substr(2), 16) }' . chr(10) . ' return parseInt(v, 8);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.abs = function(List__) { return Math.abs(List__[0]) };' . chr(10) . 'CORE.exp = function(List__) { return Math.exp(List__[0]) };' . chr(10) . 'CORE.log = function(List__) { return Math.log(List__[0]) };' . chr(10) . 'CORE.cos = function(List__) { return Math.cos(List__[0]) };' . chr(10) . 'CORE.sin = function(List__) { return Math.sin(List__[0]) };' . chr(10) . 'CORE.sqrt = function(List__) { return Math.sqrt(List__[0]) };' . chr(10) . 'CORE.atan2 = function(List__) { return Math.atan2(List__[0], List__[1]) };' . chr(10) . 'CORE.int = function(List__) { return List__[0] > 0 ? Math.floor(List__[0]) : Math.ceil(List__[0]) };' . chr(10) . chr(10) . 'CORE.rand = function(List__) {' . chr(10) . ' var v = p5num(List__[0]) || 1;' . chr(10) . ' return Math.random() * v;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.lc = function(List__) { return p5str(List__[0]).toLowerCase() };' . chr(10) . 'CORE.uc = function(List__) { return p5str(List__[0]).toUpperCase() };' . chr(10) . chr(10) . 'CORE.lcfirst = function(List__) {' . chr(10) . ' var s = p5str(List__[0]);' . chr(10) . ' var c = s.length > 0 ? s.slice(0,1).toLowerCase() : "";' . chr(10) . ' s = s.length > 1 ? s.substr(1) : "";' . chr(10) . ' return c + s' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.ucfirst = function(List__) {' . chr(10) . ' var s = p5str(List__[0]);' . chr(10) . ' var c = s.length > 0 ? s.slice(0,1).toUpperCase() : "";' . chr(10) . ' s = s.length > 1 ? s.substr(1) : "";' . chr(10) . ' return c + s' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.quotemeta = function(List__) {' . chr(10) . ' var s = p5str(List__[0]);' . chr(10) . ' var out = [];' . chr(10) . ' for(var i = 0; i < s.length; i++) {' . chr(10) . ' if (s.substr(i, 1).match(/[^0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz]/)) {' . chr(10) . ' out.push(String.fromCharCode(92));' . chr(10) . ' }' . chr(10) . ' out.push(s.substr(i, 1));' . chr(10) . ' }' . chr(10) . ' return out.join(""); ' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.substr = function(List__) {' . chr(10) . ' var expr = List__[0];' . chr(10) . ' var offset = List__[1];' . chr(10) . ' var length = List__[2];' . chr(10) . ' var replacement = List__[3];' . chr(10) . ' if (length < 0) {' . chr(10) . ' var s = p5str(expr);' . chr(10) . ' length = s.length - offset + length;' . chr(10) . ' } ' . chr(10) . ' return p5str(expr).substr(offset, length);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.values = function(List__, p5want) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' delete o["_each_"];' . chr(10) . ' if (p5want) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return [];' . chr(10) . ' };' . chr(10) . ' if (typeof o.values === "function") {' . chr(10) . ' return o.values();' . chr(10) . ' }' . chr(10) . ' var out = [];' . chr(10) . ' for (var i in o) {' . chr(10) . ' out.push(o[i]);' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . ' }' . chr(10) . ' return CORE.keys(List__, p5want);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.keys = function(List__, p5want) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' delete o["_each_"];' . chr(10) . ' if (p5want) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return [];' . chr(10) . ' }' . chr(10) . ' if (typeof o.keys === "function") {' . chr(10) . ' return o.keys();' . chr(10) . ' }' . chr(10) . ' var out = [];' . chr(10) . ' for (var i in o) {' . chr(10) . ' out.push(i);' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' if (o == null) {' . chr(10) . ' return 0;' . chr(10) . ' }' . chr(10) . ' if (typeof o.keys === "function") {' . chr(10) . ' return CORE.scalar([o.keys()]);' . chr(10) . ' }' . chr(10) . ' var out = 0;' . chr(10) . ' for (var i in o) {' . chr(10) . ' out++;' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.each = function(List__, p5want) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o.hasOwnProperty("_each_")) {' . chr(10) . ' return o._each_(p5want)' . chr(10) . ' }' . chr(10) . ' var keys = CORE.keys([o], 1);' . chr(10) . ' var i = 0;' . chr(10) . ' o._each_ = function () {' . chr(10) . ' if (i < keys.length) {' . chr(10) . ' i++;' . chr(10) . ' return p5want ? [keys[i-1], o[keys[i-1]]] : keys[i-1];' . chr(10) . ' }' . chr(10) . ' i = 0;' . chr(10) . ' return p5want ? [] : null;' . chr(10) . ' };' . chr(10) . ' return o._each_(p5want);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.reverse = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (typeof o === "string") {' . chr(10) . ' return o.split("").reverse().join("")' . chr(10) . ' }' . chr(10) . ' var out = [];' . chr(10) . ' for(var i = 0; i < o.length; i++) {' . chr(10) . ' out.unshift(o[i]);' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.splice = function(List__, p5want) {' . chr(10) . ' var array = List__.shift();' . chr(10) . ' // CORE.say([ array ]);' . chr(10) . ' var offset = p5num(List__.shift());' . chr(10) . ' var limit = List__.length ? p5num(List__.shift()) : (array.length + 1);' . chr(10) . chr(10) . ' if (limit < 0) {' . chr(10) . ' limit = array.length + limit - 1;' . chr(10) . ' }' . chr(10) . chr(10) . ' var list = [offset, limit];' . chr(10) . ' for(var i = 0; i < List__.length; i++) {' . chr(10) . ' list = p5list_to_a( list, List__[i]);' . chr(10) . ' }' . chr(10) . chr(10) . ' out = array.splice.apply(array, list);' . chr(10) . ' // CORE.say([ CORE.join([":",array]), " ofs=", offset, " lim=", limit, " list=", list, " out=", CORE.join([":",out]) ]);' . chr(10) . ' return p5want ? out : out.pop();' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.join = function(List__) {' . chr(10) . ' var s = List__[0];' . chr(10) . ' var o = List__[1];' . chr(10) . ' return o.join(s);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.index = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' try {' . chr(10) . ' return o.indexOf(s, p5num(List__[2]));' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' return -1;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . 'CORE.rindex = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' try {' . chr(10) . ' if (List__.length > 2) {' . chr(10) . ' var i = p5num(List__[2]);' . chr(10) . ' if (i < 0) {' . chr(10) . ' if (s.length == 0) {' . chr(10) . ' return 0;' . chr(10) . ' }' . chr(10) . ' return -1;' . chr(10) . ' }' . chr(10) . ' return o.lastIndexOf(s, i);' . chr(10) . ' }' . chr(10) . ' return o.lastIndexOf(s);' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' return -1;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.length = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (typeof o.string === "function") {' . chr(10) . ' return o.string().length;' . chr(10) . ' }' . chr(10) . ' return o.length;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.pack = function(List__) { CORE.warn([ "CORE::pack not implemented" ]) };' . chr(10) . 'CORE.unpack = function(List__) { CORE.warn([ "CORE::unpack not implemented" ]) };' . chr(10) . chr(10) . 'CORE.ref = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (o._class_ && typeof o._class_._ref_ === "string") {' . chr(10) . ' // blessed reference' . chr(10) . ' return o._class_._ref_;' . chr(10) . ' }' . chr(10) . ' if (typeof o._ref_ === "string") {' . chr(10) . ' // un-blessed reference' . chr(10) . ' return o._ref_;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "function") {' . chr(10) . ' return "CODE";' . chr(10) . ' }' . chr(10) . ' return "";' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.split = function(List__) {' . chr(10) . ' var pattern = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' var limit = List__[2];' . chr(10) . ' if (typeof pattern === "string") {' . chr(10) . ' if (pattern == " ") {' . chr(10) . ' var res = [];' . chr(10) . ' for (var i_ = 0, a_ = s.split(/(?: |' . chr(92) . 'n)+/); i_ < a_.length ; i_++) {' . chr(10) . ' if (a_[i_] != "") {' . chr(10) . ' res.push(a_[i_])' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . ' }' . chr(10) . ' return s.split(pattern);' . chr(10) . ' }' . chr(10) . ' CORE.die(["not implemented"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.prototype = function(List__, data) {' . chr(10) . ' var name = List__[0];' . chr(10) . ' // TODO - fully qualify "name" using information from "data"' . chr(10) . ' // XXX - lookup in CORE::GLOBAL?' . chr(10) . ' p5pkg["Perlito5"].v_PROTO._hash_[name] || p5pkg["Perlito5"].v_CORE_PROTO._hash_[name]' . chr(10) . '};' . chr(10) . chr(10))))
@@ -11912,6 +11924,7 @@ package Perlito5::Runtime;
say('// Do not edit this file - Generated by ', $_V5_COMPILER_NAME, ' ', $_V5_COMPILER_VERSION);
if ($expand_use) {
print(Perlito5::Javascript2::Runtime->emit_javascript2());
+ print(Perlito5::Javascript2::Array->emit_javascript2());
print(Perlito5::Javascript2::CORE->emit_javascript2());
print(Perlito5::Javascript2::IO->emit_javascript2());
print(Perlito5::Javascript2::Sprintf->emit_javascript2())
View
284 src5/lib/Perlito5/Javascript2/Array.pm
@@ -0,0 +1,284 @@
+use v5;
+
+package Perlito5::Javascript2::Array;
+
+sub emit_javascript2 {
+
+ return <<'EOT';
+//
+// lib/Perlito5/Javascript2/Runtime.js
+//
+// Runtime for "Perlito" Perl5-in-Javascript2
+//
+// AUTHORS
+//
+// Flavio Soibelmann Glock fglock@gmail.com
+//
+// COPYRIGHT
+//
+// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the same terms as Perl itself.
+//
+// See http://www.perl.com/perl/misc/Artistic.html
+
+//-------- Array
+
+Object.defineProperty( Array.prototype, "p5aget", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ return this[i]
+ }
+});
+Object.defineProperty( Array.prototype, "p5aset", {
+ enumerable : false,
+ value : function (i, v) {
+ if (i < 0) { i = this.length + i };
+ this[i] = v;
+ return this[i]
+ }
+});
+
+Object.defineProperty( Array.prototype, "p5incr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ this[i] = p5incr(this[i]);
+ return this[i];
+ }
+});
+Object.defineProperty( Array.prototype, "p5postincr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ var v = this[i];
+ this[i] = p5incr(this[i]);
+ return v;
+ }
+});
+Object.defineProperty( Array.prototype, "p5decr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ this[i] = p5decr(this[i]);
+ return this[i];
+ }
+});
+Object.defineProperty( Array.prototype, "p5postdecr", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ var v = this[i];
+ this[i] = p5decr(this[i]);
+ return v;
+ }
+});
+
+Object.defineProperty( Array.prototype, "p5aget_array", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ if (this[i] == null) { this[i] = new p5ArrayRef([]) }
+ return this[i]
+ }
+});
+Object.defineProperty( Array.prototype, "p5aget_hash", {
+ enumerable : false,
+ value : function (i) {
+ if (i < 0) { i = this.length + i };
+ if (this[i] == null) { this[i] = new p5HashRef({}) }
+ return this[i]
+ }
+});
+Object.defineProperty( Array.prototype, "p5unshift", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = args.length-1; i >= 0; i--) {
+ this.unshift(args[i]);
+ }
+ return this.length;
+ }
+});
+Object.defineProperty( Array.prototype, "p5push", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = 0; i < args.length; i++) {
+ this.push(args[i]);
+ }
+ return this.length;
+ }
+});
+
+p5tie_array = function(v, List__) {
+ var pkg_name = p5str(List__.shift());
+
+ var res = p5call(pkg_name, 'TIEARRAY', List__, null);
+
+ // TODO
+
+ // A class implementing an ordinary array should have the following methods:
+ // TIEARRAY pkg_name, LIST
+ // FETCH this, key
+ // STORE this, key, value
+ // FETCHSIZE this
+ // STORESIZE this, count
+ // CLEAR this
+ // PUSH this, LIST
+ // POP this
+ // SHIFT this
+ // UNSHIFT this, LIST
+ // SPLICE this, offset, length, LIST
+ // EXTEND this, count
+ // DESTROY this
+ // UNTIE this
+
+ Object.defineProperty( v, "p5aget", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ return p5call(res, 'FETCH', [i]);
+ }
+ });
+ Object.defineProperty( v, "p5aset", {
+ enumerable : false,
+ configurable : true,
+ value : function (i, value) {
+ p5call(res, 'STORE', [i, value]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5incr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5incr(p5call(res, 'FETCH', [i]));
+ p5call(res, 'STORE', [i, value]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5postincr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ p5call(res, 'STORE', [i, p5incr(value)]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5decr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5decr(p5call(res, 'FETCH', [i]));
+ p5call(res, 'STORE', [i, value]);
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5postdecr", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ p5call(res, 'STORE', [i, p5decr(value)]);
+ return value;
+ }
+ });
+
+ Object.defineProperty( v, "p5aget_array", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ if (value == null) {
+ value = new p5ArrayRef([]);
+ p5call(res, 'STORE', [i, value]);
+ }
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5aget_hash", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) {
+ var value = p5call(res, 'FETCH', [i]);
+ if (value == null) {
+ value = new p5HashRef({});
+ p5call(res, 'STORE', [i, value]);
+ }
+ return value;
+ }
+ });
+ Object.defineProperty( v, "p5untie", {
+ enumerable : false,
+ configurable : true,
+ value : function (i) { return p5call(res, 'UNTIE', []) }
+ });
+ Object.defineProperty( v, "shift", {
+ enumerable : false,
+ configurable : true,
+ value : function () { return p5call(res, 'SHIFT', []) }
+ });
+ Object.defineProperty( v, "pop", {
+ enumerable : false,
+ configurable : true,
+ value : function () { return p5call(res, 'POP', []) }
+ });
+ Object.defineProperty( v, "p5unshift", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = args.length-1; i >= 0; i--) {
+ p5call(res, 'UNSHIFT', [args[i]]);
+ }
+ return p5call(res, 'FETCHSIZE', []);
+ }
+ });
+ Object.defineProperty( v, "p5push", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = 0; i < args.length; i++) {
+ p5call(res, 'PUSH', [args[i]]);
+ }
+ return p5call(res, 'FETCHSIZE', []);
+ }
+ });
+
+ return res;
+};
+
+p5untie_array = function(v) {
+ if (v.hasOwnProperty('p5untie')) {
+ var res = v.p5untie(); // call UNTIE
+ delete v.p5aget;
+ delete v.p5aset;
+ delete v.p5incr;
+ delete v.p5postincr;
+ delete v.p5decr;
+ delete v.p5postdecr;
+ delete v.p5aget_array;
+ delete v.p5aget_hash;
+ delete v.p5untie;
+ delete v.shift;
+ delete v.pop;
+ delete v.p5unshift;
+ delete v.p5push;
+ return res;
+ }
+ else {
+ return null;
+ }
+};
+
+
+EOT
+
+} # end of emit_javascript2()
+
+1;
+
+
View
254 src5/lib/Perlito5/Javascript2/Runtime.pm
@@ -234,6 +234,8 @@ function p5cleanup_local(idx, value) {
return value;
}
+//-------- Reference
+
function p5HashRef(o) {
this._hash_ = o;
this._ref_ = "HASH";
@@ -258,258 +260,6 @@ function p5GlobRef(o) {
this.bool = function() { return 1 };
}
-
-//-------- Array
-
-Object.defineProperty( Array.prototype, "p5aget", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- return this[i]
- }
-});
-Object.defineProperty( Array.prototype, "p5aset", {
- enumerable : false,
- value : function (i, v) {
- if (i < 0) { i = this.length + i };
- this[i] = v;
- return this[i]
- }
-});
-
-Object.defineProperty( Array.prototype, "p5incr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- this[i] = p5incr(this[i]);
- return this[i];
- }
-});
-Object.defineProperty( Array.prototype, "p5postincr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- var v = this[i];
- this[i] = p5incr(this[i]);
- return v;
- }
-});
-Object.defineProperty( Array.prototype, "p5decr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- this[i] = p5decr(this[i]);
- return this[i];
- }
-});
-Object.defineProperty( Array.prototype, "p5postdecr", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- var v = this[i];
- this[i] = p5decr(this[i]);
- return v;
- }
-});
-
-Object.defineProperty( Array.prototype, "p5aget_array", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- if (this[i] == null) { this[i] = new p5ArrayRef([]) }
- return this[i]
- }
-});
-Object.defineProperty( Array.prototype, "p5aget_hash", {
- enumerable : false,
- value : function (i) {
- if (i < 0) { i = this.length + i };
- if (this[i] == null) { this[i] = new p5HashRef({}) }
- return this[i]
- }
-});
-Object.defineProperty( Array.prototype, "p5unshift", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = args.length-1; i >= 0; i--) {
- this.unshift(args[i]);
- }
- return this.length;
- }
-});
-Object.defineProperty( Array.prototype, "p5push", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = 0; i < args.length; i++) {
- this.push(args[i]);
- }
- return this.length;
- }
-});
-
-p5tie_array = function(v, List__) {
- var pkg_name = p5str(List__.shift());
-
- var res = p5call(pkg_name, 'TIEARRAY', List__, null);
-
- // TODO
-
- // A class implementing an ordinary array should have the following methods:
- // TIEARRAY pkg_name, LIST
- // FETCH this, key
- // STORE this, key, value
- // FETCHSIZE this
- // STORESIZE this, count
- // CLEAR this
- // PUSH this, LIST
- // POP this
- // SHIFT this
- // UNSHIFT this, LIST
- // SPLICE this, offset, length, LIST
- // EXTEND this, count
- // DESTROY this
- // UNTIE this
-
- Object.defineProperty( v, "p5aget", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- return p5call(res, 'FETCH', [i]);
- }
- });
- Object.defineProperty( v, "p5aset", {
- enumerable : false,
- configurable : true,
- value : function (i, value) {
- p5call(res, 'STORE', [i, value]);
- return value;
- }
- });
- Object.defineProperty( v, "p5incr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5incr(p5call(res, 'FETCH', [i]));
- p5call(res, 'STORE', [i, value]);
- return value;
- }
- });
- Object.defineProperty( v, "p5postincr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- p5call(res, 'STORE', [i, p5incr(value)]);
- return value;
- }
- });
- Object.defineProperty( v, "p5decr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5decr(p5call(res, 'FETCH', [i]));
- p5call(res, 'STORE', [i, value]);
- return value;
- }
- });
- Object.defineProperty( v, "p5postdecr", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- p5call(res, 'STORE', [i, p5decr(value)]);
- return value;
- }
- });
-
- Object.defineProperty( v, "p5aget_array", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- if (value == null) {
- value = new p5ArrayRef([]);
- p5call(res, 'STORE', [i, value]);
- }
- return value;
- }
- });
- Object.defineProperty( v, "p5aget_hash", {
- enumerable : false,
- configurable : true,
- value : function (i) {
- var value = p5call(res, 'FETCH', [i]);
- if (value == null) {
- value = new p5HashRef({});
- p5call(res, 'STORE', [i, value]);
- }
- return value;
- }
- });
- Object.defineProperty( v, "p5untie", {
- enumerable : false,
- configurable : true,
- value : function (i) { return p5call(res, 'UNTIE', []) }
- });
- Object.defineProperty( v, "shift", {
- enumerable : false,
- configurable : true,
- value : function () { return p5call(res, 'SHIFT', []) }
- });
- Object.defineProperty( v, "pop", {
- enumerable : false,
- configurable : true,
- value : function () { return p5call(res, 'POP', []) }
- });
- Object.defineProperty( v, "p5unshift", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = args.length-1; i >= 0; i--) {
- p5call(res, 'UNSHIFT', [args[i]]);
- }
- return p5call(res, 'FETCHSIZE', []);
- }
- });
- Object.defineProperty( v, "p5push", {
- enumerable : false,
- configurable : true,
- value : function (args) {
- for(var i = 0; i < args.length; i++) {
- p5call(res, 'PUSH', [args[i]]);
- }
- return p5call(res, 'FETCHSIZE', []);
- }
- });
-
- return res;
-};
-
-p5untie_array = function(v) {
- if (v.hasOwnProperty('p5untie')) {
- var res = v.p5untie(); // call UNTIE
- delete v.p5aget;
- delete v.p5aset;
- delete v.p5incr;
- delete v.p5postincr;
- delete v.p5decr;
- delete v.p5postdecr;
- delete v.p5aget_array;
- delete v.p5aget_hash;
- delete v.p5untie;
- delete v.shift;
- delete v.pop;
- delete v.p5unshift;
- delete v.p5push;
- return res;
- }
- else {
- return null;
- }
-};
-
//-------- Hash
Object.defineProperty( Object.prototype, "p5hget", {
View
2  src5/util/perlito5.pl
@@ -10,6 +10,7 @@ package Perlito;
use Perlito5::Javascript2::Emitter;
use Perlito5::Javascript2::Runtime;
+use Perlito5::Javascript2::Array;
use Perlito5::Javascript2::CORE;
use Perlito5::Javascript2::IO;
use Perlito5::Javascript2::Sprintf;
@@ -231,6 +232,7 @@ package Perlito;
say "// Do not edit this file - Generated by ", $_V5_COMPILER_NAME, " ", $_V5_COMPILER_VERSION;
if ( $expand_use ) {
print Perlito5::Javascript2::Runtime->emit_javascript2();
+ print Perlito5::Javascript2::Array->emit_javascript2();
print Perlito5::Javascript2::CORE->emit_javascript2();
print Perlito5::Javascript2::IO->emit_javascript2();
print Perlito5::Javascript2::Sprintf->emit_javascript2();
Please sign in to comment.
Something went wrong with that request. Please try again.