This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_gmp.cc
161 lines (105 loc) · 3.56 KB
/
node_gmp.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <obstack.h>
#include "node_gmp.h"
using namespace v8;
using namespace node;
#define GETARG(arg) \
if (arg->IsNumber() || arg->IsString()) { \
String::Utf8Value val(arg->ToString()); \
char * num = strtok(*val, "."); \
try { \
i = num; \
} catch (std::invalid_argument err) { \
return ThrowException(Exception::TypeError(String::New("bad argument"))); \
} \
} else if (!(arg->IsUndefined() || arg->IsNull())) { \
return ThrowException(Exception::TypeError(String::New("bad argument"))); \
}
Handle<Value>
GInt::New(const Arguments &args) {
HandleScope scope;
mpz_class i = 0;
GETARG(args[0]);
GInt *g = new GInt(i);
g->Wrap(args.This());
return args.This();
}
GInt::GInt(mpz_class val): ObjectWrap() {
val_ = val;
}
GInt::~GInt(){
}
Handle<Value>
GInt::Add(const Arguments &args) {
HandleScope scope;
mpz_class i = 0;
GETARG(args[0]);
GInt *self = ObjectWrap::Unwrap<GInt>(args.This());
self->val_ += i;
return args.This();
}
Handle<Value>
GInt::Sub(const Arguments &args) {
HandleScope scope;
mpz_class i = 0;
GETARG(args[0]);
GInt *self = ObjectWrap::Unwrap<GInt>(args.This());
self->val_ -= i;
return args.This();
}
Handle<Value>
GInt::Mul(const Arguments &args) {
HandleScope scope;
mpz_class i = 0;
GETARG(args[0]);
GInt *self = ObjectWrap::Unwrap<GInt>(args.This());
self->val_ *= i;
return args.This();
}
Handle<Value>
GInt::Div(const Arguments &args) {
HandleScope scope;
mpz_class i = 0;
GETARG(args[0]);
GInt *self = ObjectWrap::Unwrap<GInt>(args.This());
self->val_ /= i;
return args.This();
}
Handle<Value>
GInt::Pow(const Arguments &args) {
HandleScope scope;
if (!args[0]->IsNumber()) {
return ThrowException(Exception::TypeError(String::New("exponent must be an int")));
}
GInt *self = ObjectWrap::Unwrap<GInt>(args.This());
mpz_t c;
mpz_init(c);
mpz_pow_ui(c, self->val_.get_mpz_t(), (long)args[0]->Int32Value());
self->val_ = mpz_class(c);
mpz_clear(c);
return args.This();
}
Handle<Value>
GInt::ToString(const Arguments &args) {
HandleScope scope;
GInt *self = ObjectWrap::Unwrap<GInt>(args.This());
Local<String> str = String::New(self->val_.get_str(10).c_str());
return scope.Close(str);
}
void RegisterModule(Handle<Object> target) {
target->Set(String::NewSymbol("version"), String::New(gmp_version));
//target->Set(String::NewSymbol("toHex"), FunctionTemplate::New(ToHex)->GetFunction());
Local<FunctionTemplate> t_int = FunctionTemplate::New(GInt::New);
t_int->InstanceTemplate()->SetInternalFieldCount(1);
t_int->SetClassName(String::NewSymbol("GInt"));
NODE_SET_PROTOTYPE_METHOD(t_int, "add", GInt::Add);
NODE_SET_PROTOTYPE_METHOD(t_int, "sub", GInt::Sub);
NODE_SET_PROTOTYPE_METHOD(t_int, "mul", GInt::Mul);
NODE_SET_PROTOTYPE_METHOD(t_int, "div", GInt::Div);
NODE_SET_PROTOTYPE_METHOD(t_int, "pow", GInt::Pow);
NODE_SET_PROTOTYPE_METHOD(t_int, "toString", GInt::ToString);
target->Set(String::NewSymbol("Int"), t_int->GetFunction());
}
NODE_MODULE(gmp, RegisterModule);