Skip to content

Commit 2f94d46

Browse files
committed
work towards supporting wasmoon alternative
1 parent 8cb6f6c commit 2f94d46

File tree

6 files changed

+57
-35
lines changed

6 files changed

+57
-35
lines changed

web_playground/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ build_output.lua
22
node_modules
33
*.bundle.*
44
yarn.lock
5-
public/glue.wasm
5+
public/glue.wasm
6+
public/js

web_playground/build.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,31 @@ const path = require("path")
44
const { finished } = require("stream/promises")
55
const { Readable } = require("stream")
66

7-
const getAllFiles = function (dirPath, arrayOfFiles) {
7+
async function downloadFile(url, outputPath) {
8+
const response = await fetch(url)
9+
10+
if (!response.ok) {
11+
throw new Error(`Failed to download, status: ${response.status}`)
12+
}
13+
14+
const directory = path.dirname(outputPath)
15+
16+
fs.mkdirSync(directory, { recursive: true })
17+
fs.writeFileSync(outputPath, Buffer.from(await response.arrayBuffer()))
18+
}
19+
20+
function getAllFiles(dirPath, arrayOfFiles) {
821
files = fs.readdirSync(dirPath)
922

1023
arrayOfFiles = arrayOfFiles || []
1124

12-
files.forEach(function (file) {
25+
for (const file of files) {
1326
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
1427
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
1528
} else {
1629
arrayOfFiles.push(path.join(__dirname, dirPath, "/", file))
1730
}
18-
})
31+
}
1932

2033
return arrayOfFiles
2134
}
@@ -35,16 +48,26 @@ for (let path of getAllFiles("../test/tests/nattlua/analyzer/")) {
3548
}
3649

3750
fs.writeFileSync("src/random.json", JSON.stringify(tests))
38-
;(async () => {
39-
const res = await fetch("https://unpkg.com/[email protected]/dist/glue.wasm")
40-
fs.unlink("public/glue.wasm", (err) => {
41-
if (err) {
42-
console.error(err)
43-
}
44-
})
45-
const fileStream = fs.createWriteStream("public/glue.wasm", { flags: "wx" })
46-
await finished(Readable.fromWeb(res.body).pipe(fileStream))
47-
})()
51+
52+
async function downloadLua() {
53+
let baseUrl = "https://raw.githubusercontent.com/thenumbernine/js-util/7865018a985074f558b4337226d07e18fe5f9452/"
54+
55+
await downloadFile(baseUrl + "lua-5.4.7-with-ffi.wasm", "public/js/lua-5.4.7-with-ffi.wasm")
56+
await downloadFile(baseUrl + "lua-interop.js", "public/js/lua-interop.js")
57+
await downloadFile(baseUrl + "lua-5.4.7-with-ffi.js", "public/js/lua-5.4.7-with-ffi.js")
58+
;(async () => {
59+
const res = await fetch("https://unpkg.com/[email protected]/dist/glue.wasm")
60+
fs.unlink("public/glue.wasm", (err) => {
61+
if (err) {
62+
console.error(err)
63+
}
64+
})
65+
const fileStream = fs.createWriteStream("public/glue.wasm", { flags: "wx" })
66+
await finished(Readable.fromWeb(res.body).pipe(fileStream))
67+
})()
68+
}
69+
70+
downloadLua()
4871

4972
execSync("cd ../ && luajit nattlua.lua build fast")
5073

web_playground/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ const pathFromURI = (uri: Uri) => {
1616

1717
const main = async () => {
1818
const lua = await loadLua()
19-
await registerSyntax(lua)
19+
20+
await lua.doString(`
21+
_G.syntax_typesystem = require("nattlua.syntax.typesystem")
22+
_G.syntax_runtime = require("nattlua.syntax.runtime")
23+
`)
24+
25+
const syntax_typesystem = lua.global.get("syntax_typesystem")
26+
const syntax_runtime = lua.global.get("syntax_runtime")
27+
28+
await registerSyntax(syntax_runtime, syntax_typesystem)
2029

2130
const editor = createEditor()
2231
const tab = MonacoEditor.createModel("local x = 1337", "nattlua")

web_playground/src/lua.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { LuaEngine, LuaFactory } from "wasmoon"
2-
import { registerSyntax } from "./syntax"
32
import { loadLuaModule } from "./util"
43

54
export const loadLua = async () => {
@@ -8,19 +7,18 @@ export const loadLua = async () => {
87
openStandardLibs: true,
98
})
109

11-
await loadLuaModule(lua, import("./../../build_output.lua"), "nattlua")
10+
await loadLuaModule((str) => lua.doStringSync(str), import("./../../build_output.lua"), "nattlua")
1211
await lua.doString("for k, v in pairs(package.preload) do print(k,v) end require('nattlua') for k,v in pairs(IMPORTS) do package.preload[k] = v end")
13-
await loadLuaModule(lua, import("./../../language_server/lsp.lua"), "lsp", "@language_server/lsp.lua")
12+
await loadLuaModule((str) => lua.doStringSync(str), import("./../../language_server/lsp.lua"), "lsp", "@language_server/lsp.lua")
1413

1514
await lua.doString(`
1615
local lsp = require("lsp")
1716
1817
local listeners = {}
1918
2019
function lsp.Call(data)
21-
if listeners[data.method] then
22-
listeners[data.method](data.params)
23-
end
20+
assert(data.method, "missing method")
21+
listeners[data.method](data.params)
2422
end
2523
2624
function lsp.On(method, callback)

web_playground/src/syntax.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { languages } from "monaco-editor"
2-
import { LuaEngine } from "wasmoon"
32
import { arrayUnion, escapeRegex, mapsToArray } from "./util"
43

54
const uniqueCharacters = (str: string) => {
@@ -10,14 +9,8 @@ const uniqueCharacters = (str: string) => {
109
return Array.from(unique).join("")
1110
}
1211

13-
export const registerSyntax = async (lua: LuaEngine) => {
14-
await lua.doString(`
15-
_G.syntax_typesystem = require("nattlua.syntax.typesystem")
16-
_G.syntax_runtime = require("nattlua.syntax.runtime")
17-
`)
12+
export const registerSyntax = async (syntax_runtime: any, syntax_typesystem: any) => {
1813

19-
const syntax_typesystem = lua.global.get("syntax_typesystem")
20-
const syntax_runtime = lua.global.get("syntax_runtime")
2114
const syntax: languages.IMonarchLanguage = {
2215
defaultToken: "",
2316
tokenPostfix: ".nl",

web_playground/src/util.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { LuaEngine } from "wasmoon"
2-
31
export const mapsToArray = (maps: { [key: string]: unknown }[]) => {
42
const set = new Set<string>()
53
for (const map of maps) {
@@ -36,7 +34,7 @@ function chunkSubstr(str: string, size: number) {
3634
return chunks
3735
}
3836

39-
export const loadLuaModule = async (lua: LuaEngine, p: Promise<{ default: string }>, moduleName: string, chunkName?: string) => {
37+
export const loadLuaModule = async (doString: (luaCode: string) => void, p: Promise<{ default: string }>, moduleName: string, chunkName?: string) => {
4038
let { default: code } = await p
4139

4240
if (code.startsWith("#")) {
@@ -59,19 +57,19 @@ export const loadLuaModule = async (lua: LuaEngine, p: Promise<{ default: string
5957
bytesStringIndex++
6058
if (bytesStringIndex > 8000) {
6159
let str = `CHUNKS = CHUNKS or {};CHUNKS[#CHUNKS + 1] = "${bytesString.join("")}"`
62-
lua.doStringSync(str)
60+
doString(str)
6361
bytesString = []
6462
bytesStringIndex = 0
6563
}
6664
}
6765
{
6866
let str = `CHUNKS = CHUNKS or {};CHUNKS[#CHUNKS + 1] = "${bytesString.join("")}"`
69-
lua.doStringSync(str)
67+
doString(str)
7068
}
7169

7270
let str = `
7371
local code = "package.preload['${moduleName}'] = function(...) " .. table.concat(CHUNKS) .. " end"
7472
assert(load(code, "${chunkName}"))(...); CHUNKS = nil
7573
`
76-
lua.doStringSync(str)
74+
doString(str)
7775
}

0 commit comments

Comments
 (0)