@@ -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