Skip to content

Commit 469a34f

Browse files
committed
Perlito5 - js3 - fix local(), more tests
1 parent aa057ce commit 469a34f

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

perlito5.pl

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

src5/lib/Perlito5/Javascript3/Emitter.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ package Perlito5::AST::Apply;
13551355
'prefix:<$>' => sub {
13561356
my $self = $_[0];
13571357
my $arg = $self->{arguments}->[0];
1358-
Perlito5::Javascript3::emit_javascript3_autovivify( $arg, $level, 'scalar' ) . '.._scalar_';
1358+
Perlito5::Javascript3::emit_javascript3_autovivify( $arg, $level, 'scalar' ) . '.sderef()';
13591359
},
13601360
'prefix:<@>' => sub {
13611361
my $self = $_[0];

src5/lib/Perlito5/Javascript3/Runtime.pm

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,22 @@ function p5set_glob(name, data) {
226226
return data;
227227
}
228228
229+
var sigils = { '@' : 'List_', '%' : 'Hash_', '$' : 'v_' };
230+
229231
function p5set_local(namespace, name, sigil) {
230-
var v = name;
231-
if (sigil == "$") {
232-
v = "v_"+name;
232+
var vname = sigils[sigil] + name;
233+
p5LOCAL.push([namespace, vname, namespace[vname]]);
234+
235+
if (sigil == '$') {
236+
namespace[vname] = new p5Scalar(null);
237+
}
238+
else if (sigil == '@') {
239+
namespace[vname] = new p5Array([]);
240+
}
241+
else if (sigil == '%') {
242+
namespace[vname] = new p5Hash({});
233243
}
234-
p5LOCAL.push([namespace, v, namespace[v]]);
244+
return namespace[vname];
235245
}
236246
237247
function p5cleanup_local(idx, value) {
@@ -242,23 +252,23 @@ function p5cleanup_local(idx, value) {
242252
return value;
243253
}
244254
245-
var sigils = { '@' : 'List_', '%' : 'Hash_', '$' : 'v_' };
246255
function p5global(sigil, namespace, name) {
247256
// TODO - autovivify namespace
248-
v = p5pkg[namespace][sigils[sigil] + name ];
257+
var vname = sigils[sigil] + name;
258+
var v = p5pkg[namespace][vname];
249259
if (v != null) {
250260
return v;
251261
}
252262
if (sigil == '$') {
253-
p5pkg[namespace][sigils[sigil] + name ] = new p5Scalar(null);
263+
p5pkg[namespace][vname] = new p5Scalar(null);
254264
}
255265
else if (sigil == '@') {
256-
p5pkg[namespace][sigils[sigil] + name ] = new p5Array([]);
266+
p5pkg[namespace][vname] = new p5Array([]);
257267
}
258268
else if (sigil == '%') {
259-
p5pkg[namespace][sigils[sigil] + name ] = new p5Hash({});
269+
p5pkg[namespace][vname] = new p5Hash({});
260270
}
261-
return p5pkg[namespace][sigils[sigil] + name ];
271+
return p5pkg[namespace][vname];
262272
}
263273
264274
function p5HashRef(o) {
@@ -306,7 +316,7 @@ function p5ScalarRef(o) {
306316
this.p5string = function() {
307317
return "SCALAR(0x0000)"; // TODO
308318
};
309-
this.sderef = function(i) {
319+
this.sderef = function() {
310320
return this._scalar_;
311321
};
312322
}
@@ -435,7 +445,8 @@ function p5Scalar(o) {
435445
436446
// be a scalar ref
437447
this.sderef = function(i) {
438-
return this._v_.sderef;
448+
// TODO - autovivify scalar (with proxy object?)
449+
return this._v_.sderef();
439450
};
440451
441452
// be an array ref

t5/01-perlito/19-local.t

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ package X; # XXX javascript bug - we don't autovivify packages yet
66

77
package main;
88

9-
say '1..4';
9+
say '1..7';
1010

1111
$X::v = 10;
1212

13+
my $vv = \$X::v;
14+
1315
if (1) {
1416
print "not " if $X::v != 10;
1517
say "ok 1";
@@ -18,11 +20,21 @@ if (1) {
1820
print "not " if defined $X::v;
1921
say "ok 2";
2022

23+
print "not " if $$vv != 10;
24+
say "ok 3 # $$vv";
25+
2126
$X::v = 15;
2227
print "not " if $X::v != 15;
23-
say "ok 3";
28+
say "ok 4";
29+
30+
print "not " if $$vv != 10;
31+
say "ok 5 # $$vv";
32+
2433
}
2534

2635
print "not " if $X::v != 10;
27-
say "ok 4";
36+
say "ok 6";
37+
38+
print "not " if $$vv != 10;
39+
say "ok 7";
2840

0 commit comments

Comments
 (0)