Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement and test cloning of arrays.
  • Loading branch information
pmurias committed Aug 22, 2015
1 parent 8e9e394 commit df70364
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -341,6 +341,8 @@ op.composetype = function(obj, reprinfo) {
op.clone = function(obj) {
if (obj.$$clone) {
return obj.$$clone();
} else if (obj instanceof Array) {
return obj.slice();
} else {
// STUB
console.log("NYI cloning", obj);
Expand Down
15 changes: 14 additions & 1 deletion t/nqp/94-clone.t
@@ -1,4 +1,4 @@
plan(7);
plan(10);
sub foo() {
'hello there';
}
Expand Down Expand Up @@ -30,3 +30,16 @@ class Foo {
ok($d.get_foo == 123, "changing an attr on the cloned object doesn't affect the orginal");
ok($c.get_foo == 456, "we can change an attr on the new object");
}

my $list := nqp::list(0,100,200,300, nqp::list(400, 401, 402));

my $cloned := nqp::clone($list);

$cloned[2] := 20;

ok($cloned[2] == 20, "we can change an element of a cloned array");
ok($list[2] == 200, "...and the original array remains unchanged");

$cloned[4][2] := 42;

ok($list[4][2] == 42, "cloning is shallow");

0 comments on commit df70364

Please sign in to comment.