hxcpp icon indicating copy to clipboard operation
hxcpp copied to clipboard

[`-D scriptable`]: functions with raw pointer arguments or return type don't compile

Open lemz1 opened this issue 7 months ago • 1 comments

I have noticed an issue when compiling a project with -D scriptable that uses hxvlc and hxdiscord_rpc. Seemingly functions that have arguments that are raw pointers or a return type that is a raw pointer, don't compile. Also functions that return a defined type from raw cpp code, also don't compile.

For example: It tries to cast Dynamic to void*, which isn't possible. The reason this happens is because it uses StackContext::runObject and StackContext::getObject return Dynamic.

generated cpp code snippets:

void* videoLock( void** planes ) {
	if (__scriptVTable[140] ) {
		::hx::CppiaCtx *__ctx = ::hx::CppiaCtx::getCurrent();
		::hx::AutoStack __as(__ctx);
		__ctx->pushObject(this);
		__ctx->pushObject(planes);
		return __ctx->runObject(__scriptVTable[140] ); // returns Dynamic, but we need void*
	}  else return Video_obj::videoLock(planes);return null();}

// ...

template<bool _HX_SUPER=false>
static void CPPIA_CALL __s_videoLock(::hx::CppiaCtx *ctx) {
ctx->returnObject( _HX_SUPER ? ((Video_obj*)ctx->getThis())->Video_obj::videoLock(ctx->getObject(sizeof(void*))) : ((Video_obj*)ctx->getThis())->videoLock(ctx->getObject(sizeof(void*))));
}
 DiscordRichPresence buildPresence(  ::Dynamic params ) {
if (__scriptVTable[6] ) {
	::hx::CppiaCtx *__ctx = ::hx::CppiaCtx::getCurrent();
	::hx::AutoStack __as(__ctx);
	__ctx->pushObject(this);
	__ctx->pushObject(params);
	return __ctx->runObject(__scriptVTable[6] ); // returns Dynamic, but we need DiscordRichPresence
}  else return DiscordClient_obj::buildPresence(params);return null();}

// ...

template<bool _HX_SUPER=false>
static void CPPIA_CALL __s_buildPresence(::hx::CppiaCtx *ctx) {
ctx->returnObject( _HX_SUPER ? ((DiscordClient_obj*)ctx->getThis())->DiscordClient_obj::buildPresence(ctx->getObject(sizeof(void*))) : ((DiscordClient_obj*)ctx->getThis())->buildPresence(ctx->getObject(sizeof(void*))));
}

lemz1 avatar May 13 '25 23:05 lemz1

cpp.RawPointer and similar types are syntactic features for generating specific c++ code with the correct * -> types/operators. Cppia is not c++ code so it wouldn't make sense to be able to expose these types (or functions with these types) to cppia.

There are a few options to still be able to use pointers in a cppia host:

  • Using cpp.Pointer, which is a cppia friendly pointer wrapper with support for dynamic conversions
  • Putting the functions which operate on raw pointers into pure extern or module-private classes, which do not generate scriptable wrappers

This is also discussed in: #1051.

Also functions that return a defined type from raw cpp code, also don't compile.

It isn't very clear what kind of types you mean here, but it also sounds like something that could be solved by using cpp.Pointer for code that is meant to interface with cppia.

tobil4sk avatar Sep 29 '25 11:09 tobil4sk