Saturday, October 4, 2008

gc extension

For various reasons, I exposed the gc extension provided by V8. This is a global function which allows scripts to collect garbage at any time.

Normally, scripts shouldn't need to concern themselves with this. The garbage collector should decide for itself the best time to collect the garbage; that's its job. But I need it for a couple of reasons.

First, I've been meaning to have the bootstrap script collect the garbage just before returning. This script doesn't do a ton of work, but it does some, and it seems there's no point in cloning heap objects which will only get tossed out anyway.

Second, I need a way to exercise C++ code's capability to create "weak" objects. Weak objects can have callbacks such that when the garbage collector gets around to deleting them, they can release whatever native resource they might have been asked to hold. The best way I could think of to test this was to force garbage collection.

Unfortunately, the gc extension puts an object into the global namespace which doesn't behave like other objects. I found I could copy it into another object, but I couldn't delete the original. I had wanted to move it into a namespace called something like com.google.v8, but that was not to be.

Given that I was stuck with gc in the global namespace, my scheme to populate that space with TLDs ("org", "com", etc.) had been thwarted. So now extensions live under 'extensions'. This caused a ripple effect throughout the existing code which chewed up a good chunk of the afternoon. Sucks.

From an architectural perspective, this was something I was considering anyway. With an arbitrary collection of TLDs in the global namespace, the risk of collision with a name in a user script became non-trivial. Now user scripts will be able to know to avoid a small, well-known set of names, specifically 'gc' and 'extensions'. I may even get around to putting user scripts into their own namespace so they don't have to worry about colliding with anything at all.

No comments: