I don't think you're going to be able to have a persistent/static global object 
that can quickly be reset between requests like MikeM is trying to do. 

>From my research, the v8 examples (lineprocessor, etc.) instantiate a global 
>object and context frequently, but do not do anything like trying to use a 
>single global object across contexts.  It's my intuitive sense that you 
>wouldn't be able to do this because of how variables must be scoped.

That said, SilkJS has a pre-fork model WWW server.  There is the issue of 
polluting the global with scripts that misbehave.  However, at server start 
time, you may well want to initialize some number of global variables 
accessible throughout all the requests.  The global req and res objects, for 
example, are instantiated when an HttpChild process is forked and reused from 
request to request.  

The res object has a data member, which is an object that can be treated like a 
hash array.  The intent is that if you need to set "global" variables for a 
request, you'd set them in res.data in one function and some function later on 
(during the same request) could examine res.data for those "globals."

SilkJS exposes a global.builtin object that is used to namespace the builtin 
C++ functions callable from JavaScript.  So global.builtin.fs is the file 
system methods, global.builtin.net is the networking methods, and so on.

Thinking on MikeM's problem, I can think of using something like this in some 
JavaScript code that initializes the global object for the first time:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty

Here you'd defineProperty() with configurable=false on all the global things 
(methods, objects, etc.) you don't want user code to delete and that you want 
initially present in a "clean" global.  Then when you want to clear the global 
object back to its initial state, you'd do a for ... in loop on the global 
object and remove anything that is deletable.

Another possibility is something like the Harmony API, which SilkJS enables.
https://github.com/mschwartz/SilkJS-Harmony

Here you can write a JavaScript "Proxy" for a real JavaScript object and 
intercept any modification attempts to the properties of that object.  If (and 
I've not tried it) you can proxy for the global object, you could forbid 
setting properties, and provide a method for the API designer to call to 
legitimately set properties.

And frankly, I've not had a problem with the global variable thing, even with a 
50,000 line server application written in JS for SilkJS.  I use two different 
editors that support "use strict" and jshint (JetBrains' WebStorm and Sublime 
Text 2 with the right plugins).  I write all my code so it looks something like:
/*global whatever,global,variables,truly,exist */
(function() {
  "use strict";
  ...
  exports = whatever;
}());

Anything that attempts to write to a global gets flagged.

Something else I discovered early on in SilkJS development is that you can call 
from JS, a C++ function that calls fork() and returns.  After the return, you 
have a parent and child process resuming in JS context.  The child process is a 
literal clone of the parent, including the context and it's global and 
everything compiled in.  So if you have a "pristine" global object set up by JS 
and call fork() for each request, you have a pristine global in the child 
process.  The cost of fork() is really expensive though - 10s of milliseconds.  
That's why SilkJS (and the state of the art of many WWW servers) pre-forks the 
child processes - you take that hit at server start time, one time.


On Jun 26, 2012, at 10:26 AM, Stephan Beal wrote:

> On Tue, Jun 26, 2012 at 7:21 PM, MikeM <mi...@reteksolutions.com> wrote:
> I'm open to any method of making this happen.
> Thanks for the help!
> 
> i unfortunately cannot say much on the topic of multiple contexts and 
> de/serialization of v8 state across requests, but i suspect that Michael 
> Schwartz (who also responded earlier) can tell us something informative on 
> the topic. He's the architect behind http://silkjs.org/, which has a larger 
> scope than the library-level code i tend to write.
> 
> -- 
> ----- stephan beal
> http://wanderinghorse.net/home/stephan/
> http://gplus.to/sgbeal
> 
> 
> -- 
> v8-users mailing list
> v8-users@googlegroups.com
> http://groups.google.com/group/v8-users

-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users

Reply via email to