Skip to content

Commit

Permalink
Merge pull request #6 from pallene-lang/dev
Browse files Browse the repository at this point in the history
Userside boilerplate reduction
  • Loading branch information
singul4ri7y authored Aug 3, 2024
2 parents 3748202 + 21d8845 commit 40679d1
Show file tree
Hide file tree
Showing 31 changed files with 469 additions and 565 deletions.
44 changes: 9 additions & 35 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,15 @@ name: Github Actions CI
# - both events: runs the same checks twice, wasting compute time
on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

env:
LUA_VERSION: 5.4.6-2
LUA_VERSION: 5.4.7
LUAROCKS_VERSION: 3.9.0

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install Lua
run: |
wget -O - https://github.com/pallene-lang/lua-internals/archive/refs/tags/${{env.LUA_VERSION}}.tar.gz | tar xzf -
cd lua-internals-${{env.LUA_VERSION}}
make linux
sudo make install
- name: Install Luarocks
run: |
wget -O - https://luarocks.org/releases/luarocks-${{env.LUAROCKS_VERSION}}.tar.gz | tar xzf -
cd luarocks-${{env.LUAROCKS_VERSION}}
./configure --with-lua=/usr/local
make
sudo make install
- name: Install Luacheck
run: luarocks install --local luacheck

- name: Run Luacheck
run: |
eval "$(luarocks path)"
./run-lint
test:
name: Test
runs-on: ubuntu-latest
Expand All @@ -64,8 +35,8 @@ jobs:

- name: Install Lua
run: |
wget -O - https://github.com/pallene-lang/lua-internals/archive/refs/tags/${{env.LUA_VERSION}}.tar.gz | tar xzf -
cd lua-internals-${{env.LUA_VERSION}}
wget -O - https://www.lua.org/ftp/lua-${{env.LUA_VERSION}}.tar.gz | tar xzf -
cd lua-${{env.LUA_VERSION}}
make linux
sudo make install
Expand All @@ -78,7 +49,9 @@ jobs:
sudo make install
- name: Build
run: luarocks --local make
run: |
sudo make install
luarocks --local make
- name: Install Busted
run: luarocks --local install busted
Expand All @@ -87,3 +60,4 @@ jobs:
run: |
eval "$(luarocks path)"
busted -o gtest -v ./spec
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ptracer_header:
cp lib/ptracer.h $(INSTALL_INCDIR)

libptracer:
$(CC) -fPIC -O2 -shared src/ptracer.c -o libptracer.so
$(CC) -fPIC -DPT_DEBUG -O2 -shared src/ptracer.c -o libptracer.so

uninstall:
rm -rf $(INSTALL_INCDIR)/ptracer.h
Expand Down
4 changes: 2 additions & 2 deletions examples/fibonacci/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CC := gcc
.SILENT:

all:
$(CC) -fPIC $(CFLAGS) -shared module.c -o module.so
$(CC) -fPIC $(CFLAGS) -shared fibonacci.c -o fibonacci.so

debug:
$(CC) -fPIC $(CFLAGS) -DMODULE_DEBUG -shared module.c -o module.so
$(CC) -fPIC $(CFLAGS) -DPT_DEBUG -shared fibonacci.c -o fibonacci.so
93 changes: 93 additions & 0 deletions examples/fibonacci/fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2024, The Pallene Developers
* Pallene Tracer is licensed under the MIT license.
* Please refer to the LICENSE and AUTHORS files for details
* SPDX-License-Identifier: MIT
*/

#define PT_IMPLEMENTATION
#include <ptracer.h>

/* User specific macros when Pallene Tracer debug mode is enabled. */
#ifdef PT_DEBUG
#define FIB_GET_FNSTACK \
pt_fnstack_t *fnstack = lua_touserdata(L, \
lua_upvalueindex(1))

#else
#define FIB_GET_FNSTACK
#endif // PT_DEBUG

/* ---------------- PALLENE TRACER LUA INERFACE ---------------- */
#define FIB_LUA_FRAMEENTER(fnptr) \
FIB_GET_FNSTACK; \
PALLENE_TRACER_LUA_FRAMEENTER(L, fnstack, fnptr, \
lua_upvalueindex(2), _frame)
/* ---------------- PALLENE TRACER LUA INERFACE END ---------------- */

/* ---------------- PALLENE TRACER C INTERFACE ---------------- */

#define FIB_C_FRAMEENTER() \
FIB_GET_FNSTACK; \
PALLENE_TRACER_GENERIC_C_FRAMEENTER(L, fnstack, _frame)

#define FIB_C_SETLINE() \
PALLENE_TRACER_GENERIC_C_SETLINE(fnstack)

#define FIB_C_FRAMEEXIT() \
PALLENE_TRACER_FRAMEEXIT(fnstack)

/* ---------------- PALLENE TRACER C INTERFACE END ---------------- */

int fib(lua_State *L, int n) {
FIB_C_FRAMEENTER();

if(n <= 1) {
FIB_C_FRAMEEXIT();
return n;
}

FIB_C_SETLINE();
int result = fib(L, n - 1) + fib(L, n - 2);
FIB_C_FRAMEEXIT();
return result;
}

int fib_lua(lua_State *L) {
int top = lua_gettop(L);
FIB_LUA_FRAMEENTER(fib_lua);

/* In Lua interface frames, we always have a finalizer object pushed to the stack by
`FIB_LUA_FRAMEENTER()`. */
if(luai_unlikely(top < 1)) {
luaL_error(L, "Expected atleast 1 parameter");
}

if(luai_unlikely(lua_isinteger(L, 1) == 0)) {
luaL_error(L, "Expected the first argument to be an integer");
}

/* Dispatch. */
int result = fib(L, lua_tointeger(L, 1));
lua_pushinteger(L, result);

return 1;
}

int luaopen_fibonacci(lua_State *L) {
pt_fnstack_t *fnstack = pallene_tracer_init(L);

lua_newtable(L);
int table = lua_gettop(L);

/* ---- fib ---- */
/* One very good way to integrate our stack userdatum and finalizer
object is by using Lua upvalues. */
lua_pushlightuserdata(L, fnstack);
/* `pallene_tracer_init` function pushes the frameexit finalizer to the stack. */
lua_pushvalue(L, -3);
lua_pushcclosure(L, fib_lua, 2);
lua_setfield(L, table, "fib");

return 1;
}
6 changes: 3 additions & 3 deletions examples/fibonacci/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod = require "module"
print(mod.fib(40))
local fibonacci = require "fibonacci"
print(fibonacci.fib(40))
-- Uncomment this and trigger an error. You can debug using the 'pallene-debug' script.
-- print(mod.fib(40.0))
-- print(fibonacci.fib(40.0))
121 changes: 0 additions & 121 deletions examples/fibonacci/module.c

This file was deleted.

Loading

0 comments on commit 40679d1

Please sign in to comment.