diff options
Diffstat (limited to 'js/src/wasm/WasmInstance.cpp')
-rw-r--r-- | js/src/wasm/WasmInstance.cpp | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/js/src/wasm/WasmInstance.cpp b/js/src/wasm/WasmInstance.cpp index bf25b58c14..d025c02c16 100644 --- a/js/src/wasm/WasmInstance.cpp +++ b/js/src/wasm/WasmInstance.cpp @@ -1531,8 +1531,10 @@ static bool ArrayCopyFromData(JSContext* cx, Handle<WasmArrayObject*> arrayObj, // Because `numBytesToCopy` is an in-range `CheckedUint32`, the cast to // `size_t` is safe even on a 32-bit target. - memcpy(arrayObj->data_, &seg->bytes[segByteOffset], - size_t(numBytesToCopy.value())); + if (numElements != 0) { + memcpy(arrayObj->data_, &seg->bytes[segByteOffset], + size_t(numBytesToCopy.value())); + } return true; } @@ -1948,35 +1950,42 @@ static bool ArrayCopyFromElem(JSContext* cx, Handle<WasmArrayObject*> arrayObj, // take into account the enclosing recursion group of the type. This is // temporary until builtin module functions can specify a precise array type // for params/results. -static WasmArrayObject* CastToI16Array(HandleAnyRef ref, bool needMutable) { - if (!ref.isJSObject()) { - return nullptr; - } +template <bool isMutable> +static WasmArrayObject* UncheckedCastToArrayI16(HandleAnyRef ref) { JSObject& object = ref.toJSObject(); - if (!object.is<WasmArrayObject>()) { - return nullptr; - } WasmArrayObject& array = object.as<WasmArrayObject>(); - const ArrayType& type = array.typeDef().arrayType(); - if (type.elementType_ != StorageType::I16) { - return nullptr; + DebugOnly<const ArrayType*> type(&array.typeDef().arrayType()); + MOZ_ASSERT(type->elementType_ == StorageType::I16); + MOZ_ASSERT(type->isMutable_ == isMutable); + return &array; +} + +/* static */ +int32_t Instance::stringTest(Instance* instance, void* stringArg) { + AnyRef string = AnyRef::fromCompiledCode(stringArg); + if (string.isNull() || !string.isJSString()) { + return 0; } - if (needMutable && !type.isMutable_) { + return 1; +} + +/* static */ +void* Instance::stringCast(Instance* instance, void* stringArg) { + AnyRef string = AnyRef::fromCompiledCode(stringArg); + if (string.isNull() || !string.isJSString()) { + ReportTrapError(instance->cx(), JSMSG_WASM_BAD_CAST); return nullptr; } - return &array; + return string.forCompiledCode(); } /* static */ -void* Instance::stringFromWTF16Array(Instance* instance, void* arrayArg, - uint32_t arrayStart, uint32_t arrayCount) { +void* Instance::stringFromCharCodeArray(Instance* instance, void* arrayArg, + uint32_t arrayStart, + uint32_t arrayCount) { JSContext* cx = instance->cx(); RootedAnyRef arrayRef(cx, AnyRef::fromCompiledCode(arrayArg)); - Rooted<WasmArrayObject*> array(cx); - if (!(array = CastToI16Array(arrayRef, false))) { - ReportTrapError(cx, JSMSG_WASM_BAD_CAST); - return nullptr; - } + Rooted<WasmArrayObject*> array(cx, UncheckedCastToArrayI16<true>(arrayRef)); CheckedUint32 lastIndexPlus1 = CheckedUint32(arrayStart) + CheckedUint32(arrayCount); @@ -1997,8 +2006,8 @@ void* Instance::stringFromWTF16Array(Instance* instance, void* arrayArg, } /* static */ -int32_t Instance::stringToWTF16Array(Instance* instance, void* stringArg, - void* arrayArg, uint32_t arrayStart) { +int32_t Instance::stringIntoCharCodeArray(Instance* instance, void* stringArg, + void* arrayArg, uint32_t arrayStart) { JSContext* cx = instance->cx(); AnyRef stringRef = AnyRef::fromCompiledCode(stringArg); if (!stringRef.isJSString()) { @@ -2009,11 +2018,7 @@ int32_t Instance::stringToWTF16Array(Instance* instance, void* stringArg, size_t stringLength = string->length(); RootedAnyRef arrayRef(cx, AnyRef::fromCompiledCode(arrayArg)); - Rooted<WasmArrayObject*> array(cx); - if (!(array = CastToI16Array(arrayRef, true))) { - ReportTrapError(cx, JSMSG_WASM_BAD_CAST); - return -1; - } + Rooted<WasmArrayObject*> array(cx, UncheckedCastToArrayI16<true>(arrayRef)); CheckedUint32 lastIndexPlus1 = CheckedUint32(arrayStart) + stringLength; if (!lastIndexPlus1.isValid() || @@ -2120,8 +2125,8 @@ int32_t Instance::stringLength(Instance* instance, void* stringArg) { return (int32_t)stringRef.toJSString()->length(); } -void* Instance::stringConcatenate(Instance* instance, void* firstStringArg, - void* secondStringArg) { +void* Instance::stringConcat(Instance* instance, void* firstStringArg, + void* secondStringArg) { JSContext* cx = instance->cx(); AnyRef firstStringRef = AnyRef::fromCompiledCode(firstStringArg); @@ -2444,11 +2449,10 @@ bool Instance::init(JSContext* cx, const JSObjectVector& funcImports, if (global.isIndirect()) { // Initialize the cell - wasm::GCPtrVal& cell = globalObjs[i]->val(); - cell = val.get(); + globalObjs[i]->setVal(val); + // Link to the cell - void* address = (void*)&cell.get().cell(); - *(void**)globalAddr = address; + *(void**)globalAddr = globalObjs[i]->addressOfCell(); } else { val.get().writeToHeapLocation(globalAddr); } @@ -2539,6 +2543,7 @@ bool Instance::init(JSContext* cx, const JSObjectVector& funcImports, size_t numWords = std::max<size_t>((numFuncs + 31) / 32, 1); debugFilter_ = (uint32_t*)js_calloc(numWords, sizeof(uint32_t)); if (!debugFilter_) { + ReportOutOfMemory(cx); return false; } } @@ -2552,6 +2557,7 @@ bool Instance::init(JSContext* cx, const JSObjectVector& funcImports, // Take references to the passive data segments if (!passiveDataSegments_.resize(dataSegments.length())) { + ReportOutOfMemory(cx); return false; } for (size_t i = 0; i < dataSegments.length(); i++) { @@ -2563,6 +2569,7 @@ bool Instance::init(JSContext* cx, const JSObjectVector& funcImports, // Create InstanceElemSegments for any passive element segments, since these // are the ones available at runtime. if (!passiveElemSegments_.resize(elemSegments.length())) { + ReportOutOfMemory(cx); return false; } for (size_t i = 0; i < elemSegments.length(); i++) { @@ -2571,6 +2578,7 @@ bool Instance::init(JSContext* cx, const JSObjectVector& funcImports, passiveElemSegments_[i] = InstanceElemSegment(); InstanceElemSegment& instanceSeg = passiveElemSegments_[i]; if (!instanceSeg.reserve(seg.numElements())) { + ReportOutOfMemory(cx); return false; } |