static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
/**
+ * Converts an object to a utf8-encoded character array. Useful if
+ * you want to print the object.
+ */
+ class EXPORT Utf8Value {
+ public:
+ explicit Utf8Value(Handle<v8::Value> obj);
+ ~Utf8Value();
+ char* operator*() { return str_; }
+ int length() { return length_; }
+ private:
+ char* str_;
+ int length_;
+ };
+
+ /**
* Converts an object to an ascii string.
* Useful if you want to print the object.
*/
explicit AsciiValue(Handle<v8::Value> obj);
~AsciiValue();
char* operator*() { return str_; }
+ int length() { return length_; }
private:
char* str_;
+ int length_;
};
/**
explicit Value(Handle<v8::Value> obj);
~Value();
uint16_t* operator*() { return str_; }
+ int length() { return length_; }
private:
uint16_t* str_;
+ int length_;
};
};
if (args.Length() < 1) return v8::Undefined();
HandleScope scope;
Handle<Value> arg = args[0];
- String::AsciiValue value(arg);
+ String::Utf8Value value(arg);
HttpRequestProcessor::Log(*value);
return v8::Undefined();
}
// Compile the script and check for errors.
Handle<Script> compiled_script = Script::Compile(script);
if (compiled_script.IsEmpty()) {
- String::AsciiValue error(try_catch.Exception());
+ String::Utf8Value error(try_catch.Exception());
Log(*error);
// The script failed to compile; bail out.
return false;
Handle<Value> result = compiled_script->Run();
if (result.IsEmpty()) {
// The TryCatch above is still in effect and will have caught the error.
- String::AsciiValue error(try_catch.Exception());
+ String::Utf8Value error(try_catch.Exception());
Log(*error);
// Running the script failed; bail out.
return false;
Handle<Value> argv[argc] = { request_obj };
Handle<Value> result = process_->Call(context_->Global(), argc, argv);
if (result.IsEmpty()) {
- String::AsciiValue error(try_catch.Exception());
+ String::Utf8Value error(try_catch.Exception());
Log(*error);
return false;
} else {
// Convert a JavaScript string to a std::string. To not bother too
// much with string encodings we just use ascii.
string ObjectToString(Local<Value> value) {
- String::AsciiValue ascii_value(value);
- return string(*ascii_value);
+ String::Utf8Value utf8_value(value);
+ return string(*utf8_value);
}
void RunShell(v8::Handle<v8::Context> context);
bool ExecuteString(v8::Handle<v8::String> source,
v8::Handle<v8::Value> name,
- bool print_result);
+ bool print_result,
+ bool report_exceptions);
v8::Handle<v8::Value> Print(const v8::Arguments& args);
v8::Handle<v8::Value> Load(const v8::Arguments& args);
v8::Handle<v8::Value> Quit(const v8::Arguments& args);
printf("Error reading '%s'\n", str);
return 1;
}
- if (!ExecuteString(source, file_name, false))
+ if (!ExecuteString(source, file_name, false, true))
return 1;
}
}
} else {
printf(" ");
}
- v8::String::AsciiValue str(args[i]);
+ v8::String::Utf8Value str(args[i]);
printf("%s", *str);
}
printf("\n");
v8::Handle<v8::Value> Load(const v8::Arguments& args) {
for (int i = 0; i < args.Length(); i++) {
v8::HandleScope handle_scope;
- v8::String::AsciiValue file(args[i]);
+ v8::String::Utf8Value file(args[i]);
v8::Handle<v8::String> source = ReadFile(*file);
if (source.IsEmpty()) {
return v8::ThrowException(v8::String::New("Error loading file"));
}
- ExecuteString(source, v8::String::New(*file), false);
+ if (!ExecuteString(source, v8::String::New(*file), false, false)) {
+ return v8::ThrowException(v8::String::New("Error executing file"));
+ }
}
return v8::Undefined();
}
char* str = fgets(buffer, kBufferSize, stdin);
if (str == NULL) break;
v8::HandleScope handle_scope;
- ExecuteString(v8::String::New(str), v8::String::New("(shell)"), true);
+ ExecuteString(v8::String::New(str),
+ v8::String::New("(shell)"),
+ true,
+ true);
}
printf("\n");
}
// Executes a string within the current v8 context.
bool ExecuteString(v8::Handle<v8::String> source,
v8::Handle<v8::Value> name,
- bool print_result) {
+ bool print_result,
+ bool report_exceptions) {
v8::HandleScope handle_scope;
v8::TryCatch try_catch;
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
if (script.IsEmpty()) {
// Print errors that happened during compilation.
- ReportException(&try_catch);
+ if (report_exceptions)
+ ReportException(&try_catch);
return false;
} else {
v8::Handle<v8::Value> result = script->Run();
if (result.IsEmpty()) {
// Print errors that happened during execution.
- ReportException(&try_catch);
+ if (report_exceptions)
+ ReportException(&try_catch);
return false;
} else {
if (print_result && !result->IsUndefined()) {
// If all went well and the result wasn't undefined then print
// the returned value.
- v8::String::AsciiValue str(result);
+ v8::String::Utf8Value str(result);
printf("%s\n", *str);
}
return true;
void ReportException(v8::TryCatch* try_catch) {
v8::HandleScope handle_scope;
- v8::String::AsciiValue exception(try_catch->Exception());
+ v8::String::Utf8Value exception(try_catch->Exception());
v8::Handle<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
printf("%s\n", *exception);
} else {
// Print (filename):(line number): (message).
- v8::String::AsciiValue filename(message->GetScriptResourceName());
+ v8::String::Utf8Value filename(message->GetScriptResourceName());
int linenum = message->GetLineNumber();
printf("%s:%i: %s\n", *filename, linenum, *exception);
// Print line of source code.
- v8::String::AsciiValue sourceline(message->GetSourceLine());
+ v8::String::Utf8Value sourceline(message->GetSourceLine());
printf("%s\n", *sourceline);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
printf("^");
}
printf("\n");
+ v8::Handle<v8::String> stack_trace = message->GetStackTrace();
+ if (!stack_trace.IsEmpty()) {
+ v8::String::Utf8Value stack_trace_str(stack_trace);
+ printf("%s\n", *stack_trace_str);
+ }
}
}
}
+String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) {
+ EnsureInitialized("v8::String::Utf8Value::Utf8Value()");
+ HandleScope scope;
+ TryCatch try_catch;
+ Handle<String> str = obj->ToString();
+ if (str.IsEmpty()) {
+ str_ = NULL;
+ length_ = 0;
+ } else {
+ length_ = str->Utf8Length();
+ str_ = i::NewArray<char>(length_ + 1);
+ str->WriteUtf8(str_);
+ }
+}
+
+
+String::Utf8Value::~Utf8Value() {
+ i::DeleteArray(str_);
+}
+
+
String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj) {
EnsureInitialized("v8::String::AsciiValue::AsciiValue()");
HandleScope scope;
+ TryCatch try_catch;
Handle<String> str = obj->ToString();
- int length = str->Length();
- str_ = i::NewArray<char>(length + 1);
- str->WriteAscii(str_);
+ if (str.IsEmpty()) {
+ str_ = NULL;
+ length_ = 0;
+ } else {
+ length_ = str->Length();
+ str_ = i::NewArray<char>(length_ + 1);
+ str->WriteAscii(str_);
+ }
}
String::Value::Value(v8::Handle<v8::Value> obj) {
EnsureInitialized("v8::String::Value::Value()");
HandleScope scope;
+ TryCatch try_catch;
Handle<String> str = obj->ToString();
- int length = str->Length();
- str_ = i::NewArray<uint16_t>(length + 1);
- str->Write(str_);
+ if (str.IsEmpty()) {
+ str_ = NULL;
+ length_ = 0;
+ } else {
+ length_ = str->Length();
+ str_ = i::NewArray<uint16_t>(length_ + 1);
+ str->Write(str_);
+ }
}
const char* value_source,
v8::Handle<v8::Value> value) {
if (!expected->Equals(value)) {
- v8::String::AsciiValue value_str(value);
- v8::String::AsciiValue expected_str(expected);
+ v8::String::Utf8Value value_str(value);
+ v8::String::Utf8Value expected_str(expected);
V8_Fatal(file, line,
"CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s",
expected_source, value_source, *expected_str, *value_str);
const char* value_source,
v8::Handle<v8::Value> value) {
if (unexpected->Equals(value)) {
- v8::String::AsciiValue value_str(value);
+ v8::String::Utf8Value value_str(value);
V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s",
unexpected_source, value_source, *value_str);
}
args[i] = elem.slice(0,20).concat("...");
}
}
+ } else if (IS_UNDEFINED(args)) {
+ args = [];
}
var e = new constructor();
if (IS_STRING(x)) return new $String(x);
if (IS_NUMBER(x)) return new $Number(x);
if (IS_BOOLEAN(x)) return new $Boolean(x);
- if (x == null) throw %MakeTypeError('null_to_object');
+ if (x == null) throw %MakeTypeError('null_to_object', []);
return x;
};
--- /dev/null
+try {
+ delete (void 0).x;
+} catch (e) {
+ print(e.toString());
+}