From 4fa677c09a7925f54d06f4363c17ad1d165211ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barra=20=C3=93=20Cath=C3=A1in?= Date: Mon, 20 Nov 2023 21:30:30 +0000 Subject: [PATCH] Update C Codebase to use modules for Scheme primitives --- source/server/main.c | 20 ++++++++++++-------- source/server/scheme-integration.c | 12 +++++++++++- source/server/scheme-integration.h | 8 ++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/source/server/main.c b/source/server/main.c index 50ca891..a7b5d5f 100644 --- a/source/server/main.c +++ b/source/server/main.c @@ -37,11 +37,6 @@ int main (int argc, char ** argv) // Initialize Scheme: scm_init_guile(); - // Start the REPL server on a UNIX socket: - scm_c_eval_string("(begin (use-modules (system repl server))" - "(if (file-exists? \"silvermud-repl\") (delete-file \"silvermud-repl\"))" - "(spawn-server (make-unix-domain-server-socket #:path \"silvermud-repl\")))"); - // Create a socket to listen for connections on: int masterSocket = socket(AF_INET, SOCK_STREAM, 0); if (masterSocket < 0) @@ -107,10 +102,19 @@ int main (int argc, char ** argv) struct PlayerList * globalPlayerList = createPlayerList(); struct OutputQueue * globalOutputQueue = createOutputQueue(); - scm_c_define_gsubr("push-output-message", 6, 0, 0, &push_output_message); - scm_c_define("*globalPlayerList*", scm_from_pointer(globalPlayerList, NULL)); - scm_c_define("*globalOutputQueue*", scm_from_pointer(globalOutputQueue, NULL)); + // Define a module for use in the REPL containing our needed primitives: + SchemeModulePointers schemePointers; + schemePointers.globalPlayerList = globalPlayerList; + schemePointers.globalOutputQueue = globalOutputQueue; + + scm_c_define_module("silvermud primitives", initialize_silvermud_primitives, &schemePointers); + scm_c_use_module("silvermud primitives"); + // Start the REPL server on a UNIX socket: + scm_c_eval_string("(begin (use-modules (system repl server))" + "(if (file-exists? \"silvermud-repl\") (delete-file \"silvermud-repl\"))" + "(spawn-server (make-unix-domain-server-socket #:path \"silvermud-repl\")))"); + // Start an output thread: pthread_t outputThread; pthread_create(&outputThread, NULL, outputThreadHandler, (void *)globalOutputQueue); diff --git a/source/server/scheme-integration.c b/source/server/scheme-integration.c index ce61391..de95aa2 100644 --- a/source/server/scheme-integration.c +++ b/source/server/scheme-integration.c @@ -8,8 +8,18 @@ #include "../messages.h" #include "output-queue.h" +#include "scheme-integration.h" -SCM scheme_get_player_by_name(SCM name, SCM queue); +void initialize_silvermud_primitives (void * gameState) +{ + SchemeModulePointers * pointers = (SchemeModulePointers *)gameState; + scm_c_define_gsubr("push-output-message", 6, 0, 0, &push_output_message); + scm_c_define("*global-player-list*", scm_from_pointer(pointers->globalPlayerList, NULL)); + scm_c_define("*global-output-queue*", scm_from_pointer(pointers->globalOutputQueue, NULL)); + scm_c_export("push-output-message", "*global-player-list*", "*global-output-queue*", NULL); +} + +//SCM scheme_get_player_by_name(SCM name, SCM queue) SCM push_output_message(SCM queue, SCM deallocate_list, SCM recepients, SCM type, SCM name, SCM content) { diff --git a/source/server/scheme-integration.h b/source/server/scheme-integration.h index 41accfc..64d396a 100644 --- a/source/server/scheme-integration.h +++ b/source/server/scheme-integration.h @@ -6,6 +6,14 @@ #ifndef SCHEME_INTEGRATION_H #define SCHEME_INTEGRATION_H +typedef struct SchemeModulePointers +{ + struct PlayerList * globalPlayerList; + struct OutputQueue * globalOutputQueue; +} SchemeModulePointers; + +void initialize_silvermud_primitives (void * gameState); + SCM scheme_get_player_by_name(SCM name); SCM push_output_message(SCM queue, SCM deallocate_list, SCM recepients, SCM type, SCM name, SCM content);