Our current recommendation for the Lua context, is to save it when you initialize the extension, in the init function.
However, since you asked about the dmScript::HScript, the extension context doesn’t (currently) have access to that context. It might be a good addition.
As a temporary alternative, the following snippet calls sys.load_resource from C++:
#include <cassert>
#include <cstdlib>
#include <cstring>
/**
* Usage:
* size_t buf_len;
* unsigned char* buf = LoadResource(L, "/path/to/resource.bin", &buf_len);
* // do something
* free(buf);
*/
static unsigned char* LoadResource(lua_State* L, const char* res_name, size_t* result_len)
{
int top = lua_gettop(L);
lua_getfield(L, LUA_ENVIRONINDEX, "sys");
lua_pushliteral(L, "load_resource");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_pushstring(L, res_name);
lua_call(L, 1, 2);
assert(top + 2 == lua_gettop(L));
assert(lua_isnil(L, -1));
// lua_tolstring returns a fully aligned pointer to a string inside the
// Lua state. This string always has a zero ('\0') after its last character
// (as in C), but can contain other zeros in its body. Because Lua has
// garbage collection, there is no guarantee that the pointer returned by
// lua_tolstring will be valid after the corresponding value is removed
// from the stack.
const char* buf = lua_tolstring(L, -2, result_len);
lua_pop(L, 2);
unsigned char* result = (unsigned char*) malloc(*result_len);
memcpy(result, buf, *result_len);
return result;
}