| 320 | } |
| 321 | |
| 322 | CScriptArray::CScriptArray(asITypeInfo* ti, void* buf) |
| 323 | { |
| 324 | // The object type should be the template instance of the array |
| 325 | DEATH_ASSERT(ti != nullptr && StringView(ti->GetName()) == "array"_s); |
| 326 | |
| 327 | refCount = 1; |
| 328 | gcFlag = false; |
| 329 | objType = ti; |
| 330 | objType->AddRef(); |
| 331 | buffer = 0; |
| 332 | |
| 333 | Precache(); |
| 334 | |
| 335 | asIScriptEngine* engine = ti->GetEngine(); |
| 336 | |
| 337 | // Determine element size |
| 338 | if (subTypeId & asTYPEID_MASK_OBJECT) { |
| 339 | elementSize = sizeof(asPWORD); |
| 340 | } else { |
| 341 | elementSize = engine->GetSizeOfPrimitiveType(subTypeId); |
| 342 | } |
| 343 | |
| 344 | // Determine the initial size from the buffer |
| 345 | std::uint32_t length = *(std::uint32_t*)buf; |
| 346 | |
| 347 | // Make sure the array size isn't too large for us to handle |
| 348 | if (!CheckMaxSize(length)) { |
| 349 | // Don't continue with the initialization |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | // Copy the values of the array elements from the buffer |
| 354 | if ((ti->GetSubTypeId() & asTYPEID_MASK_OBJECT) == 0) { |
| 355 | CreateBuffer(&buffer, length); |
| 356 | |
| 357 | // Copy the values of the primitive type into the internal buffer |
| 358 | if (length > 0) { |
| 359 | std::memcpy(At(0), (((std::uint32_t*)buf) + 1), length * elementSize); |
| 360 | } |
| 361 | } else if (ti->GetSubTypeId() & asTYPEID_OBJHANDLE) { |
| 362 | CreateBuffer(&buffer, length); |
| 363 | |
| 364 | // Copy the handles into the internal buffer |
| 365 | if (length > 0) { |
| 366 | std::memcpy(At(0), (((std::uint32_t*)buf) + 1), length * elementSize); |
| 367 | } |
| 368 | |
| 369 | // With object handles it is safe to clear the memory in the received buffer |
| 370 | // instead of increasing the ref count. It will save time both by avoiding the |
| 371 | // call the increase ref, and also relieve the engine from having to release |
| 372 | // its references too |
| 373 | std::memset((((std::uint32_t*)buf) + 1), 0, length * elementSize); |
| 374 | } else if (ti->GetSubType()->GetFlags() & asOBJ_REF) { |
| 375 | // Only allocate the buffer, but not the objects |
| 376 | subTypeId |= asTYPEID_OBJHANDLE; |
| 377 | CreateBuffer(&buffer, length); |
| 378 | subTypeId &= ~asTYPEID_OBJHANDLE; |
| 379 | |