-
Notifications
You must be signed in to change notification settings - Fork 0
/
bayesian_bind.cpp
59 lines (49 loc) · 1.46 KB
/
bayesian_bind.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <v8.h>
#include <node.h>
#include <fstream>
#include <cstdlib>
#include <string.h>
#include <stdlib.h>
#include "bayes_net.h"
using namespace node;
using namespace v8;
struct simple_request {
char* bayesian_result;
Persistent<Function> cb;
char* passed_courses;
};
static Handle<Value> foo(const Arguments& args)
{
//init the struct
String::Utf8Value passed_courses_args(args[0]);
Local<Function> cb = Local<Function>::Cast(args[1]); //this is the callback(cb) in updateEvidence, which is passed by javascript
simple_request *sr = new simple_request();
sr->cb = Persistent<Function>::New(cb);
sr->passed_courses = (char*)malloc(sizeof(char)*512);
strncpy(sr->passed_courses, *passed_courses_args, passed_courses_args.length() + 1);
//heavy lifting
sr->bayesian_result = bayesian_test(sr->passed_courses);
//return results to js
Local<Value> argv[3];
argv[0] = Local<Value>::New(Null()); //cb(er)
argv[1] = String::New(sr->bayesian_result); //cb(res)
argv[2] = String::New(sr->passed_courses); //cb(n)
TryCatch try_catch;
sr->cb->Call(Context::GetCurrent()->Global(), 3, argv);//execute the js callback
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
//clean the mess
sr->cb.Dispose();
free(sr->passed_courses);
free(sr->bayesian_result);
free(sr);
return String::New("Hello World");
}
extern "C" {
static void init(Handle<Object> target)
{
NODE_SET_METHOD(target, "updateEvidence", foo);
}
NODE_MODULE(bayesian, init);
}