diff --git a/lib/loader/index.js b/lib/loader/index.js index 3eb8950baa..b1e01f5323 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -88,7 +88,10 @@ function postInstantiate(extendedExports, instance) { const __pin = exports.__pin || F_NOEXPORTRUNTIME; const __unpin = exports.__unpin || F_NOEXPORTRUNTIME; const __collect = exports.__collect || F_NOEXPORTRUNTIME; - const __rtti_base = exports.__rtti_base || ~0; // oob if not present + const __rtti_base = exports.__rtti_base; + const getRttiCount = __rtti_base + ? function (arr) { return arr[__rtti_base >>> 2]; } + : F_NOEXPORTRUNTIME; extendedExports.__new = __new; extendedExports.__pin = __pin; @@ -98,7 +101,7 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime type info for the given id. */ function getInfo(id) { const U32 = new Uint32Array(memory.buffer); - const count = U32[__rtti_base >>> 2]; + const count = getRttiCount(U32); if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); return U32[(__rtti_base + 4 >>> 2) + id * 2]; } @@ -113,7 +116,7 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime base id for the given id. */ function getBase(id) { const U32 = new Uint32Array(memory.buffer); - const count = U32[__rtti_base >>> 2]; + const count = getRttiCount(U32); if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); return U32[(__rtti_base + 4 >>> 2) + id * 2 + 1]; } @@ -284,7 +287,7 @@ function postInstantiate(extendedExports, instance) { function __instanceof(ptr, baseId) { const U32 = new Uint32Array(memory.buffer); let id = U32[ptr + ID_OFFSET >>> 2]; - if (id <= U32[__rtti_base >>> 2]) { + if (id <= getRttiCount(U32)) { do { if (id == baseId) return true; id = getBase(id);