Skip to content

Commit 08bf835

Browse files
committed
add example for handling function arguments
1 parent b2eab4b commit 08bf835

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

03_function_arguments/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
To compile, run
2+
3+
```
4+
node-waf configure
5+
node-waf build
6+
```

03_function_arguments/modulename.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <node/v8.h>
2+
#include <node/node.h>
3+
4+
using namespace v8;
5+
6+
// Returns the Nth number in the fibonacci sequence where N is the first
7+
// argument passed.
8+
Handle<Value> Fibonacci(const Arguments& args) {
9+
HandleScope scope;
10+
11+
// Check that there are enough arguments. If we access an index that doesn't
12+
// exist, it'll be Undefined().
13+
if (args.Length() < 1) {
14+
// No argument was passed. Throw an exception to alert the user to
15+
// incorrect usage. Alternatively, we could just use 0.
16+
return ThrowException(
17+
Exception::TypeError(String::New("First argument must be a number"))
18+
);
19+
}
20+
21+
// Cast a value to a specific type. See
22+
// http://izs.me/v8-docs/classv8_1_1Value.html for available To*() functions
23+
// and type checking functions. When converting to integer, make sure the
24+
// POD type you use is big enough!
25+
Local<Integer> integer = args[0]->ToInteger();
26+
int32_t seq = integer->Value();
27+
28+
// Also possible in one call. (Don't forget HandleScope, otherwise the
29+
// intermediate Local handle won't be cleaned up!)
30+
// int32_t seq = args[0]->ToInteger()->Value();
31+
32+
// Check for invalid parameter.
33+
if (seq < 0) {
34+
return ThrowException(Exception::TypeError(String::New(
35+
"Fibonacci sequence number must be positive")));
36+
}
37+
38+
// The actual algorithm.
39+
int32_t current = 1;
40+
for (int32_t previous = -1, next = 0, i = 0; i <= seq; i++) {
41+
next = previous + current;
42+
previous = current;
43+
current = next;
44+
}
45+
46+
return scope.Close(Integer::New(current));
47+
}
48+
49+
void RegisterModule(Handle<Object> target) {
50+
target->Set(String::NewSymbol("fibonacci"),
51+
FunctionTemplate::New(Fibonacci)->GetFunction());
52+
}
53+
54+
NODE_MODULE(modulename, RegisterModule);

03_function_arguments/run.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var modulename = require('./build/Release/modulename');
2+
3+
console.warn(modulename.fibonacci(9));

03_function_arguments/wscript

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
def set_options(ctx):
4+
ctx.tool_options('compiler_cxx')
5+
6+
def configure(ctx):
7+
ctx.check_tool('compiler_cxx')
8+
ctx.check_tool('node_addon')
9+
10+
def build(ctx):
11+
t = ctx.new_task_gen('cxx', 'shlib', 'node_addon')
12+
t.source = ['modulename.cc']
13+
t.target = 'modulename'

0 commit comments

Comments
 (0)