Compare commits
5 Commits
0441c109bf
...
283be71809
| Author | SHA1 | Date |
|---|---|---|
|
|
283be71809 | |
|
|
5aa1a031fa | |
|
|
f457e31740 | |
|
|
8b9eafe94c | |
|
|
dd42c2a43f |
|
|
@ -1 +1,2 @@
|
|||
*.luo
|
||||
lunaclib.so
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/sh
|
||||
|
||||
cc -shared $(pkg-config --cflags luajit) lunaclib.c $(pkg-config --libs luajit) -o lunaclib.so
|
||||
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
static int u64_to_num(lua_State* L) {
|
||||
#ifndef LUA_NUMBER_DOUBLE
|
||||
#error Lua must be compiled with number as a double floating point
|
||||
#endif
|
||||
|
||||
const uint64_t h1 = luaL_checkint(L, 1);
|
||||
const uint64_t h2 = luaL_checkint(L, 2);
|
||||
|
||||
const uint64_t stacked = (h2 << 32) | h1;
|
||||
|
||||
double res = *(double *)((char *)&stacked);
|
||||
lua_pushnumber(L, res);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const struct luaL_Reg lib[] = {
|
||||
{"u64_to_num", u64_to_num},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_lunaclib(lua_State *L) {
|
||||
luaL_openlib(L, "lunaclib", lib, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -2,7 +2,7 @@ local readbc = require('readbc')
|
|||
local vm = require('vm')
|
||||
local VM = vm.VM
|
||||
|
||||
fp = io.open('tests/test1.luo')
|
||||
fp = io.open('snippets/test1.luo')
|
||||
header = readbc.read_header(fp)
|
||||
proto = readbc.read_proto(fp)
|
||||
fp:close()
|
||||
|
|
|
|||
|
|
@ -68,9 +68,9 @@ local Metamethod = {
|
|||
}
|
||||
|
||||
local Opcodes_defs = gen_opcodes {
|
||||
opcode(37, 'POW', Mode.dst, Mode.none, Mode.lits, Metamethod.none),
|
||||
opcode(41, 'KSHORT', Mode.dst, Mode.var, Mode.var, Metamethod.pow),
|
||||
opcode(75, 'RET0', Mode.rbase, Mode.none, Mode.lit, Mode.none),
|
||||
opcode(0x25, 'POW', Mode.dst, Mode.none, Mode.lits, Metamethod.none),
|
||||
opcode(0x29, 'KSHORT', Mode.dst, Mode.var, Mode.var, Metamethod.pow),
|
||||
opcode(0x4b, 'RET0', Mode.rbase, Mode.none, Mode.lit, Mode.none),
|
||||
}
|
||||
|
||||
local function decode(ins)
|
||||
|
|
|
|||
14
readbc.lua
14
readbc.lua
|
|
@ -1,6 +1,8 @@
|
|||
-- module for reading serialized lua bytecode
|
||||
|
||||
local bit = require('bit')
|
||||
local lunaclib = require('lunaclib')
|
||||
local u64_to_num = lunaclib.u64_to_num
|
||||
|
||||
local HEADER_MAGIC = {27, string.byte('L'), string.byte('J')}
|
||||
local FLAG_STRIP = 0x02
|
||||
|
|
@ -53,6 +55,7 @@ local function read_uleb128(fp)
|
|||
while not last_byte do
|
||||
local uleb_byte = read_u8(fp)
|
||||
res = bit.bor(res, bit.lshift(bit.band(uleb_byte, 0x7f), shift))
|
||||
shift = shift + 7
|
||||
|
||||
if bit.band(uleb_byte, 0x80) == 0 then
|
||||
last_byte = true
|
||||
|
|
@ -124,7 +127,16 @@ local function read_proto(fp)
|
|||
local bcins = read_u32n(fp, numbc)
|
||||
local uvdata = read_u16n(fp, numuv)
|
||||
-- local kgc
|
||||
-- local numkn
|
||||
local num = {}
|
||||
for i = 1, numkn do
|
||||
local x = read_uleb128(fp)
|
||||
if bit.band(x, 1) == 1 then
|
||||
local y = read_uleb128(fp)
|
||||
num[i] = u64_to_num(x, y)
|
||||
else
|
||||
num[i] = (x/2)-1
|
||||
end
|
||||
end
|
||||
-- local debug = fp:read(debugLen)
|
||||
|
||||
-- TODO: remove when reading of kgc, numkn and debug is implemented
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
local num = 2^15+1
|
||||
local num64 = 2^32
|
||||
local fp = 0.75
|
||||
Loading…
Reference in New Issue