Skip to content

Commit

Permalink
Runtime: Add an early exit condition to vfs.dlopen
Browse files Browse the repository at this point in the history
This makes it possible to chain the function with ffi.load, so that the same code will work both during local development and when bundled as a self-contained LUAZIP application.
  • Loading branch information
rdw-software committed Jan 17, 2025
1 parent 0a73854 commit c14036d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Runtime/Libraries/vfs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ local vfs = {
#pragma pack(pop)
]],
cachedAppBundles = {},
errorStrings = {
MISSING_APP_BUNDLE = "Not a LUAZIP app bundle",
},
}

function vfs.decode(fileContents)
Expand Down Expand Up @@ -105,6 +108,11 @@ function vfs.searcher(zipApp, moduleName)
end

function vfs.dlopen(zipApp, libraryName)
if not zipApp then
-- Ensure it's a NOOP if run from a regular script file that may also be bundled
return nil, vfs.errorStrings.MISSING_APP_BUNDLE
end

validation.validateString(libraryName, "libraryName")

libraryName = vfs.dlname(libraryName)
Expand Down
6 changes: 6 additions & 0 deletions Tests/BDD/vfs-library.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ describe("vfs", function()
end)

describe("dlopen", function()
it("should fail if no app bundle was provided", function()
assertFailure(function()
return vfs.dlopen(nil, "foo")
end, vfs.errorStrings.MISSING_APP_BUNDLE)
end)

it("should throw if an invalid library name was passed", function()
assertThrows(function()
vfs.dlopen({}, nil)
Expand Down

0 comments on commit c14036d

Please sign in to comment.