Remove usage of to-be-deprecated APIs from samples
BUG=v8:4134 R=vogelheim@chromium.org LOG=n Review URL: https://codereview.chromium.org/1219133004 Cr-Commit-Position: refs/heads/master@{#29452}
This commit is contained in:
parent
0ecd9e1bd2
commit
ebadb68f02
@ -47,13 +47,15 @@ int main(int argc, char* argv[]) {
|
||||
Context::Scope context_scope(context);
|
||||
|
||||
// Create a string containing the JavaScript source code.
|
||||
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
|
||||
Local<String> source =
|
||||
String::NewFromUtf8(isolate, "'Hello' + ', World!'",
|
||||
NewStringType::kNormal).ToLocalChecked();
|
||||
|
||||
// Compile the source code.
|
||||
Local<Script> script = Script::Compile(source);
|
||||
Local<Script> script = Script::Compile(context, source).ToLocalChecked();
|
||||
|
||||
// Run the script to get the result.
|
||||
Local<Value> result = script->Run();
|
||||
Local<Value> result = script->Run(context).ToLocalChecked();
|
||||
|
||||
// Convert the result to an UTF8 string and print it.
|
||||
String::Utf8Value utf8(result);
|
||||
|
@ -93,7 +93,7 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
|
||||
public:
|
||||
// Creates a new processor that processes requests by invoking the
|
||||
// Process function of the JavaScript script given as an argument.
|
||||
JsHttpRequestProcessor(Isolate* isolate, Handle<String> script)
|
||||
JsHttpRequestProcessor(Isolate* isolate, Local<String> script)
|
||||
: isolate_(isolate), script_(script) {}
|
||||
virtual ~JsHttpRequestProcessor();
|
||||
|
||||
@ -104,7 +104,7 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
|
||||
private:
|
||||
// Execute the script associated with this processor and extract the
|
||||
// Process function. Returns true if this succeeded, otherwise false.
|
||||
bool ExecuteScript(Handle<String> script);
|
||||
bool ExecuteScript(Local<String> script);
|
||||
|
||||
// Wrap the options and output map in a JavaScript objects and
|
||||
// install it in the global namespace as 'options' and 'output'.
|
||||
@ -112,8 +112,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
|
||||
|
||||
// Constructs the template that describes the JavaScript wrapper
|
||||
// type for requests.
|
||||
static Handle<ObjectTemplate> MakeRequestTemplate(Isolate* isolate);
|
||||
static Handle<ObjectTemplate> MakeMapTemplate(Isolate* isolate);
|
||||
static Local<ObjectTemplate> MakeRequestTemplate(Isolate* isolate);
|
||||
static Local<ObjectTemplate> MakeMapTemplate(Isolate* isolate);
|
||||
|
||||
// Callbacks that access the individual fields of request objects.
|
||||
static void GetPath(Local<String> name,
|
||||
@ -132,19 +132,19 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
|
||||
|
||||
// Utility methods for wrapping C++ objects as JavaScript objects,
|
||||
// and going back again.
|
||||
Handle<Object> WrapMap(map<string, string>* obj);
|
||||
static map<string, string>* UnwrapMap(Handle<Object> obj);
|
||||
Handle<Object> WrapRequest(HttpRequest* obj);
|
||||
static HttpRequest* UnwrapRequest(Handle<Object> obj);
|
||||
Local<Object> WrapMap(map<string, string>* obj);
|
||||
static map<string, string>* UnwrapMap(Local<Object> obj);
|
||||
Local<Object> WrapRequest(HttpRequest* obj);
|
||||
static HttpRequest* UnwrapRequest(Local<Object> obj);
|
||||
|
||||
Isolate* GetIsolate() { return isolate_; }
|
||||
|
||||
Isolate* isolate_;
|
||||
Handle<String> script_;
|
||||
Persistent<Context> context_;
|
||||
Persistent<Function> process_;
|
||||
static Persistent<ObjectTemplate> request_template_;
|
||||
static Persistent<ObjectTemplate> map_template_;
|
||||
Local<String> script_;
|
||||
Global<Context> context_;
|
||||
Global<Function> process_;
|
||||
static Global<ObjectTemplate> request_template_;
|
||||
static Global<ObjectTemplate> map_template_;
|
||||
};
|
||||
|
||||
|
||||
@ -156,7 +156,7 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
|
||||
static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
if (args.Length() < 1) return;
|
||||
HandleScope scope(args.GetIsolate());
|
||||
Handle<Value> arg = args[0];
|
||||
Local<Value> arg = args[0];
|
||||
String::Utf8Value value(arg);
|
||||
HttpRequestProcessor::Log(*value);
|
||||
}
|
||||
@ -170,8 +170,9 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
|
||||
|
||||
// Create a template for the global object where we set the
|
||||
// built-in global functions.
|
||||
Handle<ObjectTemplate> global = ObjectTemplate::New(GetIsolate());
|
||||
global->Set(String::NewFromUtf8(GetIsolate(), "log"),
|
||||
Local<ObjectTemplate> global = ObjectTemplate::New(GetIsolate());
|
||||
global->Set(String::NewFromUtf8(GetIsolate(), "log", NewStringType::kNormal)
|
||||
.ToLocalChecked(),
|
||||
FunctionTemplate::New(GetIsolate(), LogCallback));
|
||||
|
||||
// Each processor gets its own context so different processors don't
|
||||
@ -179,7 +180,7 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
|
||||
// is what we need for the reference to remain after we return from
|
||||
// this method. That persistent handle has to be disposed in the
|
||||
// destructor.
|
||||
v8::Handle<v8::Context> context = Context::New(GetIsolate(), NULL, global);
|
||||
v8::Local<v8::Context> context = Context::New(GetIsolate(), NULL, global);
|
||||
context_.Reset(GetIsolate(), context);
|
||||
|
||||
// Enter the new context so all the following operations take place
|
||||
@ -196,17 +197,21 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
|
||||
|
||||
// The script compiled and ran correctly. Now we fetch out the
|
||||
// Process function from the global object.
|
||||
Handle<String> process_name = String::NewFromUtf8(GetIsolate(), "Process");
|
||||
Handle<Value> process_val = context->Global()->Get(process_name);
|
||||
|
||||
Local<String> process_name =
|
||||
String::NewFromUtf8(GetIsolate(), "Process", NewStringType::kNormal)
|
||||
.ToLocalChecked();
|
||||
Local<Value> process_val;
|
||||
// If there is no Process function, or if it is not a function,
|
||||
// bail out
|
||||
if (!process_val->IsFunction()) return false;
|
||||
if (!context->Global()->Get(context, process_name).ToLocal(&process_val) ||
|
||||
!process_val->IsFunction()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// It is a function; cast it to a Function
|
||||
Handle<Function> process_fun = Handle<Function>::Cast(process_val);
|
||||
Local<Function> process_fun = Local<Function>::Cast(process_val);
|
||||
|
||||
// Store the function in a Persistent handle, since we also want
|
||||
// Store the function in a Global handle, since we also want
|
||||
// that to remain after this call returns
|
||||
process_.Reset(GetIsolate(), process_fun);
|
||||
|
||||
@ -215,16 +220,18 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
|
||||
}
|
||||
|
||||
|
||||
bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) {
|
||||
bool JsHttpRequestProcessor::ExecuteScript(Local<String> script) {
|
||||
HandleScope handle_scope(GetIsolate());
|
||||
|
||||
// We're just about to compile the script; set up an error handler to
|
||||
// catch any exceptions the script might throw.
|
||||
TryCatch try_catch(GetIsolate());
|
||||
|
||||
Local<Context> context(GetIsolate()->GetCurrentContext());
|
||||
|
||||
// Compile the script and check for errors.
|
||||
Handle<Script> compiled_script = Script::Compile(script);
|
||||
if (compiled_script.IsEmpty()) {
|
||||
Local<Script> compiled_script;
|
||||
if (!Script::Compile(context, script).ToLocal(&compiled_script)) {
|
||||
String::Utf8Value error(try_catch.Exception());
|
||||
Log(*error);
|
||||
// The script failed to compile; bail out.
|
||||
@ -232,8 +239,8 @@ bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) {
|
||||
}
|
||||
|
||||
// Run the script!
|
||||
Handle<Value> result = compiled_script->Run();
|
||||
if (result.IsEmpty()) {
|
||||
Local<Value> result;
|
||||
if (!compiled_script->Run(context).ToLocal(&result)) {
|
||||
// The TryCatch above is still in effect and will have caught the error.
|
||||
String::Utf8Value error(try_catch.Exception());
|
||||
Log(*error);
|
||||
@ -249,18 +256,26 @@ bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts,
|
||||
HandleScope handle_scope(GetIsolate());
|
||||
|
||||
// Wrap the map object in a JavaScript wrapper
|
||||
Handle<Object> opts_obj = WrapMap(opts);
|
||||
Local<Object> opts_obj = WrapMap(opts);
|
||||
|
||||
v8::Local<v8::Context> context =
|
||||
v8::Local<v8::Context>::New(GetIsolate(), context_);
|
||||
|
||||
// Set the options object as a property on the global object.
|
||||
context->Global()->Set(String::NewFromUtf8(GetIsolate(), "options"),
|
||||
opts_obj);
|
||||
context->Global()
|
||||
->Set(context,
|
||||
String::NewFromUtf8(GetIsolate(), "options", NewStringType::kNormal)
|
||||
.ToLocalChecked(),
|
||||
opts_obj)
|
||||
.FromJust();
|
||||
|
||||
Handle<Object> output_obj = WrapMap(output);
|
||||
context->Global()->Set(String::NewFromUtf8(GetIsolate(), "output"),
|
||||
output_obj);
|
||||
Local<Object> output_obj = WrapMap(output);
|
||||
context->Global()
|
||||
->Set(context,
|
||||
String::NewFromUtf8(GetIsolate(), "output", NewStringType::kNormal)
|
||||
.ToLocalChecked(),
|
||||
output_obj)
|
||||
.FromJust();
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -278,7 +293,7 @@ bool JsHttpRequestProcessor::Process(HttpRequest* request) {
|
||||
Context::Scope context_scope(context);
|
||||
|
||||
// Wrap the C++ request object in a JavaScript wrapper
|
||||
Handle<Object> request_obj = WrapRequest(request);
|
||||
Local<Object> request_obj = WrapRequest(request);
|
||||
|
||||
// Set up an exception handler before calling the Process function
|
||||
TryCatch try_catch(GetIsolate());
|
||||
@ -286,11 +301,11 @@ bool JsHttpRequestProcessor::Process(HttpRequest* request) {
|
||||
// Invoke the process function, giving the global object as 'this'
|
||||
// and one argument, the request.
|
||||
const int argc = 1;
|
||||
Handle<Value> argv[argc] = { request_obj };
|
||||
Local<Value> argv[argc] = {request_obj};
|
||||
v8::Local<v8::Function> process =
|
||||
v8::Local<v8::Function>::New(GetIsolate(), process_);
|
||||
Handle<Value> result = process->Call(context->Global(), argc, argv);
|
||||
if (result.IsEmpty()) {
|
||||
Local<Value> result;
|
||||
if (!process->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
|
||||
String::Utf8Value error(try_catch.Exception());
|
||||
Log(*error);
|
||||
return false;
|
||||
@ -309,8 +324,8 @@ JsHttpRequestProcessor::~JsHttpRequestProcessor() {
|
||||
}
|
||||
|
||||
|
||||
Persistent<ObjectTemplate> JsHttpRequestProcessor::request_template_;
|
||||
Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_;
|
||||
Global<ObjectTemplate> JsHttpRequestProcessor::request_template_;
|
||||
Global<ObjectTemplate> JsHttpRequestProcessor::map_template_;
|
||||
|
||||
|
||||
// -----------------------------------
|
||||
@ -319,25 +334,26 @@ Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_;
|
||||
|
||||
// Utility function that wraps a C++ http request object in a
|
||||
// JavaScript object.
|
||||
Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
|
||||
// Handle scope for temporary handles.
|
||||
Local<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
|
||||
// Local scope for temporary handles.
|
||||
EscapableHandleScope handle_scope(GetIsolate());
|
||||
|
||||
// Fetch the template for creating JavaScript map wrappers.
|
||||
// It only has to be created once, which we do on demand.
|
||||
if (map_template_.IsEmpty()) {
|
||||
Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate());
|
||||
Local<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate());
|
||||
map_template_.Reset(GetIsolate(), raw_template);
|
||||
}
|
||||
Handle<ObjectTemplate> templ =
|
||||
Local<ObjectTemplate> templ =
|
||||
Local<ObjectTemplate>::New(GetIsolate(), map_template_);
|
||||
|
||||
// Create an empty map wrapper.
|
||||
Local<Object> result = templ->NewInstance();
|
||||
Local<Object> result =
|
||||
templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
|
||||
|
||||
// Wrap the raw C++ pointer in an External so it can be referenced
|
||||
// from within JavaScript.
|
||||
Handle<External> map_ptr = External::New(GetIsolate(), obj);
|
||||
Local<External> map_ptr = External::New(GetIsolate(), obj);
|
||||
|
||||
// Store the map pointer in the JavaScript wrapper.
|
||||
result->SetInternalField(0, map_ptr);
|
||||
@ -352,8 +368,8 @@ Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
|
||||
|
||||
// Utility function that extracts the C++ map pointer from a wrapper
|
||||
// object.
|
||||
map<string, string>* JsHttpRequestProcessor::UnwrapMap(Handle<Object> obj) {
|
||||
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
|
||||
map<string, string>* JsHttpRequestProcessor::UnwrapMap(Local<Object> obj) {
|
||||
Local<External> field = Local<External>::Cast(obj->GetInternalField(0));
|
||||
void* ptr = field->Value();
|
||||
return static_cast<map<string, string>*>(ptr);
|
||||
}
|
||||
@ -385,9 +401,10 @@ void JsHttpRequestProcessor::MapGet(Local<Name> name,
|
||||
|
||||
// Otherwise fetch the value and wrap it in a JavaScript string
|
||||
const string& value = (*iter).second;
|
||||
info.GetReturnValue().Set(String::NewFromUtf8(
|
||||
info.GetIsolate(), value.c_str(), String::kNormalString,
|
||||
static_cast<int>(value.length())));
|
||||
info.GetReturnValue().Set(
|
||||
String::NewFromUtf8(info.GetIsolate(), value.c_str(),
|
||||
NewStringType::kNormal,
|
||||
static_cast<int>(value.length())).ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@ -410,7 +427,7 @@ void JsHttpRequestProcessor::MapSet(Local<Name> name, Local<Value> value_obj,
|
||||
}
|
||||
|
||||
|
||||
Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
|
||||
Local<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
|
||||
Isolate* isolate) {
|
||||
EscapableHandleScope handle_scope(isolate);
|
||||
|
||||
@ -431,25 +448,26 @@ Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
|
||||
* Utility function that wraps a C++ http request object in a
|
||||
* JavaScript object.
|
||||
*/
|
||||
Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
|
||||
// Handle scope for temporary handles.
|
||||
Local<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
|
||||
// Local scope for temporary handles.
|
||||
EscapableHandleScope handle_scope(GetIsolate());
|
||||
|
||||
// Fetch the template for creating JavaScript http request wrappers.
|
||||
// It only has to be created once, which we do on demand.
|
||||
if (request_template_.IsEmpty()) {
|
||||
Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate());
|
||||
Local<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate());
|
||||
request_template_.Reset(GetIsolate(), raw_template);
|
||||
}
|
||||
Handle<ObjectTemplate> templ =
|
||||
Local<ObjectTemplate> templ =
|
||||
Local<ObjectTemplate>::New(GetIsolate(), request_template_);
|
||||
|
||||
// Create an empty http request wrapper.
|
||||
Local<Object> result = templ->NewInstance();
|
||||
Local<Object> result =
|
||||
templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
|
||||
|
||||
// Wrap the raw C++ pointer in an External so it can be referenced
|
||||
// from within JavaScript.
|
||||
Handle<External> request_ptr = External::New(GetIsolate(), request);
|
||||
Local<External> request_ptr = External::New(GetIsolate(), request);
|
||||
|
||||
// Store the request pointer in the JavaScript wrapper.
|
||||
result->SetInternalField(0, request_ptr);
|
||||
@ -466,8 +484,8 @@ Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
|
||||
* Utility function that extracts the C++ http request object from a
|
||||
* wrapper object.
|
||||
*/
|
||||
HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Handle<Object> obj) {
|
||||
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
|
||||
HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Local<Object> obj) {
|
||||
Local<External> field = Local<External>::Cast(obj->GetInternalField(0));
|
||||
void* ptr = field->Value();
|
||||
return static_cast<HttpRequest*>(ptr);
|
||||
}
|
||||
@ -482,9 +500,10 @@ void JsHttpRequestProcessor::GetPath(Local<String> name,
|
||||
const string& path = request->Path();
|
||||
|
||||
// Wrap the result in a JavaScript string and return it.
|
||||
info.GetReturnValue().Set(String::NewFromUtf8(
|
||||
info.GetIsolate(), path.c_str(), String::kNormalString,
|
||||
static_cast<int>(path.length())));
|
||||
info.GetReturnValue().Set(
|
||||
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
||||
NewStringType::kNormal,
|
||||
static_cast<int>(path.length())).ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@ -493,9 +512,10 @@ void JsHttpRequestProcessor::GetReferrer(
|
||||
const PropertyCallbackInfo<Value>& info) {
|
||||
HttpRequest* request = UnwrapRequest(info.Holder());
|
||||
const string& path = request->Referrer();
|
||||
info.GetReturnValue().Set(String::NewFromUtf8(
|
||||
info.GetIsolate(), path.c_str(), String::kNormalString,
|
||||
static_cast<int>(path.length())));
|
||||
info.GetReturnValue().Set(
|
||||
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
||||
NewStringType::kNormal,
|
||||
static_cast<int>(path.length())).ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@ -503,9 +523,10 @@ void JsHttpRequestProcessor::GetHost(Local<String> name,
|
||||
const PropertyCallbackInfo<Value>& info) {
|
||||
HttpRequest* request = UnwrapRequest(info.Holder());
|
||||
const string& path = request->Host();
|
||||
info.GetReturnValue().Set(String::NewFromUtf8(
|
||||
info.GetIsolate(), path.c_str(), String::kNormalString,
|
||||
static_cast<int>(path.length())));
|
||||
info.GetReturnValue().Set(
|
||||
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
||||
NewStringType::kNormal,
|
||||
static_cast<int>(path.length())).ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@ -514,13 +535,14 @@ void JsHttpRequestProcessor::GetUserAgent(
|
||||
const PropertyCallbackInfo<Value>& info) {
|
||||
HttpRequest* request = UnwrapRequest(info.Holder());
|
||||
const string& path = request->UserAgent();
|
||||
info.GetReturnValue().Set(String::NewFromUtf8(
|
||||
info.GetIsolate(), path.c_str(), String::kNormalString,
|
||||
static_cast<int>(path.length())));
|
||||
info.GetReturnValue().Set(
|
||||
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
||||
NewStringType::kNormal,
|
||||
static_cast<int>(path.length())).ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
|
||||
Local<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
|
||||
Isolate* isolate) {
|
||||
EscapableHandleScope handle_scope(isolate);
|
||||
|
||||
@ -529,16 +551,20 @@ Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
|
||||
|
||||
// Add accessors for each of the fields of the request.
|
||||
result->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "path", String::kInternalizedString),
|
||||
String::NewFromUtf8(isolate, "path", NewStringType::kInternalized)
|
||||
.ToLocalChecked(),
|
||||
GetPath);
|
||||
result->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "referrer", String::kInternalizedString),
|
||||
String::NewFromUtf8(isolate, "referrer", NewStringType::kInternalized)
|
||||
.ToLocalChecked(),
|
||||
GetReferrer);
|
||||
result->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "host", String::kInternalizedString),
|
||||
String::NewFromUtf8(isolate, "host", NewStringType::kInternalized)
|
||||
.ToLocalChecked(),
|
||||
GetHost);
|
||||
result->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "userAgent", String::kInternalizedString),
|
||||
String::NewFromUtf8(isolate, "userAgent", NewStringType::kInternalized)
|
||||
.ToLocalChecked(),
|
||||
GetUserAgent);
|
||||
|
||||
// Again, return the result through the current handle scope.
|
||||
@ -604,9 +630,9 @@ void ParseOptions(int argc,
|
||||
|
||||
|
||||
// Reads a file into a v8 string.
|
||||
Handle<String> ReadFile(Isolate* isolate, const string& name) {
|
||||
MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) {
|
||||
FILE* file = fopen(name.c_str(), "rb");
|
||||
if (file == NULL) return Handle<String>();
|
||||
if (file == NULL) return MaybeLocal<String>();
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t size = ftell(file);
|
||||
@ -618,12 +644,12 @@ Handle<String> ReadFile(Isolate* isolate, const string& name) {
|
||||
i += fread(&chars[i], 1, size - i, file);
|
||||
if (ferror(file)) {
|
||||
fclose(file);
|
||||
return Handle<String>();
|
||||
return MaybeLocal<String>();
|
||||
}
|
||||
}
|
||||
fclose(file);
|
||||
Handle<String> result = String::NewFromUtf8(
|
||||
isolate, chars, String::kNormalString, static_cast<int>(size));
|
||||
MaybeLocal<String> result = String::NewFromUtf8(
|
||||
isolate, chars, NewStringType::kNormal, static_cast<int>(size));
|
||||
delete[] chars;
|
||||
return result;
|
||||
}
|
||||
@ -676,8 +702,8 @@ int main(int argc, char* argv[]) {
|
||||
Isolate* isolate = Isolate::New(create_params);
|
||||
Isolate::Scope isolate_scope(isolate);
|
||||
HandleScope scope(isolate);
|
||||
Handle<String> source = ReadFile(isolate, file);
|
||||
if (source.IsEmpty()) {
|
||||
Local<String> source;
|
||||
if (!ReadFile(isolate, file).ToLocal(&source)) {
|
||||
fprintf(stderr, "Error reading '%s'.\n", file.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
@ -40,6 +40,10 @@
|
||||
'include_dirs': [
|
||||
'..',
|
||||
],
|
||||
'defines': [
|
||||
# TODO(jochen): Remove again after this is globally turned on.
|
||||
'V8_IMMINENT_DEPRECATION_WARNINGS',
|
||||
],
|
||||
'conditions': [
|
||||
['v8_enable_i18n_support==1', {
|
||||
'dependencies': [
|
||||
|
149
samples/shell.cc
149
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,23 +113,30 @@ 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"),
|
||||
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"),
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user