Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shallow copying by repeatAppend() #177

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/prebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*
*/

import * as common from './common';

declare interface ProtoM21ObjectConstructorInterface extends Function {
className: string;
}
Expand Down Expand Up @@ -129,9 +131,8 @@ export class ProtoM21Object {
memo = new WeakMap();
}

// TODO(msc): test if Arrays work?
for (const key in this) {
// not that we ONLY copy the keys in Ret -- it's easier that way.
// note that we ONLY copy the keys in Ret -- it's easier that way.
if ({}.hasOwnProperty.call(this, key) === false) {
continue;
}
Expand Down Expand Up @@ -170,11 +171,18 @@ export class ProtoM21Object {
clonedVersion = m21Obj.clone(deep, memo);
}
ret[key] = <any> clonedVersion;
} else if (
deep
&& this[key] instanceof Array
) {
ret[key] = <any[]> Array.from(this[key] as any);
} else {
try {
// for deep this should be:
// music21.common.merge(ret[key], this[key]);
ret[key] = this[key];
if (deep && typeof this[key] !== 'string') {
common.merge(ret[key], this[key] as any);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is not enough. Probably need to do something more like:

Split the section beginning:
for (const key in this) {
into its own method (common.cloneHelper?) (i.e. a function that doesn't use "this" but obj or something like that). And then when encountering another dictionary/object, calling cloneHelper on that.

} else {
ret[key] = this[key];
}
} catch (e) {
if (e instanceof TypeError) {
console.log('typeError:', e, key);
Expand Down
4 changes: 4 additions & 0 deletions tests/moduleTests/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,12 @@ export default function tests() {
test('music21.stream.Stream.repeatAppend', assert => {
const a = new music21.stream.Stream();
const n = new music21.note.Note();
n.groups.push('all-notes-get-this-group');
a.repeatAppend(n, 10);
assert.equal(a.notes.length, 10);
a.notes.first().groups.push('first-note-only');
assert.equal(a.notes.first().groups, ['all-notes-get-this-group', 'first-note-only']);
assert.equal(a.notes.last().groups, ['all-notes-get-this-group']);
});

test('music21.stream.Stream.insert and offsets', assert => {
Expand Down
Loading