Skip to content

Commit 571ff00

Browse files
committed
Cloning code refs.
1 parent 1d4d34c commit 571ff00

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/vm/js/nqp-runtime/code-ref.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ CodeRef.prototype.setInfo = function(ctx, closureTemplate, staticInfo) {
2929
return this;
3030
};
3131

32+
CodeRef.prototype.$$clone = function() {
33+
var clone = new CodeRef(this.name);
34+
clone.$call = this.$call;
35+
clone.codeObj = this.codeObj;
36+
return clone;
37+
};
38+
3239
module.exports = CodeRef;

src/vm/js/nqp-runtime/core.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,14 @@ op.rebless = function(obj, new_type) {
293293
return obj;
294294
};
295295

296-
// STUB
297296
op.clone = function(obj) {
298-
console.log("cloning", obj);
299-
return obj;
297+
if (obj.$$clone) {
298+
return obj.$$clone();
299+
} else {
300+
// STUB
301+
console.log("NYI cloning", obj);
302+
return obj;
303+
}
300304
};
301305

302306
var where_counter = 0;

t/nqp/94-clone.t

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plan(5);
2+
sub foo() {
3+
'hello there';
4+
}
5+
my $a := &foo;
6+
my $b := nqp::clone($a);
7+
8+
nqp::setcodeobj($a, "obj");
9+
ok(nqp::getcodename($a) eq 'foo', 'sanity check');
10+
ok(nqp::getcodename($b) eq 'foo', 'cloned sub has correct name');
11+
ok($b() eq 'hello there', 'you can call a cloned sub');
12+
nqp::setcodename($b, 'bumblebee');
13+
ok(nqp::getcodename($a) eq 'foo', "changing the name on the cloned sub doesn't affect orginal");
14+
ok(nqp::getcodename($b) eq 'bumblebee', "you can change the name on the cloned sub");

0 commit comments

Comments
 (0)