Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Code re-organization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan (egorich) Egorov committed Jun 5, 2010
1 parent a94f22c commit 73d29f9
Showing 1 changed file with 46 additions and 68 deletions.
114 changes: 46 additions & 68 deletions src/zlib.h
Expand Up @@ -18,30 +18,22 @@ using namespace node;

template <class Processor>
class ZipLib : ObjectWrap {
private:
enum State {
Idle,
Destroyed,
Data,
Eos,
Error
};

private:
typedef typename Processor::Utils Utils;
typedef typename Processor::Blob Blob;

typedef ZipLib<Processor> Self;
typedef StateTransition<State> Transition;

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

Local<FunctionTemplate> t = FunctionTemplate::New(New);

t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(t, "write", Write);
NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
NODE_SET_PROTOTYPE_METHOD(t, "destroy", Destroy);

target->Set(String::NewSymbol(Processor::Name), t->GetFunction());
}

private:
struct Request {
public:
enum Kind {
Expand Down Expand Up @@ -138,6 +130,23 @@ class ZipLib : ObjectWrap {
int status_;
};

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

Local<FunctionTemplate> t = FunctionTemplate::New(New);

t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(t, "write", Write);
NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
NODE_SET_PROTOTYPE_METHOD(t, "destroy", Destroy);

target->Set(String::NewSymbol(Processor::Name), t->GetFunction());
}

public:
static Handle<Value> New(const Arguments &args) {
Self *result = new(std::nothrow) Self();
Expand Down Expand Up @@ -309,6 +318,24 @@ class ZipLib : ObjectWrap {
DoHandleCallbacks(0);
}

static void DoCallback(Persistent<Function> cb, int r, Blob &out) {
if (!cb.IsEmpty()) {
HandleScope scope;

Local<Value> argv[2];
argv[0] = Utils::GetException(r);
argv[1] = Encode(out.data(), out.length(), BINARY);

TryCatch try_catch;

cb->Call(Context::GetCurrent()->Global(), 2, argv);

if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
}

private:
static bool ReentrantPop(Queue<Request*> &queue, pthread_mutex_t &mutex,
Request*& request) {
Expand Down Expand Up @@ -407,28 +434,7 @@ class ZipLib : ObjectWrap {
}


public:
static Handle<Value> ReturnThisOrThrow(const Arguments &args,
int zipStatus) {
if (!Utils::IsError(zipStatus)) {
return args.This();
} else {
return ThrowError(zipStatus);
}
}


static Handle<Value> ReturnOrThrow(HandleScope &scope,
const Local<Value> &value,
int zipStatus) {
if (!Utils::IsError(zipStatus)) {
return scope.Close(value);
} else {
return ThrowError(zipStatus);
}
}


private:
static Handle<Value> ThrowError(int zipStatus) {
assert(Utils::IsError(zipStatus));

Expand All @@ -449,34 +455,6 @@ class ZipLib : ObjectWrap {
}


static void DoCallback(Persistent<Function> cb, int r, Blob &out) {
if (!cb.IsEmpty()) {
HandleScope scope;

Local<Value> argv[2];
argv[0] = Utils::GetException(r);
argv[1] = Encode(out.data(), out.length(), BINARY);

TryCatch try_catch;

cb->Call(Context::GetCurrent()->Global(), 2, argv);

if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
}

enum State {
Idle,
Destroyed,
Data,
Eos,
Error
};

typedef StateTransition<State> Transition;

private:
Processor processor_;
State state_;
Expand Down

0 comments on commit 73d29f9

Please sign in to comment.