Perlito5/JavaScript3/Runtime.pm untested

filter: Statementstmt 0.0% 0 / 4 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 7 CC 1 SCAR 6.9
CC
1
Cov
0%
emit_javascript3
2.0
BEGIN
2.0
CRAP 2.0
1 use v5;
2  
3 package Perlito5::JavaScript3::Runtime;
4  
5 sub emit_javascript3 {
6  
7 return <<'EOT';
8 //
9 // lib/Perlito5/JavaScript3/Runtime.js
10 //
11 // Runtime for "Perlito" Perl5-in-JavaScript3
12 //
13 // AUTHORS
14 //
15 // Flavio Soibelmann Glock fglock@gmail.com
16 //
17 // COPYRIGHT
18 //
19 // Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.
20 //
21 // This program is free software; you can redistribute it and/or modify it
22 // under the same terms as Perl itself.
23 //
24 // See http://www.perl.com/perl/misc/Artistic.html
25  
26 var isNode = typeof require != "undefined";
27  
28 if (typeof p5pkg !== "object") {
29 p5pkg = {};
30 p5LOCAL = [];
31  
32 var universal = function () {};
33 p5pkg.UNIVERSAL = new universal();
34 p5pkg.UNIVERSAL._ref_ = "UNIVERSAL";
35 p5pkg.UNIVERSAL.isa = function (List__) {
36 // TODO - use @ISA
37 var o = List__[0];
38 var s = p5str(List__[1]);
39 if (o instanceof p5Scalar) {
40 o = o.FETCH();
41 }
42 return o._class_._ref_ == s
43 };
44 p5pkg.UNIVERSAL.can = function (List__) {
45 var o = List__[0];
46 var s = p5str(List__[1]);
47 if (o instanceof p5Scalar) {
48 o = o.FETCH();
49 }
50 if ( s.indexOf("::") == -1 ) {
51 return p5method_lookup(s, o._class_._ref_, {})
52 }
53 var c = s.split("::");
54 s = c.pop();
55 return p5method_lookup(s, c.join("::"), {});
56 };
57 p5pkg.UNIVERSAL.DOES = p5pkg.UNIVERSAL.can;
58  
59 var core = function () {};
60 p5pkg["CORE"] = new core();
61 p5pkg["CORE"]._ref_ = "CORE";
62  
63 var core_global = function () {};
64 core_global.prototype = p5pkg.CORE;
65 p5pkg["CORE::GLOBAL"] = new core_global();
66 p5pkg["CORE::GLOBAL"]._ref_ = "CORE::GLOBAL";
67  
68 p5_error = function (type, v) {
69 this.type = type;
70 this.v = this.message = v;
71 this.toString = function(){
72 if (this.type == 'break') {
73 return 'Can\'t "break" outside a given block'
74 }
75 if (this.type == 'next' || this.type == 'last' || this.type == 'redo') {
76 if (this.v == "") { return 'Can\'t "' + this.type + '" outside a loop block' }
77 return 'Label not found for "' + this.type + ' ' + this.v + '"';
78 }
79 return this.v;
80 };
81 };
82 p5_error.prototype = Error.prototype;
83 }
84  
85 function p5make_package(pkg_name) {
86 if (!p5pkg.hasOwnProperty(pkg_name)) {
87 var tmp = function () {};
88 tmp.prototype = p5pkg["CORE::GLOBAL"];
89 p5pkg[pkg_name] = new tmp();
90 p5pkg[pkg_name]._ref_ = pkg_name;
91 p5pkg[pkg_name]._class_ = p5pkg[pkg_name]; // XXX memory leak
92  
93 // TODO - add the other package global variables
94 p5pkg[pkg_name]["List_ISA"] = new p5Array([]);
95 p5pkg[pkg_name]["v_a"] = new p5Scalar(null);
96 p5pkg[pkg_name]["v_b"] = new p5Scalar(null);
97 p5pkg[pkg_name]["v__"] = new p5Scalar(null);
98 p5pkg[pkg_name]["v_AUTOLOAD"] = new p5Scalar(null);
99 }
100 return p5pkg[pkg_name];
101 }
102  
103 function p5code_lookup_by_name(package_name, sub_name) {
104 // sub_name can have an optional namespace
105 var parts = sub_name.split(/::/);
106 if (parts.length > 1) {
107 sub_name = parts.pop();
108 package_name = parts.join("::");
109 }
110 if (p5pkg.hasOwnProperty(package_name)) {
111 var c = p5pkg[package_name];
112 if ( c.hasOwnProperty(sub_name) ) {
113 return c[sub_name]
114 }
115 }
116 return null;
117 }
118  
119 function p5get_class_for_method(method, class_name, seen) {
120 // default mro
121 // TODO - cache the methods that were already looked up
122 if ( p5pkg[class_name].hasOwnProperty(method) ) {
123 return class_name
124 }
125 var isa = p5pkg[class_name].List_ISA;
126 for (var i = 0; i < isa.length; i++) {
127 if (!seen[isa[i]]) {
128 var m = p5get_class_for_method(method, isa[i], seen);
129 if (m) {
130 return m
131 }
132 seen[isa[i]]++;
133 }
134 }
135 }
136  
137 function p5method_lookup(method, class_name, seen) {
138 var c = p5get_class_for_method(method, class_name, seen);
139 if (c) {
140 return p5pkg[c][method]
141 }
142 if ( p5pkg.UNIVERSAL.hasOwnProperty(method) ) {
143 return p5pkg.UNIVERSAL[method]
144 }
145 }
146  
147 function p5call(invocant, method, list, p5want) {
148 list.unshift(invocant);
149  
150 if (invocant instanceof p5Scalar) {
151 // TODO - move p5call() to p5Scalar method
152 invocant = invocant.FETCH();
153 }
154  
155 if ( invocant.hasOwnProperty("_class_") ) {
156  
157 if ( invocant._class_.hasOwnProperty(method) ) {
158 return invocant._class_[method](list, p5want)
159 }
160 var m = p5method_lookup(method, invocant._class_._ref_, {});
161 if (m) {
162 return m(list, p5want)
163 }
164  
165 // method can have an optional namespace
166 var pkg_name = method.split(/::/);
167 if (pkg_name.length > 1) {
168 var name = pkg_name.pop();
169 pkg_name = pkg_name.join("::");
170 m = p5method_lookup(name, pkg_name, {});
171 if (m) {
172 return m(list, p5want)
173 }
174 p5pkg.CORE.die(["method not found: ", name, " in class ", pkg_name]);
175 }
176  
177 pkg_name = p5get_class_for_method('AUTOLOAD', invocant._class_._ref_, {}) || p5get_class_for_method('AUTOLOAD', "UNIVERSAL", {});
178 if (pkg_name) {
179 p5pkg[pkg_name]["v_AUTOLOAD"] = invocant._class_._ref_ + "::" + method;
180 return p5pkg[pkg_name]["AUTOLOAD"](list, p5want);
181 }
182  
183 p5pkg.CORE.die(["method not found: ", method, " in class ", invocant._class_._ref_]);
184  
185 }
186  
187 // the invocant doesn't have a class
188  
189 if (typeof invocant === "string") {
190 var aclass = p5make_package(invocant);
191 return p5call(aclass, method, list, p5want);
192 }
193  
194 p5pkg.CORE.die(["Can't call method ", method, " on unblessed reference"]);
195  
196 }
197  
198 function p5call_sub(namespace, name, list, p5want) {
199 if(p5pkg[namespace].hasOwnProperty(name)) {
200 return p5pkg[namespace][name](list, p5want)
201 }
202 if(p5pkg[namespace].hasOwnProperty("AUTOLOAD")) {
203 p5pkg[namespace]["v_AUTOLOAD"] = namespace + "::" + name;
204 return p5pkg[namespace]["AUTOLOAD"](list, p5want)
205 }
206 p5pkg.CORE.die(["Undefined subroutine &" + namespace + "::" + name]);
207 }
208  
209 function p5scalar_deref(v) {
210 if (typeof v === "string") {
211 var pkg_name = v.split(/::/);
212 if (pkg_name.length > 1) {
213 v = pkg_name.pop();
214 pkg_name = pkg_name.join("::");
215 }
216 else {
217 pkg_name = p5pkg["Perlito5"].v_PKG_NAME;
218 }
219 var c = v.charCodeAt(0);
220 if (c < 27) {
221 v = String.fromCharCode(c + 64) + v.substr(1);
222 pkg_name = 'main';
223 }
224 return p5pkg[pkg_name]["v_"+v];
225 }
226 return v._scalar_;
227 }
228  
229 function p5scalar_deref_set(v, n) {
230 if (typeof v === "string") {
231 var pkg_name = v.split(/::/);
232 if (pkg_name.length > 1) {
233 v = pkg_name.pop();
234 pkg_name = pkg_name.join("::");
235 }
236 else {
237 pkg_name = p5pkg["Perlito5"].v_PKG_NAME;
238 }
239 var c = v.charCodeAt(0);
240 if (c < 27) {
241 v = String.fromCharCode(c + 64) + v.substr(1);
242 pkg_name = 'main';
243 }
244 p5pkg[pkg_name]["v_"+v] = n;
245 return p5pkg[pkg_name]["v_"+v];
246 }
247 v._scalar_ = n;
248 return v._scalar_;
249 }
250  
251 p5make_package("main");
252 p5make_package("Perlito5");
253 p5pkg["Perlito5"].v_PKG_NAME = "main";
254 p5pkg["main"]["v_@"] = new p5Scalar(""); // $@
255 p5pkg["main"]["v_|"] = new p5Scalar(0); // $|
256 p5pkg["main"]["List_#"] = new p5Array([]); // @#
257 p5scalar_deref_set(String.fromCharCode(15), isNode ? "node.js" : "javascript2"); // $^O
258 p5pkg["main"]["List_INC"] = new p5Array([]);
259 p5pkg["main"]["Hash_INC"] = new p5Hash({});
260 p5pkg["main"]["List_ARGV"] = new p5Array([]);
261 p5pkg["main"]["Hash_ENV"] = new p5Hash({});
262 if (isNode) {
263 p5pkg["main"]["List_ARGV"] = new p5Array(process.argv.splice(2));
264 p5pkg["main"]["Hash_ENV"] = new p5Hash(process.env);
265 p5pkg["main"]["v_$"] = new p5Scalar(process.pid);
266 } else if (typeof arguments === "object") {
267 p5pkg["main"]["List_ARGV"] = new p5Array(arguments);
268 }
269  
270 p5make_package("Perlito5::IO");
271 p5make_package("Perlito5::Runtime");
272 p5make_package("Perlito5::Grammar");
273  
274 function p5typeglob_set(pkg_name, sub_name, func) {
275 p5make_package(pkg_name);
276 p5pkg[pkg_name][sub_name] = func;
277 }
278  
279 function p5set_glob(name, data) {
280 if ( name.indexOf("::") == -1 ) {
281 p5pkg[ p5pkg["Perlito5"].v_PKG_NAME.FETCH() ][name] = data;
282 return data;
283 }
284 var c = name.split("::");
285 s = c.pop();
286 var pkg = c.join("::");
287 p5make_package(pkg);
288 p5pkg[pkg][s] = data;
289 return data;
290 }
291  
292 var sigils = { '@' : 'List_', '%' : 'Hash_', '$' : 'v_' };
293  
294 function p5set_local(namespace, name, sigil) {
295 var vname = sigils[sigil] + name;
296 p5LOCAL.push([namespace, vname, namespace[vname]]);
297  
298 if (sigil == '$') {
299 namespace[vname] = new p5Scalar(null);
300 }
301 else if (sigil == '@') {
302 namespace[vname] = new p5Array([]);
303 }
304 else if (sigil == '%') {
305 namespace[vname] = new p5Hash({});
306 }
307 return namespace[vname];
308 }
309  
310 function p5cleanup_local(idx, value) {
311 while (p5LOCAL.length > idx) {
312 l = p5LOCAL.pop();
313 l[0][l[1]] = l[2];
314 }
315 return value;
316 }
317  
318 function p5global(sigil, namespace, name) {
319 // TODO - autovivify namespace
320 var vname = sigils[sigil] + name;
321 var v = p5pkg[namespace][vname];
322 if (v != null) {
323 return v;
324 }
325 if (sigil == '$') {
326 p5pkg[namespace][vname] = new p5Scalar(null);
327 }
328 else if (sigil == '@') {
329 p5pkg[namespace][vname] = new p5Array([]);
330 }
331 else if (sigil == '%') {
332 p5pkg[namespace][vname] = new p5Hash({});
333 }
334 return p5pkg[namespace][vname];
335 }
336  
337 function p5HashRef(o) {
338 this._href_ = o;
339 this._ref_ = "HASH";
340 this.p5bool = function() { return 1 };
341 this.p5string = function() {
342 return "HASH(0x0000)"; // TODO
343 };
344 this.hderef = function() {
345 return this._href_;
346 };
347 this.hset = function(i, v) {
348 return this._href_.hset(i, v);
349 };
350 this.hget = function(i, autoviv) {
351 return this._href_.hget(i, autoviv);
352 }
353 }
354  
355 function p5ArrayRef(o) {
356 this._aref_ = o;
357 this._ref_ = "ARRAY";
358 this.p5bool = function() { return 1 };
359 this.p5string = function() {
360 return "ARRAY(0x0000)"; // TODO
361 };
362 this.aderef = function() {
363 return this._aref_;
364 };
365 this.aset = function(i, v) {
366 return this._aref_.aset(i, v);
367 }
368 this.aget = function(i, autoviv) {
369 return this._aref_.aget(i, autoviv);
370 }
371 }
372  
373 function p5ScalarRef(o) {
374 this._scalar_ = o;
375 this._ref_ = "SCALAR";
376 this.p5bool = function() { return 1 };
377 this.p5string = function() {
378 return "SCALAR(0x0000)"; // TODO
379 };
380 this.sderef = function() {
381 return this._scalar_;
382 };
383 }
384  
385 function p5GlobRef(o) {
386 this._scalar_ = o;
387 this._ref_ = "GLOB";
388 this.p5bool = function() { return 1 };
389 this.p5string = function() {
390 return "GLOB(0x0000)"; // TODO
391 };
392 }
393  
394 function p5Array(o) {
395 // TODO - array slice
396 this._array_ = o;
397 this._ref_ = "";
398 this.p5bool = function() {
399 return this._array_.length != 0
400 };
401 this.p5string = function() {
402 return '' + this._array_.length;
403 };
404 this.p5num = function() {
405 return this._array_.length;
406 };
407 this.aset = function(i, v) {
408 if (i < 0) {
409 i = this._array_.length + i;
410 }
411 if (this._array_[i] instanceof p5Scalar) {
412 this._array_[i].assign(v);
413 }
414 else {
415 if (v instanceof p5Scalar) {
416 this._array_[i] = v.FETCH();
417 }
418 else {
419 this._array_[i] = v;
420 }
421 }
422 return v;
423 };
424 this.aget = function(i, autoviv) {
425 if (i < 0) {
426 i = this._array_.length + i;
427 }
428 if (autoviv) {
429 if (autoviv == 'lvalue') {
430 if (this._array_.length < i) {
431 // don't vivify yet; create a proxy object
432 return new p5ArrayProxy(this, i);
433 }
434 if (!(this._array_[i] instanceof p5Scalar)) {
435 this._array_[i] = new p5Scalar(this._array_[i]);
436 }
437 }
438 else if (autoviv == 'array') {
439 if (!(this._array_[i] instanceof p5ArrayRef) &&
440 !(this._array_[i] instanceof p5Scalar))
441 {
442 this._array_[i] = new p5ArrayRef(new p5Array([]));
443 }
444 }
445 else if (autoviv == 'hash') {
446 if (!(this._array_[i] instanceof p5HashRef) &&
447 !(this._array_[i] instanceof p5Scalar))
448 {
449 this._array_[i] = new p5HashRef(new p5Hash({}));
450 }
451 }
452 }
453 return this._array_[i];
454 };
455 this.get_values = function(o) {
456 // add values to the param (a native js list)
457 for(var i = 0; i < this._array_.length; i++) {
458 o.push(this._array_[i]);
459 }
460 return o;
461 };
462 this.get_lvalues = function(o) {
463 // add lvalues to the param (a native js list)
464 for(var i = 0; i < this._array_.length; i++) {
465 o.push(this._array_[i] instanceof p5Scalar ? this._array_[i] : this.aget(i, "lvalue"));
466 }
467 return o;
468 };
469 this.assign = function(a) {
470 if (a instanceof Array) {
471 // TODO - cleanup, this shouldn't happen
472 this._array_ = a;
473 }
474 else {
475 this._array_ = a._array_;
476 }
477 return this;
478 };
479  
480 // operations that can be tie()
481 this.FETCHSIZE = function() {
482 return this._array_.length;
483 };
484 this.PUSH = function(v) {
485 for(var i = 0; i < v._array_.length; i++) {
486 this._array_.push(v._array_[i] instanceof p5Scalar ? v._array_[i].FETCH() : v._array_[i]);
487 }
488 return this._array_.length;
489 };
490 this.UNSHIFT = function(v) {
491 for(var i = v._array_.length-1; i >= 0; i--) {
492 this._array_.unshift(v._array_[i] instanceof p5Scalar ? v._array_[i].FETCH() : v._array_[i]);
493 }
494 return this._array_.length;
495 };
496 this.POP = function() {
497 if (this._array_.length == null) {
498 return null;
499 }
500 return this._array_.pop();
501 };
502 this.SHIFT = function(v) {
503 if (this._array_.length == null) {
504 return null;
505 }
506 return this._array_.shift();
507 };
508 }
509  
510 function p5Hash(o) {
511 // TODO - hash slice
512 this._hash_ = o;
513 this._ref_ = "";
514 this.p5bool = function() {
515 o = this._hash_;
516 for (var i in o) {
517 return true;
518 }
519 return false;
520 };
521 this.p5string = function() {
522 return '' + this.p5num() + '/8';
523 };
524 this.p5num = function() {
525 var out = 0;
526 for (var i in this._hash_) {
527 out++;
528 }
529 return out;
530 };
531 this.exists = function(i) {
532 return this._hash_.hasOwnProperty(i);
533 };
534 this.hset = function(i, v) {
535 if (this._hash_[i] instanceof p5Scalar) {
536 this._hash_[i].assign(v);
537 }
538 else {
539 if (v instanceof p5Scalar) {
540 this._hash_[i] = v.FETCH();
541 }
542 else {
543 this._hash_[i] = v;
544 }
545 }
546 return v;
547 };
548 this.hget = function(i, autoviv) {
549 if (autoviv) {
550 if (autoviv == 'lvalue') {
551 if (! this._hash_.hasOwnProperty(i)) {
552 // don't autovivify yet; create a proxy object
553 return new p5HashProxy(this, i);
554 }
555 if (!(this._hash_[i] instanceof p5Scalar)) {
556 this._hash_[i] = new p5Scalar(this._hash_[i]);
557 }
558 }
559 else if (autoviv == 'array') {
560 if (!(this._hash_[i] instanceof p5ArrayRef) &&
561 !(this._hash_[i] instanceof p5Scalar))
562 {
563 this._hash_[i] = new p5ArrayRef(new p5Array([]));
564 }
565 }
566 else if (autoviv == 'hash') {
567 if (!(this._hash_[i] instanceof p5HashRef) &&
568 !(this._hash_[i] instanceof p5Scalar))
569 {
570 this._hash_[i] = new p5HashRef(new p5Hash({}));
571 }
572 }
573 }
574 return this._hash_[i];
575 };
576 this.get_values = function(o) {
577 // add a native list of values to the param
578 for (var i in this._hash_) {
579 o.push(i);
580 o.push(this._hash_[i]);
581 }
582 return o;
583 };
584 this.get_lvalues = function(o) {
585 // add a native list of lvalues to the param
586 for (var i in this._hash_) {
587 o.push(i);
588 o.push(this._hash_[i] instanceof p5Scalar ? this._hash_[i] : this.hget(i, "lvalue"));
589 }
590 return o;
591 };
592 this.assign = function(h) {
593 if (h instanceof p5Hash) {
594 this._hash_ = h._hash_;
595 }
596 else {
597 // TODO - cleanup, this shouldn't happen
598 this._hash_ = h;
599 }
600 return this;
601 }
602 }
603  
604 function p5Scalar(o) {
605 this._v_ = o;
606 this._ref_ = "";
607  
608 // be a value
609 this.p5bool = function() {
610 return p5bool(this._v_);
611 };
612 this.p5string = function() {
613 return p5str(this._v_);
614 };
615 this.p5num = function() {
616 return p5num(this._v_);
617 };
618 this.p5code = function() {
619 return p5code(this._v_);
620 };
621 this.p5incr = function() {
622 this._v_ = p5incr(this._v_);
623 return this._v_;
624 };
625 this.p5postincr = function() {
626 var v = this._v_;
627 this._v_ = p5incr(this._v_);
628 return v;
629 };
630 this.p5decr = function() {
631 this._v_ = p5decr(this._v_);
632 return this._v_;
633 };
634 this.p5postdecr = function() {
635 var v = this._v_;
636 this._v_ = p5decr(this._v_);
637 return v;
638 };
639  
640 // be a scalar ref
641 this.sderef = function(i) {
642 // TODO - autovivify scalar (with proxy object?)
643 return this._v_.sderef();
644 };
645  
646 // be an array ref
647 this.aderef = function() {
648 // TODO - autovivify array (with proxy object?)
649 return this._v_.aderef();
650 };
651 this.aget = function(i, autoviv) {
652 // TODO - autovivify array (with proxy object?)
653 if (this._v_ == null) {
654 this._v_ = new p5ArrayRef(new p5Array([]));
655 }
656 return this._v_.aget(i, autoviv);
657 };
658 this.aset = function(i, v) {
659 if (this._v_ == null) {
660 this._v_ = new p5ArrayRef(new p5Array([]));
661 }
662 return this._v_.aset(i, v);
663 };
664  
665 // be a hash ref
666 this.hderef = function() {
667 // TODO - autovivify hash (with proxy object?)
668 if (this._v_ == null) {
669 this._v_ = new p5HashRef(new p5Hash([]));
670 }
671 return this._v_.hderef();
672 };
673 this.hget = function(i, autoviv) {
674 // TODO - autovivify hash (with proxy object?)
675 if (this._v_ == null) {
676 this._v_ = new p5HashRef(new p5Hash([]));
677 }
678 return this._v_.hget(i, autoviv);
679 }
680 this.hset = function(i, v) {
681 if (this._v_ == null) {
682 this._v_ = new p5HashRef(new p5Hash([]));
683 }
684 return this._v_.hset(i, v);
685 }
686  
687 // be a container
688 this.get_values = function(o) {
689 // add a native list of values to the param
690 o.push(this);
691 return o;
692 };
693 this.get_lvalues = function(o) {
694 // add a native list of lvalues to the param
695 o.push(this);
696 return o;
697 };
698 this.assign = function(v) {
699 if (v instanceof p5Scalar) {
700 this._v_ = v.FETCH();
701 }
702 else {
703 this._v_ = v;
704 }
705 return this;
706 };
707  
708 // operations that can be tie()
709 this.FETCH = function() {
710 // not an lvalue
711 return this._v_;
712 };
713 }
714  
715  
716 function p5HashProxy(h, k) {
717 this._hashobj_ = h;
718 this._key_ = k;
719 this._v_ = null;
720 this.assign = function(v) {
721 // write-through; alternately, use read-through
722 if (v instanceof p5Scalar) {
723 this._v_ = v.FETCH();
724 }
725 else {
726 this._v_ = v;
727 }
728 return this._hashobj_.hset(this._key_, this._v_);
729 }
730 }
731 p5HashProxy.prototype = new p5Scalar();
732  
733  
734 function p5ArrayProxy(a, k) {
735 this._arrayobj_ = a;
736 this._key_ = k;
737 this._v_ = null;
738 this.assign = function(v) {
739 // write-through; alternately, use read-through
740 if (v instanceof p5Scalar) {
741 this._v_ = v.FETCH();
742 }
743 else {
744 this._v_ = v;
745 }
746 return this._arrayobj_.aset(this._key_, this._v_);
747 }
748 }
749 p5ArrayProxy.prototype = new p5Scalar();
750  
751  
752 p5param_list = function() {
753 var res = [];
754 for (i = 0; i < arguments.length; i++) {
755 if (arguments[i] == null) {
756 res.push(null)
757 }
758 else if (arguments[i].hasOwnProperty("get_lvalues")) {
759 // container
760 arguments[i].get_lvalues(res);
761 }
762 else if (arguments[i] instanceof Array) {
763 // js Array -- possibly generated by p5context()
764 // maybe too late to get lvalues -- needs more testing
765 // this doesn't handle nested Array
766 o = arguments[i];
767 for (j = 0; j < o.length; j++) {
768 res.push(o[j]);
769 }
770 }
771 else {
772 // non-container
773 res.push(arguments[i]);
774 }
775 }
776 return res;
777 };
778  
779 p5list_to_a = function() {
780 var res = [];
781 for (i = 0; i < arguments.length; i++) {
782 if (arguments[i] == null) {
783 res.push(null)
784 }
785 else if (arguments[i].hasOwnProperty("get_values")) {
786 // container
787 arguments[i].get_values(res);
788 }
789 else if (arguments[i] instanceof Array) {
790 // js Array -- possibly generated by p5context()
791 o = arguments[i];
792 for (j = 0; j < o.length; j++) {
793 res.push(o[j]);
794 }
795 }
796 else {
797 // non-container
798 res.push(arguments[i]);
799 }
800 }
801 return res;
802 };
803  
804 p5a_to_h = function(a) {
805 var res = {};
806 for (i = 0; i < a.length; i+=2) {
807 res[p5str(a[i])] = a[i+1];
808 }
809 return res;
810 };
811  
812 if (isNode) {
813 var fs = require("fs");
814 }
815  
816 p5context = function(List__, p5want) {
817 if (p5want) {
818 return p5list_to_a.apply(null, List__);
819 }
820 // scalar: return the last value
821 var o = List__;
822 while (o instanceof Array) {
823 o = o.length
824 ? o[o.length-1]
825 : null;
826 }
827 return o;
828 }
829  
830 p5code = function(o) {
831 if (typeof o === "function") {
832 return o;
833 }
834 return o.p5code();
835 };
836  
837 p5str = function(o) {
838 if (o == null) {
839 return "";
840 }
841 if (typeof o === "object" && (o instanceof Array)) {
842 return CORE.join(["", o]);
843 }
844 if (typeof o.p5string === "function") {
845 return o.p5string();
846 }
847 if (typeof o == "number" && Math.abs(o) < 0.0001 && o != 0) {
848 return o.toExponential().replace(/e-(\d)$/,"e-0$1");
849 }
850 if (typeof o === "boolean") {
851 return o ? "1" : "";
852 }
853 if (typeof o !== "string") {
854 return "" + o;
855 }
856 return o;
857 };
858  
859 p5num = function(o) {
860 if (o == null) {
861 return 0;
862 }
863 if (typeof o === "object" && (o instanceof Array)) {
864 return o.length;
865 }
866 if (typeof o.p5num === "function") {
867 return o.p5num();
868 }
869 if (typeof o !== "number") {
870 return parseFloat(p5str(o));
871 }
872 return o;
873 };
874  
875 p5bool = function(o) {
876 if (o) {
877 if (typeof o === "boolean") {
878 return o;
879 }
880 if (typeof o === "number") {
881 return o;
882 }
883 if (typeof o === "string") {
884 return o != "" && o != "0";
885 }
886 if (typeof o.p5bool === "function") {
887 return o.p5bool();
888 }
889 if (typeof o.length === "number") {
890 return o.length;
891 }
892 if (o instanceof Error) {
893 return true;
894 }
895 for (var i in o) {
896 return true;
897 }
898 }
899 return false;
900 };
901  
902 p5incr = function(o) {
903 if (typeof o === "number") {
904 return o + 1;
905 }
906 return p5str_inc(p5str(o));
907 };
908  
909 p5decr = function(o) {
910 if (typeof o === "number") {
911 return o - 1;
912 }
913 return p5num(o) - 1;
914 };
915  
916 p5and = function(a, fb) {
917 if (p5bool(a)) {
918 return fb();
919 }
920 return a;
921 };
922  
923 p5or = function(a, fb) {
924 if (p5bool(a)) {
925 return a;
926 }
927 return fb();
928 };
929  
930 p5defined_or = function(a, fb) {
931 if (a == null) {
932 return fb();
933 }
934 return a;
935 };
936  
937 p5cmp = function(a, b) {
938 return a > b ? 1 : a < b ? -1 : 0
939 };
940  
941 p5complement = function(a) {
942 return a < 0 ? ~a : 4294967295 - a
943 // return a < 0 ? ~a : 18446744073709551615 - a
944 };
945  
946 p5str_replicate = function(o, n) {
947 n = p5num(n);
948 return n ? Array(n + 1).join(o) : "";
949 };
950  
951 p5str_inc = function(s) {
952 s = p5str(s);
953 if (s.length < 2) {
954 if (s.match(/[012345678ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy]/)) {
955 return String.fromCharCode(s.charCodeAt(0) + 1);
956 }
957 if (s == "9") {
958 return "10";
959 }
960 if (s == "Z") {
961 return "AA";
962 }
963 if (s == "z") {
964 return "aa";
965 }
966 return "1";
967 }
968 var c = p5str_inc(s.substr(s.length-1, 1));
969 if (c.length == 1) {
970 return s.substr(0, s.length-1) + c;
971 }
972 return p5str_inc(s.substr(0, s.length-1)) + c.substr(c.length-1, 1);
973 };
974  
975 p5for = function(namespace, func, args, cont, label) {
976 var _redo = false;
977  
978 var v_old = namespace["v__"];
979 namespace["v__"] = new p5Scalar(null);
980  
981 for(var i = 0; i < args.length; i++) {
982 namespace["v__"].assign(args[i]); // ??? - should this be a bind?
983 try {
984 func()
985 }
986 catch(err) {
987 if (err instanceof p5_error && err.v == label) {
988 if (err.type == 'last') { return }
989 else if (err.type == 'redo') { i--; _redo = true }
990 else if (err.type != 'next') { throw(err) }
991 }
992 else {
993 throw(err)
994 }
995 }
996 if (cont) {
997 try {
998 if (!_redo) { cont() }
999 }
1000 catch(err) {
1001 if (err instanceof p5_error && err.v == label) {
1002 if (err.type == 'last') { return }
1003 else if (err.type == 'redo') { _redo = true }
1004 else if (err.type != 'next') { throw(err) }
1005 }
1006 else {
1007 throw(err)
1008 }
1009 }
1010 }
1011 }
1012 namespace["v__"] = v_old;
1013 };
1014  
1015 p5for_lex = function(func, args, cont, label) {
1016 var _redo = false;
1017 var _arg = new p5Scalar(null);
1018 for(var i = 0; i < args.length; i++) {
1019 try {
1020 _arg.assign(args[i]);
1021 func(_arg)
1022 }
1023 catch(err) {
1024 if (err instanceof p5_error && err.v == label) {
1025 if (err.type == 'last') { return }
1026 else if (err.type == 'redo') { i--; _redo = true }
1027 else if (err.type != 'next') { throw(err) }
1028 }
1029 else {
1030 throw(err)
1031 }
1032 }
1033 if (cont) {
1034 try {
1035 if (!_redo) { cont() }
1036 }
1037 catch(err) {
1038 if (err instanceof p5_error && err.v == label) {
1039 if (err.type == 'last') { return }
1040 else if (err.type == 'redo') { _redo = true }
1041 else if (err.type != 'next') { throw(err) }
1042 }
1043 else {
1044 throw(err)
1045 }
1046 }
1047 }
1048 }
1049 };
1050  
1051 p5while = function(func, cond, cont, label) {
1052 var _redo = false;
1053 while (_redo || p5bool(cond())) {
1054 _redo = false;
1055 try {
1056 func()
1057 }
1058 catch(err) {
1059 if (err instanceof p5_error && err.v == label) {
1060 if (err.type == 'last') { return }
1061 else if (err.type == 'redo') { _redo = true }
1062 else if (err.type != 'next') { throw(err) }
1063 }
1064 else {
1065 throw(err)
1066 }
1067 }
1068 if (cont) {
1069 try {
1070 if (!_redo) { cont() }
1071 }
1072 catch(err) {
1073 if (err instanceof p5_error && err.v == label) {
1074 if (err.type == 'last') { return }
1075 else if (err.type == 'redo') { _redo = true }
1076 else if (err.type != 'next') { throw(err) }
1077 }
1078 else {
1079 throw(err)
1080 }
1081 }
1082 }
1083 }
1084 };
1085  
1086 p5map = function(namespace, func, args) {
1087 var v_old = namespace["v__"];
1088 namespace["v__"] = new p5Scalar(null);
1089  
1090 var out = [];
1091 for(var i = 0; i < args.length; i++) {
1092 namespace["v__"].assign(args[i]);
1093 var o = p5list_to_a(func(1));
1094 for(var j = 0; j < o.length; j++) {
1095 out.push(o[j]);
1096 }
1097 }
1098 namespace["v__"] = v_old;
1099 return out;
1100 };
1101  
1102 p5grep = function(namespace, func, args) {
1103 var v_old = namespace["v__"];
1104 namespace["v__"] = new p5Scalar(null);
1105  
1106 var out = [];
1107 for(var i = 0; i < args.length; i++) {
1108 namespace["v__"].assign(args[i]);
1109 if (p5bool(func(0))) {
1110 out.push(args[i])
1111 }
1112 }
1113 namespace["v__"] = v_old;
1114 return out;
1115 };
1116  
1117 p5sort = function(namespace, func, args) {
1118 var a_old = namespace["v_a"];
1119 var b_old = namespace["v_b"];
1120 namespace["v_a"] = new p5Scalar(null);
1121 namespace["v_b"] = new p5Scalar(null);
1122  
1123 var out =
1124 func == null
1125 ? args.sort()
1126 : args.sort(
1127 function(a, b) {
1128 namespace["v_a"].assign(a);
1129 namespace["v_b"].assign(b);
1130 return func(0);
1131 }
1132 );
1133 namespace["v_a"] = a_old;
1134 namespace["v_b"] = b_old;
1135 return out;
1136 };
1137  
1138 perl5_to_js = function( source, namespace, var_env_js, p5want ) {
1139 // CORE.say(["source: [" + source + "]"]);
1140  
1141 var namespace_old = p5global("$", "Perlito5", "PKG_NAME").FETCH();
1142 p5pkg["Perlito5"].v_PKG_NAME.assign(namespace);
1143  
1144 match = p5call(p5pkg["Perlito5::Grammar"], "exp_stmts", [source, 0]);
1145  
1146 if ( !p5bool(match) || p5str(match.hget("to")) != source.length ) {
1147 CORE.die(["Syntax error in eval near pos ", match.hget("to") ]);
1148 }
1149  
1150 ast = p5pkg.CORE.bless([
1151 new p5HashRef(new p5Hash({
1152 code: "do",
1153 arguments: [ p5pkg.CORE.bless([
1154 new p5HashRef(new p5Hash({
1155 stmts: p5pkg["Perlito5::Match"].flat([match])
1156 })),
1157 p5pkg["Perlito5::AST::Block"]
1158 ]) ]
1159 })),
1160 p5pkg["Perlito5::AST::Apply"]
1161 ]);
1162  
1163 // CORE.say(["ast: [" + ast + "]"]);
1164 js_code = p5call(ast, "emit_javascript3", [0, p5want]);
1165 // CORE.say(["js-source: [" + js_code + "]"]);
1166  
1167 p5pkg["Perlito5"].v_PKG_NAME.assign(namespace_old);
1168 return js_code;
1169 }
1170  
1171 EOT
1172  
1173 } # end of emit_javascript3()
1174  
1175 1;
1176  
1177