Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Unified Diff: samples/shell.cc

Issue 1219133004: Remove usage of to-be-deprecated APIs from samples (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « samples/samples.gyp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/shell.cc
diff --git a/samples/shell.cc b/samples/shell.cc
index c537fa539cf1c38fc0237d5be1d5483f89386c73..3a743d6c13bebe4e919bb69f01146fb3568363ab 100644
--- a/samples/shell.cc
+++ b/samples/shell.cc
@@ -44,20 +44,18 @@
*/
-v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate);
-void RunShell(v8::Handle<v8::Context> context);
+v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate);
+void RunShell(v8::Local<v8::Context> context);
int RunMain(v8::Isolate* isolate, int argc, char* argv[]);
-bool ExecuteString(v8::Isolate* isolate,
- v8::Handle<v8::String> source,
- v8::Handle<v8::Value> name,
- bool print_result,
+bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
+ v8::Local<v8::Value> name, bool print_result,
bool report_exceptions);
void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
+v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
@@ -90,7 +88,7 @@ int main(int argc, char* argv[]) {
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
- v8::Handle<v8::Context> context = CreateShellContext(isolate);
+ v8::Local<v8::Context> context = CreateShellContext(isolate);
if (context.IsEmpty()) {
fprintf(stderr, "Error creating context\n");
return 1;
@@ -115,24 +113,31 @@ const char* ToCString(const v8::String::Utf8Value& value) {
// Creates a new execution environment containing the built-in
// functions.
-v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) {
+v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
// Create a template for the global object.
- v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
+ v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
// Bind the global 'print' function to the C++ Print callback.
- global->Set(v8::String::NewFromUtf8(isolate, "print"),
- v8::FunctionTemplate::New(isolate, Print));
+ global->Set(
+ v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal)
+ .ToLocalChecked(),
+ v8::FunctionTemplate::New(isolate, Print));
// Bind the global 'read' function to the C++ Read callback.
- global->Set(v8::String::NewFromUtf8(isolate, "read"),
+ global->Set(v8::String::NewFromUtf8(
+ isolate, "read", v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(isolate, Read));
// Bind the global 'load' function to the C++ Load callback.
- global->Set(v8::String::NewFromUtf8(isolate, "load"),
+ global->Set(v8::String::NewFromUtf8(
+ isolate, "load", v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(isolate, Load));
// Bind the 'quit' function
- global->Set(v8::String::NewFromUtf8(isolate, "quit"),
+ global->Set(v8::String::NewFromUtf8(
+ isolate, "quit", v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(isolate, Quit));
// Bind the 'version' function
- global->Set(v8::String::NewFromUtf8(isolate, "version"),
- v8::FunctionTemplate::New(isolate, Version));
+ global->Set(
+ v8::String::NewFromUtf8(isolate, "version", v8::NewStringType::kNormal)
+ .ToLocalChecked(),
+ v8::FunctionTemplate::New(isolate, Version));
return v8::Context::New(isolate, NULL, global);
}
@@ -165,19 +170,22 @@ void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
v8::String::Utf8Value file(args[0]);
if (*file == NULL) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
- v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
- if (source.IsEmpty()) {
+ v8::Local<v8::String> source;
+ if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
args.GetReturnValue().Set(source);
@@ -193,22 +201,21 @@ void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::String::Utf8Value file(args[i]);
if (*file == NULL) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
- v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
- if (source.IsEmpty()) {
+ v8::Local<v8::String> source;
+ if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
- if (!ExecuteString(args.GetIsolate(),
- source,
- v8::String::NewFromUtf8(args.GetIsolate(), *file),
- false,
- false)) {
+ if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
}
@@ -220,7 +227,8 @@ void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
// If not arguments are given args[0] will yield undefined which
// converts to the integer value 0.
- int exit_code = args[0]->Int32Value();
+ int exit_code =
+ args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromMaybe(0);
fflush(stdout);
fflush(stderr);
exit(exit_code);
@@ -229,14 +237,15 @@ void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(
- v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion()));
+ v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion(),
+ v8::NewStringType::kNormal).ToLocalChecked());
}
// Reads a file into a v8 string.
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
+v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
FILE* file = fopen(name, "rb");
- if (file == NULL) return v8::Handle<v8::String>();
+ if (file == NULL) return v8::MaybeLocal<v8::String>();
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
@@ -248,12 +257,12 @@ v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
i += fread(&chars[i], 1, size - i, file);
if (ferror(file)) {
fclose(file);
- return v8::Handle<v8::String>();
+ return v8::MaybeLocal<v8::String>();
}
}
fclose(file);
- v8::Handle<v8::String> result = v8::String::NewFromUtf8(
- isolate, chars, v8::String::kNormalString, static_cast<int>(size));
+ v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8(
+ isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size));
delete[] chars;
return result;
}
@@ -274,16 +283,23 @@ int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
"Warning: unknown flag %s.\nTry --help for options\n", str);
} else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
// Execute argument given to -e option directly.
- v8::Handle<v8::String> file_name =
- v8::String::NewFromUtf8(isolate, "unnamed");
- v8::Handle<v8::String> source =
- v8::String::NewFromUtf8(isolate, argv[++i]);
+ v8::Local<v8::String> file_name =
+ v8::String::NewFromUtf8(isolate, "unnamed",
+ v8::NewStringType::kNormal).ToLocalChecked();
+ v8::Local<v8::String> source;
+ if (!v8::String::NewFromUtf8(isolate, argv[++i],
+ v8::NewStringType::kNormal)
+ .ToLocal(&source)) {
+ return 1;
+ }
if (!ExecuteString(isolate, source, file_name, false, true)) return 1;
} else {
// Use all other arguments as names of files to load and run.
- v8::Handle<v8::String> file_name = v8::String::NewFromUtf8(isolate, str);
- v8::Handle<v8::String> source = ReadFile(isolate, str);
- if (source.IsEmpty()) {
+ v8::Local<v8::String> file_name =
+ v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal)
+ .ToLocalChecked();
+ v8::Local<v8::String> source;
+ if (!ReadFile(isolate, str).ToLocal(&source)) {
fprintf(stderr, "Error reading '%s'\n", str);
continue;
}
@@ -295,47 +311,47 @@ int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
// The read-eval-execute loop of the shell.
-void RunShell(v8::Handle<v8::Context> context) {
+void RunShell(v8::Local<v8::Context> context) {
fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion());
static const int kBufferSize = 256;
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Local<v8::String> name(
- v8::String::NewFromUtf8(context->GetIsolate(), "(shell)"));
+ v8::String::NewFromUtf8(context->GetIsolate(), "(shell)",
+ v8::NewStringType::kNormal).ToLocalChecked());
while (true) {
char buffer[kBufferSize];
fprintf(stderr, "> ");
char* str = fgets(buffer, kBufferSize, stdin);
if (str == NULL) break;
v8::HandleScope handle_scope(context->GetIsolate());
- ExecuteString(context->GetIsolate(),
- v8::String::NewFromUtf8(context->GetIsolate(), str),
- name,
- true,
- true);
+ ExecuteString(
+ context->GetIsolate(),
+ v8::String::NewFromUtf8(context->GetIsolate(), str,
+ v8::NewStringType::kNormal).ToLocalChecked(),
+ name, true, true);
}
fprintf(stderr, "\n");
}
// Executes a string within the current v8 context.
-bool ExecuteString(v8::Isolate* isolate,
- v8::Handle<v8::String> source,
- v8::Handle<v8::Value> name,
- bool print_result,
+bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
+ v8::Local<v8::Value> name, bool print_result,
bool report_exceptions) {
v8::HandleScope handle_scope(isolate);
v8::TryCatch try_catch(isolate);
v8::ScriptOrigin origin(name);
- v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin);
- if (script.IsEmpty()) {
+ v8::Local<v8::Context> context(isolate->GetCurrentContext());
+ v8::Local<v8::Script> script;
+ if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) {
// Print errors that happened during compilation.
if (report_exceptions)
ReportException(isolate, &try_catch);
return false;
} else {
- v8::Handle<v8::Value> result = script->Run();
- if (result.IsEmpty()) {
+ v8::Local<v8::Value> result;
+ if (!script->Run(context).ToLocal(&result)) {
assert(try_catch.HasCaught());
// Print errors that happened during execution.
if (report_exceptions)
@@ -360,7 +376,7 @@ void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
v8::HandleScope handle_scope(isolate);
v8::String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
- v8::Handle<v8::Message> message = try_catch->Message();
+ v8::Local<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
@@ -368,24 +384,27 @@ void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
} else {
// Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
+ v8::Local<v8::Context> context(isolate->GetCurrentContext());
const char* filename_string = ToCString(filename);
- int linenum = message->GetLineNumber();
+ int linenum = message->GetLineNumber(context).FromJust();
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
- v8::String::Utf8Value sourceline(message->GetSourceLine());
+ v8::String::Utf8Value sourceline(
+ message->GetSourceLine(context).ToLocalChecked());
const char* sourceline_string = ToCString(sourceline);
fprintf(stderr, "%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
- int start = message->GetStartColumn();
+ int start = message->GetStartColumn(context).FromJust();
for (int i = 0; i < start; i++) {
fprintf(stderr, " ");
}
- int end = message->GetEndColumn();
+ int end = message->GetEndColumn(context).FromJust();
for (int i = start; i < end; i++) {
fprintf(stderr, "^");
}
fprintf(stderr, "\n");
- v8::String::Utf8Value stack_trace(try_catch->StackTrace());
+ v8::String::Utf8Value stack_trace(
+ try_catch->StackTrace(context).ToLocalChecked());
if (stack_trace.length() > 0) {
const char* stack_trace_string = ToCString(stack_trace);
fprintf(stderr, "%s\n", stack_trace_string);
« no previous file with comments | « samples/samples.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698