title: Do any languages provide support for "true" modularity within a process?
body: <p>I've been reading <code>Principles of Computer System Design</code> (Saltzer &amp; Kaashoek), and one of the early chapters is on modularity. For example:</p>
  
  <ul>
  <li>Different modules should only interact through specified interfaces</li>
  <li>Modules should be constructed to expose as little of their internal implementation as 
  possible</li>
  </ul>
  
  <p>Now that's all pretty standard stuff, and is the way most OO languages work. However, they mention even stricter requirements, such as:</p>
  
  <ul>
  <li>A called module should not be able to lock up the caller by failing to return</li>
  <li>A called module should not be able to cause its caller to die by running out of stack space</li>
  </ul>
  
  <p>Now these requirements make perfect sense to me, and would do much to stop errors propagating through the entirety of a massive program and bring it all crashing down.</p>
  
  <p>However, the method they suggest, splitting everything up into client/server processes, seems somewhat of an overkill for many purposes. Writing <em>everything</em> you want to modularize as client/server seems to be both:</p>
  
  <ul>
  <li>incredibly tedious</li>
  <li>slow (w.r.t. execution speed)</li>
  </ul>
  
  <p>For example, I would like to be able to delegate work to my Math module, and place limits on execution time and memory use, but I don't really want to have to make a separate Math <em>server</em> running in the background just for these benefits! Furthermore, the IPC message-passing overhead (both computation and latency) is certain to be pretty huge compared to direct procedure calls.</p>
  
  <p>Are there any languages that provide this kind of modularity <em>within</em> a process? As an in between before the tight-coupling of direct procedure-calls and the overhead of a multi-process client-server design? The way I envision it, instead of :</p>
  
  <pre><code>y = Math.sin(x)
  </code></pre>
  
  <p>which leaves me open to infinite loops and stack-overflows in the <code>Math</code> module, I would like to do something like</p>
  
  <pre><code>y = try(maxMemory=1024kb, maxTime=12ms){
      Math.sin(x)
  }catch(Anything){
      0
  }
  </code></pre>
  
  <p>Which would set y to a default value (<code>0</code>) if anything fails within <code>Math.sin</code>. Invalid input, bugs, infinite loops, runtime-exceptions, anything. Does anything like this exist already in some language, and if not, why? It seems to me like something that would be immensely useful, in a wide variety of places.</p>