2016-02-23 16:48:30 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Core.hpp"
|
2016-02-27 10:57:10 +01:00
|
|
|
|
2016-02-23 16:48:30 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-18 11:56:48 +01:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2016-02-23 16:48:30 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <sqstdio.h>
|
|
|
|
#include <sqstdblob.h>
|
|
|
|
#include <sqstdstring.h>
|
2016-02-27 10:57:10 +01:00
|
|
|
#include <SqMod.h>
|
2016-02-23 16:48:30 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static HSQUIRRELVM GetSquirrelVM()
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return Core::Get().GetVM();
|
2016-02-23 16:48:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-17 02:50:29 +02:00
|
|
|
static SQRESULT SqModImpl_LoadScript(const SQChar * filepath, SQBool delay)
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
|
|
|
// Attempt to add the specified script to the load queue
|
2016-06-18 19:31:35 +02:00
|
|
|
if (Core::Get().LoadScript(filepath, delay))
|
2016-03-18 11:56:48 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
return SQ_OK; // The script as added or already existed
|
2016-03-18 11:56:48 +01:00
|
|
|
}
|
2016-07-17 02:24:07 +02:00
|
|
|
// The path was invalid or was unable to pool the script
|
2016-03-10 04:57:13 +01:00
|
|
|
return SQ_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-02-23 16:48:30 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-17 02:24:07 +02:00
|
|
|
static int32_t SqExport_PopulateModuleAPI(HSQMODAPI api, size_t size)
|
2016-02-23 16:48:30 +01:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
if (!api)
|
|
|
|
{
|
|
|
|
return 0; // Nothing to populate!
|
|
|
|
}
|
|
|
|
else if (size != sizeof(SQMODAPI))
|
|
|
|
{
|
|
|
|
return -1; // Incompatible API!
|
|
|
|
}
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-07-17 02:24:07 +02:00
|
|
|
//primitive functions
|
|
|
|
api->GetSquirrelVM = GetSquirrelVM;
|
2016-02-27 10:57:10 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//logging utilities
|
2016-07-17 02:24:07 +02:00
|
|
|
api->LogDbg = LogDbg;
|
|
|
|
api->LogUsr = LogUsr;
|
|
|
|
api->LogScs = LogScs;
|
|
|
|
api->LogInf = LogInf;
|
|
|
|
api->LogWrn = LogWrn;
|
|
|
|
api->LogErr = LogErr;
|
|
|
|
api->LogFtl = LogFtl;
|
|
|
|
api->LogSDbg = LogSDbg;
|
|
|
|
api->LogSUsr = LogSUsr;
|
|
|
|
api->LogSScs = LogSScs;
|
|
|
|
api->LogSInf = LogSInf;
|
|
|
|
api->LogSWrn = LogSWrn;
|
|
|
|
api->LogSErr = LogSErr;
|
|
|
|
api->LogSFtl = LogSFtl;
|
2016-02-27 10:57:10 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//script loading
|
2016-07-17 02:50:29 +02:00
|
|
|
api->LoadScript = SqModImpl_LoadScript;
|
2016-03-10 04:57:13 +01:00
|
|
|
|
2016-07-17 02:24:07 +02:00
|
|
|
return 1; // Successfully populated!
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static int32_t SqExport_PopulateSquirrelAPI(HSQLIBAPI api, size_t size)
|
|
|
|
{
|
|
|
|
if (!api)
|
|
|
|
{
|
|
|
|
return 0; // Nothing to populate!
|
|
|
|
}
|
|
|
|
else if (size != sizeof(SQLIBAPI))
|
|
|
|
{
|
|
|
|
return -1; // Incompatible API!
|
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//vm
|
2016-07-17 02:24:07 +02:00
|
|
|
api->open = sq_open;
|
|
|
|
api->newthread = sq_newthread;
|
|
|
|
api->seterrorhandler = sq_seterrorhandler;
|
|
|
|
api->close = sq_close;
|
|
|
|
api->setforeignptr = sq_setforeignptr;
|
|
|
|
api->getforeignptr = sq_getforeignptr;
|
|
|
|
api->setsharedforeignptr = sq_setsharedforeignptr;
|
|
|
|
api->getsharedforeignptr = sq_getsharedforeignptr;
|
|
|
|
api->setvmreleasehook = sq_setvmreleasehook;
|
|
|
|
api->getvmreleasehook = sq_getvmreleasehook;
|
|
|
|
api->setsharedreleasehook = sq_setsharedreleasehook;
|
|
|
|
api->getsharedreleasehook = sq_getsharedreleasehook;
|
|
|
|
api->setprintfunc = sq_setprintfunc;
|
|
|
|
api->getprintfunc = sq_getprintfunc;
|
|
|
|
api->geterrorfunc = sq_geterrorfunc;
|
|
|
|
api->suspendvm = sq_suspendvm;
|
|
|
|
api->wakeupvm = sq_wakeupvm;
|
|
|
|
api->getvmstate = sq_getvmstate;
|
|
|
|
api->getversion = sq_getversion;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//compiler
|
2016-07-17 02:24:07 +02:00
|
|
|
api->compile = sq_compile;
|
|
|
|
api->compilebuffer = sq_compilebuffer;
|
|
|
|
api->enabledebuginfo = sq_enabledebuginfo;
|
|
|
|
api->notifyallexceptions = sq_notifyallexceptions;
|
|
|
|
api->setcompilererrorhandler = sq_setcompilererrorhandler;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//stack operations
|
2016-07-17 02:24:07 +02:00
|
|
|
api->push = sq_push;
|
|
|
|
api->pop = sq_pop;
|
|
|
|
api->poptop = sq_poptop;
|
|
|
|
api->remove = sq_remove;
|
|
|
|
api->gettop = sq_gettop;
|
|
|
|
api->settop = sq_settop;
|
|
|
|
api->reservestack = sq_reservestack;
|
|
|
|
api->cmp = sq_cmp;
|
|
|
|
api->move = sq_move;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//object creation handling
|
2016-07-17 02:24:07 +02:00
|
|
|
api->newuserdata = sq_newuserdata;
|
|
|
|
api->newtable = sq_newtable;
|
|
|
|
api->newtableex = sq_newtableex;
|
|
|
|
api->newarray = sq_newarray;
|
|
|
|
api->newclosure = sq_newclosure;
|
|
|
|
api->setparamscheck = sq_setparamscheck;
|
|
|
|
api->bindenv = sq_bindenv;
|
|
|
|
api->setclosureroot = sq_setclosureroot;
|
|
|
|
api->getclosureroot = sq_getclosureroot;
|
|
|
|
api->pushstring = sq_pushstring;
|
2019-04-30 21:28:23 +02:00
|
|
|
api->pushstringf = sq_pushstringf;
|
2016-07-17 02:24:07 +02:00
|
|
|
api->pushfloat = sq_pushfloat;
|
|
|
|
api->pushinteger = sq_pushinteger;
|
|
|
|
api->pushbool = sq_pushbool;
|
|
|
|
api->pushuserpointer = sq_pushuserpointer;
|
|
|
|
api->pushnull = sq_pushnull;
|
|
|
|
api->pushthread = sq_pushthread;
|
|
|
|
api->gettype = sq_gettype;
|
|
|
|
api->typeof_ = sq_typeof;
|
|
|
|
api->getsize = sq_getsize;
|
|
|
|
api->gethash = sq_gethash;
|
|
|
|
api->getbase = sq_getbase;
|
|
|
|
api->instanceof = sq_instanceof;
|
|
|
|
api->tostring = sq_tostring;
|
|
|
|
api->tobool = sq_tobool;
|
|
|
|
api->getstringandsize = sq_getstringandsize;
|
|
|
|
api->getstring = sq_getstring;
|
|
|
|
api->getinteger = sq_getinteger;
|
|
|
|
api->getfloat = sq_getfloat;
|
|
|
|
api->getbool = sq_getbool;
|
|
|
|
api->getthread = sq_getthread;
|
|
|
|
api->getuserpointer = sq_getuserpointer;
|
|
|
|
api->getuserdata = sq_getuserdata;
|
|
|
|
api->settypetag = sq_settypetag;
|
|
|
|
api->gettypetag = sq_gettypetag;
|
|
|
|
api->setreleasehook = sq_setreleasehook;
|
|
|
|
api->getreleasehook = sq_getreleasehook;
|
|
|
|
api->getscratchpad = sq_getscratchpad;
|
|
|
|
api->getfunctioninfo = sq_getfunctioninfo;
|
|
|
|
api->getclosureinfo = sq_getclosureinfo;
|
|
|
|
api->getclosurename = sq_getclosurename;
|
|
|
|
api->setnativeclosurename = sq_setnativeclosurename;
|
2018-10-31 19:28:18 +01:00
|
|
|
api->getnativeclosurepointer = sq_getnativeclosurepointer;
|
2016-07-17 02:24:07 +02:00
|
|
|
api->setinstanceup = sq_setinstanceup;
|
|
|
|
api->getinstanceup = sq_getinstanceup;
|
|
|
|
api->setclassudsize = sq_setclassudsize;
|
|
|
|
api->newclass = sq_newclass;
|
|
|
|
api->createinstance = sq_createinstance;
|
|
|
|
api->setattributes = sq_setattributes;
|
|
|
|
api->getattributes = sq_getattributes;
|
|
|
|
api->getclass = sq_getclass;
|
|
|
|
api->weakref = sq_weakref;
|
|
|
|
api->getdefaultdelegate = sq_getdefaultdelegate;
|
|
|
|
api->getmemberhandle = sq_getmemberhandle;
|
|
|
|
api->getbyhandle = sq_getbyhandle;
|
|
|
|
api->setbyhandle = sq_setbyhandle;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//object manipulation
|
2016-07-17 02:24:07 +02:00
|
|
|
api->pushroottable = sq_pushroottable;
|
|
|
|
api->pushregistrytable = sq_pushregistrytable;
|
|
|
|
api->pushconsttable = sq_pushconsttable;
|
|
|
|
api->setroottable = sq_setroottable;
|
|
|
|
api->setconsttable = sq_setconsttable;
|
|
|
|
api->newslot = sq_newslot;
|
|
|
|
api->deleteslot = sq_deleteslot;
|
|
|
|
api->set = sq_set;
|
|
|
|
api->get = sq_get;
|
|
|
|
api->rawget = sq_rawget;
|
|
|
|
api->rawset = sq_rawset;
|
|
|
|
api->rawdeleteslot = sq_rawdeleteslot;
|
|
|
|
api->newmember = sq_newmember;
|
|
|
|
api->rawnewmember = sq_rawnewmember;
|
|
|
|
api->arrayappend = sq_arrayappend;
|
|
|
|
api->arraypop = sq_arraypop;
|
|
|
|
api->arrayresize = sq_arrayresize;
|
|
|
|
api->arrayreverse = sq_arrayreverse;
|
|
|
|
api->arrayremove = sq_arrayremove;
|
|
|
|
api->arrayinsert = sq_arrayinsert;
|
|
|
|
api->setdelegate = sq_setdelegate;
|
|
|
|
api->getdelegate = sq_getdelegate;
|
|
|
|
api->clone = sq_clone;
|
|
|
|
api->setfreevariable = sq_setfreevariable;
|
|
|
|
api->next = sq_next;
|
|
|
|
api->getweakrefval = sq_getweakrefval;
|
|
|
|
api->clear = sq_clear;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//calls
|
2016-07-17 02:24:07 +02:00
|
|
|
api->call = sq_call;
|
|
|
|
api->resume = sq_resume;
|
|
|
|
api->getlocal = sq_getlocal;
|
|
|
|
api->getcallee = sq_getcallee;
|
|
|
|
api->getfreevariable = sq_getfreevariable;
|
2018-10-31 19:28:18 +01:00
|
|
|
api->getonefreevariable = sq_getonefreevariable;
|
2016-07-17 02:24:07 +02:00
|
|
|
api->throwerror = sq_throwerror;
|
2019-04-30 21:28:23 +02:00
|
|
|
api->throwerrorf = sq_throwerrorf;
|
2016-07-17 02:24:07 +02:00
|
|
|
api->throwobject = sq_throwobject;
|
|
|
|
api->reseterror = sq_reseterror;
|
|
|
|
api->getlasterror = sq_getlasterror;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//raw object handling
|
2016-07-17 02:24:07 +02:00
|
|
|
api->getstackobj = sq_getstackobj;
|
|
|
|
api->pushobject = sq_pushobject;
|
|
|
|
api->addref = sq_addref;
|
|
|
|
api->release = sq_release;
|
|
|
|
api->getrefcount = sq_getrefcount;
|
|
|
|
api->resetobject = sq_resetobject;
|
|
|
|
api->objtostring = sq_objtostring;
|
|
|
|
api->objtobool = sq_objtobool;
|
|
|
|
api->objtointeger = sq_objtointeger;
|
|
|
|
api->objtofloat = sq_objtofloat;
|
|
|
|
api->objtouserpointer = sq_objtouserpointer;
|
|
|
|
api->getobjtypetag = sq_getobjtypetag;
|
|
|
|
api->getvmrefcount = sq_getvmrefcount;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//GC
|
2016-07-17 02:24:07 +02:00
|
|
|
api->collectgarbage = sq_collectgarbage;
|
|
|
|
api->resurrectunreachable = sq_resurrectunreachable;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//serialization
|
2016-07-17 02:24:07 +02:00
|
|
|
api->writeclosure = sq_writeclosure;
|
|
|
|
api->readclosure = sq_readclosure;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//mem allocation
|
2016-07-17 02:24:07 +02:00
|
|
|
api->malloc = sq_malloc;
|
|
|
|
api->realloc = sq_realloc;
|
|
|
|
api->free = sq_free;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//debug
|
2016-07-17 02:24:07 +02:00
|
|
|
api->stackinfos = sq_stackinfos;
|
|
|
|
api->setdebughook = sq_setdebughook;
|
|
|
|
api->setnativedebughook = sq_setnativedebughook;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//compiler helpers
|
2016-07-17 02:24:07 +02:00
|
|
|
api->loadfile = sqstd_loadfile;
|
|
|
|
api->dofile = sqstd_dofile;
|
|
|
|
api->writeclosuretofile = sqstd_writeclosuretofile;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//blob
|
2016-07-17 02:24:07 +02:00
|
|
|
api->createblob = sqstd_createblob;
|
|
|
|
api->getblob = sqstd_getblob;
|
|
|
|
api->getblobsize = sqstd_getblobsize;
|
2016-02-23 16:48:30 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
//string
|
2016-07-17 02:24:07 +02:00
|
|
|
api->format = sqstd_format;
|
|
|
|
|
|
|
|
return 1; // Successfully populated!
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static const SQMODEXPORTS g_SqModExports{
|
|
|
|
sizeof(SQMODEXPORTS),
|
|
|
|
SqExport_PopulateModuleAPI,
|
|
|
|
SqExport_PopulateSquirrelAPI
|
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void InitExports()
|
|
|
|
{
|
|
|
|
// The server needs a pointer to a pointer, and a persistent one
|
|
|
|
static const SQMODEXPORTS * sqmodexports = &g_SqModExports;
|
|
|
|
|
|
|
|
// Tell the server about the pointer to the exports structure
|
|
|
|
_Func->ExportFunctions(_Info->pluginId, reinterpret_cast< const void ** >(&sqmodexports),
|
|
|
|
sizeof(HSQMODEXPORTS));
|
2016-02-23 16:48:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|