2012-03-19 10:12:27 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Internal.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "MemAccess.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "VersionInfo.h"
|
|
|
|
#include "tinythread.h"
|
|
|
|
// must be last due to MS stupidity
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "DataIdentity.h"
|
|
|
|
|
|
|
|
#include "MiscUtils.h"
|
|
|
|
|
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
static luaL_Reg no_functions[] = { { NULL, NULL } };
|
|
|
|
|
|
|
|
inline void lua_dup(lua_State *state) { lua_pushvalue(state, -1); }
|
|
|
|
inline void lua_swap(lua_State *state) { lua_insert(state, -2); }
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Registry name: hash of type metatables <-> type identities.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
#define DFHACK_TYPETABLE_NAME "DFHack::DFTypes"
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Registry name: hash of type identity -> node in df.etc...
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
#define DFHACK_TYPEID_TABLE_NAME "DFHack::DFTypeIds"
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Registry name: hash of enum/bitfield identity -> index lookup table
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
#define DFHACK_ENUM_TABLE_NAME "DFHack::DFEnums"
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
// Function registry names
|
2012-03-20 11:34:27 -06:00
|
|
|
#define DFHACK_CHANGEERROR_NAME "DFHack::ChangeError"
|
|
|
|
#define DFHACK_COMPARE_NAME "DFHack::ComparePtrs"
|
|
|
|
#define DFHACK_TYPE_TOSTRING_NAME "DFHack::TypeToString"
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Upvalue: contents of DFHACK_TYPETABLE_NAME
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
#define UPVAL_TYPETABLE lua_upvalueindex(1)
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Expected metatable of the current object.
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
#define UPVAL_METATABLE lua_upvalueindex(2)
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Table mapping field names to indices or data structure pointers.
|
|
|
|
* Enum index table is linked into here via getmetatable($).__index.
|
|
|
|
* Fields that are actually in UPVAL_METATABLE are marked with NULL light udata.
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
#define UPVAL_FIELDTABLE lua_upvalueindex(3)
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Only for containers: light udata with container identity.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
#define UPVAL_CONTAINER_ID lua_upvalueindex(4)
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only for containers: light udata with item identity.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
#define UPVAL_ITEM_ID lua_upvalueindex(5)
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only for containers: if not nil, overrides the item count.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
#define UPVAL_ITEM_COUNT lua_upvalueindex(6)
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
namespace {
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Object references are represented as userdata instances
|
|
|
|
* with an appropriate metatable; the payload of userdata is
|
|
|
|
* this structure:
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
struct DFRefHeader {
|
|
|
|
void *ptr;
|
|
|
|
};
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* The system might be extended to carry some simple
|
|
|
|
* objects inline inside the reference buffer.
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
inline bool is_self_contained(DFRefHeader *ptr) {
|
|
|
|
void **pp = &ptr->ptr;
|
|
|
|
return **(void****)pp == (pp + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Report an error while accessing a field (index = field name).
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static void field_error(lua_State *state, int index, const char *err, const char *mode)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
|
|
|
lua_getfield(state, UPVAL_METATABLE, "__metatable");
|
|
|
|
const char *cname = lua_tostring(state, -1);
|
|
|
|
const char *fname = lua_tostring(state, index);
|
|
|
|
luaL_error(state, "Cannot %s field %s.%s: %s.",
|
|
|
|
mode, (cname ? cname : "?"), (fname ? fname : "?"), err);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* If is_method is true, these use UPVAL_TYPETABLE to save a hash lookup.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static void push_object_internal(lua_State *state, type_identity *type, void *ptr, bool in_method = true);
|
2012-03-20 03:56:29 -06:00
|
|
|
static void *get_object_internal(lua_State *state, type_identity *type, int val_index, bool in_method = true);
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
void DFHack::PushDFObject(lua_State *state, type_identity *type, void *ptr)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
push_object_internal(state, type, ptr, false);
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void *DFHack::GetDFObject(lua_State *state, type_identity *type, int val_index)
|
|
|
|
{
|
|
|
|
return get_object_internal(state, type, val_index, false);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**************************************
|
|
|
|
* Identity object read/write methods *
|
|
|
|
**************************************/
|
2012-03-20 03:56:29 -06:00
|
|
|
|
|
|
|
int constructed_identity::lua_read(lua_State *state, int fname_idx, void *ptr)
|
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
push_object_internal(state, this, ptr);
|
|
|
|
return 1;
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void constructed_identity::lua_write(lua_State *state, int fname_idx, void *ptr, int val_index)
|
|
|
|
{
|
|
|
|
field_error(state, fname_idx, "complex object", "write");
|
|
|
|
}
|
|
|
|
|
|
|
|
int enum_identity::lua_read(lua_State *state, int fname_idx, void *ptr)
|
|
|
|
{
|
|
|
|
return base_type->lua_read(state, fname_idx, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void enum_identity::lua_write(lua_State *state, int fname_idx, void *ptr, int val_index)
|
|
|
|
{
|
|
|
|
base_type->lua_write(state, fname_idx, ptr, val_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int df::number_identity_base::lua_read(lua_State *state, int fname_idx, void *ptr)
|
|
|
|
{
|
|
|
|
lua_pushnumber(state, read(ptr));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void df::number_identity_base::lua_write(lua_State *state, int fname_idx, void *ptr, int val_index)
|
|
|
|
{
|
|
|
|
if (!lua_isnumber(state, val_index))
|
|
|
|
field_error(state, fname_idx, "number expected", "write");
|
|
|
|
|
|
|
|
write(ptr, lua_tonumber(state, val_index));
|
|
|
|
}
|
|
|
|
|
|
|
|
int df::bool_identity::lua_read(lua_State *state, int fname_idx, void *ptr)
|
|
|
|
{
|
|
|
|
lua_pushboolean(state, *(bool*)ptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void df::bool_identity::lua_write(lua_State *state, int fname_idx, void *ptr, int val_index)
|
|
|
|
{
|
|
|
|
char *pb = (char*)ptr;
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
if (lua_isboolean(state, val_index) || lua_isnil(state, val_index))
|
2012-03-20 03:56:29 -06:00
|
|
|
*pb = lua_toboolean(state, val_index);
|
|
|
|
else if (lua_isnumber(state, val_index))
|
2012-03-21 03:26:53 -06:00
|
|
|
*pb = lua_tointeger(state, val_index);
|
2012-03-20 03:56:29 -06:00
|
|
|
else
|
|
|
|
field_error(state, fname_idx, "boolean or number expected", "write");
|
|
|
|
}
|
|
|
|
|
|
|
|
int df::stl_string_identity::lua_read(lua_State *state, int fname_idx, void *ptr)
|
|
|
|
{
|
|
|
|
auto pstr = (std::string*)ptr;
|
|
|
|
lua_pushlstring(state, pstr->data(), pstr->size());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void df::stl_string_identity::lua_write(lua_State *state, int fname_idx, void *ptr, int val_index)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
const char *bytes = lua_tolstring(state, val_index, &size);
|
|
|
|
if (!bytes)
|
|
|
|
field_error(state, fname_idx, "string expected", "write");
|
|
|
|
|
|
|
|
*(std::string*)ptr = std::string(bytes, size);
|
|
|
|
}
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
int df::pointer_identity::lua_read(lua_State *state, int fname_idx, void *ptr, type_identity *target)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
push_object_internal(state, target, *(void**)ptr);
|
|
|
|
return 1;
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int df::pointer_identity::lua_read(lua_State *state, int fname_idx, void *ptr)
|
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
return lua_read(state, fname_idx, ptr, target);
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
void df::pointer_identity::lua_write(lua_State *state, int fname_idx, void *ptr,
|
|
|
|
type_identity *target, int val_index)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
|
|
|
auto pptr = (void**)ptr;
|
|
|
|
|
|
|
|
if (lua_isnil(state, val_index))
|
|
|
|
*pptr = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
void *nval = get_object_internal(state, target, val_index);
|
|
|
|
if (nval)
|
|
|
|
*pptr = nval;
|
|
|
|
else
|
|
|
|
field_error(state, fname_idx, "incompatible pointer type", "write");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void df::pointer_identity::lua_write(lua_State *state, int fname_idx, void *ptr, int val_index)
|
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_write(state, fname_idx, ptr, target, val_index);
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
int container_identity::lua_item_count(lua_State *state, void *ptr)
|
|
|
|
{
|
|
|
|
if (lua_isnumber(state, UPVAL_ITEM_COUNT))
|
|
|
|
return lua_tointeger(state, UPVAL_ITEM_COUNT);
|
|
|
|
else
|
|
|
|
return item_count(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int container_identity::lua_item_read(lua_State *state, int fname_idx, void *ptr, int idx)
|
|
|
|
{
|
|
|
|
auto id = (type_identity*)lua_touserdata(state, UPVAL_ITEM_ID);
|
2012-03-22 00:56:32 -06:00
|
|
|
void *pitem = item_pointer(id, ptr, idx);
|
2012-03-21 03:26:53 -06:00
|
|
|
return id->lua_read(state, fname_idx, pitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void container_identity::lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index)
|
|
|
|
{
|
|
|
|
auto id = (type_identity*)lua_touserdata(state, UPVAL_ITEM_ID);
|
2012-03-22 00:56:32 -06:00
|
|
|
void *pitem = item_pointer(id, ptr, idx);
|
2012-03-21 03:26:53 -06:00
|
|
|
id->lua_write(state, fname_idx, pitem, val_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ptr_container_identity::lua_item_read(lua_State *state, int fname_idx, void *ptr, int idx)
|
|
|
|
{
|
|
|
|
auto id = (type_identity*)lua_touserdata(state, UPVAL_ITEM_ID);
|
2012-03-22 00:56:32 -06:00
|
|
|
void *pitem = item_pointer(&df::identity_traits<void*>::identity, ptr, idx);
|
2012-03-21 03:26:53 -06:00
|
|
|
return df::pointer_identity::lua_read(state, fname_idx, pitem, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ptr_container_identity::lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index)
|
|
|
|
{
|
|
|
|
auto id = (type_identity*)lua_touserdata(state, UPVAL_ITEM_ID);
|
2012-03-22 00:56:32 -06:00
|
|
|
void *pitem = item_pointer(&df::identity_traits<void*>::identity, ptr, idx);
|
2012-03-21 03:26:53 -06:00
|
|
|
df::pointer_identity::lua_write(state, fname_idx, pitem, id, val_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bit_container_identity::lua_item_read(lua_State *state, int fname_idx, void *ptr, int idx)
|
|
|
|
{
|
|
|
|
lua_pushboolean(state, get_item(ptr, idx));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bit_container_identity::lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index)
|
|
|
|
{
|
|
|
|
if (lua_isboolean(state, val_index) || lua_isnil(state, val_index))
|
|
|
|
set_item(ptr, idx, lua_toboolean(state, val_index));
|
|
|
|
else if (lua_isnumber(state, val_index))
|
|
|
|
set_item(ptr, idx, lua_tointeger(state, val_index) != 0);
|
|
|
|
else
|
|
|
|
field_error(state, fname_idx, "boolean or number expected", "write");
|
|
|
|
}
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
/* */
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
static int change_error(lua_State *state)
|
|
|
|
{
|
|
|
|
luaL_error(state, "Attempt to change a read-only table.\n");
|
2012-03-22 00:56:32 -06:00
|
|
|
return 0;
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Wrap a table so that it can't be modified.
|
|
|
|
*/
|
2012-03-19 10:12:27 -06:00
|
|
|
static void freeze_table(lua_State *state, bool leave_metatable = false, const char *name = NULL)
|
|
|
|
{
|
|
|
|
// rv = {}; setmetatable(rv, { __index = in, __newindex = change_error, __metatable = name })
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
lua_newtable(state);
|
|
|
|
lua_swap(state);
|
|
|
|
lua_setfield(state, base, "__index");
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_CHANGEERROR_NAME);
|
2012-03-19 10:12:27 -06:00
|
|
|
lua_setfield(state, base, "__newindex");
|
|
|
|
lua_newtable(state);
|
|
|
|
lua_swap(state);
|
|
|
|
lua_dup(state);
|
|
|
|
lua_setmetatable(state, base);
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
lua_pushstring(state, name);
|
|
|
|
lua_setfield(state, -2, "__metatable");
|
|
|
|
}
|
|
|
|
// result: [frozen table] [metatable]
|
|
|
|
if (!leave_metatable)
|
|
|
|
lua_pop(state, 1);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Look up the key on the stack in DFHACK_TYPETABLE;
|
|
|
|
* if found, put result on the stack and return true.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static bool LookupTypeInfo(lua_State *state, bool in_method)
|
|
|
|
{
|
|
|
|
// stack: [lookup key]
|
|
|
|
|
|
|
|
if (in_method)
|
|
|
|
{
|
|
|
|
lua_rawget(state, UPVAL_TYPETABLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_TYPETABLE_NAME);
|
|
|
|
lua_swap(state);
|
|
|
|
lua_rawget(state, -2);
|
|
|
|
lua_remove(state, -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// stack: [info]
|
|
|
|
|
|
|
|
if (lua_isnil(state, -1))
|
|
|
|
{
|
|
|
|
lua_pop(state, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
static void LookupInTable(lua_State *state, void *id, const char *tname)
|
|
|
|
{
|
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, tname);
|
|
|
|
lua_pushlightuserdata(state, id);
|
|
|
|
lua_rawget(state, -2);
|
|
|
|
lua_remove(state, -2);
|
|
|
|
}
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
static void SaveInTable(lua_State *state, void *node, const char *tname)
|
2012-03-19 10:12:27 -06:00
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
// stack: [info]
|
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, tname);
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
lua_pushlightuserdata(state, node);
|
|
|
|
lua_pushvalue(state, -3);
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_rawset(state, -3);
|
|
|
|
|
|
|
|
lua_pushvalue(state, -2);
|
|
|
|
lua_pushlightuserdata(state, node);
|
|
|
|
lua_rawset(state, -3);
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
lua_pop(state, 1);
|
2012-03-20 11:34:27 -06:00
|
|
|
// stack: [info]
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
static void SaveTypeInfo(lua_State *state, void *node)
|
2012-03-19 10:12:27 -06:00
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
SaveInTable(state, node, DFHACK_TYPETABLE_NAME);
|
|
|
|
}
|
2012-03-19 10:12:27 -06:00
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
static void BuildTypeMetatable(lua_State *state, type_identity *type);
|
2012-03-19 10:12:27 -06:00
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Push the pointer as DF object ref using metatable on the stack.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static void push_object_ref(lua_State *state, void *ptr)
|
|
|
|
{
|
|
|
|
// stack: [metatable]
|
|
|
|
auto ref = (DFRefHeader*)lua_newuserdata(state, sizeof(DFRefHeader));
|
|
|
|
ref->ptr = ptr;
|
2012-03-19 10:12:27 -06:00
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_swap(state);
|
|
|
|
lua_setmetatable(state, -2);
|
|
|
|
// stack: [userdata]
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Push the pointer using given identity.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static void push_object_internal(lua_State *state, type_identity *type, void *ptr, bool in_method)
|
|
|
|
{
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* If NULL pointer or no type, push something simple
|
|
|
|
*/
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
if (!ptr || !type)
|
2012-03-19 10:12:27 -06:00
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
if (!ptr)
|
|
|
|
lua_pushnil(state);
|
|
|
|
else
|
|
|
|
lua_pushlightuserdata(state, ptr);
|
2012-03-19 10:12:27 -06:00
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
return;
|
|
|
|
}
|
2012-03-19 10:12:27 -06:00
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Resolve actual class using vtable
|
|
|
|
*/
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
if (type->type() == IDTYPE_CLASS)
|
|
|
|
{
|
|
|
|
virtual_identity *class_vid = virtual_identity::get(virtual_ptr(ptr));
|
|
|
|
if (class_vid)
|
|
|
|
type = class_vid;
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Resolve metatable by identity, and push the object
|
|
|
|
*/
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_pushlightuserdata(state, type); // () -> type
|
|
|
|
|
|
|
|
if (!LookupTypeInfo(state, in_method)) // type -> metatable?
|
|
|
|
BuildTypeMetatable(state, type); // () -> metatable
|
|
|
|
|
|
|
|
push_object_ref(state, ptr); // metatable -> userdata
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Verify that the value matches the identity, and return ptr if so.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static void *get_object_internal(lua_State *state, type_identity *type, int val_index, bool in_method)
|
|
|
|
{
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Non-userdata results in NULL; nil for NULL gets handled here too.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
if (!lua_isuserdata(state, val_index))
|
|
|
|
return NULL;
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Light user data is allowed with null type; otherwise bail out.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
if (!lua_getmetatable(state, val_index)) // () -> metatable?
|
|
|
|
{
|
|
|
|
if (!type && lua_islightuserdata(state, val_index))
|
|
|
|
return lua_touserdata(state, val_index);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Verify that the metatable is known, and refers to the correct type.
|
|
|
|
* Here doing reverse lookup of identity by metatable.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
if (!LookupTypeInfo(state, in_method)) // metatable -> type?
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bool ok = lua_islightuserdata(state, -1) &&
|
|
|
|
(!type || lua_touserdata(state, -1) == type);
|
|
|
|
|
|
|
|
lua_pop(state, 1); // type -> ()
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Finally decode the reference.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
auto ref = (DFRefHeader*)lua_touserdata(state, val_index);
|
|
|
|
return ref->ptr;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: compare two DF object references.
|
|
|
|
*
|
|
|
|
* Equal if same pointer and same metatable.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static int meta_ptr_compare(lua_State *state)
|
|
|
|
{
|
|
|
|
if (!lua_isuserdata(state, 1) || !lua_isuserdata(state, 2) ||
|
|
|
|
!lua_getmetatable(state, 1) || !lua_getmetatable(state, 2))
|
|
|
|
{
|
|
|
|
lua_pushboolean(state, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lua_equal(state, -1, -2))
|
|
|
|
{
|
|
|
|
// todo: nonidentical type comparison
|
|
|
|
lua_pushboolean(state, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ref1 = (DFRefHeader*)lua_touserdata(state, 1);
|
|
|
|
auto ref2 = (DFRefHeader*)lua_touserdata(state, 2);
|
|
|
|
lua_pushboolean(state, ref1->ptr == ref2->ptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Resolve the field name in UPVAL_FIELDTABLE, die if not found.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void lookup_field(lua_State *state, int index, const char *mode)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
|
|
|
lua_pushvalue(state, index);
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_gettable(state, UPVAL_FIELDTABLE); // uses metatable with enum keys
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
if (lua_isnil(state, -1))
|
2012-03-20 11:34:27 -06:00
|
|
|
field_error(state, index, "not found", mode);
|
2012-03-21 03:26:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *find_field(lua_State *state, int index, const char *mode)
|
|
|
|
{
|
|
|
|
lookup_field(state, index, mode);
|
2012-03-20 03:56:29 -06:00
|
|
|
|
|
|
|
void *p = lua_touserdata(state, -1);
|
|
|
|
lua_pop(state, 1);
|
2012-03-21 03:26:53 -06:00
|
|
|
return p;
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Verify that the object is a DF ref with UPVAL_METATABLE.
|
|
|
|
* If everything ok, extract the address.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static uint8_t *get_object_addr(lua_State *state, int obj, int field, const char *mode)
|
|
|
|
{
|
|
|
|
if (!lua_isuserdata(state, obj) ||
|
|
|
|
!lua_getmetatable(state, obj))
|
|
|
|
field_error(state, field, "invalid object", mode);
|
|
|
|
|
|
|
|
if (!lua_equal(state, -1, UPVAL_METATABLE))
|
|
|
|
field_error(state, field, "invalid object metatable", mode);
|
|
|
|
|
|
|
|
lua_pop(state, 1);
|
|
|
|
|
|
|
|
auto ref = (DFRefHeader*)lua_touserdata(state, obj);
|
|
|
|
return (uint8_t*)ref->ptr;
|
|
|
|
}
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
static void GetAdHocMetatable(lua_State *state, const struct_field_info *field);
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
static int read_field(lua_State *state, const struct_field_info *field, void *ptr)
|
|
|
|
{
|
|
|
|
switch (field->mode)
|
|
|
|
{
|
|
|
|
case struct_field_info::STATIC_STRING:
|
|
|
|
{
|
|
|
|
int len = strnlen((char*)ptr, field->count);
|
|
|
|
lua_pushlstring(state, (char*)ptr, len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case struct_field_info::PRIMITIVE:
|
|
|
|
case struct_field_info::SUBSTRUCT:
|
|
|
|
return field->type->lua_read(state, 2, ptr);
|
|
|
|
|
|
|
|
case struct_field_info::POINTER:
|
2012-03-20 11:34:27 -06:00
|
|
|
return df::pointer_identity::lua_read(state, 2, ptr, field->type);
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
case struct_field_info::CONTAINER:
|
|
|
|
if (!field->eid || !field->type->isContainer() ||
|
|
|
|
field->eid == ((container_identity*)field->type)->getIndexEnumType())
|
|
|
|
return field->type->lua_read(state, 2, ptr);
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
case struct_field_info::STATIC_ARRAY:
|
|
|
|
case struct_field_info::STL_VECTOR_PTR:
|
2012-03-21 03:26:53 -06:00
|
|
|
GetAdHocMetatable(state, field);
|
|
|
|
push_object_ref(state, ptr);
|
|
|
|
return 1;
|
2012-03-20 03:56:29 -06:00
|
|
|
|
|
|
|
case struct_field_info::END:
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-22 00:56:32 -06:00
|
|
|
|
|
|
|
return 0;
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
static void write_field(lua_State *state, const struct_field_info *field, void *ptr, int value_idx)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
|
|
|
switch (field->mode)
|
|
|
|
{
|
|
|
|
case struct_field_info::STATIC_STRING:
|
|
|
|
{
|
|
|
|
size_t size;
|
2012-03-20 11:34:27 -06:00
|
|
|
const char *str = lua_tolstring(state, value_idx, &size);
|
2012-03-20 03:56:29 -06:00
|
|
|
if (!str)
|
|
|
|
field_error(state, 2, "string expected", "write");
|
|
|
|
memcpy(ptr, str, std::min(size+1, size_t(field->count)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case struct_field_info::PRIMITIVE:
|
|
|
|
case struct_field_info::SUBSTRUCT:
|
|
|
|
case struct_field_info::CONTAINER:
|
2012-03-20 11:34:27 -06:00
|
|
|
field->type->lua_write(state, 2, ptr, value_idx);
|
2012-03-20 03:56:29 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
case struct_field_info::POINTER:
|
2012-03-20 11:34:27 -06:00
|
|
|
df::pointer_identity::lua_write(state, 2, ptr, field->type, value_idx);
|
2012-03-20 03:56:29 -06:00
|
|
|
|
|
|
|
case struct_field_info::STATIC_ARRAY:
|
|
|
|
case struct_field_info::STL_VECTOR_PTR:
|
|
|
|
field_error(state, 2, "complex object", "write");
|
|
|
|
|
|
|
|
case struct_field_info::END:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: represent a type node as string.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static int meta_type_tostring(lua_State *state)
|
|
|
|
{
|
|
|
|
if (!lua_getmetatable(state, 1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lua_getfield(state, -1, "__metatable");
|
|
|
|
const char *cname = lua_tostring(state, -1);
|
|
|
|
|
|
|
|
lua_pushstring(state, stl_sprintf("<type: %s>", cname).c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: represent a DF object reference as string.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static int meta_ptr_tostring(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 0, "access");
|
|
|
|
|
|
|
|
lua_getfield(state, UPVAL_METATABLE, "__metatable");
|
|
|
|
const char *cname = lua_tostring(state, -1);
|
|
|
|
|
|
|
|
lua_pushstring(state, stl_sprintf("<%s: 0x%08x>", cname, (unsigned)ptr).c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
// Resolve the field in the metatable and return
|
2012-03-20 11:34:27 -06:00
|
|
|
static int get_metafield(lua_State *state)
|
|
|
|
{
|
|
|
|
lua_rawget(state, UPVAL_METATABLE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __index for structures.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static int meta_struct_index(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "read");
|
2012-03-21 03:26:53 -06:00
|
|
|
auto field = (struct_field_info*)find_field(state, 2, "read");
|
2012-03-20 11:34:27 -06:00
|
|
|
if (!field)
|
|
|
|
return get_metafield(state);
|
|
|
|
return read_field(state, field, ptr + field->offset);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __newindex for structures.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static int meta_struct_newindex(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "write");
|
2012-03-21 03:26:53 -06:00
|
|
|
auto field = (struct_field_info*)find_field(state, 2, "write");
|
2012-03-20 11:34:27 -06:00
|
|
|
write_field(state, field, ptr + field->offset, 3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __index for primitives, i.e. simple object references.
|
|
|
|
* Fields point to identity, or NULL for metafields.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static int meta_primitive_index(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "read");
|
|
|
|
auto type = (type_identity*)find_field(state, 2, "read");
|
|
|
|
if (!type)
|
|
|
|
return get_metafield(state);
|
|
|
|
return type->lua_read(state, 2, ptr);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __newindex for primitives.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static int meta_primitive_newindex(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "write");
|
|
|
|
auto type = (type_identity*)find_field(state, 2, "write");
|
|
|
|
type->lua_write(state, 2, ptr, 3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __len for containers.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static int meta_container_len(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 0, "get length");
|
|
|
|
auto id = (container_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID);
|
|
|
|
int len = id->lua_item_count(state, ptr);
|
|
|
|
lua_pushinteger(state, len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Field lookup for containers:
|
|
|
|
*
|
|
|
|
* - Numbers are indices and handled directly.
|
|
|
|
* - NULL userdata are metafields; push and exit;
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static int lookup_container_field(lua_State *state, int field, const char *mode)
|
|
|
|
{
|
|
|
|
if (lua_type(state, field) == LUA_TNUMBER)
|
|
|
|
return field;
|
|
|
|
|
|
|
|
lookup_field(state, field, mode);
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
if (lua_isuserdata(state, -1) && !lua_touserdata(state, -1))
|
|
|
|
{
|
|
|
|
lua_pop(state, 1);
|
|
|
|
get_metafield(state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Index verification: number and in range.
|
|
|
|
*/
|
|
|
|
static int check_container_index(lua_State *state, int len,
|
2012-03-21 03:26:53 -06:00
|
|
|
int fidx, int iidx, const char *mode)
|
|
|
|
{
|
|
|
|
if (!lua_isnumber(state, iidx))
|
|
|
|
field_error(state, fidx, "invalid index", mode);
|
|
|
|
|
|
|
|
int idx = lua_tointeger(state, iidx);
|
|
|
|
if (idx < 0 || idx >= len)
|
|
|
|
field_error(state, fidx, "index out of bounds", mode);
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __index for containers.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static int meta_container_index(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "read");
|
|
|
|
int iidx = lookup_container_field(state, 2, "read");
|
2012-03-21 10:04:37 -06:00
|
|
|
if (!iidx)
|
|
|
|
return 1;
|
2012-03-21 03:26:53 -06:00
|
|
|
|
|
|
|
auto id = (container_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID);
|
2012-03-21 10:04:37 -06:00
|
|
|
int len = id->lua_item_count(state, ptr);
|
|
|
|
int idx = check_container_index(state, len, 2, iidx, "read");
|
2012-03-21 03:26:53 -06:00
|
|
|
return id->lua_item_read(state, 2, ptr, idx);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __index for containers.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static int meta_container_newindex(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "write");
|
|
|
|
int iidx = lookup_container_field(state, 2, "write");
|
2012-03-21 10:04:37 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
auto id = (container_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID);
|
2012-03-21 10:04:37 -06:00
|
|
|
int len = id->lua_item_count(state, ptr);
|
|
|
|
int idx = check_container_index(state, len, 2, iidx, "write");
|
2012-03-21 03:26:53 -06:00
|
|
|
id->lua_item_write(state, 2, ptr, idx, 3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __len for bitfields.
|
|
|
|
*/
|
|
|
|
static int meta_bitfield_len(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 0, "get size");
|
|
|
|
auto id = (bitfield_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID);
|
|
|
|
lua_pushinteger(state, id->getNumBits());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metamethod: __index for bitfields.
|
|
|
|
*/
|
|
|
|
static int meta_bitfield_index(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "read");
|
|
|
|
int iidx = lookup_container_field(state, 2, "read");
|
|
|
|
if (!iidx)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
auto id = (bitfield_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID);
|
|
|
|
|
|
|
|
// whole
|
|
|
|
if (lua_isuserdata(state, iidx) && lua_touserdata(state, iidx) == id)
|
|
|
|
{
|
|
|
|
lua_Integer intv = 0;
|
|
|
|
memcpy(&intv, ptr, std::min(sizeof(intv), size_t(id->byte_size())));
|
|
|
|
lua_pushinteger(state, intv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int idx = check_container_index(state, id->getNumBits(), 2, iidx, "read");
|
|
|
|
int size = id->getBits()[idx].size;
|
|
|
|
|
|
|
|
int value = getBitfieldField(ptr, idx, size);
|
|
|
|
if (size <= 1)
|
|
|
|
lua_pushboolean(state, value != 0);
|
|
|
|
else
|
|
|
|
lua_pushinteger(state, value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metamethod: __newindex for bitfields.
|
|
|
|
*/
|
|
|
|
static int meta_bitfield_newindex(lua_State *state)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = get_object_addr(state, 1, 2, "write");
|
|
|
|
int iidx = lookup_container_field(state, 2, "write");
|
|
|
|
|
|
|
|
auto id = (bitfield_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID);
|
|
|
|
|
|
|
|
// whole
|
|
|
|
if (lua_isuserdata(state, iidx) && lua_touserdata(state, iidx) == id)
|
|
|
|
{
|
|
|
|
if (!lua_isnumber(state, 3))
|
|
|
|
field_error(state, 2, "number expected", "write");
|
|
|
|
|
|
|
|
lua_Integer intv = lua_tointeger(state, 3);
|
|
|
|
memcpy(ptr, &intv, std::min(sizeof(intv), size_t(id->byte_size())));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int idx = check_container_index(state, id->getNumBits(), 2, iidx, "write");
|
|
|
|
int size = id->getBits()[idx].size;
|
|
|
|
|
|
|
|
if (lua_isboolean(state, 3) || lua_isnil(state, 3))
|
|
|
|
setBitfieldField(ptr, idx, size, lua_toboolean(state, 3));
|
|
|
|
else if (lua_isnumber(state, 3))
|
|
|
|
setBitfieldField(ptr, idx, size, lua_tointeger(state, 3));
|
|
|
|
else
|
|
|
|
field_error(state, 2, "number or boolean expected", "write");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metamethod: __index for df.global
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
static int meta_global_index(lua_State *state)
|
|
|
|
{
|
2012-03-21 03:26:53 -06:00
|
|
|
auto field = (struct_field_info*)find_field(state, 2, "read");
|
2012-03-20 11:34:27 -06:00
|
|
|
if (!field)
|
|
|
|
return get_metafield(state);
|
2012-03-20 03:56:29 -06:00
|
|
|
void *ptr = *(void**)field->offset;
|
|
|
|
if (!ptr)
|
2012-03-20 11:34:27 -06:00
|
|
|
field_error(state, 2, "global address not known", "read");
|
2012-03-20 03:56:29 -06:00
|
|
|
return read_field(state, field, ptr);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Metamethod: __newindex for df.global
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
static int meta_global_newindex(lua_State *state)
|
|
|
|
{
|
2012-03-21 03:26:53 -06:00
|
|
|
auto field = (struct_field_info*)find_field(state, 2, "write");
|
2012-03-20 03:56:29 -06:00
|
|
|
void *ptr = *(void**)field->offset;
|
|
|
|
if (!ptr)
|
|
|
|
field_error(state, 2, "global address not known", "write");
|
2012-03-20 11:34:27 -06:00
|
|
|
write_field(state, field, ptr, 3);
|
2012-03-20 03:56:29 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Add fields in the array to the UPVAL_FIELDTABLE candidates on the stack.
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
static void IndexFields(lua_State *state, const struct_field_info *fields)
|
|
|
|
{
|
2012-03-21 03:26:53 -06:00
|
|
|
// stack: read write
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
int base = lua_gettop(state) - 2;
|
|
|
|
|
|
|
|
for (; fields; ++fields)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
|
|
|
switch (fields->mode)
|
|
|
|
{
|
|
|
|
case struct_field_info::END:
|
2012-03-21 03:26:53 -06:00
|
|
|
return;
|
2012-03-20 03:56:29 -06:00
|
|
|
|
|
|
|
case struct_field_info::PRIMITIVE:
|
|
|
|
case struct_field_info::STATIC_STRING:
|
|
|
|
case struct_field_info::POINTER:
|
|
|
|
lua_pushstring(state,fields->name);
|
|
|
|
lua_pushlightuserdata(state,(void*)fields);
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_rawset(state,base+2);
|
2012-03-20 03:56:29 -06:00
|
|
|
// fallthrough
|
|
|
|
|
|
|
|
case struct_field_info::STATIC_ARRAY:
|
|
|
|
case struct_field_info::SUBSTRUCT:
|
|
|
|
case struct_field_info::CONTAINER:
|
|
|
|
case struct_field_info::STL_VECTOR_PTR:
|
|
|
|
lua_pushstring(state,fields->name);
|
|
|
|
lua_pushlightuserdata(state,(void*)fields);
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_rawset(state,base+1);
|
2012-03-20 03:56:29 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Enable a metafield by injecting an entry into a UPVAL_FIELDTABLE.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void EnableMetaField(lua_State *state, int ftable_idx, const char *name, void *id = NULL)
|
|
|
|
{
|
|
|
|
lua_pushlightuserdata(state, id);
|
|
|
|
lua_setfield(state, ftable_idx, name);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Set metatable properties common to all actual DF object references.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void SetPtrMethods(lua_State *state, int meta_idx, int read_idx)
|
2012-03-20 11:34:27 -06:00
|
|
|
{
|
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_COMPARE_NAME);
|
|
|
|
lua_setfield(state, meta_idx, "__eq");
|
|
|
|
|
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_TYPETABLE_NAME);
|
|
|
|
lua_pushvalue(state, meta_idx);
|
|
|
|
lua_pushcclosure(state, meta_ptr_tostring, 2);
|
|
|
|
lua_setfield(state, meta_idx, "__tostring");
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
EnableMetaField(state, read_idx, "_type");
|
2012-03-21 10:04:37 -06:00
|
|
|
EnableMetaField(state, read_idx, "_kind");
|
2012-03-20 11:34:27 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Add a struct-style (3 upvalues) metamethod to the metatable.
|
|
|
|
*/
|
2012-03-20 11:34:27 -06:00
|
|
|
static void SetStructMethod(lua_State *state, int meta_idx, int ftable_idx,
|
|
|
|
lua_CFunction function, const char *name)
|
|
|
|
{
|
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_TYPETABLE_NAME);
|
|
|
|
lua_pushvalue(state, meta_idx);
|
|
|
|
lua_pushvalue(state, ftable_idx);
|
|
|
|
lua_pushcclosure(state, function, 3);
|
|
|
|
lua_setfield(state, meta_idx, name);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Make a metatable with most common fields, and two empty tables for UPVAL_FIELDTABLE.
|
|
|
|
*/
|
|
|
|
static void MakeMetatable(lua_State *state, type_identity *type, const char *kind)
|
2012-03-21 03:26:53 -06:00
|
|
|
{
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
lua_newtable(state); // metatable
|
|
|
|
|
|
|
|
lua_pushstring(state, type->getFullName().c_str());
|
|
|
|
lua_setfield(state, base+1, "__metatable");
|
|
|
|
|
|
|
|
lua_pushlightuserdata(state, type);
|
|
|
|
lua_setfield(state, base+1, "_identity");
|
|
|
|
|
|
|
|
LookupInTable(state, type, DFHACK_TYPEID_TABLE_NAME);
|
|
|
|
if (lua_isnil(state, -1))
|
|
|
|
{
|
|
|
|
lua_pop(state, 1);
|
|
|
|
lua_getfield(state, base+1, "__metatable");
|
|
|
|
}
|
|
|
|
lua_setfield(state, base+1, "_type");
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
lua_pushstring(state, kind);
|
|
|
|
lua_setfield(state, base+1, "_kind");
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_newtable(state); // read
|
|
|
|
lua_newtable(state); // write
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Make a struct-style object metatable.
|
|
|
|
*/
|
2012-03-20 03:56:29 -06:00
|
|
|
static void MakeFieldMetatable(lua_State *state, struct_identity *pstruct,
|
|
|
|
lua_CFunction reader, lua_CFunction writer)
|
|
|
|
{
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
MakeMetatable(state, pstruct, "struct"); // meta, read, write
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
for (struct_identity *p = pstruct; p; p = p->getParent())
|
|
|
|
{
|
|
|
|
IndexFields(state, p->getFields());
|
|
|
|
}
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
SetStructMethod(state, base+1, base+2, reader, "__index");
|
|
|
|
SetStructMethod(state, base+1, base+3, writer, "__newindex");
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
// returns: [metatable readfields writefields];
|
|
|
|
}
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Make a primitive-style metatable
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void MakePrimitiveMetatable(lua_State *state, type_identity *type)
|
|
|
|
{
|
|
|
|
int base = lua_gettop(state);
|
2012-03-20 03:56:29 -06:00
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
MakeMetatable(state, type, "primitive");
|
2012-03-21 03:26:53 -06:00
|
|
|
SetPtrMethods(state, base+1, base+2);
|
|
|
|
|
|
|
|
EnableMetaField(state, base+2, "value", type);
|
|
|
|
EnableMetaField(state, base+3, "value", type);
|
|
|
|
|
|
|
|
SetStructMethod(state, base+1, base+2, meta_primitive_index, "__index");
|
|
|
|
SetStructMethod(state, base+1, base+3, meta_primitive_newindex, "__newindex");
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Add a 6 upvalue metamethod to the metatable.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void SetContainerMethod(lua_State *state, int meta_idx, int ftable_idx,
|
|
|
|
lua_CFunction function, const char *name,
|
|
|
|
type_identity *container, type_identity *item, int count)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_TYPETABLE_NAME);
|
|
|
|
lua_pushvalue(state, meta_idx);
|
|
|
|
lua_pushvalue(state, ftable_idx);
|
|
|
|
|
|
|
|
lua_pushlightuserdata(state, container);
|
|
|
|
lua_pushlightuserdata(state, item);
|
|
|
|
if (count < 0)
|
|
|
|
lua_pushnil(state);
|
|
|
|
else
|
|
|
|
lua_pushinteger(state, count);
|
|
|
|
|
|
|
|
lua_pushcclosure(state, function, 6);
|
|
|
|
lua_setfield(state, meta_idx, name);
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* If ienum refers to a valid enum, attach its keys to UPVAL_FIELDTABLE,
|
|
|
|
* and the enum itself to the _enum metafield.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void AttachEnumKeys(lua_State *state, int base, type_identity *ienum)
|
|
|
|
{
|
|
|
|
LookupInTable(state, ienum, DFHACK_ENUM_TABLE_NAME);
|
|
|
|
|
|
|
|
if (!lua_isnil(state, -1))
|
|
|
|
{
|
|
|
|
lua_newtable(state);
|
|
|
|
lua_swap(state);
|
|
|
|
lua_setfield(state, -2, "__index");
|
|
|
|
lua_dup(state);
|
|
|
|
lua_setmetatable(state, base+2);
|
|
|
|
lua_setmetatable(state, base+3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pop(state, 1);
|
2012-03-21 10:04:37 -06:00
|
|
|
|
|
|
|
LookupInTable(state, ienum, DFHACK_TYPEID_TABLE_NAME);
|
|
|
|
lua_setfield(state, base+1, "_enum");
|
|
|
|
|
|
|
|
EnableMetaField(state, base+2, "_enum");
|
2012-03-21 03:26:53 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Make a container-style object metatable.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void MakeContainerMetatable(lua_State *state, container_identity *type,
|
|
|
|
type_identity *item, int count, type_identity *ienum)
|
2012-03-20 03:56:29 -06:00
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
int base = lua_gettop(state);
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
MakeMetatable(state, type, "container");
|
2012-03-21 03:26:53 -06:00
|
|
|
SetPtrMethods(state, base+1, base+2);
|
|
|
|
|
|
|
|
lua_pushstring(state, type->getFullName(item).c_str());
|
|
|
|
lua_dup(state);
|
|
|
|
lua_setfield(state, base+1, "__metatable");
|
|
|
|
lua_setfield(state, base+1, "_type");
|
|
|
|
|
|
|
|
lua_pushlightuserdata(state, item);
|
|
|
|
lua_setfield(state, base+1, "_field_identity");
|
|
|
|
|
|
|
|
if (count >= 0)
|
2012-03-20 11:34:27 -06:00
|
|
|
{
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_pushinteger(state, count);
|
|
|
|
lua_setfield(state, base+1, "_count");
|
|
|
|
}
|
2012-03-20 11:34:27 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
SetContainerMethod(state, base+1, base+2, meta_container_len, "__len", type, item, count);
|
|
|
|
SetContainerMethod(state, base+1, base+2, meta_container_index, "__index", type, item, count);
|
|
|
|
SetContainerMethod(state, base+1, base+3, meta_container_newindex, "__newindex", type, item, count);
|
|
|
|
|
|
|
|
AttachEnumKeys(state, base, ienum);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Metatable construction identity methods.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
void type_identity::build_metatable(lua_State *state)
|
|
|
|
{
|
|
|
|
MakePrimitiveMetatable(state, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void container_identity::build_metatable(lua_State *state)
|
|
|
|
{
|
|
|
|
MakeContainerMetatable(state, this, getItemType(), -1, getIndexEnumType());
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointer_identity::build_metatable(lua_State *state)
|
|
|
|
{
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
|
|
|
|
primitive_identity::build_metatable(state);
|
|
|
|
|
|
|
|
EnableMetaField(state, base+2, "target", this);
|
|
|
|
EnableMetaField(state, base+3, "target", this);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
void bitfield_identity::build_metatable(lua_State *state)
|
|
|
|
{
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
|
|
|
|
MakeMetatable(state, this, "bitfield");
|
|
|
|
SetPtrMethods(state, base+1, base+2);
|
|
|
|
|
|
|
|
SetContainerMethod(state, base+1, base+2, meta_bitfield_len, "__len", this, NULL, -1);
|
|
|
|
SetContainerMethod(state, base+1, base+2, meta_bitfield_index, "__index", this, NULL, -1);
|
|
|
|
SetContainerMethod(state, base+1, base+3, meta_bitfield_newindex, "__newindex", this, NULL, -1);
|
|
|
|
|
|
|
|
AttachEnumKeys(state, base, this);
|
|
|
|
|
|
|
|
EnableMetaField(state, base+2, "whole", this);
|
|
|
|
EnableMetaField(state, base+3, "whole", this);
|
|
|
|
}
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
void struct_identity::build_metatable(lua_State *state)
|
|
|
|
{
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
MakeFieldMetatable(state, this, meta_struct_index, meta_struct_newindex);
|
|
|
|
SetPtrMethods(state, base+1, base+2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void global_identity::build_metatable(lua_State *state)
|
|
|
|
{
|
|
|
|
MakeFieldMetatable(state, this, meta_global_index, meta_global_newindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BuildTypeMetatable(lua_State *state, type_identity *type)
|
|
|
|
{
|
|
|
|
type->build_metatable(state);
|
|
|
|
|
|
|
|
lua_pop(state, 2);
|
|
|
|
|
|
|
|
SaveTypeInfo(state, type);
|
|
|
|
}
|
2012-03-20 11:34:27 -06:00
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Construct a metatable for an object type folded into the field descriptor.
|
|
|
|
* This is done to reduce compile-time symbol table bloat due to templates.
|
|
|
|
*/
|
2012-03-21 03:26:53 -06:00
|
|
|
static void GetAdHocMetatable(lua_State *state, const struct_field_info *field)
|
|
|
|
{
|
|
|
|
lua_pushlightuserdata(state, (void*)field);
|
|
|
|
|
|
|
|
if (!LookupTypeInfo(state, true))
|
|
|
|
{
|
|
|
|
switch (field->mode)
|
|
|
|
{
|
|
|
|
case struct_field_info::CONTAINER:
|
|
|
|
{
|
|
|
|
auto ctype = (container_identity*)field->type;
|
|
|
|
MakeContainerMetatable(state, ctype, ctype->getItemType(), -1, field->eid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case struct_field_info::STATIC_ARRAY:
|
|
|
|
MakeContainerMetatable(state, &df::buffer_container_identity::base_instance,
|
|
|
|
field->type, field->count, field->eid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case struct_field_info::STL_VECTOR_PTR:
|
|
|
|
MakeContainerMetatable(state, &df::identity_traits<std::vector<void*> >::identity,
|
|
|
|
field->type, -1, field->eid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
luaL_error(state, "Invalid ad-hoc field: %d", field->mode);
|
|
|
|
}
|
2012-03-20 11:34:27 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_pop(state, 2);
|
|
|
|
|
|
|
|
SaveTypeInfo(state, (void*)field);
|
2012-03-20 11:34:27 -06:00
|
|
|
}
|
2012-03-20 03:56:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/*
|
|
|
|
* Recursive walk of scopes to construct the df... tree.
|
|
|
|
*/
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
static void RenderTypeChildren(lua_State *state, const std::vector<compound_identity*> &children);
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
static void AssociateId(lua_State *state, int table, int val, const char *name)
|
|
|
|
{
|
|
|
|
lua_pushinteger(state, val);
|
|
|
|
lua_pushstring(state, name);
|
|
|
|
lua_dup(state);
|
|
|
|
lua_pushinteger(state, val);
|
|
|
|
|
|
|
|
lua_rawset(state, table);
|
|
|
|
lua_rawset(state, table);
|
|
|
|
}
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
static void RenderType(lua_State *state, compound_identity *node)
|
|
|
|
{
|
|
|
|
assert(node->getName());
|
2012-03-21 03:26:53 -06:00
|
|
|
std::string name = node->getFullName();
|
2012-03-19 10:12:27 -06:00
|
|
|
|
|
|
|
lua_newtable(state);
|
|
|
|
if (!lua_checkstack(state, 20))
|
|
|
|
return;
|
|
|
|
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
|
|
|
|
lua_pushlightuserdata(state, node);
|
|
|
|
lua_setfield(state, base, "_identity");
|
|
|
|
|
|
|
|
switch (node->type())
|
|
|
|
{
|
2012-03-21 10:04:37 -06:00
|
|
|
case IDTYPE_STRUCT:
|
|
|
|
lua_pushstring(state, "struct-type");
|
|
|
|
lua_setfield(state, base, "_kind");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IDTYPE_CLASS:
|
|
|
|
lua_pushstring(state, "class-type");
|
|
|
|
lua_setfield(state, base, "_kind");
|
|
|
|
break;
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
case IDTYPE_ENUM:
|
|
|
|
{
|
2012-03-21 10:04:37 -06:00
|
|
|
lua_pushstring(state, "enum-type");
|
|
|
|
lua_setfield(state, base, "_kind");
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
enum_identity *eid = (enum_identity*)node;
|
|
|
|
const char *const *keys = eid->getKeys();
|
|
|
|
|
|
|
|
// For enums, set mapping between keys and values
|
|
|
|
for (int64_t i = eid->getFirstItem(), j = 0; i <= eid->getLastItem(); i++, j++)
|
|
|
|
{
|
2012-03-21 10:04:37 -06:00
|
|
|
if (keys[j])
|
|
|
|
AssociateId(state, base, i, keys[j]);
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (eid->getFirstItem() <= eid->getLastItem())
|
|
|
|
{
|
|
|
|
lua_pushinteger(state, eid->getFirstItem());
|
2012-03-20 03:56:29 -06:00
|
|
|
lua_setfield(state, base, "_first_item");
|
2012-03-19 10:12:27 -06:00
|
|
|
|
|
|
|
lua_pushinteger(state, eid->getLastItem());
|
2012-03-20 03:56:29 -06:00
|
|
|
lua_setfield(state, base, "_last_item");
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
SaveInTable(state, node, DFHACK_ENUM_TABLE_NAME);
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
case IDTYPE_BITFIELD:
|
|
|
|
{
|
|
|
|
lua_pushstring(state, "bitfield-type");
|
|
|
|
lua_setfield(state, base, "_kind");
|
|
|
|
|
|
|
|
bitfield_identity *eid = (bitfield_identity*)node;
|
|
|
|
auto bits = eid->getBits();
|
|
|
|
|
|
|
|
for (int i = 0; i < eid->getNumBits(); i++)
|
|
|
|
{
|
|
|
|
if (bits[i].name)
|
|
|
|
AssociateId(state, base, i, bits[i].name);
|
|
|
|
if (bits[i].size > 1)
|
|
|
|
i += bits[i].size-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushinteger(state, 0);
|
|
|
|
lua_setfield(state, base, "_first_item");
|
|
|
|
|
|
|
|
lua_pushinteger(state, eid->getNumBits()-1);
|
|
|
|
lua_setfield(state, base, "_last_item");
|
|
|
|
|
|
|
|
SaveInTable(state, node, DFHACK_ENUM_TABLE_NAME);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTypeChildren(state, node->getScopeChildren());
|
|
|
|
|
|
|
|
assert(base == lua_gettop(state));
|
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
if (node->type() == IDTYPE_GLOBAL)
|
|
|
|
{
|
2012-03-21 03:26:53 -06:00
|
|
|
BuildTypeMetatable(state, node);
|
2012-03-20 03:56:29 -06:00
|
|
|
|
|
|
|
lua_dup(state);
|
|
|
|
lua_setmetatable(state, base);
|
|
|
|
lua_swap(state); // -> meta curtable
|
|
|
|
|
|
|
|
freeze_table(state, true, "global");
|
2012-03-20 11:34:27 -06:00
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
lua_getfield(state, base, "__newindex");
|
|
|
|
lua_setfield(state, -2, "__newindex");
|
2012-03-20 11:34:27 -06:00
|
|
|
|
2012-03-20 03:56:29 -06:00
|
|
|
lua_remove(state, base);
|
|
|
|
}
|
|
|
|
else
|
2012-03-20 11:34:27 -06:00
|
|
|
{
|
2012-03-21 03:26:53 -06:00
|
|
|
freeze_table(state, true, name.c_str());
|
|
|
|
}
|
2012-03-20 11:34:27 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_TYPE_TOSTRING_NAME);
|
|
|
|
lua_setfield(state, -2, "__tostring");
|
2012-03-20 11:34:27 -06:00
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_pop(state, 1);
|
2012-03-20 11:34:27 -06:00
|
|
|
|
|
|
|
SaveInTable(state, node, DFHACK_TYPEID_TABLE_NAME);
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RenderTypeChildren(lua_State *state, const std::vector<compound_identity*> &children)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < children.size(); i++)
|
|
|
|
{
|
|
|
|
RenderType(state, children[i]);
|
|
|
|
lua_pushstring(state, children[i]->getName());
|
|
|
|
lua_swap(state);
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_rawset(state, -3);
|
2012-03-19 10:12:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DoAttach(lua_State *state)
|
|
|
|
{
|
|
|
|
int base = lua_gettop(state);
|
|
|
|
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_newtable(state);
|
|
|
|
lua_setfield(state, LUA_REGISTRYINDEX, DFHACK_TYPEID_TABLE_NAME);
|
|
|
|
|
2012-03-21 03:26:53 -06:00
|
|
|
lua_newtable(state);
|
|
|
|
lua_setfield(state, LUA_REGISTRYINDEX, DFHACK_ENUM_TABLE_NAME);
|
|
|
|
|
2012-03-19 10:12:27 -06:00
|
|
|
lua_pushcfunction(state, change_error);
|
2012-03-20 11:34:27 -06:00
|
|
|
lua_setfield(state, LUA_REGISTRYINDEX, DFHACK_CHANGEERROR_NAME);
|
|
|
|
|
|
|
|
lua_pushcfunction(state, meta_ptr_compare);
|
|
|
|
lua_setfield(state, LUA_REGISTRYINDEX, DFHACK_COMPARE_NAME);
|
|
|
|
|
|
|
|
lua_pushcfunction(state, meta_type_tostring);
|
|
|
|
lua_setfield(state, LUA_REGISTRYINDEX, DFHACK_TYPE_TOSTRING_NAME);
|
2012-03-19 10:12:27 -06:00
|
|
|
|
|
|
|
luaL_register(state, "df", no_functions);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Assign df a metatable with read-only contents
|
|
|
|
lua_newtable(state);
|
|
|
|
|
|
|
|
// Render the type structure
|
|
|
|
RenderTypeChildren(state, compound_identity::getTopScope());
|
|
|
|
|
|
|
|
freeze_table(state, true, "df");
|
|
|
|
lua_remove(state, -2);
|
|
|
|
lua_setmetatable(state, -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(state, 1);
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:04:37 -06:00
|
|
|
/**
|
|
|
|
* Initialize access to DF objects from the interpreter
|
|
|
|
* context, unless it has already been done.
|
|
|
|
*/
|
2012-03-19 10:12:27 -06:00
|
|
|
void DFHack::AttachDFGlobals(lua_State *state)
|
|
|
|
{
|
2012-03-20 11:34:27 -06:00
|
|
|
if (luaL_newmetatable(state, DFHACK_TYPETABLE_NAME))
|
2012-03-19 10:12:27 -06:00
|
|
|
DoAttach(state);
|
|
|
|
|
|
|
|
lua_pop(state, 1);
|
|
|
|
}
|