Permalink
Browse files

Perlito5 - js - global autovivification fix

  • Loading branch information...
1 parent a80bdf6 commit 5a6928d92f01d4bf820e6bc25dfeaba622ab6ec3 @fglock committed Sep 13, 2013
Showing with 66 additions and 15 deletions.
  1. +3 −2 TODO-perlito5
  2. +32 −10 html/perlito5.js
  3. +8 −2 perlito5.pl
  4. +7 −1 src5/lib/Perlito5/Javascript2/Emitter.pm
  5. +16 −0 src5/lib/Perlito5/Javascript2/Runtime.pm
View
@@ -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

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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 . '"]'
}
@@ -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";

0 comments on commit 5a6928d

Please sign in to comment.