Sorry about taking so long to answer this -- it slipped my mind completely.
> Ok, that was the most important part of the patch.
> (I wrote a longer response here but I'm reconsidering now.) It seems
> that HandleScope could be considered just a shortcut for managing
> handle lifetimes. Maybe I could just avoid using them from C code?
> For example, are these the same:
> 1)
> {
> v8::HandleScope hs;
> v8::Local<v8::String> str = v8::String::New("test");
> } // str is freed here
> 2)
> v8::Persistent<v8::String> str = v8::String::New("test");
> str.Dispose(); // str is freed here
No. All our operations create local handles, there is no way to escape that.
> Another option would be making some static function on HandleScope
> that is an alias for calling new, with an uglier name to discourage
> people from using it. E.g.
> class HandleScope {
> // [... as before ...]
> // Returns a new HandleScope. This is not necessary from C++ code,
> // as HandleScopes should be allocated on the stack, and is only available
> // to work with languages other than C++.
> static HandleScope* NewHeapAllocated();
> static DeleteHeapAllocated(HandleScope*);
> }
>
> A final option is that maybe it just makes sense to allow
> heap-allocated HandleScopes.
>
> You understand all this much better than me -- what do you think?
We should remove the restriction on allocation of handle scopes
through static methods -- I fear that allowing 'new' would be too
confusing -- it's just a matter of naming. Maybe the best thing would
be to rephrase this, so instead of talking about new'ing and deleting
handle scopes we call it entering and exiting or opening and closing.
And of course add some warnings that not observing strict fifo
discipline with these operations will cause insidious errors.
> PS: here's a snippet of some Haskell code that uses my bindings, with
> some helper functions elided. It uses type classes to simulate
> subtyping (e.g. "global" is an ObjectTemplate but you're allowed to
> pass it to templateSet). Dunno if there are any Haskell fans on the
> v8 team...
>
> main = do
> args <- getArgs
> let code = case args of
> [code] -> code
> [] -> "'this is a sample ' + 'string of code'"
> V8.withHandleScope $ do
> print_cb <- V8.functionTemplateNew printCallback
> global <- V8.objectTemplateNew
> V8.templateSet global "print" print_cb
> V8.withContext global $ do
> V8.withTryCatch $ \trycatch -> do
> script <- V8.scriptCompile code
> case script of
> Nothing -> printError trycatch "compile error"
> Just script -> do
> result <- V8.scriptRun script
> case result of
> Nothing -> printError trycatch "run error"
> Just result -> do
> putStr " => "
> printValue result
> putStrLn ""
Cool! Is it possible to pass haskell functions into v8 and call them
from javascript?
--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---