title: Using C/C++ as scripting language
body: <p>I am trying to create an architecture that would use C/C++ as "scripting language". I already have a prototype working based on:</p>
  
  <p><a href="http://www.codeproject.com/Articles/29999/Compiling-C-code-at-runtime" rel="noreferrer">http://www.codeproject.com/Articles/29999/Compiling-C-code-at-runtime</a></p>
  
  <p>and</p>
  
  <p><a href="http://runtimecompiledcplusplus.blogspot.com" rel="noreferrer">http://runtimecompiledcplusplus.blogspot.com</a></p>
  
  <p>My prototype allows me to recompile a dynamic link library / shared object and reload it at runtime. I only have a small problem lets take in example the following code:</p>
  
  <p>[ bot.c ]</p>
  
  <pre><code>typedef struct
  {
      float health;
  
      float speed;
  
      bool  alive;
  
  } Bot;
  
  Bot bot = { .health = 100.0f,
          .speed  = 1.0f,
          .alive  = true };
  
  void OnCollision( void )
  {
      bot.health -= 10.0f;
  
      if( bot.health &lt;= 0.0f )
      { bot.alive = false; }
  }
  
  
  void OnUpdate( void )
  {
      if( bot.alive )
      {
          /* Update animation */
      }
  }
  </code></pre>
  
  <p>If the bot.c script is assigned to 1 bot its ok, but if I assign the callbacks to multiple bots they share the same data (1 get hit, they all get hit!). How can run the bot script "separately" for each bots?</p>
  
  <p>I already research on coroutine in C/C++ (using setjmp/longjmp) but its risky for C++ constructors &amp; destructors (and also force you to integrate macros inside your functions, which in my case is not an options for my users).</p>
  
  <p>I was thinking about running multiple threads, but the same problem will occur (since the data comes from a dll/so it is shared). Im not too familiar with fork/co-process but it does not seems to apply for my case. And I do not think that pipes are an option as well... Im pretty much stuck.</p>
  
  <p>Is there anyway to solve this problem?</p>
  
  <p>ps: Yes I know that scripting language like Lua or Javascript (V8) have built-in coroutine or in the case of Lua lua_thread that would fix my issue but I want to stick with a C/C++ interface for the users to code since its for gaming performance is critical!</p>