-
Notifications
You must be signed in to change notification settings - Fork 9
/
class_number.go
300 lines (243 loc) · 8.01 KB
/
class_number.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
import "C"
import "unsafe"
func cnCallUnary(fn, obj unsafe.Pointer) unsafe.Pointer {
// Turn the function into something we can call
f := (*func(unsafe.Pointer) (Object, error))(unsafe.Pointer(&fn))
ret, err := (*f)(obj)
if err != nil {
raise(err)
return nil
}
return unsafe.Pointer(c(ret))
}
func cnCallBinary(fn, obj1, obj2 unsafe.Pointer) unsafe.Pointer {
// Turn the function into something we can call
f := (*func(unsafe.Pointer, Object) (Object, error))(unsafe.Pointer(&fn))
// Get obj2 ready to use
arg := newObject((*C.PyObject)(obj2))
ret, err := (*f)(obj1, arg)
if err != nil {
raise(err)
return nil
}
return unsafe.Pointer(c(ret))
}
func cnCallTernary(fn, obj1, obj2, obj3 unsafe.Pointer) unsafe.Pointer {
// Turn the function into something we can call
f := (*func(unsafe.Pointer, Object, Object) (Object, error))(unsafe.Pointer(&fn))
// Get obj2 and obj3 ready to use
arg1 := newObject((*C.PyObject)(obj2))
arg2 := newObject((*C.PyObject)(obj3))
ret, err := (*f)(obj1, arg1, arg2)
if err != nil {
raise(err)
return nil
}
return unsafe.Pointer(c(ret))
}
//export goClassNumAdd
func goClassNumAdd(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_add, obj1, obj2)
}
//export goClassNumSubtract
func goClassNumSubtract(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_subtract, obj1, obj2)
}
//export goClassNumMultiply
func goClassNumMultiply(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_multiply, obj1, obj2)
}
//export goClassNumDivide
func goClassNumDivide(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_divide, obj1, obj2)
}
//export goClassNumRemainder
func goClassNumRemainder(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_remainder, obj1, obj2)
}
//export goClassNumDivmod
func goClassNumDivmod(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_divmod, obj1, obj2)
}
//export goClassNumPower
func goClassNumPower(obj1, obj2, obj3 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallTernary(ctxt.nb_power, obj1, obj2, obj3)
}
//export goClassNumNegative
func goClassNumNegative(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_negative, obj)
}
//export goClassNumPositive
func goClassNumPositive(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_positive, obj)
}
//export goClassNumAbsolute
func goClassNumAbsolute(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_absolute, obj)
}
//export goClassNumNonzero
func goClassNumNonzero(obj unsafe.Pointer) int {
ctxt := getClassContext(obj)
// Turn the function into something we can call
f := (*func(unsafe.Pointer) (bool, error))(unsafe.Pointer(&ctxt.nb_nonzero))
ret, err := (*f)(obj)
if err != nil {
raise(err)
return -1
}
if ret {
return 1
}
return 0
}
//export goClassNumInvert
func goClassNumInvert(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_invert, obj)
}
//export goClassNumLshift
func goClassNumLshift(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_lshift, obj1, obj2)
}
//export goClassNumRshift
func goClassNumRshift(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_rshift, obj1, obj2)
}
//export goClassNumAnd
func goClassNumAnd(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_and, obj1, obj2)
}
//export goClassNumXor
func goClassNumXor(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_xor, obj1, obj2)
}
//export goClassNumOr
func goClassNumOr(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_or, obj1, obj2)
}
//nb_coerce
//export goClassNumInt
func goClassNumInt(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_int, obj)
}
//export goClassNumLong
func goClassNumLong(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_long, obj)
}
//export goClassNumFloat
func goClassNumFloat(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_float, obj)
}
//export goClassNumOct
func goClassNumOct(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_oct, obj)
}
//export goClassNumHex
func goClassNumHex(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_hex, obj)
}
//export goClassNumInplaceAdd
func goClassNumInplaceAdd(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_add, obj1, obj2)
}
//export goClassNumInplaceSubtract
func goClassNumInplaceSubtract(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_subtract, obj1, obj2)
}
//export goClassNumInplaceMultiply
func goClassNumInplaceMultiply(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_multiply, obj1, obj2)
}
//export goClassNumInplaceDivide
func goClassNumInplaceDivide(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_divide, obj1, obj2)
}
//export goClassNumInplaceRemainder
func goClassNumInplaceRemainder(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_remainder, obj1, obj2)
}
//export goClassNumInplacePower
func goClassNumInplacePower(obj1, obj2, obj3 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallTernary(ctxt.nb_ip_power, obj1, obj2, obj3)
}
//export goClassNumInplaceLshift
func goClassNumInplaceLshift(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_lshift, obj1, obj2)
}
//export goClassNumInplaceRshift
func goClassNumInplaceRshift(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_rshift, obj1, obj2)
}
//export goClassNumInplaceAnd
func goClassNumInplaceAnd(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_and, obj1, obj2)
}
//export goClassNumInplaceXor
func goClassNumInplaceXor(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_xor, obj1, obj2)
}
//export goClassNumInplaceOr
func goClassNumInplaceOr(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_or, obj1, obj2)
}
//export goClassNumFloorDivide
func goClassNumFloorDivide(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_floordiv, obj1, obj2)
}
//export goClassNumTrueDivide
func goClassNumTrueDivide(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_truediv, obj1, obj2)
}
//export goClassNumInplaceFloorDivide
func goClassNumInplaceFloorDivide(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_floordiv, obj1, obj2)
}
//export goClassNumInplaceTrueDivide
func goClassNumInplaceTrueDivide(obj1, obj2 unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj1)
return cnCallBinary(ctxt.nb_ip_truediv, obj1, obj2)
}
//export goClassNumIndex
func goClassNumIndex(obj unsafe.Pointer) unsafe.Pointer {
ctxt := getClassContext(obj)
return cnCallUnary(ctxt.nb_index, obj)
}