Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Licensed to Paul Querna under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* Paul Querna licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <v8.h> | ||
#include <node.h> | ||
#include <node_events.h> | ||
#include <node_buffer.h> | ||
|
||
#include <unistd.h> | ||
|
||
using namespace node; | ||
using namespace v8; | ||
|
||
#define REQ_FUN_ARG(I, VAR) \ | ||
if (args.Length() <= (I) || !args[I]->IsFunction()) \ | ||
return ThrowException(Exception::TypeError( \ | ||
String::New("Argument " #I " must be a function"))); \ | ||
Local<Function> VAR = Local<Function>::Cast(args[I]); | ||
|
||
class HelloWorldEio: ObjectWrap | ||
{ | ||
private: | ||
int m_count; | ||
public: | ||
|
||
static Persistent<FunctionTemplate> s_ct; | ||
static void Init(Handle<Object> target) | ||
{ | ||
HandleScope scope; | ||
|
||
Local<FunctionTemplate> t = FunctionTemplate::New(New); | ||
|
||
s_ct = Persistent<FunctionTemplate>::New(t); | ||
s_ct->InstanceTemplate()->SetInternalFieldCount(1); | ||
s_ct->SetClassName(String::NewSymbol("HelloWorldEio")); | ||
|
||
NODE_SET_PROTOTYPE_METHOD(s_ct, "hello", Hello); | ||
|
||
target->Set(String::NewSymbol("HelloWorldEio"), | ||
s_ct->GetFunction()); | ||
} | ||
|
||
HelloWorldEio() : | ||
m_count(0) | ||
{ | ||
} | ||
|
||
~HelloWorldEio() | ||
{ | ||
} | ||
|
||
static Handle<Value> New(const Arguments& args) | ||
{ | ||
HandleScope scope; | ||
HelloWorldEio* hw = new HelloWorldEio(); | ||
hw->Wrap(args.This()); | ||
return args.This(); | ||
} | ||
|
||
struct hello_baton_t { | ||
HelloWorldEio *hw; | ||
int increment_by; | ||
int sleep_for; | ||
Persistent<Function> cb; | ||
}; | ||
|
||
static Handle<Value> Hello(const Arguments& args) | ||
{ | ||
HandleScope scope; | ||
|
||
REQ_FUN_ARG(0, cb); | ||
|
||
HelloWorldEio* hw = ObjectWrap::Unwrap<HelloWorldEio>(args.This()); | ||
|
||
hello_baton_t *baton = new hello_baton_t(); | ||
baton->hw = hw; | ||
baton->increment_by = 2; | ||
baton->sleep_for = 1; | ||
baton->cb = Persistent<Function>::New(cb); | ||
|
||
hw->Ref(); | ||
|
||
eio_custom(EIO_Hello, EIO_PRI_DEFAULT, EIO_AfterHello, baton); | ||
ev_ref(EV_DEFAULT_UC); | ||
|
||
return Undefined(); | ||
} | ||
|
||
|
||
static int EIO_Hello(eio_req *req) | ||
{ | ||
hello_baton_t *baton = static_cast<hello_baton_t *>(req->data); | ||
|
||
sleep(baton->sleep_for); | ||
|
||
baton->hw->m_count += baton->increment_by; | ||
|
||
return 0; | ||
} | ||
|
||
static int EIO_AfterHello(eio_req *req) | ||
{ | ||
HandleScope scope; | ||
ev_unref(EV_DEFAULT_UC); | ||
hello_baton_t *baton = static_cast<hello_baton_t *>(req->data); | ||
|
||
Local<Value> argv[1]; | ||
|
||
argv[0] = String::New("Hello World"); | ||
|
||
TryCatch try_catch; | ||
|
||
baton->hw->Unref(); | ||
baton->cb->Call(Context::GetCurrent()->Global(), 1, argv); | ||
|
||
if (try_catch.HasCaught()) { | ||
FatalException(try_catch); | ||
} | ||
|
||
baton->cb.Dispose(); | ||
|
||
delete baton; | ||
return 0; | ||
} | ||
|
||
}; | ||
|
||
Persistent<FunctionTemplate> HelloWorldEio::s_ct; | ||
|
||
extern "C" { | ||
static void init (Handle<Object> target) | ||
{ | ||
HelloWorldEio::Init(target); | ||
} | ||
|
||
NODE_MODULE(helloworld_eio, init); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
|
||
def set_options(opt): | ||
opt.tool_options("compiler_cxx") | ||
|
||
def configure(conf): | ||
conf.check_tool("compiler_cxx") | ||
conf.check_tool("node_addon") | ||
|
||
def build(bld): | ||
obj = bld.new_task_gen("cxx", "shlib", "node_addon") | ||
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"] | ||
obj.target = "helloworld_eio" | ||
obj.source = "helloworld_eio.cc" |