Skip to content

Commit 4d85484

Browse files
committed
Update wasmoon and change api
1 parent d8e442f commit 4d85484

File tree

5 files changed

+89
-49
lines changed

5 files changed

+89
-49
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
You love Lua but the runtime API is very weak and you don't want to handle and learn a lot of different luarock libraries? You came at the right place! This project aims to offer the bests of **Lua** and **NodeJS** together.
44

5+
> Don't use this in production!
6+
57
## Usage
68

79
You don't need `lua` installed to run demoon, but you need `node` and npm as well, firstly, install demoon globally:

bin/demoon

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/usr/bin/env node
22

3-
const { start } = require('../src/index');
3+
const Demoon = require('../src/index');
44

55
const snippets = process.argv.splice(2);
66
const [entryFile, ...arg] = snippets
77

8-
start(entryFile, arg)
8+
const demoon = new Demoon();
9+
demoon.getLuaEngine()
10+
.then(engine => {
11+
engine.global.set('arg', arg)
12+
}).then(async () => {
13+
await demoon.runFile(entryFile)
14+
})

package-lock.json

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "demoon",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Lua + Node",
55
"main": "src/index.js",
66
"bin": {
@@ -13,6 +13,6 @@
1313
"lua"
1414
],
1515
"dependencies": {
16-
"wasmoon": "1.12.1"
16+
"wasmoon": "1.15.0"
1717
}
1818
}

src/index.js

+68-36
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,80 @@
1-
const { LuaFactory, LuaMultiReturn } = require('wasmoon')
1+
const { LuaFactory, LuaEngine } = require('wasmoon')
22
const path = require('path')
33
const FunctionClassTypeExtension = require('./legacyclasses')
44
const fs = require('fs/promises')
55

6-
const start = async (entryFile, arg) => {
7-
const factory = new LuaFactory(undefined, process.env)
6+
module.exports = class {
7+
/** @returns {Promise<LuaEngine>} */
8+
async getLuaEngine() {
9+
await this.#setupIfNeeded()
10+
return this.#engine
11+
}
812

9-
const fullEntryFile = path.resolve(process.cwd(), entryFile)
10-
const fullStdFile = path.resolve(__dirname, "std.lua")
13+
/**
14+
* @param {string} file
15+
* @returns {Promise<void>}
16+
*/
17+
async runFile(file) {
18+
await this.#setupIfNeeded()
1119

12-
await factory.mountFile(fullEntryFile, await fs.readFile(fullEntryFile))
13-
await factory.mountFile(fullStdFile, await fs.readFile(fullStdFile))
20+
this.#factory.mountFileSync(await this.#factory.getLuaModule(), file, await fs.readFile(file))
1421

15-
const engine = await factory.createEngine({ injectObjects: true })
22+
try {
23+
const thread = this.#engine.global.newThread()
24+
thread.loadFile(file)
1625

17-
engine.global.registerTypeExtension(10, new FunctionClassTypeExtension)
18-
engine.global.set('arg', arg)
19-
engine.global.set('typeof', value => typeof value)
20-
engine.global.set('instanceof', (value, type) => value instanceof type)
21-
engine.global.set('new', (constructor, ...args) => new constructor(...args))
22-
engine.global.set('global', global)
23-
engine.global.set('mountFile', factory.mountFileSync.bind(factory))
24-
engine.global.set('jsRequire', (modulename, metaDirectory) => {
25-
if (metaDirectory) {
26-
if (modulename.startsWith('.')) {
27-
modulename = path.resolve(metaDirectory, '..', modulename)
28-
}
26+
this.#engine.global.set('jsRequire', (modulename, metaDirectory) => {
27+
if (metaDirectory) {
28+
if (modulename.startsWith('.')) {
29+
modulename = path.resolve(metaDirectory, '..', modulename)
30+
}
2931

30-
modulename = require.resolve(modulename, { paths: [fullEntryFile] })
31-
}
32+
modulename = require.resolve(modulename, { paths: [file] })
33+
}
34+
35+
return module.require(modulename)
36+
})
3237

33-
return module.require(modulename)
34-
})
35-
36-
try {
37-
engine.doFileSync(fullStdFile)
38-
39-
const thread = engine.global.newThread()
40-
thread.loadFile(fullEntryFile)
41-
42-
await thread.run(0)
43-
} catch (e) {
44-
console.error(e)
38+
await thread.run(0)
39+
} catch (e) {
40+
console.error(e)
41+
}
4542
}
46-
}
4743

48-
module.exports = { start }
44+
/** @type {LuaEngine | undefined} */
45+
#engine
46+
47+
/** @type {LuaFactory | undefined} */
48+
#factory
49+
50+
async #setupIfNeeded() {
51+
if (this.#factory) return
52+
53+
this.#factory = new LuaFactory(undefined, process.env)
54+
const luamodule = await this.#factory.getLuaModule()
55+
56+
const fullStdFile = path.resolve(__dirname, "std.lua")
57+
this.#factory.mountFileSync(luamodule, fullStdFile, await fs.readFile(fullStdFile))
58+
59+
this.#engine = await this.#factory.createEngine({ injectObjects: true })
60+
61+
this.#engine.global.registerTypeExtension(10, new FunctionClassTypeExtension)
62+
this.#engine.global.set('typeof', value => typeof value)
63+
this.#engine.global.set('instanceof', (value, type) => value instanceof type)
64+
this.#engine.global.set('new', (constructor, ...args) => new constructor(...args))
65+
this.#engine.global.set('global', global)
66+
this.#engine.global.set('mountFile', (path, content) => this.#factory.mountFileSync(luamodule, path, content))
67+
this.#engine.global.set('jsRequire', (modulename, metaDirectory) => {
68+
if (metaDirectory) {
69+
if (modulename.startsWith('.')) {
70+
modulename = path.resolve(metaDirectory, '..', modulename)
71+
}
72+
73+
modulename = require.resolve(modulename)
74+
}
75+
76+
return module.require(modulename)
77+
})
78+
this.#engine.doFileSync(fullStdFile)
79+
}
80+
}

0 commit comments

Comments
 (0)