Skip to content

Commit

Permalink
Module initialization has been rewritten
Browse files Browse the repository at this point in the history
  • Loading branch information
AIkorsky committed Oct 10, 2013
1 parent c1d05a6 commit 35aa4e2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 104 deletions.
1 change: 1 addition & 0 deletions binding.gyp
Expand Up @@ -3,6 +3,7 @@
{
"target_name": "poppler",
"sources": [
"src/poppler.cc",
"src/NodePopplerDocument.cc",
"src/NodePopplerPage.cc",
"src/iconv_string.cc"
Expand Down
71 changes: 16 additions & 55 deletions src/NodePopplerDocument.cc
Expand Up @@ -43,51 +43,27 @@ namespace node {
delete pages;
}

void NodePopplerDocument::Initialize(v8::Handle<v8::Object> target) {
HandleScope scope;

Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("PopplerDocument"));
void NodePopplerDocument::Init(v8::Handle<v8::Object> exports) {
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("PopplerDocument"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);

constructor_template->Set(
tpl->PrototypeTemplate()->Set(
String::NewSymbol("POPPLER_VERSION_MAJOR"), Uint32::New(POPPLER_VERSION_MAJOR));
constructor_template->Set(
tpl->PrototypeTemplate()->Set(
String::NewSymbol("POPPLER_VERSION_MINOR"), Uint32::New(POPPLER_VERSION_MINOR));
constructor_template->Set(
tpl->PrototypeTemplate()->Set(
String::NewSymbol("POPPLER_VERSION_MICRO"), Uint32::New(POPPLER_VERSION_MICRO));

/** Instance methods
* Prototype looks like this:
* static Handle<Value> funcName(const Arguments &args);
* to access the object do:
* ClassName* self = ObjectWrap::Unwrap<ClassName>(args.Holder());
*
* Then do:
* NODE_SET_PROTOTYPE_METHOD(constructor_template, "getPageCount", funcName);
*/

/** Class methods
* NODE_SET_METHOD(constructor_template->GetFunction(), "GetPageCount", funcName);
*/

/** Getters:
* Prototype looks like this:
* static Handle<Value> funcName(Local<String> property, const AccessorInfo& info);
* to access the object do:
* ClassName* self = ObjectWrap::Unwrap<ClassName>(info.This());
*
* Then do:
* constructor_template->PrototypeTemplate()->SetAccessor(String::NewSymbol("page_count"), funcName);
*/
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("pageCount"), NodePopplerDocument::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("PDFMajorVersion"), NodePopplerDocument::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("PDFMinorVersion"), NodePopplerDocument::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("pdfVersion"), NodePopplerDocument::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("isLinearized"), NodePopplerDocument::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("fileName"), NodePopplerDocument::paramsGetter);
target->Set(String::NewSymbol("PopplerDocument"), constructor_template->GetFunction());
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("pageCount"), NodePopplerDocument::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("PDFMajorVersion"), NodePopplerDocument::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("PDFMinorVersion"), NodePopplerDocument::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("pdfVersion"), NodePopplerDocument::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("isLinearized"), NodePopplerDocument::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("fileName"), NodePopplerDocument::paramsGetter);

Persistent<v8::Function> constructor = Persistent<v8::Function>::New(tpl->GetFunction());
exports->Set(String::NewSymbol("PopplerDocument"), constructor);
}

Handle<Value> NodePopplerDocument::paramsGetter(Local< String > property, const AccessorInfo &info) {
Expand Down Expand Up @@ -183,18 +159,3 @@ namespace node {
}

}

// Exporting function
extern "C" void
init (v8::Handle<v8::Object> target)
{
HandleScope scope;

// Require for poppler
globalParams = new GlobalParams();

NodePopplerDocument::Initialize(target);
NodePopplerPage::Initialize(target);
}

NODE_MODULE(poppler, init);
2 changes: 1 addition & 1 deletion src/NodePopplerDocument.h
Expand Up @@ -17,7 +17,7 @@ namespace node {
inline bool isOk() {
return doc->isOk();
}
static void Initialize(v8::Handle<v8::Object> target);
static void Init(v8::Handle<v8::Object> exports);

protected:
static v8::Handle<v8::Value> New(const v8::Arguments &args);
Expand Down
72 changes: 25 additions & 47 deletions src/NodePopplerPage.cc
Expand Up @@ -30,60 +30,38 @@ static Persistent<String> index_sym;

namespace node {

void NodePopplerPage::Initialize(v8::Handle<v8::Object> target) {
HandleScope scope;

void NodePopplerPage::Init(v8::Handle<v8::Object> exports) {
width_sym = Persistent<String>::New(String::NewSymbol("width"));
height_sym = Persistent<String>::New(String::NewSymbol("height"));
index_sym = Persistent<String>::New(String::NewSymbol("index"));

Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("PopplerPage"));

constructor_template->Set(
String::NewSymbol("POPPLER_VERSION_MAJOR"), Uint32::New(POPPLER_VERSION_MAJOR));
constructor_template->Set(
String::NewSymbol("POPPLER_VERSION_MINOR"), Uint32::New(POPPLER_VERSION_MINOR));
constructor_template->Set(
String::NewSymbol("POPPLER_VERSION_MICRO"), Uint32::New(POPPLER_VERSION_MICRO));

/** Instance methods
* static Handle<Value> funcName(const Arguments &args);
* NODE_SET_PROTOTYPE_METHOD(constructor_template, "getPageCount", funcName);
*/
NODE_SET_PROTOTYPE_METHOD(constructor_template, "renderToFile", NodePopplerPage::renderToFile);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "renderToBuffer", NodePopplerPage::renderToBuffer);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "findText", NodePopplerPage::findText);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "getWordList", NodePopplerPage::getWordList);
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("PopplerPage"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(tpl, "renderToFile", NodePopplerPage::renderToFile);
NODE_SET_PROTOTYPE_METHOD(tpl, "renderToBuffer", NodePopplerPage::renderToBuffer);
NODE_SET_PROTOTYPE_METHOD(tpl, "findText", NodePopplerPage::findText);
NODE_SET_PROTOTYPE_METHOD(tpl, "getWordList", NodePopplerPage::getWordList);
#if POPPLER_VERSION_MINOR >= 19
NODE_SET_PROTOTYPE_METHOD(constructor_template, "addAnnot", NodePopplerPage::addAnnot);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "deleteAnnots", NodePopplerPage::deleteAnnots);
NODE_SET_PROTOTYPE_METHOD(tpl, "addAnnot", NodePopplerPage::addAnnot);
NODE_SET_PROTOTYPE_METHOD(tpl, "deleteAnnots", NodePopplerPage::deleteAnnots);
#endif

/** Getters:
* static Handle<Value> funcName(Local<String> property, const AccessorInfo& info);
* constructor_template->PrototypeTemplate()->SetAccessor(String::NewSymbol("page_count"), funcName);
*/
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("num"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("width"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("height"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("crop_box"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("numAnnots"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("media_box"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("art_box"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("trim_box"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("bleed_box"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("rotate"), NodePopplerPage::paramsGetter);
constructor_template->InstanceTemplate()->SetAccessor(String::NewSymbol("isCropped"), NodePopplerPage::paramsGetter);

/** Class methods
* NODE_SET_METHOD(constructor_template->GetFunction(), "GetPageCount", funcName);
*/
// NODE_SET_METHOD(constructor_template->GetFunction(), "pixbufToImage", NodePopplerPage::pixbufToImage);

target->Set(String::NewSymbol("PopplerPage"), constructor_template->GetFunction());
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("num"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("width"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("height"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("crop_box"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("numAnnots"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("media_box"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("art_box"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("trim_box"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("bleed_box"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("rotate"), NodePopplerPage::paramsGetter);
tpl->InstanceTemplate()->SetAccessor(String::NewSymbol("isCropped"), NodePopplerPage::paramsGetter);

Persistent<v8::Function> constructor = Persistent<v8::Function>::New(tpl->GetFunction());
exports->Set(String::NewSymbol("PopplerPage"), constructor);
}

NodePopplerPage::~NodePopplerPage() {
Expand Down
4 changes: 3 additions & 1 deletion src/NodePopplerPage.h
Expand Up @@ -93,7 +93,9 @@ namespace node {

NodePopplerPage(NodePopplerDocument* doc, const int32_t pageNum);
~NodePopplerPage();
static void Initialize(v8::Handle<v8::Object> target);

static void Init(v8::Handle<v8::Object> exports);

bool isOk() {
return pg != NULL && pg->isOk();
}
Expand Down
14 changes: 14 additions & 0 deletions src/poppler.cc
@@ -0,0 +1,14 @@
#include <node.h>
#include "NodePopplerDocument.h"
#include "NodePopplerPage.h"

using namespace v8;
using namespace node;

void InitAll(Handle<v8::Object> exports) {
globalParams = new GlobalParams();
NodePopplerDocument::Init(exports);
NodePopplerPage::Init(exports);
}

NODE_MODULE(poppler, InitAll)

0 comments on commit 35aa4e2

Please sign in to comment.