aboutsummaryrefslogtreecommitdiff
path: root/samples/process.cc
diff options
context:
space:
mode:
Diffstat (limited to 'samples/process.cc')
-rw-r--r--samples/process.cc243
1 files changed, 146 insertions, 97 deletions
diff --git a/samples/process.cc b/samples/process.cc
index f4479706..cfbd054c 100644
--- a/samples/process.cc
+++ b/samples/process.cc
@@ -29,12 +29,26 @@
#include <include/libplatform/libplatform.h>
+#include <stdlib.h>
+#include <string.h>
+
#include <map>
#include <string>
using namespace std;
using namespace v8;
+class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
+ public:
+ virtual void* Allocate(size_t length) {
+ void* data = AllocateUninitialized(length);
+ return data == NULL ? data : memset(data, 0, length);
+ }
+ virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
+ virtual void Free(void* data, size_t) { free(data); }
+};
+
+
// These interfaces represent an existing request processing interface.
// The idea is to imagine a real application that uses these interfaces
// and then add scripting capabilities that allow you to interact with
@@ -79,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,
@@ -90,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'.
@@ -98,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,
@@ -118,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_;
};
@@ -142,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);
}
@@ -156,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
@@ -165,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
@@ -182,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);
@@ -201,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;
+ 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.
@@ -218,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);
@@ -235,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;
}
@@ -264,19 +293,19 @@ 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;
+ TryCatch try_catch(GetIsolate());
// 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;
@@ -295,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_;
// -----------------------------------
@@ -305,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);
@@ -338,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);
}
@@ -371,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());
}
@@ -396,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);
@@ -417,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);
@@ -452,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);
}
@@ -468,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());
}
@@ -479,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());
}
@@ -489,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());
}
@@ -500,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);
@@ -515,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.
@@ -590,23 +630,26 @@ 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);
- int size = ftell(file);
+ size_t size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
- for (int i = 0; i < size;) {
- int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
- i += read;
+ for (size_t i = 0; i < size;) {
+ i += fread(&chars[i], 1, size - i, file);
+ if (ferror(file)) {
+ fclose(file);
+ return MaybeLocal<String>();
+ }
}
fclose(file);
- Handle<String> result =
- String::NewFromUtf8(isolate, chars, String::kNormalString, size);
+ MaybeLocal<String> result = String::NewFromUtf8(
+ isolate, chars, NewStringType::kNormal, static_cast<int>(size));
delete[] chars;
return result;
}
@@ -623,11 +666,13 @@ StringHttpRequest kSampleRequests[kSampleSize] = {
};
-bool ProcessEntries(HttpRequestProcessor* processor, int count,
- StringHttpRequest* reqs) {
+bool ProcessEntries(v8::Platform* platform, HttpRequestProcessor* processor,
+ int count, StringHttpRequest* reqs) {
for (int i = 0; i < count; i++) {
- if (!processor->Process(&reqs[i]))
- return false;
+ bool result = processor->Process(&reqs[i]);
+ while (v8::platform::PumpMessageLoop(platform, Isolate::GetCurrent()))
+ continue;
+ if (!result) return false;
}
return true;
}
@@ -643,6 +688,7 @@ void PrintMap(map<string, string>* m) {
int main(int argc, char* argv[]) {
v8::V8::InitializeICU();
+ v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
@@ -653,11 +699,14 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "No script was specified.\n");
return 1;
}
- Isolate* isolate = Isolate::New();
+ ArrayBufferAllocator array_buffer_allocator;
+ Isolate::CreateParams create_params;
+ create_params.array_buffer_allocator = &array_buffer_allocator;
+ 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;
}
@@ -667,7 +716,7 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Error initializing processor.\n");
return 1;
}
- if (!ProcessEntries(&processor, kSampleSize, kSampleRequests))
+ if (!ProcessEntries(platform, &processor, kSampleSize, kSampleRequests))
return 1;
PrintMap(&output);
}