/////////////////////////////////////////////////////////////////////
| 776 | |
| 777 | ////////////////////////////////////////////////////////////////////////// |
| 778 | bool CScriptTable::AddFunction(const SUserFunctionDesc& fd) |
| 779 | { |
| 780 | CHECK_STACK(L); |
| 781 | |
| 782 | assert(fd.pFunctor || fd.pUserDataFunc != 0); |
| 783 | |
| 784 | // Make function signature. |
| 785 | char sFuncSignature[256]; |
| 786 | if (fd.sGlobalName[0] != 0) |
| 787 | cry_sprintf(sFuncSignature, "%s.%s(%s)", fd.sGlobalName, fd.sFunctionName, fd.sFunctionParams); |
| 788 | else |
| 789 | cry_sprintf(sFuncSignature, "%s.%s(%s)", fd.sGlobalName, fd.sFunctionName, fd.sFunctionParams); |
| 790 | |
| 791 | PushRef(); |
| 792 | lua_pushstring(L, fd.sFunctionName); |
| 793 | |
| 794 | int8 nParamIdOffset = fd.nParamIdOffset; |
| 795 | if (fd.pFunctor) |
| 796 | { |
| 797 | int nDataSize = sizeof(fd.pFunctor) + strlen(sFuncSignature) + 1 + 1; |
| 798 | |
| 799 | // Store functor in first upvalue. |
| 800 | unsigned char* pBuffer = (unsigned char*)lua_newuserdata(L, nDataSize); |
| 801 | memcpy(pBuffer, &fd.pFunctor, sizeof(fd.pFunctor)); |
| 802 | memcpy(pBuffer + sizeof(fd.pFunctor), &nParamIdOffset, 1); |
| 803 | memcpy(pBuffer + sizeof(fd.pFunctor) + 1, sFuncSignature, strlen(sFuncSignature) + 1); |
| 804 | lua_pushcclosure(L, StdCFunction, 1); |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | assert(fd.pDataBuffer != NULL && fd.nDataSize > 0); |
| 809 | UserDataFunction function = fd.pUserDataFunc; |
| 810 | int nSize = fd.nDataSize; |
| 811 | int nTotalSize = sizeof(function) + sizeof(int) + nSize + strlen(sFuncSignature) + 1 + 1; |
| 812 | // Store functor in first upvalue. |
| 813 | unsigned char* pBuffer = (unsigned char*)lua_newuserdata(L, nTotalSize); |
| 814 | memcpy(pBuffer, &function, sizeof(function)); |
| 815 | memcpy(pBuffer + sizeof(function), &nSize, sizeof(nSize)); |
| 816 | memcpy(pBuffer + sizeof(function) + sizeof(nSize), fd.pDataBuffer, nSize); |
| 817 | memcpy(pBuffer + sizeof(function) + sizeof(nSize) + nSize, &nParamIdOffset, 1); |
| 818 | memcpy(pBuffer + sizeof(function) + sizeof(nSize) + nSize + 1, sFuncSignature, strlen(sFuncSignature) + 1); |
| 819 | lua_pushcclosure(L, StdCUserDataFunction, 1); |
| 820 | } |
| 821 | |
| 822 | lua_rawset(L, -3); |
| 823 | lua_pop(L, 1); // pop table. |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | ////////////////////////////////////////////////////////////////////////// |
| 828 | void* CScriptTable::operator new(size_t nSize) |
no test coverage detected