Skip to content

Commit

Permalink
Use a custom cloneObject implementation.
Browse files Browse the repository at this point in the history
Tizen 2016 throws when trying to use JSON to clone cyclic objects.  To
avoid this and to remove a hack to clone objects, this replaces it with
an explicit clone implementation.

Closes #935

Change-Id: Ia057e3f6853b813dd89fe33c291b558b71534726
  • Loading branch information
TheModMaker authored and joeyparrish committed Sep 26, 2017
1 parent 88b174b commit 06c9844
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/media/playhead_observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ shaka.media.PlayheadObserver.prototype.addTimelineRegion = function(
* @private
*/
shaka.media.PlayheadObserver.cloneTimelineInfo_ = function(source) {
// cloneObject will ignore non-simple objects like the DOM element.
var copy = shaka.util.ConfigUtils.cloneObject(source);
// cloneObject uses JSON to clone, which won't copy the DOM element.
copy.eventElement = source.eventElement;
return copy;
};
Expand Down
42 changes: 40 additions & 2 deletions lib/util/config_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,50 @@ shaka.util.ConfigUtils.mergeConfigObjects =
/**
* Performs a deep clone of the given simple object. This does not copy
* prototypes, custom properties (e.g. read-only), or multiple references to
* the same object. This uses JSON to clone.
* the same object. If the caller needs these fields, it will need to set them
* after this returns.
*
* @template T
* @param {T} arg
* @return {T}
*/
shaka.util.ConfigUtils.cloneObject = function(arg) {
return JSON.parse(JSON.stringify(arg));
var seenObjects = [];
// This recursively clones the value |val|, using the captured variable
// |seenObjects| to track the objects we have already cloned.
var clone = function(val) {
switch (typeof val) {
case 'undefined':
case 'boolean':
case 'number':
case 'string':
case 'symbol':
case 'function':
return val;
case 'object':
default:
// typeof null === 'object'
if (!val) return val;

if (seenObjects.indexOf(val) >= 0)
return null;
var isArray = val.constructor == Array;
if (val.constructor != Object && !isArray)
return null;

seenObjects.push(val);
var ret = isArray ? [] : {};
// Note |name| will equal a number for arrays.
for (var name in val) {
ret[name] = clone(val[name]);
}

// Length is a non-enumerable property, but we should copy it over in
// case it is not the default.
if (isArray)
ret.length = val.length;
return ret;
}
};
return clone(arg);
};
94 changes: 94 additions & 0 deletions test/util/config_utils_unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

describe('ConfigUtils', function() {
/** @const */
var ConfigUtils = shaka.util.ConfigUtils;

describe('cloneObject', function() {
/** @const */
var cloneObject = ConfigUtils.cloneObject;

it('clones values and plain objects', function() {
expect(cloneObject(2)).toBe(2);
expect(cloneObject('foo')).toBe('foo');
expect(cloneObject(false)).toBe(false);

var o = {foo: 'bar', count: 123};
var copy = cloneObject(o);
expect(copy).not.toBe(o);
expect(copy).toEqual(o);

o = [1, 2, undefined, 4, 5];
copy = cloneObject(o);
expect(copy).not.toBe(o);
expect(copy).toEqual(o);
});

it('clones nested objects', function() {
var o = {
foo: 'bar',
lorem: 'ipsum',
dolor: {
sit: 123,
amet: ',',
consectetur: {},
adipisecing: [1, 2, 3],
elit: [
1, null,
{
sed: null,
'do': undefined,
eiusmod: []
}
]
},
player: {
other: 123
}
};
var copy = cloneObject(o);
expect(copy).not.toBe(o);
expect(copy).toEqual(o);
});

it('clones Arrays with non-default length', function() {
var a = [1, 2, 3];
a.length = 10;
var copy = cloneObject(a);
expect(copy).toEqual(a);
expect(copy.length).toEqual(10);
});

it('ignores cyclic objects', function() {
var o = {foo: 'bar'};
o['baz'] = o;
expect(cloneObject(o)).toEqual({foo: 'bar', baz: null});
});

it('ignores non-simple Object objects', function() {
var o = {foo: 1, baz: /foo/g};
expect(cloneObject(o)).toEqual({foo: 1, baz: null});

o = {foo: 2, baz: new Date(123)};
expect(cloneObject(o)).toEqual({foo: 2, baz: null});

o = {foo: 3, baz: document.createElement('div')};
expect(cloneObject(o)).toEqual({foo: 3, baz: null});
});
});
});

0 comments on commit 06c9844

Please sign in to comment.