Skip to content

Commit

Permalink
game module import (not quite)
Browse files Browse the repository at this point in the history
  • Loading branch information
abentkamp committed Nov 15, 2023
1 parent 8d29761 commit 32a2ff3
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ node_modules
client/dist
server/build
server/lakefile.olean
server32bit
**/lake-packages/
**/.DS_Store
2 changes: 1 addition & 1 deletion client/public/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ onmessage = (ev) => {

setInterval(() => {
if (stderrBuffer !== "") {
console.error(stderrBuffer);
console.log(stderrBuffer);
stderrBuffer = ""
}
}, 1000)
Expand Down
6 changes: 6 additions & 0 deletions server/GameServer.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import GameServer.Commands

Game "TestGame"
Title "Hello Test"

MakeGame
114 changes: 59 additions & 55 deletions server/GameServer/WasmServer.lean
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,52 @@ open Lsp
open JsonRpc
open System.Uri

def counter := IO.mkRef 0
def ServerState := GameServerState

@[export game_make_state]
def makeState : IO (Ref ServerState) := do
let e ← IO.getStderr
try
searchPathRef.set ["/lib", "/gamelib"]
let state : GameServerState := {
env := ← importModules #[
{ module := `Init : Import }
-- { module := `GameServer : Import }
] {} 0 --← createEnv gameDir module,
game := "TEST",
gameDir := "test",
inventory := #[]
difficulty := 0
}
return ← IO.mkRef state
catch err =>
e.putStrLn s!"Import error: {err}"
throw err

def readLspRequestAs (s : String) (expectedMethod : String) (α : Type) [FromJson α] : IO (Request α) := do
def readMessage (s : String) : IO JsonRpc.Message := 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}'"
return m

def readLspRequestAs (s : String) (expectedMethod : String) (α : Type) [FromJson α] : IO (Request α) := do
let m ← readMessage s
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?}}
def initializeServer (id : RequestID) : IO Unit := do
let o ← IO.getStdout
o.writeLspResponse {
id := initRequest.id
id := id
result := {
capabilities := mkLeanServerCapabilities
serverInfo? := some {
Expand All @@ -71,17 +65,27 @@ def sendMessage (s : String) : IO Unit := do
: 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 ()

@[export game_send_message]
def sendMessage (s : String) (state : Ref ServerState) : IO Unit := do
let o ← IO.getStdout
let e ← IO.getStderr
try
let m ← readMessage s
match m with
| Message.request id "initialize" params? =>
initializeServer id
| Message.request id "info" _ =>
let some game := (gameExt.getState (← state.get).env).find? `TestGame
| throwServerError "Game not found"
let gameJson : Json := toJson game
-- Add world sizes to Json object
let worldSize := game.worlds.nodes.toList.map (fun (n, w) => (n.toString, w.levels.size))
let gameJson := gameJson.mergeObj (Json.mkObj [("worldSize", Json.mkObj worldSize)])
o.writeLspResponse ⟨id, gameJson⟩
| _ => throw $ userError s!"Expected JSON-RPC request, got: '{(toJson m).compress}'"

catch err =>
e.putStrLn s!"Server error: {err}"
return ()
1 change: 1 addition & 0 deletions server/lakefile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Lake DSL

package GameServer

@[default_target]
lean_lib GameServer

@[default_target]
Expand Down
11 changes: 8 additions & 3 deletions server/main.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#include <stdio.h>
#include <lean/lean.h>

extern lean_object* game_send_message(lean_object*, lean_object*);
extern lean_object* game_send_message(lean_object*, lean_object*, lean_object*);
extern lean_object* game_make_state(lean_object*);

// see https://leanprover.github.io/lean4/doc/dev/ffi.html#initialization
extern void lean_initialize_runtime_module();
extern void lean_initialize();
extern void lean_io_mark_end_initialization();
extern lean_object * initialize_GameServer_WasmServer(uint8_t builtin, lean_object *);

lean_object * state;
lean_object * io_world;

int main() {
lean_initialize();
lean_initialize_runtime_module();
lean_object * res;
// use same default as for Lean executables
uint8_t builtin = 1;
lean_object * io_world = lean_io_mk_world();
io_world = lean_io_mk_world();
res = initialize_GameServer_WasmServer(builtin, io_world);
if (lean_io_result_is_ok(res)) {
lean_dec_ref(res);
Expand All @@ -25,9 +29,10 @@ int main() {
return 1; // do not access Lean declarations if initialization failed
}
lean_io_mark_end_initialization();
state = lean_io_result_get_value(game_make_state(io_world));
}

void send_message(char* msg){
lean_object * s = lean_mk_string(msg);
game_send_message(s, lean_io_mk_world());
game_send_message(s, state, io_world);
}
17 changes: 15 additions & 2 deletions wasm.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/bin/bash

rm -rf server32bit
cp -r server server32bit

cd server
cd server32bit
/home/alex/Projects/lean4/build/test/stage1/bin/lake update -R
/home/alex/Projects/lean4/build/test/stage1/bin/lake build

cd ../server

lake build

Expand All @@ -11,4 +17,11 @@ LEAN_SYSROOT=/home/alex/Projects/lean4/build/release/stage1
LEAN_LIBDIR=$LEAN_SYSROOT/lib/lean

emcc -o $OUT_DIR/server.js main.c -I $LEAN_SYSROOT/include -L $LEAN_LIBDIR build/ir/GameServer/*.c -lInit -lLean -lleancpp -lleanrt \
-sFORCE_FILESYSTEM -lnodefs.js -s EXIT_RUNTIME=0 -s MAIN_MODULE=1 -s LINKABLE=1 -s EXPORT_ALL=1 -s ALLOW_MEMORY_GROWTH=1 -fwasm-exceptions -pthread -flto
-sFORCE_FILESYSTEM -lnodefs.js -s EXIT_RUNTIME=0 -s MAIN_MODULE=1 -s LINKABLE=1 -s EXPORT_ALL=1 -s ALLOW_MEMORY_GROWTH=1 -fwasm-exceptions -pthread -flto \
--preload-file "${LEAN_SYSROOT}/lib/lean/Init"@/lib/Init \
--preload-file "${LEAN_SYSROOT}/lib/lean/Init.olean"@/lib/Init.olean \
--preload-file "${LEAN_SYSROOT}/lib/lean/Init.ilean"@/lib/Init.ilean \
# --preload-file "${LEAN_SYSROOT}/lib/lean/Lean"@/lib/Lean \
# --preload-file "${LEAN_SYSROOT}/lib/lean/Lean.olean"@/lib/Lean.olean \
# --preload-file "${LEAN_SYSROOT}/lib/lean/Lean.ilean"@/lib/Lean.ilean \
# --preload-file "../server32bit/build/lib"@/gamelib

0 comments on commit 32a2ff3

Please sign in to comment.