On Tue, Apr 7, 2009 at 10:28 PM, Dennis H <[email protected]> wrote:
> v8::Handle<v8::Script> script = v8::Script::Compile(jsfile);
> script->Run();
> return v8::Undefined();
> }

A small tip here: return the value of Run(), since that allows the
import to fail gracefully (e.g. by throwing an exception). However,
the implementation must (in my experience) do something really weird
if it wants to get the exception text from exceptions thrown by the
included code to pass up the call chain properly. My implementation
looks like:

    Handle<Value> IncludeScript( Arguments const & argv )
    {
        HandleScope scope;
        Handle<Value> rv = Undefined();
        Handle<Value> exc;
        for (int i = 0; i < argv.Length(); i++)
        {
            HandleScope handle_scope;
            // Man, everyone needs a different type of String value...
            std::string farg = convert::JSToStdString(argv[i]);
            std::string found = ScriptsPath().Find( farg );
            if( found.empty() )
            {
                std::ostringstream os;
                os << "Could not find file ["<<farg<<"] in ScriptsPath()!";
                return ThrowException(v8::String::New(os.str().c_str()));
            }
            Handle<String> fnameS( String::New(found.c_str()) );
            String::Utf8Value fname( fnameS );
            if( !*fname || !**fname )
            {
                return ThrowException(v8::String::New("Filename
argument is empty!"));
            }
            std::ostringstream os;
            {
                std::ifstream is( *fname );
                if( ! is.good() )
                {
                    os.str("");
                    os << "Error opening file "<<*fname<<"!";
                    return ThrowException(v8::String::New(os.str().c_str()));
                }
                is >> std::noskipws;
                std::copy( std::istream_iterator<char>(is),
                           std::istream_iterator<char>(),
                           std::ostream_iterator<char>(os) );
            }
            std::string src = os.str();
            Handle<String> jsrc( String::New( src.c_str(),
static_cast<int>( src.size() ) ) );
            do
            {
                TryCatch tryer;
                tryer.SetCaptureMessage(true);
                tryer.SetVerbose(true);
                Handle<Script> Scr( Script::Compile( jsrc,
convert::CastToJS(found) ) );
                if( Scr.IsEmpty() )
                {
                    exc = tryer.Exception();
                    break;
                }
                rv = Scr->Run();
                if( rv.IsEmpty() )
                {
                    exc = tryer.Exception();
                    break;
                }
            } while(false);
            if( ! exc.IsEmpty() )
            { // kludge to get exception thing to propagate properly
                return ThrowException(exc);
            }
        }
        return rv;
    }


-- 
----- stephan beal
http://wanderinghorse.net/home/stephan/

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to