Sign in to follow this  

Using const char*

This topic is 1933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++:[CODE]
void print(const char *str){
printf("%s",str);
}
[/CODE]

Script:
[CODE]print("text");[/CODE]

Thanks in advance. Edited by Cordan

Share this post


Link to post
Share on other sites
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:

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

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

[code]
engine->RegisterGlobalFunction("void print(charptr)", asFUNCTION(print), asCALL_CDECL);
[/code]

Regards,
Andreas

Share this post


Link to post
Share on other sites
I have problems with this on 64bit mac. When i try to build the script i get errors:

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

How can i fix this problem?
Thanks in advance.

Share this post


Link to post
Share on other sites
A couple of changes in the library is needed:

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

[code]
#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

[/code]

and in

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

[code]
// 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
[/code]

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

Share this post


Link to post
Share on other sites

This topic is 1933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this