Subject: | Possibly better compile exception handling |
Hello,
many thanks for the recent updates !
I think compile exception handling could be improved by applying the attached
patch.
It basically catches compile error(s) as early as possible and return "undef" to Perl.
In my precise case, calling "script.isEmpty()" if source script is invalid leads to a C++
exception which is not catchable using Perl's "eval"
(I'm using V8 3.1.1 on OSX 10.6.6 with Perl 5.10 from MacPorts)
Could you please take a look at it ?
Many thanks !
--
Rémy Chibois
Subject: | patch-Compile-Catch.diff |
diff -Nur JavaScript-V8-0.03.orig/V8Context.cpp JavaScript-V8-0.03/V8Context.cpp
--- JavaScript-V8-0.03.orig/V8Context.cpp 2011-02-15 22:16:44.000000000 +0100
+++ JavaScript-V8-0.03/V8Context.cpp 2011-02-18 17:22:13.000000000 +0100
@@ -131,25 +131,27 @@
}
SV* V8Context::eval(const char* source) {
HandleScope handle_scope;
- Context::Scope context_scope(context);
TryCatch try_catch;
+ Context::Scope context_scope(context);
Handle<Script> script = Script::Compile(String::New(source));
- if(script.IsEmpty()) {
- String::Utf8Value error(try_catch.Exception());
- sv_setsv(ERRSV, sv_2mortal(newSVpvn(*error, error.length())));
- return &PL_sv_undef;
- }
-
- Handle<Value> val = script->Run();
- if (val.IsEmpty()) {
+
+ if (try_catch.HasCaught()) {
Handle<Value> exception = try_catch.Exception();
String::AsciiValue exception_str(exception);
-
- sv_setsv(ERRSV, sv_2mortal(newSVpvn(*exception_str, exception_str.length())));
+ sv_setpv(ERRSV,*exception_str);
return &PL_sv_undef;
} else {
- sv_setsv(ERRSV, &PL_sv_undef);
- return _convert_v8value_to_sv(val);
+ Handle<Value> val = script->Run();
+
+ if (val.IsEmpty()) {
+ Handle<Value> exception = try_catch.Exception();
+ String::AsciiValue exception_str(exception);
+ sv_setpv(ERRSV,*exception_str);
+ return &PL_sv_undef;
+ } else {
+ sv_setsv(ERRSV,&PL_sv_undef);
+ return _convert_v8value_to_sv(val);
+ }
}
}