🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

asINVALID_DECLARATION w/ latest WIP

Started by
6 comments, last by Deyja 18 years, 5 months ago
I've finally upgraded, and am porting over old code. This worked fine in 1.10.1d, and the current documentation tells me I'm doing it properly. Unless I've misinterpreted the meaning of the error, the problem is with the function declaration string. Yes, I am compiling with AS_ALLOW_UNSAFE_REFERENCES. :P I'm just glad I was able to replicate the error! I don't believe it's because I'm returning by reference because A) The documentation tells me to and B) A registration I've omitted (uint8& string::operator[](uint)) works fine.

#include "angelscript.h"
#include <string>
#include <iostream>

void Construct(std::string* ptr) { new (ptr) std::string(); }
void CopyConstruct(const std::string& rhs, std::string* ptr) { new (ptr) std::string(rhs); }
void Destroy(std::string* ptr) { ptr->~basic_string(); } 
std::string& Assign(const std::string& rhs, std::string* ptr) { return (*ptr) = rhs; }
	
int main(int argc, char* argv[])
{
	asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	int error_code = 0;

	std::cout << "Registering string type. ";
	error_code = engine->RegisterObjectType("string",sizeof(std::string),asOBJ_CLASS_CDA);
	if (error_code < 0) std::cout << "Error: " << error_code;

	std::cout << "\nRegistering constructor. ";
	error_code = engine->RegisterObjectBehaviour("string",
		asBEHAVE_CONSTRUCT,
		"void constructor()",
		asFUNCTION(Construct),
		asCALL_CDECL_OBJLAST);	
	if (error_code < 0) std::cout << "Error: " << error_code;

	std::cout << "\nRegistering destructor. ";	
	error_code = engine->RegisterObjectBehaviour("string",
		asBEHAVE_DESTRUCT,
		"void destructor()",
		asFUNCTION(Destroy),
		asCALL_CDECL_OBJLAST);	
	if (error_code < 0) std::cout << "Error: " << error_code;

	//Returns error code -10: asINVALID_DECLARATION
	std::cout << "\nRegistering operator=. ";
	error_code = engine->RegisterObjectBehaviour("string",
		asBEHAVE_ASSIGNMENT,
		"string& op_assign(const string&)",
		asFUNCTION(Assign),
		asCALL_CDECL_OBJLAST);	
	if (error_code < 0) std::cout << "Error: " << error_code;

	std::cout << "\nRegistering copy constructor. ";
	error_code = engine->RegisterObjectBehaviour("string",
		asBEHAVE_CONSTRUCT,
		"void constructor(const string&)",
		asFUNCTION(CopyConstruct),
		asCALL_CDECL_OBJLAST);					
	if (error_code < 0) std::cout << "Error: " << error_code;

	//Returns error code -10: asINVALID_DECLARATION
	std::cout << "\nRegistering operator+=. ";	
	error_code = engine->RegisterObjectBehaviour("string",
		asBEHAVE_ADD_ASSIGN,
		"string& op_addassign(const string&)",
		asMETHODPR(std::string,operator+=,(const std::string&),std::string&),
		asCALL_THISCALL);
	if (error_code < 0) std::cout << "Error: " << error_code;
}




[Edited by - Deyja on January 5, 2006 8:26:33 PM]
Advertisement
If I add the 'in' keyword in appropriate places, it WORKS! Could I be applying the AS_ALLOW_UNSAFE_REFERENCES flag improperly? I #defined it as_config.h, as that seemed the appropriate place.
AS_ALLOW_UNSAFE_REFERENCES works as advertised for everything EXCEPT ObjectBehaviors. Free functions and global behaviors work fine. Is this intentional, and, if so, why?

Since I'm passing by const& anyway, adding the in keyword doesn't do anything anyway. :)
This has to be some left-over code that is verifying that the 'in', 'out', or 'inout' keyword is available. I'll look into this as soon as possible.

Defining the AS_ALLOW_UNSAFE_REFERENCES in as_config.h works just fine. You could also define it in the project settings or make file, whichever you use.

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

The problem is on line 1143 in as_scriptengine.cpp.

When AS_ALLOW_UNSAFE_REFERENCES is defined it shouldn't validate the inOutFlags. Change the code as such:

#ifndef AS_ALLOW_UNSAFE_REFERENCES		// Verify that the rvalue is marked as in if a reference		if( func.parameterTypes[0].IsReference() && func.inOutFlags[0] != 1 )			return ConfigError(asINVALID_DECLARATION);#endif


This should be enough to fix your problem. :)

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

I'm hoping this fixes my other problem as well. Objects I pass as parameters to script functions are being corrupted somehow, and I think it's because their copy constructors aren't being called. If it's not related, I'll have another post with the problem isolated.
AngelScript currently don't use the copy constructor. Instead it uses the default constructor and then does an assignment. Maybe this is somehow corrupting your objects?

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

Turns out it wasn't that at all. It's something else, as per the other thread I just started. I had some trouble tracking it down as Angelscript doesn't link properly in debug mode. It's missing the crt heap debug functions, and I have no idea what library they are defined it. I haven't really looked at that problem yet, though.

This topic is closed to new replies.

Advertisement