Skip to content

Commit

Permalink
feat(dev-cli): added a gasused function to aolibc thats exposed to lua
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterFarber committed Sep 26, 2024
1 parent 76f05f4 commit 37e2419
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions dev-cli/container/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ RUN chmod +x /usr/local/bin/ao-build-module
###################################
COPY ./src/aolibc /opt/aolibc
## Build aolibc for 32-bit and rename to aolibc32
RUN cd /opt/aolibc && make CC="emcc -s WASM=1 -s SUPPORT_LONGJMP=1" && cp ./aolibc.a ./aolibc32.a
RUN cd /opt/aolibc && make CC="emcc -s WASM=1 -s SUPPORT_LONGJMP=1 /lua-5.3.4-32/src/liblua.a -I/lua-5.3.4-32/src" && cp ./aolibc.a ./aolibc32.a
RUN rm /opt/aolibc/aolibc.a
RUN rm /opt/aolibc/aostdio.o
# Build aolibc for 64-bit
RUN cd /opt/aolibc && make CC="emcc -s WASM=1 -s MEMORY64=1 -s SUPPORT_LONGJMP=1"
RUN cd /opt/aolibc && make CC="emcc -s WASM=1 -s MEMORY64=1 -s SUPPORT_LONGJMP=1 /lua-5.3.4/src/liblua.a -I/lua-5.3.4/src"

ENV CC='emcc -s WASM=1'
ENV NM='/emsdk/upstream/bin/llvm-nm'
Expand Down
4 changes: 1 addition & 3 deletions dev-cli/container/src/ao-build-module
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def main():
c_program = inject_lua_files(definition, c_program, injected_lua_files)

# Load all libraries
c_program = load_libraries(definition, c_program, link_libraries, c_source_files, injected_lua_files,dependency_libraries)
c_program = load_libraries(config, definition, c_program, link_libraries, c_source_files, injected_lua_files,dependency_libraries)

# Inject the lua files into the c program
c_program = c_program.replace(
Expand Down Expand Up @@ -119,12 +119,10 @@ def main():
# If the target is 64 bit, add the MEMORY64 flag and link against 64 bit libraries
if config.target == 64:
cmd.extend(['-sMEMORY64=1'])
cmd.extend(['-L/opt/aolibc', '-l:aolibc.a'])
cmd.extend(['-I', quote('/lua-{}/src'.format(os.environ.get('LUA_VERSION')))])
cmd.extend([quote('/lua-{}/src/liblua.a'.format(os.environ.get('LUA_VERSION')))])
# If the target is 32 bit, link against 32 bit libraries
else:
cmd.extend(['-L/opt/aolibc', '-l:aolibc32.a'])
cmd.extend(['-I', quote('/lua-{}-32/src'.format(os.environ.get('LUA_VERSION')))])
cmd.extend([quote('/lua-{}-32/src/liblua.a'.format(os.environ.get('LUA_VERSION')))])

Expand Down
9 changes: 8 additions & 1 deletion dev-cli/container/src/ao_module_lib/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import subprocess
import shutil
from ao_module_lib.definition import Definition
from ao_module_lib.config import Config
from ao_module_lib.file import LuaFile, ModuleFile, BundleFile
from ao_module_lib.helper import is_lua_source_file, is_binary_library, encode_hex_literals, shell_exec, debug_print

Expand All @@ -23,7 +24,7 @@ def __get_uname():
return uname


def load_libraries(definition: Definition, c_program: str, link_libraries: list, c_source_files: list, injected_lua_files: list, dependency_libraries: list):
def load_libraries(config: Config, definition: Definition, c_program: str, link_libraries: list, c_source_files: list, injected_lua_files: list, dependency_libraries: list):

uname = __get_uname()

Expand All @@ -46,6 +47,12 @@ def load_libraries(definition: Definition, c_program: str, link_libraries: list,
bundle_files += glob.glob('/src/libs/**/*.o', recursive=True)
bundle_files += glob.glob('/src/libs/**/*.dylib', recursive=True)

# Load aolib from /opt/aolibc/
if(config.target == 64):
bundle_files += glob.glob('/opt/aolibc/aolibc.a', recursive=True)
else:
bundle_files += glob.glob('/opt/aolibc/aolibc32.a', recursive=True)

bundle_files += glob.glob('/src/libs/**/*.lua', recursive=True)
# bundle_files += glob.glob(local_include_dir + '/**/*.so', recursive=True)
bundle_files += glob.glob(LUAROCKS_LOCAL_MODULE_DIR +
Expand Down
26 changes: 26 additions & 0 deletions dev-cli/container/src/aolibc/aostdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <unistd.h>
#include <string.h>
#include <emscripten.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#if 0
#define AO_LOG(...) fprintf(stderr, __VA_ARGS__)
Expand Down Expand Up @@ -93,3 +96,26 @@ int munmap(void* addr, size_t length) {
return 0;
}
*/
EM_JS(int, metering_gasUsed, (), {
return Module.gas.used;
});

static int gasUsed(lua_State *L)
{
lua_pushnumber(L, metering_gasUsed());

return 1;
}

// Library registration function
static const struct luaL_Reg aolib_funcs[] = {
{"gasUsed", gasUsed},
{NULL, NULL} /* Sentinel */
};

// Initialization function
int luaopen_metering(lua_State *L)
{
luaL_newlib(L, aolib_funcs);
return 1;
}
6 changes: 5 additions & 1 deletion loader/src/formats/emscripten4.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var Module = (() => {
readyPromiseReject = reject;
});

["_malloc", "_memory", "___asyncjs__weavedrive_open", "___asyncjs__weavedrive_read", "___asyncjs__weavedrive_close", "_handle", "___indirect_function_table", "onRuntimeInitialized"].forEach(prop => {
["_malloc", "_memory", "___asyncjs__weavedrive_open", "___asyncjs__weavedrive_read", "___asyncjs__weavedrive_close", "_metering_gasUsed", "_handle", "___indirect_function_table", "onRuntimeInitialized"].forEach(prop => {
if (!Object.getOwnPropertyDescriptor(readyPromise, prop)) {
Object.defineProperty(readyPromise, prop, {
get: () => abort("You are getting " + prop + " on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js"),
Expand Down Expand Up @@ -690,6 +690,9 @@ var Module = (() => {
return drive.close(fd);
});
}
function metering_gasUsed() {
return Module.gas.used;
}

/** @constructor */ function ExitStatus(status) {
this.name = "ExitStatus";
Expand Down Expand Up @@ -4943,6 +4946,7 @@ var Module = (() => {

var wasmImports = {
/** @export */ __assert_fail: ___assert_fail,
/** @export */ metering_gasUsed: metering_gasUsed,
/** @export */ __asyncjs__weavedrive_close: __asyncjs__weavedrive_close,
/** @export */ __asyncjs__weavedrive_open: __asyncjs__weavedrive_open,
/** @export */ __asyncjs__weavedrive_read: __asyncjs__weavedrive_read,
Expand Down
8 changes: 5 additions & 3 deletions loader/src/formats/wasm64-emscripten.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var Module = (() => {
readyPromiseReject = reject;
});

["_malloc", "_memory", "___asyncjs__weavedrive_open", "___asyncjs__weavedrive_read", "___asyncjs__weavedrive_close", "_handle", "___indirect_function_table", "onRuntimeInitialized"].forEach(prop => {
["_malloc", "_memory", "___asyncjs__weavedrive_open", "___asyncjs__weavedrive_read", "___asyncjs__weavedrive_close", "_metering_gasUsed", "_handle", "___indirect_function_table", "onRuntimeInitialized"].forEach(prop => {
if (!Object.getOwnPropertyDescriptor(readyPromise, prop)) {
Object.defineProperty(readyPromise, prop, {
get: () => abort("You are getting " + prop + " on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js"),
Expand Down Expand Up @@ -690,6 +690,9 @@ function __asyncjs__weavedrive_close(fd) {
return drive.close(fd);
});
}
function metering_gasUsed() {
return Module.gas.used;
}

/** @constructor */ function ExitStatus(status) {
this.name = "ExitStatus";
Expand Down Expand Up @@ -4955,9 +4958,8 @@ function __asyncjs__weavedrive_close(fd) {

var wasmImports = {
/** @export */ __assert_fail: ___assert_fail,
/** @export */ metering_gasUsed: metering_gasUsed,
/** @export */ __asyncjs__weavedrive_close: __asyncjs__weavedrive_close,


/** @export */ __asyncjs__weavedrive_open: __asyncjs__weavedrive_open,
/** @export */ __asyncjs__weavedrive_read: __asyncjs__weavedrive_read,
/** @export */ __cxa_throw: ___cxa_throw,
Expand Down

0 comments on commit 37e2419

Please sign in to comment.