Skip to content

Commit

Permalink
Merge pull request #597 from pallene-lang/improved-stack-trace
Browse files Browse the repository at this point in the history
Improved stack trace in Pallene using Pallene Tracer
  • Loading branch information
hugomg authored Jun 11, 2024
2 parents 48104cb + 752686a commit 8366118
Show file tree
Hide file tree
Showing 15 changed files with 729 additions and 31 deletions.
17 changes: 17 additions & 0 deletions spec/traceback/depth_recursion/depth_recursion.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod: module = {}

function mod.pallene_fn(lua_fn: integer -> (), depth: integer)
if depth == 0 then
-- Call 'lua_fn' for the last time so that we can raise an error.
lua_fn(depth)
end

lua_fn(depth - 1)
end

return mod
25 changes: 25 additions & 0 deletions spec/traceback/depth_recursion/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local pallene = require 'spec.traceback.depth_recursion.depth_recursion'

-- luacheck: globals lua_fn
function lua_fn(depth)
if depth == 0 then
error "Depth reached 0!"
end

pallene.pallene_fn(lua_fn, depth - 1)
end

-- Should be local.
-- Making it global so that it is visible in the traceback.
-- luacheck: globals wrapper
function wrapper()
lua_fn(10)
end

-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
12 changes: 12 additions & 0 deletions spec/traceback/module_lua/another_module.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local function call_lua_callback(callback)
callback()
end

return {
call_lua_callback = call_lua_callback
}
36 changes: 36 additions & 0 deletions spec/traceback/module_lua/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local another_module = require 'spec.traceback.module_lua.another_module'
local pallene = require 'spec.traceback.module_lua.module_lua'

-- luacheck: globals lua_1
function lua_1()
-- luacheck: globals lua_2
pallene.pallene_1(lua_2)
end

-- luacheck: globals lua_2
function lua_2()
-- luacheck: globals lua_3
pallene.pallene_2(lua_3, 33, 79)
end

-- luacheck: globals lua_3
function lua_3(sum)
print("The summation is: ", sum)

error "Any normal error from Lua!"
end

-- Should be local.
-- Making it global so that it is visible in the traceback.
-- luacheck: globals wrapper
function wrapper()
another_module.call_lua_callback(lua_1)
end

-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
16 changes: 16 additions & 0 deletions spec/traceback/module_lua/module_lua.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod: module = {}

function mod.pallene_1(lua_2: () -> ())
lua_2()
end

function mod.pallene_2(lua_3: integer -> (), a: integer, b: integer)
lua_3(a + b)
end

return mod
27 changes: 27 additions & 0 deletions spec/traceback/module_pallene/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local pallene = require 'spec.traceback.module_pallene.module_pallene'
local pallene_alt = require 'spec.traceback.module_pallene.module_pallene_alt'

-- luacheck: globals lua_2
function lua_2()
error "There's an error in everyday life. Shame!"
end

-- luacheck: globals lua_1
function lua_1()
pallene_alt.alternate_everyday_fn(lua_2)
end

-- Should be local.
-- Making it global so that it is visible in the traceback.
-- luacheck: globals wrapper
function wrapper()
pallene.normal_everyday_fn(lua_1)
end

-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
12 changes: 12 additions & 0 deletions spec/traceback/module_pallene/module_pallene.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod: module = {}

function mod.normal_everyday_fn(callback: () -> ())
callback()
end

return mod
13 changes: 13 additions & 0 deletions spec/traceback/module_pallene/module_pallene_alt.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod: module = {}

function mod.alternate_everyday_fn(callback: () -> ())
callback()
end

return mod

16 changes: 16 additions & 0 deletions spec/traceback/rect/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local rect = require 'spec.traceback.rect.rect'

-- Should be local.
-- Making it global so that it is visible in the traceback.
-- luacheck: globals wrapper
function wrapper()
print(rect.area { width = "Huh, gotcha!", height = 16.0 })
end

-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
17 changes: 17 additions & 0 deletions spec/traceback/rect/rect.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod: module = {}
typealias rect = { width: any, height: any }

function mod.universal_calc_area(x: any, y: any): any
return (x as float * y as float) as any
end

function mod.area(r: rect): float
return mod.universal_calc_area(r.width, r.height) as float
end

return mod
96 changes: 96 additions & 0 deletions spec/traceback_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local util = require "pallene.util"

local function assert_test(test, expected_traceback)
local plnfile = util.shell_quote("spec/traceback/"..test.."/"..test..".pln")
local ok, err = util.execute("pallenec "..plnfile.." --use-traceback")
assert(ok, err)

-- Compile the second Pallene file if exists.
local alt_plnfile = util.shell_quote("spec/traceback/"..test.."/"..test.."_alt.pln")
local ok, _ = util.execute("test -f "..alt_plnfile)
if ok then
local ok, err = util.execute("pallenec "..alt_plnfile.." --use-traceback")
assert(ok, err)
end

local luafile = util.shell_quote("spec/traceback/"..test.."/main.lua")
local ok, err, _, err_content = util.outputs_of_execute("lua "..luafile)
assert(ok, err)
assert.are.same(expected_traceback, err_content)
end

it("Rectangle", function()
assert_test("rect", [[
Runtime error: spec/traceback/rect/main.lua:12: file spec/traceback/rect/rect.pln: line 10: wrong type for downcasted value, expected float but found string
Stack traceback:
spec/traceback/rect/rect.pln:10: in function 'universal_calc_area'
spec/traceback/rect/rect.pln:13: in function 'area'
spec/traceback/rect/main.lua:12: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/rect/main.lua:16: in <main>
C: in function '<?>'
]])
end)

it("Multi-module Lua", function()
assert_test("module_lua", [[
Runtime error: spec/traceback/module_lua/main.lua:25: Any normal error from Lua!
Stack traceback:
C: in function 'error'
spec/traceback/module_lua/main.lua:25: in function 'lua_3'
spec/traceback/module_lua/module_lua.pln:12: in function 'pallene_2'
spec/traceback/module_lua/main.lua:18: in function 'lua_2'
spec/traceback/module_lua/module_lua.pln:8: in function 'pallene_1'
spec/traceback/module_lua/main.lua:12: in function 'callback'
./spec/traceback/module_lua/another_module.lua:7: in function 'call_lua_callback'
spec/traceback/module_lua/main.lua:32: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/module_lua/main.lua:36: in <main>
C: in function '<?>'
]])
end)

it("Multi-module Pallene", function()
assert_test("module_pallene", [[
Runtime error: spec/traceback/module_pallene/main.lua:11: There's an error in everyday life. Shame!
Stack traceback:
C: in function 'error'
spec/traceback/module_pallene/main.lua:11: in function 'lua_2'
spec/traceback/module_pallene/module_pallene_alt.pln:8: in function 'alternate_everyday_fn'
spec/traceback/module_pallene/main.lua:16: in function 'lua_1'
spec/traceback/module_pallene/module_pallene.pln:8: in function 'normal_everyday_fn'
spec/traceback/module_pallene/main.lua:23: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/module_pallene/main.lua:27: in <main>
C: in function '<?>'
]])
end)

it("Depth recursion", function()
assert_test("depth_recursion", [[
Runtime error: spec/traceback/depth_recursion/main.lua:11: Depth reached 0!
Stack traceback:
C: in function 'error'
spec/traceback/depth_recursion/main.lua:11: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:21: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/depth_recursion/main.lua:25: in <main>
C: in function '<?>'
]])
end)

Loading

0 comments on commit 8366118

Please sign in to comment.