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
3 changed files
with
93 additions
and
3 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,63 @@ | ||
#include <v8.h> | ||
#include <node.h> | ||
using namespace v8; | ||
|
||
Handle<Value> Inventory(const Arguments &args) { | ||
return args.This(); | ||
} | ||
|
||
Handle<Value> AddStock(const Arguments &args) { | ||
HandleScope scope; | ||
|
||
Handle<Object> This = args.This(); | ||
|
||
int items = This->Get(String::New("items"))->Uint32Value(); | ||
|
||
items += args[0]->Uint32Value(); | ||
|
||
This->Set(String::New("items"), Integer::New(items)); | ||
|
||
return Undefined(); | ||
} | ||
|
||
Handle<Value> Ship(const Arguments &args) { | ||
HandleScope scope; | ||
|
||
Handle<Object> This = args.This(); | ||
int items = This->Get(String::New("items"))->Uint32Value(); | ||
|
||
int orders = args[0]->Uint32Value(); | ||
|
||
if (items < orders) | ||
return ThrowException(String::New("Not enough items")); | ||
|
||
This->Set(String::New("items"), Integer::New(items - orders)); | ||
|
||
return Undefined(); | ||
} | ||
|
||
extern "C" { | ||
static void Init(Handle<Object> target) { | ||
HandleScope scope; | ||
|
||
Handle<FunctionTemplate> inventoryTpl = | ||
FunctionTemplate::New(Inventory); | ||
|
||
Handle<ObjectTemplate> instance = | ||
inventoryTpl->InstanceTemplate(); | ||
|
||
instance->Set(String::New("items"), Integer::New(257)); | ||
|
||
// operating on inventoryTpl->PrototypeTemplate() | ||
NODE_SET_PROTOTYPE_METHOD(inventoryTpl, | ||
"addStock", | ||
AddStock); | ||
NODE_SET_PROTOTYPE_METHOD(inventoryTpl, | ||
"ship", | ||
Ship); | ||
|
||
target->Set(String::NewSymbol("Inventory"), | ||
inventoryTpl->GetFunction()); | ||
} | ||
NODE_MODULE(methods, 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,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 = "methods" | ||
obj.source = "main.cc" |
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