From 4f5ee37ac96eba10fd5b31be09b9599902386c44 Mon Sep 17 00:00:00 2001 From: Yannick Assogba Date: Thu, 10 Sep 2020 16:17:45 -0400 Subject: [PATCH 1/3] make tensor instanceof more robust --- tfjs-core/src/tensor.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tfjs-core/src/tensor.ts b/tfjs-core/src/tensor.ts index c9c2d68ff01..af7e260dfbf 100644 --- a/tfjs-core/src/tensor.ts +++ b/tfjs-core/src/tensor.ts @@ -292,7 +292,8 @@ export class Tensor { return opHandler.buffer(this.shape, this.dtype as D, vals); } - /** Returns a `tf.TensorBuffer` that holds the underlying data. + /** + * Returns a `tf.TensorBuffer` that holds the underlying data. * @doc {heading: 'Tensors', subheading: 'Classes'} */ bufferSync(): TensorBuffer { @@ -411,7 +412,8 @@ export class Tensor { return opHandler.print(this, verbose); } - /** Returns a copy of the tensor. See `tf.clone` for details. + /** + * Returns a copy of the tensor. See `tf.clone` for details. * @doc {heading: 'Tensors', subheading: 'Classes'} */ clone(this: T): T { @@ -441,8 +443,13 @@ export class Tensor { } Object.defineProperty(Tensor, Symbol.hasInstance, { value: (instance: Tensor) => { - return !!instance && instance.dataId != null && instance.shape != null && - instance.dtype != null; + // Implementation note: we should use properties of the object will be + // defined before the constructor body has finished executing (methods). + // This is because when this code is transpiled by babel, babel will call + // classCallCheck before the constructor body is run. + // See https://github.com/tensorflow/tfjs/issues/3384 for backstory. + return !!instance && instance.data != null && instance.dataSync != null && + instance.throwIfDisposed != null; } }); From ffbc0aa8b7ea1fcd0b629aa15f7347a8cfc57773 Mon Sep 17 00:00:00 2001 From: Yannick Assogba Date: Thu, 10 Sep 2020 17:06:46 -0400 Subject: [PATCH 2/3] save --- tfjs-core/src/tensor_test.ts | 5 ----- tfjs-core/src/variable_test.ts | 5 ----- 2 files changed, 10 deletions(-) diff --git a/tfjs-core/src/tensor_test.ts b/tfjs-core/src/tensor_test.ts index 207845fb35f..ae95b38ffd1 100644 --- a/tfjs-core/src/tensor_test.ts +++ b/tfjs-core/src/tensor_test.ts @@ -2208,11 +2208,6 @@ describeWithFlags('x instanceof Tensor', ALL_ENVS, () => { expect(t instanceof Tensor).toBe(true); }); - it('x: Tensor-like', () => { - const t = {shape: [2], dtype: 'float32', dataId: {}}; - expect(t instanceof Tensor).toBe(true); - }); - it('x: other object, fails', () => { const t = {something: 'else'}; expect(t instanceof Tensor).toBe(false); diff --git a/tfjs-core/src/variable_test.ts b/tfjs-core/src/variable_test.ts index a3fac13c02a..80763bb0320 100644 --- a/tfjs-core/src/variable_test.ts +++ b/tfjs-core/src/variable_test.ts @@ -220,11 +220,6 @@ describeWithFlags('x instanceof Variable', ALL_ENVS, () => { expect(t instanceof Variable).toBe(true); }); - it('x: Variable-like', () => { - const t = {assign: () => {}, shape: [2], dtype: 'float32', dataId: {}}; - expect(t instanceof Variable).toBe(true); - }); - it('x: other object, fails', () => { const t = {something: 'else'}; expect(t instanceof Variable).toBe(false); From 8195fafbac856a840aa97fad676d80d7054d0785 Mon Sep 17 00:00:00 2001 From: Yannick Assogba Date: Thu, 10 Sep 2020 19:20:08 -0400 Subject: [PATCH 3/3] save --- tfjs-core/src/tensor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfjs-core/src/tensor.ts b/tfjs-core/src/tensor.ts index af7e260dfbf..e1c09828676 100644 --- a/tfjs-core/src/tensor.ts +++ b/tfjs-core/src/tensor.ts @@ -443,7 +443,7 @@ export class Tensor { } Object.defineProperty(Tensor, Symbol.hasInstance, { value: (instance: Tensor) => { - // Implementation note: we should use properties of the object will be + // Implementation note: we should use properties of the object that will be // defined before the constructor body has finished executing (methods). // This is because when this code is transpiled by babel, babel will call // classCallCheck before the constructor body is run.