Skip to content

Commit a055dab

Browse files
committed
Version 0.0.3
1 parent adb990e commit a055dab

File tree

8 files changed

+62
-82
lines changed

8 files changed

+62
-82
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/
1+
node_modules/
2+
bench/

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ http.createServer(async(function (req, res)
4444
sleep(1000):await()
4545

4646
res:write('Hello World!');
47-
-- because end is a lua keyword you have to put the '_'
48-
res:_end();
47+
res['end']();
4948
end)):listen(port);
5049

5150
print('Your server is running on port ' .. port .. '!')

example/main.lua

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
#!/usr/bin/env demoon
2-
-- you can require JS module files
3-
local jssleep = require('./sleep.js')
4-
-- you can require node modules
2+
-- you can require node modules (package.json/node_modules works as well)
53
local http = require('http')
6-
-- you can require Lua files
4+
5+
-- you can require js modules and lua files
6+
local jssleep = require('./sleep.js')
77
local luasleep = require('sleep.lua')
88

99
local port = os.getenv('PORT') or 8080
1010

11-
-- yes, we have top level await
11+
-- top level await works!
1212
luasleep(1000):await()
1313

14-
-- create a server object:
1514
http.createServer(async(function (req, res)
1615
-- you can await inside async bounded functions
1716
jssleep(1000):await()
1817

19-
res:write('Hello World!'); -- write a response to the client
20-
res['end'](); -- end the response
21-
end)):listen(port); -- the server object listens on port 8080
18+
res:write('Hello World!');
19+
res['end']();
20+
end)):listen(port);
2221

2322
print('Your server is running on port ' .. port .. '!')

package-lock.json

+7-7
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.2",
3+
"version": "0.0.3",
44
"description": "Lua + Node",
55
"main": "src/index.js",
66
"bin": {
@@ -13,6 +13,6 @@
1313
"lua"
1414
],
1515
"dependencies": {
16-
"wasmoon": "1.6.1"
16+
"wasmoon": "1.7.0"
1717
}
1818
}

src/file.js

-16
This file was deleted.

src/index.js

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
const { LuaFactory } = require('wasmoon')
22
const path = require('path')
3-
const { walk } = require('./file')
43
const fs = require('fs').promises
54

6-
const registerDirectory = async (factory, dir) => {
7-
for await (const file of walk(dir)) {
8-
await factory.mountFile(file, await fs.readFile(file))
9-
}
10-
}
11-
125
const start = async (entryFile) => {
13-
const factory = new LuaFactory()
14-
15-
await factory.mountFile(path.resolve(process.cwd(), entryFile), await fs.readFile(entryFile))
16-
await registerDirectory(factory, path.resolve(__dirname, "std"))
17-
18-
const lua = await factory.createEngine({ injectObjects: true })
19-
20-
lua.global.set('new', (constructor, ...args) => new constructor(...args))
21-
lua.global.set('global', global)
22-
lua.global.set('mountFile', factory.mountFileSync.bind(factory))
23-
lua.global.set('jsRequire', (modulename, metaDirectory) => {
24-
if (modulename.startsWith('.')) {
25-
modulename = path.resolve(metaDirectory, '..', modulename)
26-
}
27-
28-
return require(modulename)
29-
})
30-
31-
await lua.doFile(path.resolve(__dirname, "std/main.lua"))
32-
await lua.doFile(path.resolve(process.cwd(), entryFile))
6+
const factory = new LuaFactory(undefined, process.env)
7+
8+
const fullEntryFile = path.resolve(process.cwd(), entryFile)
9+
const fullStdFile = path.resolve(__dirname, "std.lua")
10+
11+
await factory.mountFile(fullEntryFile, await fs.readFile(fullEntryFile))
12+
await factory.mountFile(fullStdFile, await fs.readFile(fullStdFile))
13+
14+
const engine = await factory.createEngine({ injectObjects: true })
15+
16+
engine.global.set('new', (constructor, ...args) => new constructor(...args))
17+
engine.global.set('global', global)
18+
engine.global.set('mountFile', factory.mountFileSync.bind(factory))
19+
engine.global.set('jsRequire', (modulename, metaDirectory) => {
20+
if (modulename.startsWith('.')) {
21+
modulename = path.resolve(metaDirectory, '..', modulename)
22+
}
23+
24+
return require(modulename)
25+
})
26+
27+
try {
28+
engine.doFileSync(fullStdFile)
29+
30+
const thread = engine.global.newThread()
31+
thread.loadFile(fullEntryFile)
32+
33+
await thread.run(0)
34+
} catch (e) {
35+
console.error(e)
36+
}
3337
}
3438

3539
module.exports = { start }

src/std/main.lua src/std.lua

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
local path = jsRequire("path")
2+
local fs = jsRequire("fs")
3+
4+
function tick(co)
5+
if coroutine.status(co) == "suspended" then
6+
coroutine.resume(co)
7+
setImmediate(tick, co)
8+
end
9+
end
10+
111
-- async function to bound awaits
212
function async(callback)
313
return function(...)
@@ -14,26 +24,14 @@ function async(callback)
1424
end
1525
end)
1626

17-
function tick()
18-
local status = coroutine.status(co)
19-
20-
if status == "suspended" then
21-
coroutine.resume(co)
22-
setImmediate(tick)
23-
end
24-
end
25-
26-
tick()
27+
tick(co)
2728
end)
2829
end
2930
end
3031

3132
-- package searcher to handle lua files on the fly
3233
table.insert(package.searchers, function(moduleName)
3334
if moduleName:sub(-4) == ".lua" then
34-
local path = jsRequire("path")
35-
local fs = jsRequire("fs")
36-
3735
local calledDirectory = debug.getinfo(3).short_src
3836
local luafile = path.resolve(calledDirectory, "..", moduleName)
3937

@@ -51,11 +49,6 @@ table.insert(package.searchers, function(moduleName)
5149
if success then return function() return result end end
5250
end)
5351

54-
-- replace the origin getenv by the nodejs one
55-
function os.getenv(varname)
56-
return process.env[varname]
57-
end
58-
5952
-- set the nodejs global as fallback to default lua global
6053
setmetatable(_G, {
6154
__index = function(t, k) return global[k] end

0 commit comments

Comments
 (0)