Skip to content

Commit

Permalink
Added simple function example and relevant slides
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilm committed Jan 14, 2012
1 parent 316ef59 commit e114e08
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
25 changes: 25 additions & 0 deletions simplefunctions/main.cc
@@ -0,0 +1,25 @@
#include <v8.h>
#include <node.h>
using namespace v8;

Handle<Value> Square(const Arguments &args) {
HandleScope scope;

int a = args[0]->Int32Value();
int sq = a * a;

return scope.Close(Integer::New(sq));
}

extern "C" {
static void Init(Handle<Object> target) {
HandleScope scope;

Handle<FunctionTemplate> squareTpl =
FunctionTemplate::New(Square);

target->Set(String::New("square"),
squareTpl->GetFunction());
}
NODE_MODULE(simplefunctions, Init)
}
13 changes: 13 additions & 0 deletions simplefunctions/wscript
@@ -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 = "simplefunctions"
obj.source = "main.cc"
43 changes: 43 additions & 0 deletions slides.rst
Expand Up @@ -96,3 +96,46 @@ Injecting primitives
:include: primitives/primitives.cc :include: primitives/primitives.cc
Templates
---------

* Creation of functions and objects in C++

.. note::

Explain how they differ, what each is for and
whats the deal with FunctionTemplate && FunctionTemplate::GetFunction

Simple functions
----------------

Registering with V8:

.. code-block:: cpp
:include: simplefunctions/main.cc
:start-at: Handle<Value>
:end-before: {
.. code-block:: cpp
:include: simplefunctions/main.cc
:start-at: static void
:end-before: NODE_MODULE
TODO TODO TODO

Simple functions
----------------

Implementation:

.. code-block:: cpp
:include: simplefunctions/main.cc
:start-after: {
:end-before: }
ObjectWrap
----------

TODO diagram of how the nesting happens of native object and v8 object

MakeCallback

0 comments on commit e114e08

Please sign in to comment.