Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Methods example

  • Loading branch information...
commit 90c8a4e06ddeaf027bc271dd4edaa007dcee2a46 1 parent bae624f
Nikhil Marathe authored

Showing 3 changed files with 93 additions and 3 deletions. Show diff stats Hide diff stats

  1. +63 0 methods/main.cc
  2. +13 0 methods/wscript
  3. +17 3 slides.rst
63 methods/main.cc
... ... @@ -0,0 +1,63 @@
  1 +#include <v8.h>
  2 +#include <node.h>
  3 +using namespace v8;
  4 +
  5 +Handle<Value> Inventory(const Arguments &args) {
  6 + return args.This();
  7 +}
  8 +
  9 +Handle<Value> AddStock(const Arguments &args) {
  10 + HandleScope scope;
  11 +
  12 + Handle<Object> This = args.This();
  13 +
  14 + int items = This->Get(String::New("items"))->Uint32Value();
  15 +
  16 + items += args[0]->Uint32Value();
  17 +
  18 + This->Set(String::New("items"), Integer::New(items));
  19 +
  20 + return Undefined();
  21 +}
  22 +
  23 +Handle<Value> Ship(const Arguments &args) {
  24 + HandleScope scope;
  25 +
  26 + Handle<Object> This = args.This();
  27 + int items = This->Get(String::New("items"))->Uint32Value();
  28 +
  29 + int orders = args[0]->Uint32Value();
  30 +
  31 + if (items < orders)
  32 + return ThrowException(String::New("Not enough items"));
  33 +
  34 + This->Set(String::New("items"), Integer::New(items - orders));
  35 +
  36 + return Undefined();
  37 +}
  38 +
  39 +extern "C" {
  40 + static void Init(Handle<Object> target) {
  41 + HandleScope scope;
  42 +
  43 + Handle<FunctionTemplate> inventoryTpl =
  44 + FunctionTemplate::New(Inventory);
  45 +
  46 + Handle<ObjectTemplate> instance =
  47 + inventoryTpl->InstanceTemplate();
  48 +
  49 + instance->Set(String::New("items"), Integer::New(257));
  50 +
  51 + // operating on inventoryTpl->PrototypeTemplate()
  52 + NODE_SET_PROTOTYPE_METHOD(inventoryTpl,
  53 + "addStock",
  54 + AddStock);
  55 + NODE_SET_PROTOTYPE_METHOD(inventoryTpl,
  56 + "ship",
  57 + Ship);
  58 +
  59 + target->Set(String::NewSymbol("Inventory"),
  60 + inventoryTpl->GetFunction());
  61 + }
  62 + NODE_MODULE(methods, Init)
  63 +}
13 methods/wscript
... ... @@ -0,0 +1,13 @@
  1 +import Options
  2 +
  3 +def set_options(opt):
  4 + opt.tool_options("compiler_cxx")
  5 +
  6 +def configure(conf):
  7 + conf.check_tool("compiler_cxx")
  8 + conf.check_tool("node_addon")
  9 +
  10 +def build(bld):
  11 + obj = bld.new_task_gen("cxx", "shlib", "node_addon")
  12 + obj.target = "methods"
  13 + obj.source = "main.cc"
20 slides.rst
Source Rendered
@@ -198,10 +198,24 @@ Simple objects
198 198
199 199 .. code-block:: cpp
200 200 :include: simpleobject/main.cc
201   - :start-at: static void
202   - :end-at: }
  201 + :start-at: static void Init
  202 + :end-before: NODE_MODULE
  203 +
  204 +Methods
  205 +-------
  206 +
  207 +.. code-block:: js
  208 +
  209 + Inventory.prototype.addStock = function(newStock) {
  210 + this.items += newStock;
  211 + }
203 212
204   -TODO fix indentation in inclusion
  213 + Inventory.prototype.ship = function(orders) {
  214 + if (this.items < orders)
  215 + throw Exception("Not enough items");
  216 +
  217 + this.items -= orders
  218 + }
205 219
206 220 ObjectWrap
207 221 ----------

0 comments on commit 90c8a4e

Please sign in to comment.
Something went wrong with that request. Please try again.