| 1334 | } |
| 1335 | |
| 1336 | int asCContext::SetArgObject(asUINT arg, void *obj) |
| 1337 | { |
| 1338 | if( m_status != asEXECUTION_PREPARED ) |
| 1339 | return asCONTEXT_NOT_PREPARED; |
| 1340 | |
| 1341 | if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() ) |
| 1342 | { |
| 1343 | m_status = asEXECUTION_ERROR; |
| 1344 | return asINVALID_ARG; |
| 1345 | } |
| 1346 | |
| 1347 | // Verify the type of the argument |
| 1348 | asCDataType *dt = &m_initialFunction->parameterTypes[arg]; |
| 1349 | if( !dt->IsObject() && !dt->IsFuncdef() ) |
| 1350 | { |
| 1351 | m_status = asEXECUTION_ERROR; |
| 1352 | return asINVALID_TYPE; |
| 1353 | } |
| 1354 | |
| 1355 | // If the object should be sent by value we must make a copy of it |
| 1356 | if( !dt->IsReference() ) |
| 1357 | { |
| 1358 | if( dt->IsObjectHandle() ) |
| 1359 | { |
| 1360 | // Increase the reference counter |
| 1361 | if (obj && dt->IsFuncdef()) |
| 1362 | ((asIScriptFunction*)obj)->AddRef(); |
| 1363 | else |
| 1364 | { |
| 1365 | asSTypeBehaviour *beh = &CastToObjectType(dt->GetTypeInfo())->beh; |
| 1366 | if (obj && beh->addref) |
| 1367 | m_engine->CallObjectMethod(obj, beh->addref); |
| 1368 | } |
| 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | obj = m_engine->CreateScriptObjectCopy(obj, dt->GetTypeInfo()); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | // Determine the position of the argument |
| 1377 | int offset = 0; |
| 1378 | if( m_initialFunction->objectType ) |
| 1379 | offset += AS_PTR_SIZE; |
| 1380 | |
| 1381 | // If function returns object by value an extra pointer is pushed on the stack |
| 1382 | if( m_returnValueSize ) |
| 1383 | offset += AS_PTR_SIZE; |
| 1384 | |
| 1385 | for( asUINT n = 0; n < arg; n++ ) |
| 1386 | offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords(); |
| 1387 | |
| 1388 | // Set the value |
| 1389 | *(asPWORD*)(&m_regs.stackFramePointer[offset]) = (asPWORD)obj; |
| 1390 | |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |