Permalink
Browse files

Add synchronous callback example

  • Loading branch information...
1 parent ab9234d commit 425d326eed08018a717f5f1cd0b4d31d08d740fd @nikhilm committed Jan 20, 2012
Showing with 133 additions and 0 deletions.
  1. +114 −0 sync/main.cc
  2. +6 −0 sync/test.js
  3. +13 −0 sync/wscript
View
@@ -0,0 +1,114 @@
+#include <unistd.h>
+#include <v8.h>
+#include <node.h>
+#include <node_object_wrap.h>
+using namespace v8;
+using namespace node;
+
+namespace Library {
+class Inventory {
+public:
+ Inventory() : items(0) {}
+
+ void addStock(int n) {
+ items += n;
+ }
+
+ int ship(int orders) {
+ if (orders > items)
+ return -1;
+
+ items -= orders;
+ return 0;
+ }
+
+ int getItems() {
+ return items;
+ }
+
+ void reshelve() {
+ sleep(5);
+ }
+
+private:
+ int items;
+};
+}
+
+namespace binding {
+class Inventory : public ObjectWrap {
+public:
+ Inventory() : ObjectWrap()
+ , inv(new Library::Inventory()) {};
+
+ ~Inventory() { delete inv; }
+
+ static Handle<Value> New(const Arguments &args) {
+ Inventory *wrapper = new Inventory();
+ wrapper->Wrap(args.Holder());
+ return args.Holder();
+ }
+
+ static Handle<Value> AddStock(const Arguments &args) {
+ Inventory *wrapper = Unwrap<Inventory>(args.Holder());
+ int n = args[0]->Uint32Value();
+
+ wrapper->inv->addStock(n);
+ return Undefined();
+ }
+
+ static Handle<Value> Ship(const Arguments &args) {
+ Inventory *wrapper = Unwrap<Inventory>(args.Holder());
+ int orders = args[0]->Uint32Value();
+ int result = wrapper->inv->ship(orders);
+
+ if (result == -1)
+ return ThrowException(String::New("Not enough items"));
+
+ return Undefined();
+ }
+
+ static Handle<Value> GetItems(Local<String> property, const AccessorInfo &info) {
+ Inventory *wrapper = Unwrap<Inventory>(info.Holder());
+ return Integer::New(wrapper->inv->getItems());
+ }
+
+ static Handle<Value> Reshelve(const Arguments &args) {
+ Inventory *wrapper = Unwrap<Inventory>(args.Holder());
+ wrapper->inv->reshelve();
+
+ Handle<Function> cb = Handle<Function>::Cast(args[0]);
+ Handle<Value> argv[] = { Null() }; // no error
+ cb->Call(args.Holder(), 1, argv);
+
+ return Undefined();
+ }
+
+private:
+ Library::Inventory *inv;
+};
+
+extern "C" {
+ static void Init(Handle<Object> target) {
+ HandleScope scope;
+
+ Handle<FunctionTemplate> inventoryTpl =
+ FunctionTemplate::New(Inventory::New);
+
+ Handle<ObjectTemplate> instance =
+ inventoryTpl->InstanceTemplate();
+
+ instance->SetInternalFieldCount(1);
+
+ instance->SetAccessor(String::New("items"), Inventory::GetItems);
+
+ NODE_SET_PROTOTYPE_METHOD(inventoryTpl, "addStock", Inventory::AddStock);
+ NODE_SET_PROTOTYPE_METHOD(inventoryTpl, "ship", Inventory::Ship);
+ NODE_SET_PROTOTYPE_METHOD(inventoryTpl, "reshelve", Inventory::Reshelve);
+
+ target->Set(String::NewSymbol("Inventory"),
+ inventoryTpl->GetFunction());
+ }
+ NODE_MODULE(sync, Init)
+}
+}
View
@@ -0,0 +1,6 @@
+var inv = new (require('./build/Release/sync')).Inventory()
+
+inv.reshelve(function() {
+ console.log("Reshelving done");
+})
+console.log("After reshelve in source");
View
@@ -0,0 +1,13 @@
+import Options
+
+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.target = "sync"
+ obj.source = "main.cc"

0 comments on commit 425d326

Please sign in to comment.