Skip to content

Commit

Permalink
feat: unmount file
Browse files Browse the repository at this point in the history
  • Loading branch information
X3ZvaWQ committed Feb 11, 2024
1 parent 24d089b commit 343a0bc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
14 changes: 13 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"editor.formatOnSave": true,
"files.insertFinalNewline": true
"files.insertFinalNewline": true,
"cSpell.words": [
"createtable",
"getmetatable",
"newindex",
"newuserdata",
"openlibs",
"pushcclosure",
"pushcfunction",
"setfield",
"setmetatable",
"xmove"
]
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wasmoon-lua5.1",
"version": "1.17.1",
"version": "1.17.2",
"description": "A real lua 5.1 VM with JS bindings made with webassembly",
"main": "dist/index.js",
"scripts": {
Expand Down
8 changes: 6 additions & 2 deletions src/lua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export default class Lua {
this.luaApi.module.FS.writeFile(path, content);
}

public unmountFile(path: string): void {
this.luaApi.module.FS.unlink(path);
}

public doString(script: string): Promise<any> {
return this.callByteCode((thread) => thread.loadString(script));
}
Expand Down Expand Up @@ -123,7 +127,7 @@ export default class Lua {
thread.luaApi.lua_setmetatable(thread.address, -2);
thread.luaApi.lua_pushcclosure(thread.address, registerFuncCallFunction(this.global), 1);

// proxy for function, aviod `attempt to index global 'TestFunction' (a function value)`
// proxy for function, avoid `attempt to index global 'TestFunction' (a function value)`
thread.luaApi.lua_createtable(thread.address, 0, 0);
thread.luaApi.lua_pushcfunction(thread.address, registerRedirectIndexFunction(this.global, target));
thread.luaApi.lua_setfield(thread.address, -2, '__index');
Expand Down Expand Up @@ -163,7 +167,7 @@ export default class Lua {
// Move all stack results to the global state to avoid referencing the thread values
// which will be cleaned up in the finally below.
this.luaApi.lua_xmove(thread.address, this.global.address, result.length);
// The shenanigans here are to return the first reuslt value on the stack.
// The shenanigans here are to return the first result value on the stack.
// Say there's 2 values at stack indexes 1 and 2. Then top is 2, result.length is 2.
// That's why there's a + 1 sitting at the end.
return this.global.getValue(this.global.getTop() - result.length + 1);
Expand Down
8 changes: 8 additions & 0 deletions test/filesystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ describe('Filesystem', () => {
const value = await lua.doFile('init.lua');
expect(value).to.be.equal(content);
});

it('unmount a file and require it should throw', async () => {
const lua = await Lua.create();
lua.mountFile('init.lua', `return 42`);
lua.unmountFile('init.lua');

await expect(lua.doString('require("init")')).to.eventually.be.rejected;
});
});

0 comments on commit 343a0bc

Please sign in to comment.