Hi Jeremy, the short answer: you do not need a HandleScope for func2.
Every (local) Handle you create in C++ belongs to a scope; when the scope terminates, the V8 garbage collector knows that all JS values referenced by handles created within this scope can be safely removed. There are several exceptions to this rule: - the Handle which is passed to HandleScope::Close escapes this mechanism (and is, if I am not mistaken, automatically managed by the next scope in HandleScope hierarchy); - all Handles which are somehow referenced from the JS world (by, for instance, assigning them as properties of a global object); - persistent Handles (it is their nature to persist HandleScope deletion). So, the HandleScope helps GC to detect which values can be erased, but it is definitely not necessary to declare a HandleScope in every C++ function. Ondrej 2010/12/10 Jeremy <[email protected]>: > There isn't a whole lot of documentation on v8, and I couldn't figure > out the answer to this. So maybe the mailing list can help. > > Usually if you are writing a function in C[++] that deals with > transient javascript values you do something like this: > > Local<Object> func1 () { > > HandleScope scope; > Local<Object> foo = Object::New(); > > //some stuff goes here > > return scope.Close(foo); > > } > > Declaring the scope prevents the object from being allocated in the > global JS scope (if I understand correctly) and using > HandleScope::Close will allow that value to be marshalled out of that > scope for use elsewhere so that it doesn't get garbage collected when > the variable "scope" goes out of scope (the scope goes out of scope? > Man, that is confusing!) > > My question is this: if I'm writing an axillary function (let's say a > small inline function) that I am always intending to call from another > function that has already declared a scope, do I need to declare > another scope within the aux. function? Example: > > Local<Object> func2 () { > > Local<Object> foo = Object::New(); > //do some stuff > > return foo; > > } > > Local<Object> func1 () { > > HandleScope scope; > return scope.Close( func2() ); > > } > > Should func2 define its own HandleScope and then close it before it > returns? Or will everything work out OK in this case? Or is there > altogether some other thing I should be doing? > > Thanks, > Jeremy > > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
