Index: samples/process.cc |
diff --git a/samples/process.cc b/samples/process.cc |
index c34ba7bfa9ee9c72bda98f270ff3f7f189ca7c5e..a62950a76ab4b7885771cf4f56bcd6693410f898 100644 |
--- a/samples/process.cc |
+++ b/samples/process.cc |
@@ -93,8 +93,8 @@ 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) |
- : isolate_(isolate), script_(script) { } |
+ JsHttpRequestProcessor(Isolate* isolate, Local<String> script) |
+ : isolate_(isolate), script_(script) {} |
virtual ~JsHttpRequestProcessor(); |
virtual bool Initialize(map<string, string>* opts, |
@@ -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); |
- |
- Handle<Object> output_obj = WrapMap(output); |
- context->Global()->Set(String::NewFromUtf8(GetIsolate(), "output"), |
- output_obj); |
+ context->Global() |
+ ->Set(context, |
+ String::NewFromUtf8(GetIsolate(), "options", NewStringType::kNormal) |
+ .ToLocalChecked(), |
+ opts_obj) |
+ .FromJust(); |
+ |
+ 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; |
} |