-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_wrap.cc
More file actions
134 lines (106 loc) · 3.36 KB
/
Copy pathfunction_wrap.cc
File metadata and controls
134 lines (106 loc) · 3.36 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <utility>
#include "function_wrap.h"
napi_value function_wrap::fib_a(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
if (!(valuetype0 == napi_number)) {
napi_value errChar;
napi_throw_error(env, nullptr, "assertion (Wrong argument type. Numbers expected.) failed");
napi_create_string_utf8(env, "Wrong argument type. Numbers expected.",
sizeof("Wrong argument type. Numbers expected."), &errChar);
return errChar;
}
int value0;
napi_get_value_int32(env, args[0], &value0);
long fbnqRes = 0;
fbnqRes = fibonacci_a(value0);
napi_value result;
napi_create_int32(env, fbnqRes, &result);
return result;
}
napi_value function_wrap::fib_b(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
if (!(valuetype0 == napi_number)) {
napi_value errChar;
napi_throw_error(env, NULL, "assertion (Wrong argument type. Numbers expected.) failed");
napi_create_string_utf8(env, "Wrong argument type. Numbers expected.", sizeof("Wrong argument type. Numbers expected."), &errChar);
return errChar;
}
int value0;
napi_get_value_int32(env, args[0], &value0);
long fbnqRes = 0;
fbnqRes = fibonacci_b(value0);
napi_value result;
napi_create_int32(env, fbnqRes, &result);
return result;
}
napi_value function_wrap::add(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
if (!(valuetype0 == napi_number)) {
napi_value errChar;
napi_throw_error(env, NULL, "assertion (Wrong argument type. Numbers expected.) failed");
napi_create_string_utf8(env, "Wrong argument type. Numbers expected.", sizeof("Wrong argument type. Numbers expected."), &errChar);
return errChar;
}
napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);
if (!(valuetype1 == napi_number)) {
napi_value errChar;
napi_throw_error(env, NULL, "assertion (Wrong argument type. Numbers expected.) failed");
napi_create_string_utf8(env, "Wrong argument type. Numbers expected.", sizeof("Wrong argument type. Numbers expected."), &errChar);
return errChar;
}
double value0;
napi_get_value_double(env, args[0], &value0);
double value1;
napi_get_value_double(env, args[1], &value1);
double addRes = 0;
addRes = value0 + value1;
char str1[32];
sprintf(str1,"%.10f",addRes);
int eff_len = 0;
for (int i = 0; i < 32; ++i) {
if (str1[i] != '\0') {
eff_len += 1;
}
}
char* str2 = new char[ eff_len ];
sprintf(str2,"%.10f",addRes);
napi_value result;
napi_create_string_utf8(env, &str2[0], sizeof(str2), &result);
delete str2;
return result;
}
long fibonacci_a(int n) {
if (n < 2) {
return n;
}
return fibonacci_a(n - 2) + fibonacci_a(n - 1);
}
long fibonacci_b(int &n) {
long feb_n_val = 0;
int i;
long a = 1;
long b = 1;
if (n < 3) {
return 1;
}
for(i = 3;i <= n;i++) {
feb_n_val = a + b;
b = a;
a = feb_n_val;
}
return feb_n_val;
}