Browse Source

Remove was-code-modified. This breaks hooks!

macOS
Macoy Madson 2 years ago
parent
commit
4209e60c0f
  1. 15
      doc/Tutorial_Basics.org
  2. 9
      runtime/HotReloadingCodeModifier.cake
  3. 5
      src/Evaluator.cpp
  4. 3
      src/Evaluator.hpp
  5. 6
      test/CodeModification.cake
  6. 4
      test/Tutorial_Basics.cake

15
doc/Tutorial_Basics.org

@ -343,8 +343,7 @@ We attach the compile-time function to compile-time hooks, or call from macros o
It's time to create a compile-time function which will create our runtime command look-up table.
#+BEGIN_SRC lisp
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment)
was-code-modified (& bool) &return bool)
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment) &return bool)
(return true))
(add-compile-time-hook post-references-resolved
@ -356,8 +355,7 @@ Each hook has a pre-defined signature, which is what the ~environment~ and other
From our previous note on ~post-references-resolved~ we learned that our hook can be invoked multiple times. Let's store a comptime var to prevent it from being called more than once:
#+BEGIN_SRC lisp
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment)
was-code-modified (& bool) &return bool)
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment) &return bool)
(get-or-create-comptime-var command-table-already-created bool false)
(when (deref command-table-already-created)
(return true))
@ -372,8 +370,7 @@ Our compile-time function is now hooked up and running when all references are r
Let's get our command table and make a loop to iterate over it, printing each command:
#+BEGIN_SRC lisp
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment)
was-code-modified (& bool) &return bool)
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment) &return bool)
(get-or-create-comptime-var command-table-already-created bool false)
(when (deref command-table-already-created)
(return true))
@ -442,8 +439,7 @@ We use ~token-splice-addr~ because ~command-name-string~ is a ~Token~, not a /po
Let's output the generated command data to the console to make sure it's good. Here's the full ~create-command-lookup-table~ so far:
#+BEGIN_SRC lisp
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment)
was-code-modified (& bool) &return bool)
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment) &return bool)
(get-or-create-comptime-var command-table-already-created bool false)
(when (deref command-table-already-created)
(return true))
@ -535,10 +531,9 @@ We use a /splice point/ to save a spot to insert code later. Define a splice poi
(splice-point command-lookup-table)
#+END_SRC
Finally, let's evaluate our generated code, outputting it to the splice point. We'll change ~create-command-lookup-table~ to return the result of the evaluation. We set ~was-code-modified~ to tell Cakelisp that we actually made changes that may need more processing.
Finally, let's evaluate our generated code, outputting it to the splice point. We'll change ~create-command-lookup-table~ to return the result of the evaluation.
#+BEGIN_SRC lisp
(set was-code-modified true)
(return (ClearAndEvaluateAtSplicePoint environment "command-lookup-table" command-table-tokens))
#+END_SRC

9
runtime/HotReloadingCodeModifier.cake

@ -56,7 +56,6 @@
;; (would also need to keep track of modified references...yikes)
;; TODO: Need to take scope into account before changing a symbol (was it actually a module-private var)
(defun-comptime make-code-hot-reloadable (environment (& EvaluatorEnvironment)
was-code-modified (& bool)
&return bool)
(var verbose bool false)
@ -243,7 +242,7 @@
(unless (ReplaceAndEvaluateDefinition environment (call-on c_str (field var-name contents))
(deref new-var-tokens))
(return false))
(set was-code-modified true)
(scope ;; Evaluate initializer
(unless module
(return false))
@ -308,8 +307,7 @@
;; Replace it!
(unless (ReplaceAndEvaluateDefinition environment (call-on c_str (field def-to-modify name))
(deref new-definition))
(return false))
(set was-code-modified true))
(return false)))
;; Create global initializer function to initialize all pointers on load/reload
;; Import all modules so that their initializers are exposed
@ -343,8 +341,7 @@
(unless (ReplaceAndEvaluateDefinition environment
"hot-reload-initialize-state" (deref new-initializer-def))
(return false))
(set was-code-modified true))
(return false)))
(return true))

5
src/Evaluator.cpp

@ -27,7 +27,7 @@ const char* g_environmentPreLinkHookSignature =
"('manager (& ModuleManager) 'link-command (& ProcessCommand) 'link-time-inputs (* "
"ProcessCommandInput) 'num-link-time-inputs int &return bool)";
const char* g_environmentPostReferencesResolvedHookSignature =
"('environment (& EvaluatorEnvironment) 'was-code-modified (& bool) &return bool)";
"('environment (& EvaluatorEnvironment) &return bool)";
static const char* g_environmentCompileTimeVariableDestroySignature = "('data (* void))";
@ -1723,8 +1723,7 @@ bool EvaluateResolveReferences(EvaluatorEnvironment& environment)
// resolved, so we need to repeat the whole process until no more changes are made
for (const CompileTimeHook& hook : environment.postReferencesResolvedHooks)
{
bool codeModifiedByHook = false;
if (!((PostReferencesResolvedHook)hook.function)(environment, codeModifiedByHook))
if (!((PostReferencesResolvedHook)hook.function)(environment))
{
Log("error: hook returned failure\n");
numBuildResolveErrors += 1;

3
src/Evaluator.hpp

@ -251,8 +251,7 @@ extern const char* g_environmentPreLinkHookSignature;
typedef bool (*PreLinkHook)(ModuleManager& manager, ProcessCommand& linkCommand,
ProcessCommandInput* linkTimeInputs, int numLinkTimeInputs);
extern const char* g_environmentPostReferencesResolvedHookSignature;
typedef bool (*PostReferencesResolvedHook)(EvaluatorEnvironment& environment,
bool& wasCodeModifiedOut);
typedef bool (*PostReferencesResolvedHook)(EvaluatorEnvironment& environment);
struct CompileTimeHook
{

6
test/CodeModification.cake

@ -42,8 +42,7 @@
(return true))
(defun-comptime sabotage-main-printfs (environment (& EvaluatorEnvironment)
was-code-modified (& bool)
&return bool)
&return bool)
(get-or-create-comptime-var test-var std::string)
(fprintf stderr "%s is the message\n" (call-on-ptr c_str test-var))
(var old-definition-tags (<> std::vector std::string))
@ -94,8 +93,7 @@
;; Definition references invalid after this!
(unless (ReplaceAndEvaluateDefinition environment "main" (deref modified-main-tokens))
(return false))
(set was-code-modified true))
(return false)))
;; Find the new (replacement) definition and add a tag saying it is done replacement
;; Note that I also push the tags of the old definition

4
test/Tutorial_Basics.cake

@ -21,8 +21,7 @@
(defcommand say-your-name ()
(fprintf stderr "your name.\n"))
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment)
was-code-modified (& bool) &return bool)
(defun-comptime create-command-lookup-table (environment (& EvaluatorEnvironment) &return bool)
(get-or-create-comptime-var command-table-already-created bool false)
(when (deref command-table-already-created)
(return true))
@ -53,7 +52,6 @@
(array (token-splice-array (deref command-data)))))
(prettyPrintTokens (deref command-table-tokens))
(set was-code-modified true)
(return (ClearAndEvaluateAtSplicePoint environment "command-lookup-table" command-table-tokens)))
(add-compile-time-hook post-references-resolved

Loading…
Cancel
Save