From 4e57e160fbe141a68ee2f965acb9945856c78b95 Mon Sep 17 00:00:00 2001
From: Matthew Flatt <mflatt@racket-lang.org>
Date: Wed, 30 Mar 2016 20:50:17 -0600
Subject: [PATCH] Inside: fix embedding example to use functions

In some embedding contexts, functions must be used instead of globals
for things like `scheme_current_thread` and `scheme_false`.
---
 .../scribblings/inside/embedding.scrbl        | 34 ++++++++++++-------
 .../scribblings/inside/threads.scrbl          |  9 ++++-
 .../scribblings/inside/values.scrbl           | 28 +++++++++++++++
 3 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/pkgs/racket-doc/scribblings/inside/embedding.scrbl b/pkgs/racket-doc/scribblings/inside/embedding.scrbl
index f9ace6984e..b8cef3776b 100644
--- a/pkgs/racket-doc/scribblings/inside/embedding.scrbl
+++ b/pkgs/racket-doc/scribblings/inside/embedding.scrbl
@@ -11,7 +11,8 @@
 The Racket run-time system can be embedded into a larger program.  The
 embedding process for Racket CGC or Racket 3m (see @secref[cgc-v-3m])
 is essentially the same, but the process for Racket 3m is most easily
-understood as a variant of the process for Racket CGC.
+understood as a variant of the process for Racket CGC (even though
+Racket 3m is the standard variant of Racket).
 
 @section{CGC Embedding}
 
@@ -181,6 +182,7 @@ static int run(Scheme_Env *e, int argc, char *argv[])
 {
   Scheme_Object *curout;
   int i;
+  Scheme_Thread *th;
   mz_jmp_buf * volatile save, fresh;
 
   /* Declare embedded modules in "base.c": */
@@ -191,11 +193,13 @@ static int run(Scheme_Env *e, int argc, char *argv[])
   curout = scheme_get_param(scheme_current_config(), 
                             MZCONFIG_OUTPUT_PORT);
 
+  th = scheme_get_current_thread();
+
   for (i = 1; i < argc; i++) {
-    save = scheme_current_thread->error_buf;
-    scheme_current_thread->error_buf = &fresh;
-    if (scheme_setjmp(scheme_error_buf)) {
-      scheme_current_thread->error_buf = save;
+    save = th->error_buf;
+    th->error_buf = &fresh;
+    if (scheme_setjmp(*th->error_buf)) {
+      th->error_buf = save;
       return -1; /* There was an error */
     } else {
       Scheme_Object *v, *a[2];
@@ -206,7 +210,7 @@ static int run(Scheme_Env *e, int argc, char *argv[])
       a[0] = scheme_intern_symbol("racket/base");
       a[1] = scheme_intern_symbol("read-eval-print-loop");
       scheme_apply(scheme_dynamic_require(2, a), 0, NULL);
-      scheme_current_thread->error_buf = save;
+      th->error_buf = save;
     }
   }
   return 0;
@@ -307,15 +311,17 @@ static int run(Scheme_Env *e, int argc, char *argv[])
   Scheme_Object *curout = NULL, *v = NULL, *a[2] = {NULL, NULL};
   Scheme_Config *config = NULL;
   int i;
+  Scheme_Thread *th = NULL;
   mz_jmp_buf * volatile save = NULL, fresh;
 
-  MZ_GC_DECL_REG(8);
+  MZ_GC_DECL_REG(9);
   MZ_GC_VAR_IN_REG(0, e);
   MZ_GC_VAR_IN_REG(1, curout);
   MZ_GC_VAR_IN_REG(2, save);
   MZ_GC_VAR_IN_REG(3, config);
   MZ_GC_VAR_IN_REG(4, v);
-  MZ_GC_ARRAY_VAR_IN_REG(5, a, 2);
+  MZ_GC_VAR_IN_REG(5, th);
+  MZ_GC_ARRAY_VAR_IN_REG(6, a, 2);
 
   MZ_GC_REG();
 
@@ -327,11 +333,13 @@ static int run(Scheme_Env *e, int argc, char *argv[])
   config = scheme_current_config();
   curout = scheme_get_param(config, MZCONFIG_OUTPUT_PORT);
 
+  th = scheme_get_current_thread();
+
   for (i = 1; i < argc; i++) {
-    save = scheme_current_thread->error_buf;
-    scheme_current_thread->error_buf = &fresh;
-    if (scheme_setjmp(scheme_error_buf)) {
-      scheme_current_thread->error_buf = save;
+    save = th->error_buf;
+    th->error_buf = &fresh;
+    if (scheme_setjmp(*th->error_buf)) {
+      th->error_buf = save;
       return -1; /* There was an error */
     } else {
       v = scheme_eval_string(argv[i], e);
@@ -343,7 +351,7 @@ static int run(Scheme_Env *e, int argc, char *argv[])
       a[1] = scheme_intern_symbol("read-eval-print-loop");
       v = scheme_dynamic_require(2, a);
       scheme_apply(v, 0, NULL);
-      scheme_current_thread->error_buf = save;
+      th->error_buf = save;
     }
   }
 
diff --git a/pkgs/racket-doc/scribblings/inside/threads.scrbl b/pkgs/racket-doc/scribblings/inside/threads.scrbl
index ce9cd4368a..a749b56ea6 100644
--- a/pkgs/racket-doc/scribblings/inside/threads.scrbl
+++ b/pkgs/racket-doc/scribblings/inside/threads.scrbl
@@ -9,7 +9,8 @@ Racket thread; all other threads are created through calls to
 
 Information about each internal Racket thread is kept in a
 @cppi{Scheme_Thread} structure. A pointer to the current thread's
-structure is available as @cppi{scheme_current_thread}.  A
+structure is available as @cppdef{scheme_current_thread} or
+from @cppi{scheme_get_current_thread}.  A
 @cpp{Scheme_Thread} structure includes the following fields:
 
 @itemize[
@@ -378,6 +379,12 @@ The following function @cpp{mzsleep} is an appropriate
 
 @section{Thread Functions}
 
+@function[(Scheme_Thread* scheme_get_current_thread)]{
+
+Returns the currently executing thread. The result is equivalent to
+@cppi{scheme_current_thread}, but the function form must be used in
+some embedding contexts.}
+
 @function[(Scheme_Object* scheme_thread
            [Scheme_Object* thunk])]{
 
diff --git a/pkgs/racket-doc/scribblings/inside/values.scrbl b/pkgs/racket-doc/scribblings/inside/values.scrbl
index 565acf8e6d..bf11ddd7ce 100644
--- a/pkgs/racket-doc/scribblings/inside/values.scrbl
+++ b/pkgs/racket-doc/scribblings/inside/values.scrbl
@@ -296,6 +296,9 @@ There are six global constants:
 
 ]
 
+In some embedding contexts, the function forms
+@cppi{scheme_make_null}, etc., must be used, instead.
+
 @; ----------------------------------------------------------------------
 
 @section[#:tag "im:strings"]{Strings}
@@ -325,6 +328,31 @@ For more fine-grained control over UTF-8 encoding, use the
 
 @section{Value Functions}
 
+@function[(Scheme_Object* scheme_make_null)]{
+
+Returns @cppi{scheme_null}.
+}
+
+@function[(Scheme_Object* scheme_make_eof)]{
+
+Returns @cppi{scheme_eof}.
+}
+
+@function[(Scheme_Object* scheme_make_true)]{
+
+Returns @cppi{scheme_true}.
+}
+
+@function[(Scheme_Object* scheme_make_false)]{
+
+Returns @cppi{scheme_false}.
+}
+
+@function[(Scheme_Object* scheme_make_void)]{
+
+Returns @cppi{scheme_void}.
+}
+
 @function[(Scheme_Object* scheme_make_char
            [mzchar ch])]{