Skip to content

Commit 44e4a0c

Browse files
committed
get js interop working
1 parent 3cd4566 commit 44e4a0c

4 files changed

Lines changed: 66 additions & 77 deletions

File tree

web_playground/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ for (let path of getAllFiles("../test/tests/nattlua/analyzer/")) {
5050
fs.writeFileSync("src/random.json", JSON.stringify(tests))
5151

5252
async function downloadLua() {
53-
let baseUrl = "https://raw.githubusercontent.com/thenumbernine/js-util/7865018a985074f558b4337226d07e18fe5f9452/"
53+
let baseUrl = "https://raw.githubusercontent.com/thenumbernine/js-util/630b456a151c411b8ddb595d0cdfeb8d03b27fe4/"
5454

5555
await downloadFile(baseUrl + "lua-5.4.7-with-ffi.wasm", "public/js/lua-5.4.7-with-ffi.wasm")
5656
await downloadFile(baseUrl + "lua-interop.js", "public/js/lua-interop.js")

web_playground/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const pathFromURI = (uri: Uri) => {
1515
}
1616

1717
const main = async () => {
18-
const { syntax_runtime, syntax_typesystem, lsp, prettyPrint } = await loadLuaWasmoon()
19-
//const { syntax_runtime, syntax_typesystem, lsp, prettyPrint } = await loadLuaInterop()
18+
//const { syntax_runtime, syntax_typesystem, lsp, prettyPrint } = await loadLuaWasmoon()
19+
const { syntax_runtime, syntax_typesystem, lsp, prettyPrint } = await loadLuaInterop()
2020

2121
await registerSyntax(syntax_runtime, syntax_typesystem)
2222

@@ -30,14 +30,15 @@ const main = async () => {
3030
})
3131

3232
const callMethodOnServer = (method: string, params: any) => {
33-
console.log("calling", method, params)
34-
let response = lsp.methods[method](params)
33+
console.log("lsp.methods['", method, "'](", params, ")")
34+
let [response] = lsp.methods[method](JSON.stringify(params))
3535
console.log("\tgot", response)
3636
return response
3737
}
3838

3939
const onMessageFromServer = (method: string, callback: (params: any) => void) => {
4040
lsp.On(method, (params) => {
41+
params = JSON.parse(params)
4142
console.log("received", method, params)
4243
callback(params)
4344
})

web_playground/src/lua-utils/luaInterop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface LuaInterop {
22
newState: () => void;
3-
doString: (s: string) => void;
3+
doString: (s: string, ...any) => any;
44
L: any;
55
lib: any;
66
push_js: (L: any, jsValue: any, isArrow?: boolean) => number;

web_playground/src/lua.ts

Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,31 @@ export const loadLuaWasmoon = async () => {
9292
const syntax_runtime = lua.global.get("syntax_runtime")
9393
const lsp = lua.global.get("lsp")
9494
console.log("Lua engine initialized successfully")
95+
await lua.doString(`
96+
function _G.prettyPrint(code)
97+
local nl = require("nattlua")
98+
local compiler = nl.Compiler(code, "temp", {
99+
emitter = {
100+
preserve_whitespace = false,
101+
string_quote = "\\"",
102+
no_semicolon = true,
103+
type_annotations = "explicit",
104+
force_parenthesis = true,
105+
comment_type_annotations = false,
106+
},
107+
parser = {
108+
skip_import = true,
109+
}
110+
})
111+
return assert(compiler:Emit())
112+
end
113+
`)
114+
const prettyPrintFunc = lua.global.get("prettyPrint") as (code: string) => string
95115
return {
96116
syntax_typesystem,
97117
syntax_runtime,
98118
lsp,
99-
prettyPrint: (lua: LuaEngine, code: string) => {
100-
lua.doStringSync(`
101-
function _G.prettyPrint(code)
102-
local nl = require("nattlua")
103-
local compiler = nl.Compiler(code, "temp", {
104-
emitter = {
105-
preserve_whitespace = false,
106-
string_quote = "\\"",
107-
no_semicolon = true,
108-
type_annotations = "explicit",
109-
force_parenthesis = true,
110-
comment_type_annotations = false,
111-
},
112-
parser = {
113-
skip_import = true,
114-
}
115-
})
116-
return assert(compiler:Emit())
117-
end
118-
`)
119-
120-
return lua.global.get("prettyPrint")(code) as string
121-
}
119+
prettyPrint: prettyPrintFunc,
122120

123121
}
124122
}
@@ -138,21 +136,14 @@ export const loadLuaInterop = async () => {
138136

139137
globalThis.lua = lua
140138

141-
let lsp: any
142-
globalThis.loadLSP = (obj) => lsp = obj
143-
144-
let syntax_runtime: any
145-
globalThis.loadSyntaxRuntime = (obj) => syntax_runtime = obj
146-
147-
let syntax_typesystem: any
148-
globalThis.loadSyntaxTypesystem = (obj) => syntax_typesystem = obj
149-
150139
await loadLuaModule((str) => lua.doString(str), import("./../../build_output.lua"), "nattlua")
151140
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")
152141
await loadLuaModule((str) => lua.doString(str), import("./../../language_server/lsp.lua"), "lsp", "@language_server/lsp.lua")
142+
await loadLuaModule((str) => lua.doString(str), import("./../../language_server/json.lua"), "json", "@language_server/json.lua")
153143

154-
await lua.doString(`
144+
const [lsp] = await lua.doString(`
155145
local lsp = require("lsp")
146+
local json = require("json")
156147
157148
local listeners = {}
158149
@@ -162,12 +153,17 @@ export const loadLuaInterop = async () => {
162153
end
163154
164155
function lsp.On(method, callback)
165-
listeners[method] = callback
156+
-- callback is js function, first argument is "this"
157+
listeners[method] = function(params)
158+
params = json.encode(params)
159+
return callback(nil, params)
160+
end
166161
end
167162
168163
for k,v in pairs(lsp.methods) do
169164
lsp.methods[k] = function(params)
170-
print("calling on server", k)
165+
params = json.decode(params)
166+
print("calling on server", k, params)
171167
local ok, res = xpcall(function()
172168
return v(params)
173169
end, debug.traceback)
@@ -177,47 +173,39 @@ export const loadLuaInterop = async () => {
177173
return res
178174
end
179175
end
180-
181-
jsToLua[0].loadLSP(lsp)`)
182-
183-
184-
await lua.doString(`
185-
jsToLua[0].loadSyntaxRuntime(require("nattlua.syntax.typesystem"))
186-
jsToLua[0].loadSyntaxTypesystem(require("nattlua.syntax.runtime"))
187-
`)
188-
189-
globalThis.syntax_typesystem = syntax_typesystem
190-
globalThis.syntax_runtime = syntax_runtime
191-
globalThis.lsp = lsp
192-
176+
return lsp
177+
`)
178+
179+
180+
const [syntax_runtime_json] = await lua.doString(`return require("json").encode(require("nattlua.syntax.runtime"))`)
181+
const [syntax_typesystem_json] = await lua.doString(`return require("json").encode(require("nattlua.syntax.typesystem"))`)
182+
const syntax_runtime = JSON.parse(syntax_runtime_json)
183+
const syntax_typesystem = JSON.parse(syntax_typesystem_json)
184+
const [prettyPrintFunc] = await lua.doString(`
185+
return function(code)
186+
local nl = require("nattlua")
187+
local compiler = nl.Compiler(code, "temp", {
188+
emitter = {
189+
preserve_whitespace = false,
190+
string_quote = "\\"",
191+
no_semicolon = true,
192+
type_annotations = "explicit",
193+
force_parenthesis = true,
194+
comment_type_annotations = false,
195+
},
196+
parser = {
197+
skip_import = true,
198+
}
199+
})
200+
return assert(compiler:Emit())
201+
end
202+
`)
193203
console.log("Lua interop initialized successfully")
194204
return {
195205
syntax_typesystem,
196206
syntax_runtime,
197207
lsp,
198-
prettyPrint: (lua: LuaEngine, code: string) => {
199-
lua.doStringSync(`
200-
function _G.prettyPrint(code)
201-
local nl = require("nattlua")
202-
local compiler = nl.Compiler(code, "temp", {
203-
emitter = {
204-
preserve_whitespace = false,
205-
string_quote = "\\"",
206-
no_semicolon = true,
207-
type_annotations = "explicit",
208-
force_parenthesis = true,
209-
comment_type_annotations = false,
210-
},
211-
parser = {
212-
skip_import = true,
213-
}
214-
})
215-
return assert(compiler:Emit())
216-
end
217-
`)
218-
219-
return lua.global.get("prettyPrint")(code) as string
220-
}
208+
prettyPrint: (code: string) => prettyPrintFunc(code)[0] as string,
221209

222210
}
223211

0 commit comments

Comments
 (0)