I'm trying to make wz API export to jscript more straightforward using some Template trickery ; basically I'm providing a "wrap_(f, engine, context);" function that takes a C function whose argument are DROID*, STRUCTURE* (actually object holding id and player since it's what is used internally), int ... and calls it using argument found in QtScript engine and context, wrapping the result value in a QScriptValue.
For instance :
Code: Select all
static QScriptValue js_activateStructure(QScriptContext *context, QScriptEngine *)
{
QScriptValue structVal = context->argument(0);
int id = structVal.property("id").toInt32();
int player = structVal.property("player").toInt32();
STRUCTURE *psStruct = IdToStruct(id, player);
SCRIPT_ASSERT(context, psStruct, "No such structure id %d belonging to player %d", id, player);
// ... and then do nothing with psStruct yet
QScriptValue objVal = context->argument(1);
int oid = objVal.property("id").toInt32();
int oplayer = objVal.property("player").toInt32();
OBJECT_TYPE otype = (OBJECT_TYPE)objVal.property("type").toInt32();
BASE_OBJECT *psObj = IdToObject(otype, oid, oplayer);
SCRIPT_ASSERT(context, psObj, "No such object id %d belonging to player %d", oid, oplayer);
orderStructureObj(player, psObj);
return QScriptValue(true);
}
Code: Select all
static bool activateStructure(structure_id_player structVal, object_id_player_type objVal)
{
STRUCTURE *psStruct = IdToStruct(structVal.id, structVal.player);
BASE_OBJECT *psObj = IdToObject(objVal.type, objVal.id, objVal.player);
orderStructureObj(structVal.player, psObj);
return true;
}
static QScriptValue js_activateStructure(QScriptContext *context, QScriptEngine *engine)
{
return wrap_(activateStructure, context, engine);
}
There are however some drawbacks : for the wrapper to work the API function have some restriction : they must not be overloaded (the compiler has no idea which overloads to pick), they must not use jscript global variable (the mechanism is based on the function signature) and they must not rely on jscript engine to display message.
This means some jscript function must be modified to comply with these requirements : overloads must be renamed, global variable must be passed as argument to the functions and error message won't be jscript specific (ie they will be printed in the wz log not in jscript console). On the other hand the API should be easier to maintain and expand.
Are such modifications acceptable ? I think API breakage may require changing version number. On the other hand it may be incremental : compatible functions can be ported to this system and new can be introduced while the non compatible one may be marked as deprecated.