aboutsummaryrefslogtreecommitdiffstats
path: root/bruiser/lua-scripts/wasmtest.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--bruiser/lua-scripts/wasmtest.lua120
1 files changed, 94 insertions, 26 deletions
diff --git a/bruiser/lua-scripts/wasmtest.lua b/bruiser/lua-scripts/wasmtest.lua
index 32c8442..dec8d66 100644
--- a/bruiser/lua-scripts/wasmtest.lua
+++ b/bruiser/lua-scripts/wasmtest.lua
@@ -1,15 +1,29 @@
colors = require("ansicolors")
--- aka dump wasm
-function demo4()
- local a = getwasmobj("/home/bloodstalker/devi/hell2/bruiser/autogen/wasm/ft/test.wasm")
+local libwasm = {}
+
+function libwasm.demo(wasm_path)
+ local a = getwasmobj(wasm_path)
print(a)
print(type(a))
for k, v in pairs(a) do
print(k, v, type(k), type(v))
end
+ libwasm.dump_import_section(a)
+ libwasm.dump_type_section(a)
+ libwasm.dump_function_section(a)
+ libwasm.dump_table_section(a)
+ libwasm.dump_memory_section(a)
+ libwasm.dump_global_section(a)
+ libwasm.dump_export_section(a)
+ libwasm.dump_start_section(a)
+ libwasm.dump_element_section(a)
+ libwasm.dump_code_section(a)
+ libwasm.dump_data_section(a)
+end
+function libwasm.dump_type_section(a)
print(colors("%{green}".."\ntype section:"))
if a["type_section"] ~= nil then
io.write(tostring("id:"..a["type_section"]:id()).."\n")
@@ -28,7 +42,9 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_import_section(a)
print(colors("%{green}".."\nimport section:"))
if a["import_section"] ~= nil then
io.write("id:"..tostring(a["import_section"]:id()).."\n")
@@ -39,16 +55,19 @@ function demo4()
io.write("entries"..tostring(a["import_section"]:entries()).."\n")
for k, v in pairs(a["import_section"]:entries()) do
--print(k, v, type(v))
- io.write(v:module_length().."\t")
- io.write(v:module_str().."\t")
- io.write(v:field_len().."\t")
- io.write(v:field_str().."\t")
- io.write(v:kind().."\n")
+ io.write("module length:"..v:module_length().."\t")
+ io.write("module str:"..v:module_str().."\t")
+ io.write("field len:"..v:field_len().."\t")
+ io.write("field str:"..v:field_str().."\t")
+ io.write("kind:"..v:kind().."\t")
+ io.write("type:"..v:type().."\n")
end
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_function_section(a)
print(colors("%{green}".."\nfunction section:"))
if a["function_section"] ~= nil then
io.write("id:"..tostring(a["function_section"]:id()).."\n")
@@ -60,7 +79,9 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_table_section(a)
print(colors("%{green}".."\ntable section:"))
if a["table_section"] ~= nil then
io.write("id:"..tostring(a["table_section"]:id()).."\n")
@@ -79,7 +100,10 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+
+function libwasm.dump_memory_section(a)
print(colors("%{green}".."\nmemory section:"))
if a["memory_section"] ~= nil then
io.write("id:"..tostring(a["memory_section"]:id()).."\n")
@@ -95,7 +119,9 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_global_section(a)
print(colors("%{green}".."\nglobal section:"))
if (a["global_section"] ~= nil) then
io.write("id:"..tostring(a["global_section"]:id()).."\n")
@@ -112,7 +138,9 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_export_section(a)
print(colors("%{green}".."\nexport section:"))
if (a["export_section"] ~= nil) then
io.write("id:"..tostring(a["export_section"]:id()).."\n")
@@ -130,7 +158,9 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_start_section(a)
print(colors("%{green}".."\nstart section:"))
if (a["start_section"] ~= nil) then
io.write("id:"..tostring(a["start_section"]:id()).."\n")
@@ -141,7 +171,9 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_element_section(a)
print(colors("%{green}".."\nelement section:"))
if (a["element_section"] ~= nil) then
io.write("id:"..tostring(a["element_section"]:id()).."\n")
@@ -149,19 +181,28 @@ function demo4()
io.write("namelength:"..tostring(a["element_section"]:namelength()).."\n")
io.write("name:"..tostring(a["element_section"]:name()).."\n")
io.write("count:"..tostring(a["element_section"]:count()).."\n")
- io.write("entries:"..tostring(a["element_section"]:entries()).."\n")
+ io.write(colors("%{cyan}".."entries:"..tostring(a["element_section"]:entries()).."\n"))
for k, v in pairs(a["element_section"]:entries()) do
- io.write(v:index().."\t")
- io.write(tostring(v:init()).."\t")
- io.write(v:init():code().."\t")
- io.write(v:num_length().."\t")
- -- FIXME
- io.write(v:elems().."\n")
+ io.write("index:"..v:index().."\t")
+ io.write("init:")
+ for i = 1, #v:init():code() do
+ local c = v:init():code():sub(i,i)
+ io.write(colors("%{red}"..string.byte(c)).." ")
+ end
+ io.write("\n")
+ io.write("number of elements:"..v:num_length().."\t")
+ io.write("elems:")
+ for i, j in pairs(v:elems()) do
+ io.write(colors("%{red}"..j.." "))
+ end
+ io.write("\n")
end
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_code_section(a)
print(colors("%{green}".."\ncode section:"))
if (a["code_section"] ~= nil) then
io.write("id:"..tostring(a["code_section"]:id()).."\n")
@@ -171,20 +212,26 @@ function demo4()
io.write("count:"..tostring(a["code_section"]:count()).."\n")
io.write("bodies:"..tostring(a["code_section"]:bodies()).."\n")
for k,v in pairs(a["code_section"]:bodies()) do
- io.write(v:body_size().."\t")
- io.write(v:local_count().."\t")
+ io.write(colors("\n%{cyan}".."entry:\n"))
+ io.write("body_size:"..v:body_size().."\t")
+ io.write("local_count:"..v:local_count().."\t")
io.write(tostring(v:locals()).."\t")
- print("number of locals:"..#v:locals())
for i, j in pairs(v:locals()) do
io.write("locals count:"..j:count().."\t")
io.write("locals type:"..j:type().."\t")
end
- io.write(v:code().."\n")
+ io.write("function body:\n")
+ for i,j in pairs(v:code()) do
+ io.write(colors("%{red}"..j.." "))
+ end
+ io.write("\n")
end
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+function libwasm.dump_data_section(a)
print(colors("%{green}".."\ndata section:"))
if (a["data_section"] ~= nil) then
io.write("id:"..tostring(a["data_section"]:id()).."\n")
@@ -194,17 +241,16 @@ function demo4()
io.write("count:"..tostring(a["data_section"]:count()).."\n")
io.write("entries:"..tostring(a["data_section"]:entries()).."\n")
if type(a["data_section"]:entries()) == "table" then
+ io.write(colors("%{cyan}".."entries:").."\n")
for k,v in pairs(a["data_section"]:entries()) do
- io.write(v:index().."\t")
- io.write(tostring(v:offset()).."\t")
- print(colors("%{red}"..tostring(#v:offset():code())))
+ io.write("index:"..v:index().."\n")
+ io.write("offset: ")
for i = 1, #v:offset():code() do
local c = v:offset():code():sub(i,i)
- print(colors("%{red}"..string.byte(c)))
+ io.write(colors("%{red}"..string.byte(c)).." ")
end
- --io.write(colors("%{yellow}"..v:offset():code()))
+ io.write("\n")
io.write("size:"..v:size().."\n")
- io.write(tostring(v:data()).."\n")
for i, j in pairs(v:data()) do
io.write(colors("%{blue}"..string.char(j)))
end
@@ -214,7 +260,29 @@ function demo4()
else
print(colors("%{red}".."section doesnt exist."))
end
+end
+
+function libwasm.dump_all(wasm_path)
+ local a = getwasmobj(wasm_path)
+ libwasm.dump_import_section(a)
+ libwasm.dump_type_section(a)
+ libwasm.dump_function_section(a)
+ libwasm.dump_table_section(a)
+ libwasm.dump_memory_section(a)
+ libwasm.dump_global_section(a)
+ libwasm.dump_export_section(a)
+ libwasm.dump_start_section(a)
+ libwasm.dump_element_section(a)
+ libwasm.dump_code_section(a)
+ libwasm.dump_data_section(a)
+end
+function libwasm.dev(wasm_path)
+ local a = getwasmobj(wasm_path)
+ libwasm.dump_import_section(a)
end
-demo4()
+--libwasm.dev("/home/bloodstalker/devi/hell2/bruiser/autogen/wasm/ft/test.wasm")
+libwasm.demo("/home/bloodstalker/devi/hell2/bruiser/autogen/wasm/ft/test.wasm")
+--libwasm.dump_all("/home/bloodstalker/devi/hell2/bruiser/autogen/wasm/ft/test.wasm")
+return libwasm