This is an automated email from the ASF dual-hosted git repository.

junrushao pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/unity by this push:
     new 082d98e8ab Fix equality comparison and code consistency in web (#15621)
082d98e8ab is described below

commit 082d98e8aba5a5aecfa8ef61877395c5bd985c78
Author: Max Base <[email protected]>
AuthorDate: Sat Aug 26 06:57:26 2023 +0100

    Fix equality comparison and code consistency in web (#15621)
    
    * Change == to === in runtime.ts file
    
    * Change == to === in  web/src/rpc_server.ts
    
    * Change == to === in web/src/memory.ts
---
 web/src/memory.ts     |   2 +-
 web/src/rpc_server.ts |  36 +++++++++---------
 web/src/runtime.ts    | 102 +++++++++++++++++++++++++-------------------------
 3 files changed, 70 insertions(+), 70 deletions(-)

diff --git a/web/src/memory.ts b/web/src/memory.ts
index 0fb75fc1db..dbbb449a0b 100644
--- a/web/src/memory.ts
+++ b/web/src/memory.ts
@@ -251,7 +251,7 @@ export class CachedCallStack implements Disposable {
    */
   reset(): void {
     this.stackTop = 0;
-    assert(this.addressToSetTargetValue.length == 0);
+    assert(this.addressToSetTargetValue.length === 0);
     while (this.tempArgs.length != 0) {
       (this.tempArgs.pop() as Disposable).dispose();
     }
diff --git a/web/src/rpc_server.ts b/web/src/rpc_server.ts
index 25f89672cd..46848a6dec 100644
--- a/web/src/rpc_server.ts
+++ b/web/src/rpc_server.ts
@@ -140,7 +140,7 @@ export class RPCServer {
       this.log(this.inst.runtimeStatsText());
       this.inst.dispose();
     }
-    if (this.state == RPCServerState.ReceivePacketHeader) {
+    if (this.state === RPCServerState.ReceivePacketHeader) {
       this.log("Closing the server in clean state");
       this.log("Automatic reconnecting..");
       new RPCServer(
@@ -197,20 +197,20 @@ export class RPCServer {
         this.currPacketHeader = this.readFromBuffer(SizeOf.I64);
         const reader = new ByteStreamReader(this.currPacketHeader);
         this.currPacketLength = reader.readU64();
-        assert(this.pendingBytes == 0);
+        assert(this.pendingBytes === 0);
         this.requestBytes(this.currPacketLength);
         this.state = RPCServerState.ReceivePacketBody;
         break;
       }
       case RPCServerState.ReceivePacketBody: {
         const body = this.readFromBuffer(this.currPacketLength);
-        assert(this.pendingBytes == 0);
+        assert(this.pendingBytes === 0);
         assert(this.currPacketHeader !== undefined);
         this.onPacketReady(this.currPacketHeader, body);
         break;
       }
       case RPCServerState.WaitForCallback: {
-        assert(this.pendingBytes == 0);
+        assert(this.pendingBytes === 0);
         break;
       }
       default: {
@@ -236,10 +236,10 @@ export class RPCServer {
 
       for (let i = 0; i < nargs; ++i) {
         const tcode = tcodes[i];
-        if (tcode == ArgTypeCode.TVMStr) {
+        if (tcode === ArgTypeCode.TVMStr) {
           const str = Uint8ArrayToString(reader.readByteArray());
           args.push(str);
-        } else if (tcode == ArgTypeCode.TVMBytes) {
+        } else if (tcode === ArgTypeCode.TVMBytes) {
           args.push(reader.readByteArray());
         } else {
           throw new Error("cannot support type code " + tcode);
@@ -261,8 +261,8 @@ export class RPCServer {
     body: Uint8Array
   ): void {
     // start the server
-    assert(args[0] == "rpc.WasmSession");
-    assert(this.pendingBytes == 0);
+    assert(args[0] === "rpc.WasmSession");
+    assert(this.pendingBytes === 0);
 
     const asyncInitServer = async (): Promise<void> => {
       assert(args[1] instanceof Uint8Array);
@@ -293,10 +293,10 @@ export class RPCServer {
       }
 
       if (this.ndarrayCacheUrl.length != 0) {
-        if (this.ndarrayCacheDevice == "cpu") {
+        if (this.ndarrayCacheDevice === "cpu") {
           await this.inst.fetchNDArrayCache(this.ndarrayCacheUrl, 
this.inst.cpu());
         } else {
-          assert(this.ndarrayCacheDevice == "webgpu");
+          assert(this.ndarrayCacheDevice === "webgpu");
           await this.inst.fetchNDArrayCache(this.ndarrayCacheUrl, 
this.inst.webgpu());
         }
       }
@@ -309,7 +309,7 @@ export class RPCServer {
       const messageHandler = fcreate(
         (cbytes: Uint8Array): runtime.Scalar => {
           assert(this.inst !== undefined);
-          if (this.socket.readyState == 1) {
+          if (this.socket.readyState === 1) {
             // WebSocket will automatically close the socket
             // if we burst send data that exceeds its internal buffer
             // wait a bit before we send next one.
@@ -349,10 +349,10 @@ export class RPCServer {
       const writeFlag = this.inst.scalar(3, "int32");
 
       this.serverRecvData = (header: Uint8Array, body: Uint8Array): void => {
-        if (messageHandler(header, writeFlag) == 0) {
+        if (messageHandler(header, writeFlag) === 0) {
           this.socket.close();
         }
-        if (messageHandler(body, writeFlag) == 0) {
+        if (messageHandler(body, writeFlag) === 0) {
           this.socket.close();
         }
       };
@@ -396,14 +396,14 @@ export class RPCServer {
   private handleInitHeader(): void {
     const reader = new ByteStreamReader(this.readFromBuffer(SizeOf.I32 * 2));
     const magic = reader.readU32();
-    if (magic == RPC_MAGIC + 1) {
+    if (magic === RPC_MAGIC + 1) {
       throw new Error("key: " + this.key + " has already been used in proxy");
-    } else if (magic == RPC_MAGIC + 2) {
+    } else if (magic === RPC_MAGIC + 2) {
       throw new Error("RPCProxy do not have matching client key " + this.key);
     }
-    assert(magic == RPC_MAGIC, this.url + " is not an RPC Proxy");
+    assert(magic === RPC_MAGIC, this.url + " is not an RPC Proxy");
     this.remoteKeyLength = reader.readU32();
-    assert(this.pendingBytes == 0);
+    assert(this.pendingBytes === 0);
     this.requestBytes(this.remoteKeyLength);
     this.state = RPCServerState.InitHeaderKey;
   }
@@ -413,7 +413,7 @@ export class RPCServer {
     const remoteKey = Uint8ArrayToString(
       this.readFromBuffer(this.remoteKeyLength)
     );
-    assert(this.pendingBytes == 0);
+    assert(this.pendingBytes === 0);
     this.requestBytes(SizeOf.I64);
     this.state = RPCServerState.ReceivePacketHeader;
   }
diff --git a/web/src/runtime.ts b/web/src/runtime.ts
index ca93012f5e..da58080953 100644
--- a/web/src/runtime.ts
+++ b/web/src/runtime.ts
@@ -206,7 +206,7 @@ class RuntimeContext implements Disposable {
   }
 
   endScope(): void {
-    if (this.autoDisposeScope.length == 0) {
+    if (this.autoDisposeScope.length === 0) {
       throw Error("tvm.endScope called when the stack is empty.");
     }
     // automatically dispose all the tracked values in the current scope.
@@ -228,7 +228,7 @@ class RuntimeContext implements Disposable {
    *       The return value of PackedFunc will be automatically tracked.
    */
   attachToCurrentScope<T extends Disposable>(obj: T): T {
-    if (this.autoDisposeScope.length == 0) {
+    if (this.autoDisposeScope.length === 0) {
       throw Error("Must call beginScope to use functions that returns TVM 
objects");
     }
     const currScope = this.autoDisposeScope[this.autoDisposeScope.length - 1];
@@ -303,7 +303,7 @@ class PackedFuncCell implements Disposable {
   }
 
   getHandle(requireNotNull = true): Pointer {
-    if (requireNotNull && this.handle == 0) {
+    if (requireNotNull && this.handle === 0) {
       throw Error("PackedFunc has already been disposed");
     }
     return this.handle;
@@ -341,12 +341,12 @@ export class DLDevice {
 
   constructor(deviceType: number | string, deviceId: number, lib: FFILibrary) {
     const tp = typeof deviceType;
-    if (tp == "string") {
+    if (tp === "string") {
       this.deviceType = DeviceStrToEnum[deviceType];
-      if (this.deviceType == undefined) {
+      if (this.deviceType === undefined) {
         throw new Error("Cannot recogonize deviceType " + deviceType);
       }
-    } else if (tp == "number") {
+    } else if (tp === "number") {
       this.deviceType = deviceType as number;
     } else {
       throw new Error("Cannot take type " + tp + " as deviceType");
@@ -359,7 +359,7 @@ export class DLDevice {
    * Synchronize the device
    */
   async sync(): Promise<void> {
-    if (this.deviceType == DeviceStrToEnum.webgpu) {
+    if (this.deviceType === DeviceStrToEnum.webgpu) {
       assert(this.lib.webGPUContext !== undefined);
       await this.lib.webGPUContext.sync();
     }
@@ -509,7 +509,7 @@ export class NDArray implements Disposable {
    * @returns The handle.
    */
   getHandle(requireNotNull = true): Pointer {
-    if (requireNotNull && this.handle == 0) {
+    if (requireNotNull && this.handle === 0) {
       throw Error("NDArray has already been disposed");
     }
     return this.handle;
@@ -521,7 +521,7 @@ export class NDArray implements Disposable {
    * @returns The handle.
    */
   getDataPtr(): Pointer {
-    if (this.handle == 0) {
+    if (this.handle === 0) {
       throw Error("NDArray has already been disposed");
     }
     return this.dataPtr;
@@ -565,15 +565,15 @@ export class NDArray implements Disposable {
         );
       }
       let buffer: ArrayBuffer;
-      if (this.dtype == "float32") {
+      if (this.dtype === "float32") {
         buffer = Float32Array.from(data).buffer;
-      } else if (this.dtype == "float64") {
+      } else if (this.dtype === "float64") {
         buffer = Float64Array.from(data).buffer;
-      } else if (this.dtype == "int32") {
+      } else if (this.dtype === "int32") {
         buffer = Int32Array.from(data).buffer;
-      } else if (this.dtype == "int8") {
+      } else if (this.dtype === "int8") {
         buffer = Int8Array.from(data).buffer;
-      } else if (this.dtype == "uint8") {
+      } else if (this.dtype === "uint8") {
         buffer = Uint8Array.from(data).buffer;
       } else {
         throw new Error("Unsupported data type " + this.dtype);
@@ -588,7 +588,7 @@ export class NDArray implements Disposable {
    */
   copyFromRawBytes(data: Uint8Array): this {
     // short cut for gpu copy
-    if (this.device.deviceType == DeviceStrToEnum.webgpu) {
+    if (this.device.deviceType === DeviceStrToEnum.webgpu) {
       this.lib.webGPUContext?.copyRawBytesToBuffer(data, this.getDataPtr(), 0, 
data.length);
       return this;
     }
@@ -654,15 +654,15 @@ export class NDArray implements Disposable {
    */
   toArray(): Float32Array | Float64Array | Int32Array | Int8Array | Uint8Array 
{
     const stype = this.dtype;
-    if (stype == "float32") {
+    if (stype === "float32") {
       return new Float32Array(this.toRawBytes().buffer);
-    } else if (stype == "float64") {
+    } else if (stype === "float64") {
       return new Float64Array(this.toRawBytes().buffer);
-    } else if (stype == "int32") {
+    } else if (stype === "int32") {
       return new Int32Array(this.toRawBytes().buffer);
-    } else if (stype == "int8") {
+    } else if (stype === "int8") {
       return new Int8Array(this.toRawBytes().buffer);
-    } else if (stype == "uint8") {
+    } else if (stype === "uint8") {
       return new Uint8Array(this.toRawBytes().buffer);
     } else {
       throw new Error("Unsupported data type " + this.dtype);
@@ -710,7 +710,7 @@ export class Module implements Disposable {
    * @returns The handle.
    */
   getHandle(requireNotNull = true): Pointer {
-    if (requireNotNull && this.handle == 0) {
+    if (requireNotNull && this.handle === 0) {
       throw Error("Module has already been disposed");
     }
     return this.handle;
@@ -723,7 +723,7 @@ export class Module implements Disposable {
    * @returns The result function.
    */
   getFunction(name: string, queryImports = true): PackedFunc {
-    if (this.handle == 0) {
+    if (this.handle === 0) {
       throw Error("Module has already been disposed");
     }
     const stack = this.lib.getOrAllocCallStack();
@@ -745,7 +745,7 @@ export class Module implements Disposable {
     );
     const handle = this.lib.memory.loadPointer(outPtr);
     this.lib.recycleCallStack(stack);
-    if (handle == 0) {
+    if (handle === 0) {
       throw Error("Cannot find function " + name);
     }
     const ret = this.makePackedFunc(handle);
@@ -800,7 +800,7 @@ export class TVMObject implements Disposable {
    * @returns The handle.
    */
   getHandle(requireNotNull = true): Pointer {
-    if (requireNotNull && this.handle == 0) {
+    if (requireNotNull && this.handle === 0) {
       throw Error("Module has already been disposed");
     }
     return this.handle;
@@ -808,7 +808,7 @@ export class TVMObject implements Disposable {
 
   /** get the type index of the object */
   typeIndex(): number {
-    if (this.handle == 0) {
+    if (this.handle === 0) {
       throw Error("The current Object has already been disposed");
     }
     const stack = this.lib.getOrAllocCallStack();
@@ -999,7 +999,7 @@ export class ArtifactCache {
       await this.cache.add(request);
       result = await this.cache.match(request);
     }
-    if (result == undefined) {
+    if (result === undefined) {
       throw Error("Cannot fetch " + url);
     }
     return result;
@@ -1321,7 +1321,7 @@ export class Instance implements Disposable {
     );
     const handle = this.memory.loadPointer(outPtr);
     this.lib.recycleCallStack(stack);
-    if (handle == 0) {
+    if (handle === 0) {
       throw Error("Cannot find global function " + name);
     }
     const ret = this.makePackedFunc(handle);
@@ -1337,7 +1337,7 @@ export class Instance implements Disposable {
    */
   isPackedFunc(func: unknown): boolean {
     // eslint-disable-next-line no-prototype-builtins
-    return typeof func == "function" && func.hasOwnProperty("_tvmPackedCell");
+    return typeof func === "function" && func.hasOwnProperty("_tvmPackedCell");
   }
 
   /**
@@ -1541,7 +1541,7 @@ export class Instance implements Disposable {
         // first sync copy to cpu.
         this.ctx.arrayDecodeStorage(cpu_arr, new Uint8Array(recSource), 
rec.format);
         // then async stream into GPU if needed
-        if (device.deviceType == DeviceStrToEnum.cpu) {
+        if (device.deviceType === DeviceStrToEnum.cpu) {
           this.ndarrayCacheUpdate(rec.name, cpu_arr, false);
           cpu_arr.dispose();
         } else {
@@ -1571,21 +1571,21 @@ export class Instance implements Disposable {
    */
   toDLDataType(dtype: string | DLDataType): DLDataType {
     if (dtype instanceof DLDataType) return dtype;
-    if (typeof dtype == "string") {
+    if (typeof dtype === "string") {
       let pattern = dtype;
       let code,
         bits = 32,
         lanes = 1;
-      if (pattern.substring(0, 5) == "float") {
+      if (pattern.substring(0, 5) === "float") {
         pattern = pattern.substring(5, pattern.length);
         code = DLDataTypeCode.Float;
-      } else if (pattern.substring(0, 3) == "int") {
+      } else if (pattern.substring(0, 3) === "int") {
         pattern = pattern.substring(3, pattern.length);
         code = DLDataTypeCode.Int;
-      } else if (pattern.substring(0, 4) == "uint") {
+      } else if (pattern.substring(0, 4) === "uint") {
         pattern = pattern.substring(4, pattern.length);
         code = DLDataTypeCode.UInt;
-      } else if (pattern.substring(0, 6) == "handle") {
+      } else if (pattern.substring(0, 6) === "handle") {
         pattern = pattern.substring(5, pattern.length);
         code = DLDataTypeCode.OpaqueHandle;
         bits = 64;
@@ -1596,7 +1596,7 @@ export class Instance implements Disposable {
       const arr = pattern.split("x");
       if (arr.length >= 1) {
         const parsed = parseInt(arr[0]);
-        if (parsed + "" == arr[0]) {
+        if (parsed + "" === arr[0]) {
           bits = parsed;
         }
       }
@@ -1659,7 +1659,7 @@ export class Instance implements Disposable {
     dev: DLDevice = this.device("cpu", 0)
   ): NDArray {
     dtype = this.toDLDataType(dtype);
-    shape = typeof shape == "number" ? [shape] : shape;
+    shape = typeof shape === "number" ? [shape] : shape;
 
     const stack = this.lib.getOrAllocCallStack();
     const shapeOffset = stack.allocRawBytes(shape.length * SizeOf.I64);
@@ -1898,7 +1898,7 @@ export class Instance implements Disposable {
    * @param mod The input module.
    */
   async asyncLoadWebGPUPipelines(mod: Module): Promise<void> {
-    if (this.lib.webGPUContext == undefined) throw Error("WebGPU not 
initialied");
+    if (this.lib.webGPUContext === undefined) throw Error("WebGPU not 
initialied");
     const webgpuContext = this.lib.webGPUContext;
 
     this.beginScope();
@@ -1922,7 +1922,7 @@ export class Instance implements Disposable {
 
     for (const [key, finfo] of fmapEntries) {
       const code = fGetShader(key);
-      assert(key == finfo.name);
+      assert(key === finfo.name);
       const event = webgpuContext.createShaderAsync(finfo, code).then((func) 
=> {
         this.beginScope();
         fUpdatePrebuild(key, func);
@@ -1955,7 +1955,7 @@ export class Instance implements Disposable {
       allEvents = Promise.all([allEvents, event]).then(() => { });
     }
     await allEvents;
-    assert(finishCounter == fmapEntries.length);
+    assert(finishCounter === fmapEntries.length);
   }
 
   /**
@@ -2033,13 +2033,13 @@ export class Instance implements Disposable {
           const tend: number = perf.now();
 
           durationMs = tend - tstart;
-          if (durationMs == 0) {
+          if (durationMs === 0) {
             absoluteZeroTimes++;
           }
         } while (durationMs < minRepeatMs && absoluteZeroTimes < 
limitZeroTimeIterations);
         const speed = durationMs / setupNumber / 1000;
         result.push(speed);
-        if (cooldownIntervalMs > 0.0 && (i % repeatsToCooldown) == 0) {
+        if (cooldownIntervalMs > 0.0 && (i % repeatsToCooldown) === 0) {
           await new Promise(r => setTimeout(r, cooldownIntervalMs));
         }
       }
@@ -2122,7 +2122,7 @@ export class Instance implements Disposable {
           stack.storeF64(valueOffset, val.value);
           stack.storeI32(codeOffset, ArgTypeCode.Float);
         } else {
-          assert(val.dtype == "handle", "Expect handle");
+          assert(val.dtype === "handle", "Expect handle");
           stack.storePtr(valueOffset, val.value);
           stack.storeI32(codeOffset, ArgTypeCode.TVMOpaqueHandle);
         }
@@ -2130,17 +2130,17 @@ export class Instance implements Disposable {
         stack.storeI32(valueOffset, val.deviceType);
         stack.storeI32(valueOffset + SizeOf.I32, val.deviceType);
         stack.storeI32(codeOffset, ArgTypeCode.DLDevice);
-      } else if (tp == "number") {
+      } else if (tp === "number") {
         stack.storeF64(valueOffset, val);
         stack.storeI32(codeOffset, ArgTypeCode.Float);
         // eslint-disable-next-line no-prototype-builtins
-      } else if (tp == "function" && val.hasOwnProperty("_tvmPackedCell")) {
+      } else if (tp === "function" && val.hasOwnProperty("_tvmPackedCell")) {
         stack.storePtr(valueOffset, val._tvmPackedCell.getHandle());
         stack.storeI32(codeOffset, ArgTypeCode.TVMPackedFuncHandle);
-      } else if (val === null || val == undefined) {
+      } else if (val === null || val === undefined) {
         stack.storePtr(valueOffset, 0);
         stack.storeI32(codeOffset, ArgTypeCode.Null);
-      } else if (tp == "string") {
+      } else if (tp === "string") {
         stack.allocThenSetArgString(valueOffset, val);
         stack.storeI32(codeOffset, ArgTypeCode.TVMStr);
       } else if (val instanceof Uint8Array) {
@@ -2182,11 +2182,11 @@ export class Instance implements Disposable {
         let tcode = lib.memory.loadI32(codePtr);
 
         if (
-          tcode == ArgTypeCode.TVMObjectHandle ||
-          tcode == ArgTypeCode.TVMObjectRValueRefArg ||
-          tcode == ArgTypeCode.TVMPackedFuncHandle ||
-          tcode == ArgTypeCode.TVMNDArrayHandle ||
-          tcode == ArgTypeCode.TVMModuleHandle
+          tcode === ArgTypeCode.TVMObjectHandle ||
+          tcode === ArgTypeCode.TVMObjectRValueRefArg ||
+          tcode === ArgTypeCode.TVMPackedFuncHandle ||
+          tcode === ArgTypeCode.TVMNDArrayHandle ||
+          tcode === ArgTypeCode.TVMModuleHandle
         ) {
           lib.checkCall(
             (lib.exports.TVMCbArgToReturn as ctypes.FTVMCbArgToReturn)(

Reply via email to