This commit is contained in:
Alexander Bentkamp
2023-11-15 17:49:41 +01:00
parent 3ff46aeb54
commit 8d29761579
7 changed files with 120 additions and 120 deletions
+87
View File
@@ -0,0 +1,87 @@
import Lean.Server.Watchdog
import GameServer.Game
namespace WasmServer.Watchdog
open Lean
open Server
open Watchdog
open IO
open Lsp
open JsonRpc
open System.Uri
def counter := IO.mkRef 0
def readLspRequestAs (s : String) (expectedMethod : String) (α : Type) [FromJson α] : IO (Request α) := do
let j ← ofExcept (Json.parse s)
let m ← match fromJson? j with
| Except.ok (m : JsonRpc.Message) => pure m
| Except.error inner => throw $ userError s!"JSON '{j.compress}' did not have the format of a JSON-RPC message.\n{inner}"
let initRequest ← match m with
| Message.request id method params? =>
if method = expectedMethod then
let j := toJson params?
match fromJson? j with
| Except.ok v => pure $ JsonRpc.Request.mk id expectedMethod (v : α)
| Except.error inner => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'\n{inner}"
else
throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'"
| _ => throw $ userError s!"Expected JSON-RPC request, got: '{(toJson m).compress}'"
@[export game_send_message]
def sendMessage (s : String) : IO Unit := do
-- IO.println s!"received {s}"
-- if args.length < 2 then
-- throwServerError s!"Expected 1-3 command line arguments in addition to `--server`:
-- game directory, the name of the main module (optional), and the name of the game (optional)."
-- let gameDir := args[1]!
-- let module := if args.length < 3 then defaultGameModule else args[2]!
-- let gameName := if args.length < 4 then defaultGameName else args[3]!
-- let workerPath := "./gameserver"
-- -- TODO: Do the following commands slow us down?
-- let srcSearchPath ← initSrcSearchPath (← getBuildDir)
-- let references ← IO.mkRef (← loadReferences)
-- let fileWorkersRef ← IO.mkRef (RBMap.empty : FileWorkerMap)
-- -- let i ← maybeTee "wdIn.txt" false i
-- -- let o ← maybeTee "wdOut.txt" true o
-- -- let e ← maybeTee "wdErr.txt" true e
-- let state : GameServerState := {
-- env := ← importModules #[] {} 0 --← createEnv gameDir module,
-- game := "TEST",
-- gameDir := "test",
-- inventory := #[]
-- difficulty := 0
-- }
let initRequest ← readLspRequestAs s "initialize" InitializeParams
-- We misuse the `rootUri` field to the gameName
let rootUri? := "TEST"
let initRequest := {initRequest with param := {initRequest.param with rootUri?}}
let o ← IO.getStdout
o.writeLspResponse {
id := initRequest.id
result := {
capabilities := mkLeanServerCapabilities
serverInfo? := some {
name := "Lean 4 Game Server"
version? := "0.1.1"
}
: InitializeResult
}
}
-- let context : ServerContext := {
-- hIn := i
-- hOut := o
-- hLog := e
-- args := args
-- fileWorkersRef := fileWorkersRef
-- initParams := initRequest.param
-- workerPath
-- srcSearchPath
-- references
-- }
-- discard $ ReaderT.run (StateT.run initAndRunWatchdogAux state) context
return ()
+1
View File
@@ -1,5 +1,6 @@
import GameServer.FileWorker
import GameServer.Watchdog
import GameServer.WasmServer
import GameServer.Commands
-- TODO: The only reason we import `Commands` is so that it gets built to on `lake build`
-30
View File
@@ -1,30 +0,0 @@
import Lean.Server.Watchdog
open Lean
open Server
open Watchdog
open Lsp
open IO
open JsonRpc
#check JsonRpc.Request
@[export game_send_message]
def sendMessage (s : String) : IO Unit := do
let expectedMethod := "initialize"
let j ← ofExcept (Json.parse s)
let m ← match fromJson? j with
| Except.ok (m : JsonRpc.Message) => pure m
| Except.error inner => throw $ userError s!"JSON '{j.compress}' did not have the format of a JSON-RPC message.\n{inner}"
let initRequest ← match m with
| Message.request id method params? =>
if method = expectedMethod then
let j := toJson params?
match fromJson? j with
| Except.ok v => pure $ JsonRpc.Request.mk id expectedMethod (v : InitializeParams)
| Except.error inner => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'\n{inner}"
else
throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'"
| _ => throw $ userError s!"Expected JSON-RPC request, got: '{(toJson m).compress}'"
IO.println s!"{initRequest.param.editDelay}"
return ()
-5
View File
@@ -10,8 +10,3 @@ lean_exe gameserver {
root := `Main
supportInterpreter := true
}
@[default_target]
lean_lib WasmServer where
defaultFacets := #[LeanLib.staticFacet]
+2 -2
View File
@@ -7,7 +7,7 @@ extern lean_object* game_send_message(lean_object*, lean_object*);
extern void lean_initialize_runtime_module();
extern void lean_initialize();
extern void lean_io_mark_end_initialization();
extern lean_object * initialize_WasmServer(uint8_t builtin, lean_object *);
extern lean_object * initialize_GameServer_WasmServer(uint8_t builtin, lean_object *);
int main() {
lean_initialize();
@@ -16,7 +16,7 @@ int main() {
// use same default as for Lean executables
uint8_t builtin = 1;
lean_object * io_world = lean_io_mk_world();
res = initialize_WasmServer(builtin, io_world);
res = initialize_GameServer_WasmServer(builtin, io_world);
if (lean_io_result_is_ok(res)) {
lean_dec_ref(res);
} else {