| 370 | } |
| 371 | |
| 372 | CScriptArray::CScriptArray(asITypeInfo *ti, void *buf) |
| 373 | { |
| 374 | // The object type should be the template instance of the array |
| 375 | assert( ti && string(ti->GetName()) == "array" ); |
| 376 | |
| 377 | refCount = 1; |
| 378 | gcFlag = false; |
| 379 | objType = ti; |
| 380 | objType->AddRef(); |
| 381 | buffer = 0; |
| 382 | |
| 383 | Precache(); |
| 384 | |
| 385 | asIScriptEngine *engine = ti->GetEngine(); |
| 386 | |
| 387 | // Determine element size |
| 388 | if( subTypeId & asTYPEID_MASK_OBJECT ) |
| 389 | elementSize = sizeof(asPWORD); |
| 390 | else |
| 391 | elementSize = engine->GetSizeOfPrimitiveType(subTypeId); |
| 392 | |
| 393 | // Determine the initial size from the buffer |
| 394 | asUINT length = *(asUINT*)buf; |
| 395 | |
| 396 | // Make sure the array size isn't too large for us to handle |
| 397 | if( !CheckMaxSize(length) ) |
| 398 | { |
| 399 | // Don't continue with the initialization |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | // Copy the values of the array elements from the buffer |
| 404 | if( (ti->GetSubTypeId() & asTYPEID_MASK_OBJECT) == 0 ) |
| 405 | { |
| 406 | CreateBuffer(&buffer, length); |
| 407 | |
| 408 | // Copy the values of the primitive type into the internal buffer |
| 409 | if( length > 0 ) |
| 410 | memcpy(At(0), (((asUINT*)buf)+1), length * elementSize); |
| 411 | } |
| 412 | else if( ti->GetSubTypeId() & asTYPEID_OBJHANDLE ) |
| 413 | { |
| 414 | CreateBuffer(&buffer, length); |
| 415 | |
| 416 | // Copy the handles into the internal buffer |
| 417 | if( length > 0 ) |
| 418 | memcpy(At(0), (((asUINT*)buf)+1), length * elementSize); |
| 419 | |
| 420 | // With object handles it is safe to clear the memory in the received buffer |
| 421 | // instead of increasing the ref count. It will save time both by avoiding the |
| 422 | // call the increase ref, and also relieve the engine from having to release |
| 423 | // its references too |
| 424 | memset((((asUINT*)buf)+1), 0, length * elementSize); |
| 425 | } |
| 426 | else if( ti->GetSubType()->GetFlags() & asOBJ_REF ) |
| 427 | { |
| 428 | // Only allocate the buffer, but not the objects |
| 429 | subTypeId |= asTYPEID_OBJHANDLE; |