|
| 1 | +#define BUILDING_NODE_EXTENSION |
| 2 | +#include <string> |
| 3 | +#include <sstream> |
| 4 | +#include <map> |
| 5 | +#include <node.h> |
| 6 | +#include "simulation.h" |
| 7 | + |
| 8 | +using namespace v8; |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +#define DEFINE_FUNCTION_HANDLE(fun_name) \ |
| 12 | +Handle<Value> fun_name##_agent(const Arguments& args) {return agent(#fun_name, args);} |
| 13 | +#define GET_FUNCTION_HANDLE(fun_name) \ |
| 14 | +fun_name##_agent |
| 15 | +#define DEFINE_FUNCTION(ret,function,arg) \ |
| 16 | +{#ret,#function,#arg, (void*)function, GET_FUNCTION_HANDLE(function)} |
| 17 | + |
| 18 | +typedef struct table_t { |
| 19 | + string return_type; |
| 20 | + string name; |
| 21 | + string arg_list; |
| 22 | + void* addr; |
| 23 | + Handle<Value> (*call)(const Arguments&); |
| 24 | +} fun_table; |
| 25 | + |
| 26 | +fun_table TABLE_NULL = { "", "", "", NULL, NULL }; |
| 27 | + |
| 28 | +extern fun_table table[]; |
| 29 | + |
| 30 | +int parser_func_arg_count(string arg_list) { |
| 31 | + int i = 1; |
| 32 | + const char* arg = arg_list.c_str(); |
| 33 | + if (arg_list.length() == 0) |
| 34 | + return 0; |
| 35 | + while (*arg != '\0') { |
| 36 | + if (*arg == ',') |
| 37 | + i++; |
| 38 | + arg++; |
| 39 | + } |
| 40 | + return i; |
| 41 | +} |
| 42 | + |
| 43 | +#define REGISTER int |
| 44 | +#define MAX_ARGC 8 |
| 45 | +#define ARG(x) (arg[x]) |
| 46 | +#define ARG0 |
| 47 | +#define ARG1 ARG(0) |
| 48 | +#define ARG2 ARG1, ARG(1) |
| 49 | +#define ARG3 ARG2, ARG(2) |
| 50 | +#define ARG4 ARG3, ARG(3) |
| 51 | +#define ARG5 ARG4, ARG(4) |
| 52 | +#define ARG6 ARG5, ARG(5) |
| 53 | +#define ARG7 ARG6, ARG(6) |
| 54 | +#define ARG8 ARG7, ARG(7) |
| 55 | +typedef int (*FUNC0)(void); |
| 56 | +typedef int (*FUNC1)(REGISTER); |
| 57 | +typedef int (*FUNC2)(REGISTER, REGISTER); |
| 58 | +typedef int (*FUNC3)(REGISTER, REGISTER, REGISTER); |
| 59 | +typedef int (*FUNC4)(REGISTER, REGISTER, REGISTER, REGISTER); |
| 60 | +typedef int (*FUNC5)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER); |
| 61 | +typedef int (*FUNC6)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER, |
| 62 | + REGISTER); |
| 63 | +typedef int (*FUNC7)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER, REGISTER, |
| 64 | + REGISTER); |
| 65 | +typedef int (*FUNC8)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER, REGISTER, |
| 66 | + REGISTER, REGISTER); |
| 67 | + |
| 68 | +Handle<Value> agent(string name, const Arguments& args) { |
| 69 | + HandleScope scope; |
| 70 | + fun_table* table_p = table; |
| 71 | + void* func_address = NULL; |
| 72 | + int argc; |
| 73 | + REGISTER ret = 0; |
| 74 | + REGISTER arg[MAX_ARGC]; |
| 75 | + |
| 76 | + while (table_p != &TABLE_NULL) { |
| 77 | + if (table_p->name == name) { |
| 78 | + func_address = table_p->addr; |
| 79 | + argc = parser_func_arg_count(table_p->arg_list); |
| 80 | + } |
| 81 | + table_p++; |
| 82 | + } |
| 83 | + |
| 84 | + if (func_address == NULL) { |
| 85 | + string message = "Wrong method name "; |
| 86 | + message+= name; |
| 87 | + ThrowException(Exception::TypeError(String::New(message.c_str()))); |
| 88 | + return scope.Close(Undefined()); |
| 89 | + } |
| 90 | + |
| 91 | + if (args.Length() != argc) { |
| 92 | + stringstream s; |
| 93 | + string message ="Wrong number of arguments need "; |
| 94 | + s << argc; |
| 95 | + message+= s.str(); |
| 96 | + ThrowException( |
| 97 | + Exception::TypeError(String::New(message.c_str()))); |
| 98 | + return scope.Close(Undefined()); |
| 99 | + } |
| 100 | + |
| 101 | + for(int i=0; i< argc;i++) { |
| 102 | + if(!args[i]->IsNumber()) { |
| 103 | + stringstream s; |
| 104 | + string message ="Wrong arguments "; |
| 105 | + s << i; |
| 106 | + message+= s.str(); |
| 107 | + ThrowException(Exception::TypeError(String::New(message.c_str()))); |
| 108 | + return scope.Close(Undefined()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + for(int i=0; i< argc;i++) |
| 113 | + arg[i] = args[i]->NumberValue(); |
| 114 | + |
| 115 | + switch (argc) { |
| 116 | + case 0: |
| 117 | + ret = ((FUNC0) func_address)(ARG0); |
| 118 | + break; |
| 119 | + case 1: |
| 120 | + ret = ((FUNC1) func_address)(ARG1); |
| 121 | + break; |
| 122 | + case 2: |
| 123 | + ret = ((FUNC2) func_address)(ARG2); |
| 124 | + break; |
| 125 | + case 3: |
| 126 | + ret = ((FUNC3) func_address)(ARG3); |
| 127 | + break; |
| 128 | + case 4: |
| 129 | + ret = ((FUNC4) func_address)(ARG4); |
| 130 | + break; |
| 131 | + case 5: |
| 132 | + ret = ((FUNC5) func_address)(ARG5); |
| 133 | + break; |
| 134 | + case 6: |
| 135 | + ret = ((FUNC6) func_address)(ARG6); |
| 136 | + break; |
| 137 | + case 7: |
| 138 | + ret = ((FUNC7) func_address)(ARG7); |
| 139 | + break; |
| 140 | + case 8: |
| 141 | + ret = ((FUNC8) func_address)(ARG8); |
| 142 | + break; |
| 143 | + default: |
| 144 | + break; |
| 145 | + } |
| 146 | + |
| 147 | + Local < Number > num = Number::New(ret); |
| 148 | + return scope.Close(num); |
| 149 | +} |
| 150 | + |
| 151 | +int add(int a, int b) { |
| 152 | + return a + b; |
| 153 | +} |
| 154 | + |
| 155 | +//Add handle |
| 156 | +DEFINE_FUNCTION_HANDLE(add); |
| 157 | +DEFINE_FUNCTION_HANDLE(led); |
| 158 | +DEFINE_FUNCTION_HANDLE(ioa); |
| 159 | +DEFINE_FUNCTION_HANDLE(iob); |
| 160 | +DEFINE_FUNCTION_HANDLE(sleep); |
| 161 | + |
| 162 | +//Add c function |
| 163 | +fun_table table[] = { |
| 164 | +DEFINE_FUNCTION(char,add,(char a, char b)), |
| 165 | +DEFINE_FUNCTION(void,led,(int id, char r, char g, char b)), |
| 166 | +DEFINE_FUNCTION(void,ioa,(int id, int value)), |
| 167 | +DEFINE_FUNCTION(void,iob,(int id, int value)), |
| 168 | +DEFINE_FUNCTION(unsigned int,sleep,(unsigned int seconds)), |
| 169 | +TABLE_NULL |
| 170 | +}; |
| 171 | +//todo map<string, void*> fun_table; |
| 172 | + |
| 173 | +void Init(Handle<Object> exports) { |
| 174 | + fun_table* table_p = table; |
| 175 | + while (table_p != &TABLE_NULL) { |
| 176 | + exports->Set(String::NewSymbol(table_p->name.c_str()), |
| 177 | + FunctionTemplate::New(table_p->call)->GetFunction()); |
| 178 | + table_p++; |
| 179 | + } |
| 180 | + //Add spetial case function whose parameter is not number |
| 181 | +} |
| 182 | + |
| 183 | +NODE_MODULE(openfpgadunino, Init) |
0 commit comments