|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
/*
* This file:
* http://angg.twu.net/peek/peek.c
* http://angg.twu.net/peek/peek.c.html
* (find-angg "peek/peek.c")
* Author: Eduardo Ochs <eduardoochs@gmail.com>
* Version: 2011jun02
*
* Quick index:
* «.description» (to "description")
* «.build» (to "build")
* «.init» (to "init")
* «.tests» (to "tests")
* «description» (to ".description")
* This file implements four very low-level functions.
* They work like this:
* str = peek_(addr, len)
* poke_(addr, str)
* addr = malloc_(len)
* free_(addr)
* where "addr" and "len" are integers, and "str" is a string.
* The functions are placed in the global namespace.
* There is no range checking at all, just like in the old Forth days.
* Have fun! 8-)
*
* «build» (to ".build")
* We compile this with:
* (find-angg "peek/peek-0.0.1-0.rockspec" "build")
*
* Old stuff related to this:
* (find-angg "DAVINCI/peek.c")
* (find-angg "vtutil4/piofontx.c")
* See also:
* (find-pilw3m "index.html")
* (find-pilw3m "index.html" "The C API")
*
* Brief and hurried announcement:
* http://article.gmane.org/gmane.comp.lang.lua.general/79307
*
* Thanks to: Marc Simpson, ToxicFrog
*/
#include <stdlib.h> // (find-man "3 malloc")
#include <string.h> // (find-man "3 memcpy")
#include <lua.h>
#include <lauxlib.h>
// (find-luamanualw3m "#luaL_checkint")
// (find-luamanualw3m "#lua_pushlstring")
static int lua_peek(lua_State* L) {
int addr = luaL_checkint(L, 1);
int len = luaL_checkint(L, 2);
lua_pushlstring(L, (void *)addr, len);
return 1;
}
// (find-luamanualw3m "#luaL_checklstring")
// (find-node "(libc)Function Index" "memcpy")
static int lua_poke(lua_State* L) {
int addr = luaL_checkint(L, 1);
int len = 0; const char* straddr = luaL_checklstring(L, 2, &len);
memcpy((void *)addr, straddr, len);
return 0;
}
// (find-luamanualw3m "#luaL_checkint")
// (find-luamanualw3m "#lua_pushinteger")
// (find-node "(libc)Function Index" "* malloc")
static int lua_malloc(lua_State* L) {
int len = luaL_checkint(L, 1);
lua_pushinteger(L, (int)malloc(len));
return 1;
}
// (find-node "(libc)Function Index" "* free")
static int lua_free(lua_State* L) {
int addr = luaL_checkint(L, 1);
free((void *)addr);
return 0;
}
// (find-luamanualw3m "#lua_topointer")
// (find-lua51grep "grep -nH -e lua_topointer src/*")
// (find-lua51srcfile "lapi.c" "lua_topointer")
// http://www.lua.org/source/5.1/lapi.c.html#lua_topointer
// (find-lua51srcfile "lua.h" "LUA_TFUNCTION")
// (find-lua51srcfile "lobject.h" "#define ttype")
static int lua_toptr(lua_State* L) {
lua_pushinteger(L, (int)lua_topointer(L, -1));
return 1;
}
// (find-luamanualw3m "#lua_register")
// (find-luamanualw3m "#pdf-package.loaders" "luaopen_")
// "peek_init" is not a standard name...
// It sounds lua-4.0-ish.
// What should I use instead?
// «init» (to ".init")
// LUALIB_API int peek_init(lua_State *L) {
LUALIB_API int luaopen_peek(lua_State *L) {
lua_register(L, "peek_", lua_peek);
lua_register(L, "poke_", lua_poke);
lua_register(L, "malloc_", lua_malloc);
lua_register(L, "free_", lua_free);
lua_register(L, "toptr_", lua_toptr);
return 0;
}
// 2011apr17:
// (find-luamanualw3m "#pdf-package.loaders")
// (find-luamanualw3m "#pdf-package.loaders" "luaopen_")
/*
* «tests» (to ".tests")
* I moved the tests to the rockspec:
* (find-angg "peek/peek-0.0.1-0.rockspec" "test")
*/
/*
* Local Variables:
* coding: raw-text-unix
* ee-anchor-format: "«%s»"
* End:
*/