Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Perlito5 - js - global autovivification fix

  • Loading branch information...
commit 5a6928d92f01d4bf820e6bc25dfeaba622ab6ec3 1 parent a80bdf6
@fglock authored
View
5 TODO-perlito5
@@ -259,8 +259,9 @@ TODO list for Perlito5
* Javascript backend
-- autovivification of fully-qualified array and hash globals
- $Exporter::Cache[2] = 3;
- # Cannot call method 'p5aset' of undefined
+ $#{"Exporter::Cache"};
+
+ ${"Exporter::Cache"}[2] = 3;
See: p5scalar_deref() / p5scalar_deref_set() in Perlito5/Javascript2/Runtime.pm
View
42 html/perlito5.js
32 additions, 10 deletions not shown
View
10 perlito5.pl
@@ -8969,7 +8969,13 @@ package Perlito5::AST::Var;
if ($self->{'namespace'}) {
$ns = ('p5make_package("' . $self->{'namespace'} . '")');
if (($self->{'sigil'} eq '$#')) {
- return ('(' . $ns . '["' . $table->{'@'} . $str_name . '"].length - 1)')
+ return ('(p5global_array("' . $self->{'namespace'} . '", "' . $str_name . '").length - 1)')
+ };
+ if (($self->{'sigil'} eq '@')) {
+ return ('p5global_array("' . $self->{'namespace'} . '", "' . $str_name . '")')
+ };
+ if (($self->{'sigil'} eq '%')) {
+ return ('p5global_hash("' . $self->{'namespace'} . '", "' . $str_name . '")')
};
return ($ns . '["' . $table->{$self->{'sigil'}} . $str_name . '"]')
};
@@ -10113,7 +10119,7 @@ sub Perlito5::Javascript2::Runtime::perl5_to_js {
return $js_code
};
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) . ' p5pkg[pkg_name]._is_package_ = 1;' . 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 be a function already' . chr(10) . ' if (typeof sub_name === "function") {' . chr(10) . ' return sub_name;' . chr(10) . ' }' . 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) . ' if (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], seen);' . 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) . 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, p5want) {' . chr(10) . chr(10) . ' if (typeof invocant === "string") {' . chr(10) . ' list.unshift(invocant);' . chr(10) . ' invocant = p5make_package(invocant);' . chr(10) . ' }' . chr(10) . ' else if ( invocant.hasOwnProperty("_is_package_") ) {' . chr(10) . ' list.unshift(invocant._ref_); // invocant is a "package" object' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' list.unshift(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, p5want)' . chr(10) . ' }' . chr(10) . ' var m = p5method_lookup(method, invocant._class_._ref_, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list, p5want)' . 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, p5want)' . 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, p5want);' . 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) . 'function p5call_sub(namespace, name, list, p5want) {' . chr(10) . ' if(p5pkg[namespace].hasOwnProperty(name)) {' . chr(10) . ' return p5pkg[namespace][name](list, p5want)' . chr(10) . ' }' . chr(10) . ' if(p5pkg[namespace].hasOwnProperty("AUTOLOAD")) {' . chr(10) . ' p5pkg[namespace]["v_AUTOLOAD"] = namespace + "::" + name;' . chr(10) . ' return p5pkg[namespace]["AUTOLOAD"](list, p5want)' . chr(10) . ' }' . chr(10) . ' p5pkg.CORE.die(["Undefined subroutine &" + namespace + "::" + name]);' . chr(10) . '}' . chr(10) . chr(10) . 'function p5scalar_deref(v) {' . chr(10) . ' if (typeof v === "string") {' . chr(10) . ' var pkg_name = v.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' v = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' pkg_name = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' }' . chr(10) . ' var c = v.charCodeAt(0);' . chr(10) . ' if (c < 27) {' . chr(10) . ' v = String.fromCharCode(c + 64) + v.substr(1);' . chr(10) . ' pkg_name = ' . chr(39) . 'main' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' return p5make_package(pkg_name)["v_"+v];' . chr(10) . ' }' . chr(10) . ' return v._scalar_;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5scalar_deref_set(v, n) {' . chr(10) . ' if (typeof v === "string") {' . chr(10) . ' var pkg_name = v.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' v = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' pkg_name = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' }' . chr(10) . ' var c = v.charCodeAt(0);' . chr(10) . ' if (c < 27) {' . chr(10) . ' v = String.fromCharCode(c + 64) + v.substr(1);' . chr(10) . ' pkg_name = ' . chr(39) . 'main' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' p5make_package(pkg_name)["v_"+v] = n;' . chr(10) . ' return p5pkg[pkg_name]["v_"+v];' . chr(10) . ' }' . chr(10) . ' v._scalar_ = n;' . chr(10) . ' return v._scalar_;' . chr(10) . '}' . chr(10) . chr(10) . 'p5make_package("main");' . chr(10) . 'p5make_package("Perlito5");' . chr(10) . 'p5pkg["Perlito5"].v_PKG_NAME = "main";' . chr(10) . 'p5pkg["main"]["v_@"] = []; // $@' . chr(10) . 'p5pkg["main"]["v_|"] = 0; // $|' . chr(10) . 'p5pkg["main"]["v_/"] = "' . chr(92) . 'n"; // $/' . chr(10) . 'p5pkg["main"][' . chr(39) . 'v_"' . chr(39) . '] = " "; // $"' . chr(10) . 'p5pkg["main"]["List_#"] = []; // @#' . chr(10) . 'p5scalar_deref_set(String.fromCharCode(15), isNode ? "node.js" : "javascript2"); // $^O' . 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::IO");' . chr(10) . 'p5make_package("Perlito5::Runtime");' . chr(10) . 'p5make_package("Perlito5::Grammar");' . 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 p5typeglob_set(namespace, name, obj) {' . chr(10) . ' p5make_package(namespace);' . chr(10) . ' if ( obj.hasOwnProperty("_ref_") ) {' . chr(10) . ' if ( obj._ref_ == "HASH" ) {' . chr(10) . ' p5pkg[namespace][sigils[' . chr(39) . '%' . chr(39) . '] + name] = obj._hash_;' . chr(10) . ' }' . chr(10) . ' else if ( obj._ref_ == "ARRAY" ) {' . chr(10) . ' p5pkg[namespace][sigils[' . chr(39) . '@' . chr(39) . '] + name] = obj._array_;' . chr(10) . ' }' . chr(10) . ' else if ( obj._ref_ == "SCALAR" ) {' . chr(10) . ' p5pkg[namespace][sigils[' . chr(39) . '$' . chr(39) . '] + name] = obj._scalar_;' . chr(10) . ' }' . chr(10) . ' else if ( obj._ref_ == "GLOB" ) {' . chr(10) . ' // TODO' . chr(10) . ' p5pkg[namespace][name] = obj;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' p5pkg[namespace][name] = obj; // CODE' . chr(10) . ' // TODO - non-reference' . chr(10) . ' }' . chr(10) . ' return p5pkg[namespace][name]; // TODO - return GLOB' . chr(10) . '}' . chr(10) . chr(10) . 'function p5typeglob_deref_set(v, obj) {' . chr(10) . ' if (typeof v === "string") {' . chr(10) . ' var pkg_name = v.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' v = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' pkg_name = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' }' . chr(10) . ' return p5typeglob_set(pkg_name, v, obj);' . chr(10) . ' }' . chr(10) . ' CORE.die(["TODO: can' . chr(39) . 't p5typeglob_deref_set()"]);' . chr(10) . '}' . 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) . 'var p5id = Math.floor(Math.random() * 1000000000) + 1000000000;' . 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) . '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") {' . chr(10) . chr(10) . ' if (o instanceof Array) {' . chr(10) . ' return CORE.join(["", o]);' . chr(10) . ' }' . chr(10) . chr(10) . ' if ( o.hasOwnProperty("_ref_") ) {' . chr(10) . ' if (!o._id_) { o._id_ = p5id++ }' . chr(10) . ' return [o._ref_, ' . chr(39) . '(0x' . chr(39) . ', o._id_.toString( 16 ), ' . chr(39) . ')' . chr(39) . '].join(' . chr(39) . chr(39) . ');' . chr(10) . ' }' . chr(10) . 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) . ' var s = p5str(o).trim();' . chr(10) . ' var s1 = s.substr(0, 3).toUpperCase();' . chr(10) . ' if ( s1 == "NAN" ) { return NaN };' . chr(10) . ' if ( s1 == "INF" ) { return Infinity };' . chr(10) . ' s1 = s.substr(0, 4).toUpperCase();' . chr(10) . ' if ( s1 == "-NAN" ) { return NaN };' . chr(10) . ' if ( s1 == "-INF" ) { return -Infinity };' . chr(10) . ' s1 = parseFloat(s);' . chr(10) . ' if ( isNaN(s1) ) { return 0 };' . chr(10) . ' return s1;' . 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) . 'p5modulo = function(o, k) {' . chr(10) . ' var m = o % k;' . chr(10) . ' if ( k < 0 && m > 0 ) {' . chr(10) . ' m = m + k;' . chr(10) . ' }' . chr(10) . ' else if ( k > 0 && m < 0 ) {' . chr(10) . ' m = m + k;' . chr(10) . ' }' . chr(10) . ' return m;' . chr(10) . '};' . chr(10) . chr(10) . 'p5shift_left = function(o, k) {' . chr(10) . ' return k < 31 ? o << k : o * Math.pow(2, k);' . 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) . 'p5negative = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return ' . chr(39) . '-0' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return -(o.length);' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "number") {' . chr(10) . ' var s = p5str(o);' . chr(10) . ' s1 = parseFloat(s.trim());' . chr(10) . ' if ( isNaN(s1) ) {' . chr(10) . ' var c = s.substr(0, 1);' . chr(10) . ' if ( c == ' . chr(39) . '+' . chr(39) . ' ) { s = s.substr(1); return ' . chr(39) . '-' . chr(39) . ' + s }' . chr(10) . ' if ( c == ' . chr(39) . '-' . chr(39) . ' ) { s = s.substr(1); return ' . chr(39) . '+' . chr(39) . ' + s }' . chr(10) . ' if ( c.length && !c.match(/[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]/) ) {' . chr(10) . ' if ( s.trim().substr(0,1) == "-" ) { return 0 };' . chr(10) . ' return ' . chr(39) . '-0' . chr(39) . ';' . chr(10) . ' };' . chr(10) . ' return ' . chr(39) . '-' . chr(39) . ' + s' . chr(10) . ' };' . chr(10) . ' return -s1;' . chr(10) . ' }' . chr(10) . ' return -o;' . chr(10) . '};' . chr(10) . chr(10) . 'p5for = function(namespace, var_name, func, args, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' var v_old = namespace[var_name];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace[var_name] = 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[var_name] = 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)))
+ 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) . ' p5pkg[pkg_name]._is_package_ = 1;' . 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 be a function already' . chr(10) . ' if (typeof sub_name === "function") {' . chr(10) . ' return sub_name;' . chr(10) . ' }' . 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) . ' if (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], seen);' . 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) . 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, p5want) {' . chr(10) . chr(10) . ' if (typeof invocant === "string") {' . chr(10) . ' list.unshift(invocant);' . chr(10) . ' invocant = p5make_package(invocant);' . chr(10) . ' }' . chr(10) . ' else if ( invocant.hasOwnProperty("_is_package_") ) {' . chr(10) . ' list.unshift(invocant._ref_); // invocant is a "package" object' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' list.unshift(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, p5want)' . chr(10) . ' }' . chr(10) . ' var m = p5method_lookup(method, invocant._class_._ref_, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list, p5want)' . 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, p5want)' . 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, p5want);' . 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) . 'function p5call_sub(namespace, name, list, p5want) {' . chr(10) . ' if(p5pkg[namespace].hasOwnProperty(name)) {' . chr(10) . ' return p5pkg[namespace][name](list, p5want)' . chr(10) . ' }' . chr(10) . ' if(p5pkg[namespace].hasOwnProperty("AUTOLOAD")) {' . chr(10) . ' p5pkg[namespace]["v_AUTOLOAD"] = namespace + "::" + name;' . chr(10) . ' return p5pkg[namespace]["AUTOLOAD"](list, p5want)' . chr(10) . ' }' . chr(10) . ' p5pkg.CORE.die(["Undefined subroutine &" + namespace + "::" + name]);' . chr(10) . '}' . chr(10) . chr(10) . 'function p5scalar_deref(v) {' . chr(10) . ' if (typeof v === "string") {' . chr(10) . ' var pkg_name = v.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' v = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' pkg_name = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' }' . chr(10) . ' var c = v.charCodeAt(0);' . chr(10) . ' if (c < 27) {' . chr(10) . ' v = String.fromCharCode(c + 64) + v.substr(1);' . chr(10) . ' pkg_name = ' . chr(39) . 'main' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' return p5make_package(pkg_name)["v_"+v];' . chr(10) . ' }' . chr(10) . ' return v._scalar_;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5scalar_deref_set(v, n) {' . chr(10) . ' if (typeof v === "string") {' . chr(10) . ' var pkg_name = v.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' v = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' pkg_name = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' }' . chr(10) . ' var c = v.charCodeAt(0);' . chr(10) . ' if (c < 27) {' . chr(10) . ' v = String.fromCharCode(c + 64) + v.substr(1);' . chr(10) . ' pkg_name = ' . chr(39) . 'main' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' p5make_package(pkg_name)["v_"+v] = n;' . chr(10) . ' return p5pkg[pkg_name]["v_"+v];' . chr(10) . ' }' . chr(10) . ' v._scalar_ = n;' . chr(10) . ' return v._scalar_;' . chr(10) . '}' . chr(10) . chr(10) . 'function p5global_array(pkg_name, name) {' . chr(10) . ' v = "List_"+name;' . chr(10) . ' if (!p5make_package(pkg_name).hasOwnProperty(v)) {' . chr(10) . ' p5pkg[pkg_name][v] = [];' . chr(10) . ' }' . chr(10) . ' return p5pkg[pkg_name][v];' . chr(10) . '}' . chr(10) . chr(10) . 'function p5global_hash(pkg_name, name) {' . chr(10) . ' v = "Hash_"+name;' . chr(10) . ' if (!p5make_package(pkg_name).hasOwnProperty(v)) {' . chr(10) . ' p5pkg[pkg_name][v] = {};' . chr(10) . ' }' . chr(10) . ' return p5pkg[pkg_name][v];' . chr(10) . '}' . chr(10) . chr(10) . 'p5make_package("main");' . chr(10) . 'p5make_package("Perlito5");' . chr(10) . 'p5pkg["Perlito5"].v_PKG_NAME = "main";' . chr(10) . 'p5pkg["main"]["v_@"] = []; // $@' . chr(10) . 'p5pkg["main"]["v_|"] = 0; // $|' . chr(10) . 'p5pkg["main"]["v_/"] = "' . chr(92) . 'n"; // $/' . chr(10) . 'p5pkg["main"][' . chr(39) . 'v_"' . chr(39) . '] = " "; // $"' . chr(10) . 'p5pkg["main"]["List_#"] = []; // @#' . chr(10) . 'p5scalar_deref_set(String.fromCharCode(15), isNode ? "node.js" : "javascript2"); // $^O' . 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::IO");' . chr(10) . 'p5make_package("Perlito5::Runtime");' . chr(10) . 'p5make_package("Perlito5::Grammar");' . 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 p5typeglob_set(namespace, name, obj) {' . chr(10) . ' p5make_package(namespace);' . chr(10) . ' if ( obj.hasOwnProperty("_ref_") ) {' . chr(10) . ' if ( obj._ref_ == "HASH" ) {' . chr(10) . ' p5pkg[namespace][sigils[' . chr(39) . '%' . chr(39) . '] + name] = obj._hash_;' . chr(10) . ' }' . chr(10) . ' else if ( obj._ref_ == "ARRAY" ) {' . chr(10) . ' p5pkg[namespace][sigils[' . chr(39) . '@' . chr(39) . '] + name] = obj._array_;' . chr(10) . ' }' . chr(10) . ' else if ( obj._ref_ == "SCALAR" ) {' . chr(10) . ' p5pkg[namespace][sigils[' . chr(39) . '$' . chr(39) . '] + name] = obj._scalar_;' . chr(10) . ' }' . chr(10) . ' else if ( obj._ref_ == "GLOB" ) {' . chr(10) . ' // TODO' . chr(10) . ' p5pkg[namespace][name] = obj;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' p5pkg[namespace][name] = obj; // CODE' . chr(10) . ' // TODO - non-reference' . chr(10) . ' }' . chr(10) . ' return p5pkg[namespace][name]; // TODO - return GLOB' . chr(10) . '}' . chr(10) . chr(10) . 'function p5typeglob_deref_set(v, obj) {' . chr(10) . ' if (typeof v === "string") {' . chr(10) . ' var pkg_name = v.split(/::/);' . chr(10) . ' if (pkg_name.length > 1) {' . chr(10) . ' v = pkg_name.pop();' . chr(10) . ' pkg_name = pkg_name.join("::");' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' pkg_name = p5pkg["Perlito5"].v_PKG_NAME;' . chr(10) . ' }' . chr(10) . ' return p5typeglob_set(pkg_name, v, obj);' . chr(10) . ' }' . chr(10) . ' CORE.die(["TODO: can' . chr(39) . 't p5typeglob_deref_set()"]);' . chr(10) . '}' . 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) . 'var p5id = Math.floor(Math.random() * 1000000000) + 1000000000;' . 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) . '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") {' . chr(10) . chr(10) . ' if (o instanceof Array) {' . chr(10) . ' return CORE.join(["", o]);' . chr(10) . ' }' . chr(10) . chr(10) . ' if ( o.hasOwnProperty("_ref_") ) {' . chr(10) . ' if (!o._id_) { o._id_ = p5id++ }' . chr(10) . ' return [o._ref_, ' . chr(39) . '(0x' . chr(39) . ', o._id_.toString( 16 ), ' . chr(39) . ')' . chr(39) . '].join(' . chr(39) . chr(39) . ');' . chr(10) . ' }' . chr(10) . 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) . ' var s = p5str(o).trim();' . chr(10) . ' var s1 = s.substr(0, 3).toUpperCase();' . chr(10) . ' if ( s1 == "NAN" ) { return NaN };' . chr(10) . ' if ( s1 == "INF" ) { return Infinity };' . chr(10) . ' s1 = s.substr(0, 4).toUpperCase();' . chr(10) . ' if ( s1 == "-NAN" ) { return NaN };' . chr(10) . ' if ( s1 == "-INF" ) { return -Infinity };' . chr(10) . ' s1 = parseFloat(s);' . chr(10) . ' if ( isNaN(s1) ) { return 0 };' . chr(10) . ' return s1;' . 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) . 'p5modulo = function(o, k) {' . chr(10) . ' var m = o % k;' . chr(10) . ' if ( k < 0 && m > 0 ) {' . chr(10) . ' m = m + k;' . chr(10) . ' }' . chr(10) . ' else if ( k > 0 && m < 0 ) {' . chr(10) . ' m = m + k;' . chr(10) . ' }' . chr(10) . ' return m;' . chr(10) . '};' . chr(10) . chr(10) . 'p5shift_left = function(o, k) {' . chr(10) . ' return k < 31 ? o << k : o * Math.pow(2, k);' . 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) . 'p5negative = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return ' . chr(39) . '-0' . chr(39) . ';' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return -(o.length);' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "number") {' . chr(10) . ' var s = p5str(o);' . chr(10) . ' s1 = parseFloat(s.trim());' . chr(10) . ' if ( isNaN(s1) ) {' . chr(10) . ' var c = s.substr(0, 1);' . chr(10) . ' if ( c == ' . chr(39) . '+' . chr(39) . ' ) { s = s.substr(1); return ' . chr(39) . '-' . chr(39) . ' + s }' . chr(10) . ' if ( c == ' . chr(39) . '-' . chr(39) . ' ) { s = s.substr(1); return ' . chr(39) . '+' . chr(39) . ' + s }' . chr(10) . ' if ( c.length && !c.match(/[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]/) ) {' . chr(10) . ' if ( s.trim().substr(0,1) == "-" ) { return 0 };' . chr(10) . ' return ' . chr(39) . '-0' . chr(39) . ';' . chr(10) . ' };' . chr(10) . ' return ' . chr(39) . '-' . chr(39) . ' + s' . chr(10) . ' };' . chr(10) . ' return -s1;' . chr(10) . ' }' . chr(10) . ' return -o;' . chr(10) . '};' . chr(10) . chr(10) . 'p5for = function(namespace, var_name, func, args, cont, label) {' . chr(10) . ' var _redo = false;' . chr(10) . ' var v_old = namespace[var_name];' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' namespace[var_name] = 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[var_name] = 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)))
};
1;
View
8 src5/lib/Perlito5/Javascript2/Emitter.pm
@@ -1110,7 +1110,13 @@ package Perlito5::AST::Var;
if ($self->{namespace}) {
$ns = 'p5make_package("' . $self->{namespace} . '")';
if ($self->{sigil} eq '$#') {
- return '(' . $ns . '["' . $table->{'@'} . $str_name . '"].length - 1)';
+ return '(p5global_array("' . $self->{namespace} . '", "' . $str_name . '").length - 1)';
+ }
+ if ($self->{sigil} eq '@') {
+ return 'p5global_array("' . $self->{namespace} . '", "' . $str_name . '")';
+ }
+ if ($self->{sigil} eq '%') {
+ return 'p5global_hash("' . $self->{namespace} . '", "' . $str_name . '")';
}
return $ns . '["' . $table->{$self->{sigil}} . $str_name . '"]'
}
View
16 src5/lib/Perlito5/Javascript2/Runtime.pm
@@ -274,6 +274,22 @@ function p5scalar_deref_set(v, n) {
return v._scalar_;
}
+function p5global_array(pkg_name, name) {
+ v = "List_"+name;
+ if (!p5make_package(pkg_name).hasOwnProperty(v)) {
+ p5pkg[pkg_name][v] = [];
+ }
+ return p5pkg[pkg_name][v];
+}
+
+function p5global_hash(pkg_name, name) {
+ v = "Hash_"+name;
+ if (!p5make_package(pkg_name).hasOwnProperty(v)) {
+ p5pkg[pkg_name][v] = {};
+ }
+ return p5pkg[pkg_name][v];
+}
+
p5make_package("main");
p5make_package("Perlito5");
p5pkg["Perlito5"].v_PKG_NAME = "main";
Please sign in to comment.
Something went wrong with that request. Please try again.