public:
explicit Utf8Value(Handle<v8::Value> obj);
~Utf8Value();
- char* operator*() { return str_; }
+ char* operator*() const { return str_; }
int length() { return length_; }
private:
char* str_;
public:
explicit AsciiValue(Handle<v8::Value> obj);
~AsciiValue();
- char* operator*() { return str_; }
+ char* operator*() const { return str_; }
int length() { return length_; }
private:
char* str_;
public:
explicit Value(Handle<v8::Value> obj);
~Value();
- uint16_t* operator*() { return str_; }
+ uint16_t* operator*() const { return str_; }
int length() { return length_; }
private:
uint16_t* str_;
}
+// Extracts a C string from a V8 Utf8Value.
+const char* ToCString(const v8::String::Utf8Value& value) {
+ return *value ? *value : "<string conversion failed>";
+}
+
+
// The callback that is invoked by v8 whenever the JavaScript 'print'
// function is called. Prints its arguments on stdout separated by
// spaces and ending with a newline.
printf(" ");
}
v8::String::Utf8Value str(args[i]);
- printf("%s", *str);
+ const char* cstr = ToCString(str);
+ printf("%s", cstr);
}
printf("\n");
return v8::Undefined();
for (int i = 0; i < args.Length(); i++) {
v8::HandleScope handle_scope;
v8::String::Utf8Value file(args[i]);
+ if (*file == NULL) {
+ return v8::ThrowException(v8::String::New("Error loading file"));
+ }
v8::Handle<v8::String> source = ReadFile(*file);
if (source.IsEmpty()) {
return v8::ThrowException(v8::String::New("Error loading file"));
// If all went well and the result wasn't undefined then print
// the returned value.
v8::String::Utf8Value str(result);
- printf("%s\n", *str);
+ const char* cstr = ToCString(str);
+ printf("%s\n", cstr);
}
return true;
}
void ReportException(v8::TryCatch* try_catch) {
v8::HandleScope handle_scope;
v8::String::Utf8Value exception(try_catch->Exception());
+ const char* exception_string = ToCString(exception);
v8::Handle<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
- printf("%s\n", *exception);
+ printf("%s\n", exception_string);
} else {
// Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptResourceName());
+ const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
- printf("%s:%i: %s\n", *filename, linenum, *exception);
+ printf("%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
v8::String::Utf8Value sourceline(message->GetSourceLine());
- printf("%s\n", *sourceline);
+ const char* sourceline_string = ToCString(sourceline);
+ printf("%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
Persistent<Context> Shell::evaluation_context_;
+// Converts a V8 value to a C string.
+const char* ToCString(const v8::String::Utf8Value& value) {
+ return *value ? *value : "<string conversion failed>";
+}
+
+
// Executes a string within the current v8 context.
bool Shell::ExecuteString(Handle<String> source,
Handle<Value> name,
if (print_result && !result->IsUndefined()) {
// If all went well and the result wasn't undefined then print
// the returned value.
- String::Utf8Value str(result);
- printf("%s\n", *str);
+ v8::String::Utf8Value str(result);
+ const char* cstr = ToCString(str);
+ printf("%s\n", cstr);
}
return true;
}
} else {
printf(" ");
}
- String::Utf8Value str(args[i]);
- printf("%s", *str);
+ v8::String::Utf8Value str(args[i]);
+ const char* cstr = ToCString(str);
+ printf("%s", cstr);
}
printf("\n");
return Undefined();
for (int i = 0; i < args.Length(); i++) {
HandleScope handle_scope;
String::Utf8Value file(args[i]);
+ if (*file == NULL) {
+ return ThrowException(String::New("Error loading file"));
+ }
Handle<String> source = ReadFile(*file);
if (source.IsEmpty()) {
return ThrowException(String::New("Error loading file"));
void Shell::ReportException(v8::TryCatch* try_catch) {
HandleScope handle_scope;
- String::Utf8Value exception(try_catch->Exception());
+ v8::String::Utf8Value exception(try_catch->Exception());
+ const char* exception_string = ToCString(exception);
Handle<Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
- printf("%s\n", *exception);
+ printf("%s\n", exception_string);
} else {
// Print (filename):(line number): (message).
- String::Utf8Value filename(message->GetScriptResourceName());
+ v8::String::Utf8Value filename(message->GetScriptResourceName());
+ const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
- printf("%s:%i: %s\n", *filename, linenum, *exception);
+ printf("%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
- String::Utf8Value sourceline(message->GetSourceLine());
- printf("%s\n", *sourceline);
+ v8::String::Utf8Value sourceline(message->GetSourceLine());
+ const char* sourceline_string = ToCString(sourceline);
+ printf("%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {