Advertisement

Using const char*

Started by August 07, 2012 08:29 PM
5 comments, last by WitchLord 12 years, 3 months ago
I have a lot of functions that using const char* as argument type. Can i bind these functions? Or i necessarily have to using std::string?

What i would like to do:
C++:
void print(const char *str){
printf("%s",str);
}


Script:
print("text");

Thanks in advance.
It is recommended to use wrappers for functions that take or return const char *. Pointers like these are simply not safe. You would have little control over when strings are created or destroyed, and will likely end up with loose pointers that can cause any kind of havok in your application.

That said, if you are in full control over who writes the scripts and accept the same potential problems that occur in C++ you can register the const char * as a type like this:


engine->RegisterObjectType("charptr", sizeof(const char *), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE);


Then use this type to register the functions that take or return the const char*, i.e.


engine->RegisterGlobalFunction("void print(charptr)", asFUNCTION(print), asCALL_CDECL);


Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
I have problems with this on 64bit mac. When i try to build the script i get errors:

Don't support passing type 'charptr' by value to application in native calling convention on this platform

How can i fix this problem?
Thanks in advance.
I'll look into it.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

A couple of changes in the library is needed:

as_callfunc.cpp, function PrepareSystemFunction(), around line 325:


#ifdef SPLIT_OBJS_BY_MEMBER_TYPES
// It's not safe to pass objects by value because different registers
// will be used depending on the memory layout of the object
// Ref: http://www.x86-64.org/documentation/abi.pdf
// Ref: http://www.agner.org/optimize/calling_conventions.pdf
if(
#ifdef COMPLEX_OBJS_PASSED_BY_REF
!(func->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) &&
#endif
#ifdef LARGE_OBJS_PASS_BY_REF
func->parameterTypes[n].GetSizeInMemoryDWords() < AS_LARGE_OBJ_MIN_SIZE &&
#endif
- !(func->parameterTypes[n].GetObjectType()->flags & (asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
+ !(func->parameterTypes[n].GetObjectType()->flags & (asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT | asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
{
engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
asCString str;
str.Format(TXT_DONT_SUPPORT_TYPE_s_BY_VAL, func->parameterTypes[n].GetObjectType()->name.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
}
#endif



and in

as_callfunc_x64_gcc.cpp, function CallSystemFunctionNative(), around line 290:


// An object is being passed by value
if( (descr->parameterTypes[a].GetObjectType()->flags & COMPLEX_MASK) ||
descr->parameterTypes[a].GetSizeInMemoryDWords() > 4 )
{
// Copy the address of the object
argsType[argIndex] = x64INTARG;
memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asQWORD));
argIndex++;
}
- else if( descr->parameterTypes[a].GetObjectType()->flags & asOBJ_APP_CLASS_ALLINTS )
+ else if( (descr->parameterTypes[a].GetObjectType()->flags & asOBJ_APP_CLASS_ALLINTS) || (descr->parameterTypes[a].GetObjectType()->flags & asOBJ_APP_PRIMITIVE) )
{
// Copy the value of the object


I'll have these changes checked in as soon as possible.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Big thanks for beautiful support, Andreas!
It works.
Advertisement
Thanks for confirming that it works.

I've checked in the changes in the SVN revision 1403.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement