Skip to content
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
30 changes: 27 additions & 3 deletions packages/engine/Source/Core/destroyObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ function returnTrue() {
return true;
}

/**
* Returns the prototype chain of a given object. Object may be an instance
* of an ES6 class, or of a class defined by prototype-based inheritance.
*
* @param {object} object
* @returns {Array<object>}
*/
function getPrototypeChain(object) {
const prototypes = [];

let value = object;
while ((value = Object.getPrototypeOf(value))) {
if (value === Object.prototype) {
break;
}
prototypes.push(value);
}

return prototypes;
}

/**
* Destroys an object. Each of the object's functions, including functions in its prototype,
* is replaced with a function that throws a {@link DeveloperError}, except for the object's
Expand Down Expand Up @@ -40,9 +61,12 @@ function destroyObject(object, message) {
//>>includeEnd('debug');
}

for (const key in object) {
if (typeof object[key] === "function") {
object[key] = throwOnDestroyed;
for (const prototype of getPrototypeChain(object)) {
const descriptors = Object.getOwnPropertyDescriptors(prototype);
for (const key in descriptors) {
if (descriptors[key].writable && typeof object[key] === "function") {
object[key] = throwOnDestroyed;
}
}
}

Expand Down
77 changes: 77 additions & 0 deletions packages/engine/Specs/Core/destroyObjectSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { destroyObject } from "../../index.js";

describe("Core/destroyObject", function () {
it("destroys prototype-based class", function () {
function Parent() {}
Parent.prototype.isDestroyed = () => false;
Parent.prototype.inheritedFn = () => true;

function Child() {}
Child.prototype = new Parent();
Child.prototype.instanceFn = () => true;
Child.staticFn = () => true;

Object.defineProperty(Child.prototype, "getterFn", {
get: () => {
// destroyObject must not execute getters on the target object;
// doing so may throw errors if the getter accesses properties
// that have already been removed.
throw new Error("Getter must not be called.");
},
});

const object = new Child();

expect(object.isDestroyed()).toBe(false);
expect(() => object.inheritedFn()).not.toThrowDeveloperError();
expect(() => object.instanceFn()).not.toThrowDeveloperError();
expect(() => Child.staticFn()).not.toThrowDeveloperError();

destroyObject(object);

expect(object.isDestroyed()).toBe(true);
expect(() => object.inheritedFn()).toThrowDeveloperError();
expect(() => object.instanceFn()).toThrowDeveloperError();
expect(() => Child.staticFn()).not.toThrowDeveloperError();
});

it("destroys ES6 class", function () {
class Parent {
isDestroyed() {
return false;
}
inheritedFn() {
return true;
}
}

class Child extends Parent {
instanceFn() {
return true;
}
staticFn() {
return true;
}
get getterFn() {
// destroyObject must not execute getters on the target object;
// doing so may throw errors if the getter accesses properties
// that have already been removed.
throw new Error("Getter must not be called.");
}
}

const object = new Child();

expect(object.isDestroyed()).toBe(false);
expect(() => object.inheritedFn()).not.toThrowDeveloperError();
expect(() => object.instanceFn()).not.toThrowDeveloperError();
expect(() => Child.staticFn()).not.toThrowDeveloperError();

destroyObject(object);

expect(object.isDestroyed()).toBe(true);
expect(() => object.inheritedFn()).toThrowDeveloperError();
expect(() => object.instanceFn()).toThrowDeveloperError();
expect(() => Child.staticFn()).not.toThrowDeveloperError();
});
});
Loading