diff --git a/tfjs-core/src/tensor.ts b/tfjs-core/src/tensor.ts index c9c2d68ff01..e1c09828676 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 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. + // See https://github.com/tensorflow/tfjs/issues/3384 for backstory. + return !!instance && instance.data != null && instance.dataSync != null && + instance.throwIfDisposed != null; } }); 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);