/
string.cc
45 lines (32 loc) · 1 KB
/
string.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "v8.h"
#include "node.h"
#include <stdio.h>
#include <string.h>
using namespace v8;
Handle<Value> Greet(const Arguments &args);
void
Initialize(Handle<Object> exports) {
exports->Set(String::NewSymbol("value"),
String::New("Hello World"));
exports->Set(String::NewSymbol("greet"),
FunctionTemplate::New(Greet)->GetFunction());
}
Handle<Value>
Greet(const Arguments &args) {
HandleScope scope;
if (!args[0]->IsString()) {
ThrowException(Exception::TypeError(
String::New("Argument should be a String.")));
return scope.Close(Undefined());
}
Local<String> str = args[0]->ToString();
int nstr_len = str->Length() + 6;
char * nstr = (char *) malloc(nstr_len);
char * nstr_arg = (* String::AsciiValue(str));
strcpy(nstr, "Hello ");
for (int i = 0; i < str->Length(); i++) {
nstr[i + 6] = nstr_arg[i];
}
return scope.Close(String::New(nstr, nstr_len));
}
NODE_MODULE(string, Initialize)