-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathbinding.cc
More file actions
745 lines (635 loc) · 22.9 KB
/
binding.cc
File metadata and controls
745 lines (635 loc) · 22.9 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unordered_map>
#include "ref-napi.h"
#ifdef _WIN32
#define __alignof__ __alignof
#define snprintf(buf, bufSize, format, arg) _snprintf_s(buf, bufSize, _TRUNCATE, format, arg)
#define strtoll _strtoi64
#define strtoull _strtoui64
#define PRId64 "lld"
#define PRIu64 "llu"
#else
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#endif
using namespace Napi;
namespace {
#if !defined(NAPI_VERSION) || NAPI_VERSION < 6
napi_status napix_set_instance_data(
napi_env env, void* data, napi_finalize finalize_cb, void* finalize_hint) {
typedef napi_status (*napi_set_instance_data_fn)(
napi_env env, void* data, napi_finalize finalize_cb, void* finalize_hint);
static const napi_set_instance_data_fn napi_set_instance_data__ =
(napi_set_instance_data_fn)
get_symbol_from_current_process("napi_set_instance_data");
if (napi_set_instance_data__ == nullptr)
return napi_generic_failure;
return napi_set_instance_data__(env, data, finalize_cb, finalize_hint);
}
napi_status napix_get_instance_data(
napi_env env, void** data) {
typedef napi_status (*napi_get_instance_data_fn)(
napi_env env, void** data);
static const napi_get_instance_data_fn napi_get_instance_data__ =
(napi_get_instance_data_fn)
get_symbol_from_current_process("napi_get_instance_data");
*data = nullptr;
if (napi_get_instance_data__ == nullptr)
return napi_generic_failure;
return napi_get_instance_data__(env, data);
}
#else // NAPI_VERSION >= 6
napi_status napix_set_instance_data(
napi_env env, void* data, napi_finalize finalize_cb, void* finalize_hint) {
return napi_set_instance_data(env, data, finalize_cb, finalize_hint);
}
napi_status napix_get_instance_data(
napi_env env, void** data) {
return napi_get_instance_data(env, data);
}
#endif
// used by the Int64 functions to determine whether to return a Number
// or String based on whether or not a Number will lose precision.
// http://stackoverflow.com/q/307179/376773
#define JS_MAX_INT +9007199254740992LL
#define JS_MIN_INT -9007199254740992LL
// mirrors deps/v8/src/objects.h.
// we could use `node::Buffer::kMaxLength`, but it's not defined on node v0.6.x
static const size_t kMaxLength = 0x3fffffff;
enum ArrayBufferMode {
AB_CREATED_BY_REF,
AB_PASSED_TO_REF
};
// Since Node.js v14.0.0, we have to keep a global list of all ArrayBuffer
// instances that we work with, in order not to create any duplicates.
// Luckily, N-API instance data is available on v14.x and above.
class InstanceData final : public RefNapi::Instance {
public:
InstanceData(Env env) : env(env) {}
struct ArrayBufferEntry {
Reference<ArrayBuffer> ab;
size_t finalizer_count;
};
Env env;
std::unordered_map<char*, ArrayBufferEntry> pointer_to_orig_buffer;
FunctionReference buffer_from;
void RegisterArrayBuffer(napi_value val) override {
ArrayBuffer buf(env, val);
RegisterArrayBuffer(buf, AB_PASSED_TO_REF);
}
inline void RegisterArrayBuffer(ArrayBuffer buf, ArrayBufferMode mode) {
char* ptr = static_cast<char*>(buf.Data());
if (ptr == nullptr) return;
auto it = pointer_to_orig_buffer.find(ptr);
if (it != pointer_to_orig_buffer.end()) {
if (!it->second.ab.Value().IsEmpty()) {
// Already have a valid entry, nothing to do.
return;
}
it->second.ab.Reset(buf, 0);
it->second.finalizer_count++;
} else {
pointer_to_orig_buffer.emplace(ptr, ArrayBufferEntry {
Reference<ArrayBuffer>::New(buf, 0),
1
});
}
// If AB_CREATED_BY_REF, then another finalizer has been added before this
// as a "real" backing store finalizer.
if (mode != AB_CREATED_BY_REF) {
buf.AddFinalizer([this](Env env, char* ptr) {
UnregisterArrayBuffer(ptr);
}, ptr);
}
}
inline void UnregisterArrayBuffer(char* ptr) {
auto it = pointer_to_orig_buffer.find(ptr);
if (--it->second.finalizer_count == 0)
pointer_to_orig_buffer.erase(it);
}
inline ArrayBuffer LookupOrCreateArrayBuffer(char* ptr, size_t length) {
assert(ptr != nullptr);
ArrayBuffer ab;
auto it = pointer_to_orig_buffer.find(ptr);
if (it != pointer_to_orig_buffer.end())
ab = it->second.ab.Value();
if (ab.IsEmpty()) {
length = std::max<size_t>(length, kMaxLength);
ab = Buffer<char>::New(env, ptr, length, [this](Env env, char* ptr) {
UnregisterArrayBuffer(ptr);
}).ArrayBuffer();
RegisterArrayBuffer(ab, AB_CREATED_BY_REF);
}
return ab;
}
napi_value WrapPointer(char* ptr, size_t length) override;
char* GetBufferData(napi_value val) override;
static InstanceData* Get(Env env) {
void* d = nullptr;
if (napix_get_instance_data(env, &d) == napi_ok)
return static_cast<InstanceData*>(d);
return nullptr;
}
};
/**
* Converts an arbitrary pointer to a node Buffer with specified length
*/
Value WrapPointer(Env env, char* ptr, size_t length) {
if (ptr == nullptr)
length = 0;
InstanceData* data;
if (ptr != nullptr && (data = InstanceData::Get(env)) != nullptr) {
ArrayBuffer ab = data->LookupOrCreateArrayBuffer(ptr, length);
assert(!ab.IsEmpty());
return data->buffer_from.Call({
ab, Number::New(env, 0), Number::New(env, length)
});
}
return Buffer<char>::New(env, ptr, length, [](Env,char*){});
}
char* GetBufferData(Value val) {
Buffer<char> buf = val.As<Buffer<char>>();
InstanceData* data = InstanceData::Get(val.Env());
if (data != nullptr)
data->RegisterArrayBuffer(buf.ArrayBuffer());
return buf.Data();
}
napi_value InstanceData::WrapPointer(char* ptr, size_t length) {
return ::WrapPointer(env, ptr, length);
}
char* InstanceData::GetBufferData(napi_value val) {
return ::GetBufferData(Value(env, val));
}
char* AddressForArgs(const CallbackInfo& args, size_t offset_index = 1) {
Value buf = args[0];
if (!buf.IsBuffer()) {
throw TypeError::New(args.Env(), "Buffer instance expected");
}
int64_t offset = args[offset_index].ToNumber();
return GetBufferData(buf) + offset;
}
/**
* Returns the pointer address as a Number of the given Buffer instance.
* It's recommended to use `hexAddress()` in most cases instead of this function.
*
* WARNING: a JavaScript Number cannot precisely store a full 64-bit memory
* address, so there's a possibility of an inaccurate value being returned
* on 64-bit systems.
*
* args[0] - Buffer - the Buffer instance get the memory address of
* args[1] - Number - optional (0) - the offset of the Buffer start at
*/
Value Address (const CallbackInfo& args) {
char* ptr = AddressForArgs(args);
uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
return Number::New(args.Env(), static_cast<double>(intptr));
}
/**
* Returns the pointer address as a hexadecimal String. This function
* is safe to use for displaying memory addresses, as compared to the
* `address()` function which could overflow since it returns a Number.
*
* args[0] - Buffer - the Buffer instance get the memory address of
* args[1] - Number - optional (0) - the offset of the Buffer start at
*/
Value HexAddress(const CallbackInfo& args) {
char* ptr = AddressForArgs(args);
char strbuf[30]; /* should be plenty... */
snprintf(strbuf, 30, "%p", ptr);
if (strbuf[0] == '0' && strbuf[1] == 'x') {
/* strip the leading "0x" from the address */
ptr = strbuf + 2;
} else {
ptr = strbuf;
}
return String::New(args.Env(), ptr);
}
/**
* Returns "true" if the given Buffer points to nullptr, "false" otherwise.
*
* args[0] - Buffer - the Buffer instance to check for nullptr
* args[1] - Number - optional (0) - the offset of the Buffer start at
*/
Value IsNull(const CallbackInfo& args) {
char* ptr = AddressForArgs(args);
return Boolean::New(args.Env(), ptr == nullptr);
}
/**
* Retreives a JS Object instance that was previously stored in
* the given Buffer instance at the given offset.
*
* args[0] - Buffer - the "buf" Buffer instance to read from
* args[1] - Number - the offset from the "buf" buffer's address to read from
*/
Value ReadObject(const CallbackInfo& args) {
char* ptr = AddressForArgs(args);
if (ptr == nullptr) {
throw Error::New(args.Env(), "readObject: Cannot read from nullptr pointer");
}
Reference<Object>* rptr = reinterpret_cast<Reference<Object>*>(ptr);
return rptr->Value();
}
/**
* Writes a weak reference to given Object to the given Buffer
* instance and offset.
*
* args[0] - Buffer - the "buf" Buffer instance to write to
* args[1] - Number - the offset from the "buf" buffer's address to write to
* args[2] - Object - the "obj" Object which will have a new Persistent reference
* created for the obj, whose memory address will be written.
*/
void WriteObject(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
if (ptr == nullptr) {
throw Error::New(env, "readObject: Cannot write to nullptr pointer");
}
Reference<Object>* rptr = reinterpret_cast<Reference<Object>*>(ptr);
if (args[2].IsObject()) {
Object val = args[2].As<Object>();
*rptr = std::move(Reference<Object>::New(val));
} else if (args[2].IsNull()) {
rptr->Reset();
} else {
throw TypeError::New(env, "WriteObject's 3rd argument needs to be an object");
}
}
/**
* Reads the memory address of the given "buf" pointer Buffer at the specified
* offset, and returns a new SlowBuffer instance from the memory address stored.
*
* args[0] - Buffer - the "buf" Buffer instance to read from
* args[1] - Number - the offset from the "buf" buffer's address to read from
* args[2] - Number - the length in bytes of the returned SlowBuffer instance
*/
Value ReadPointer(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
if (ptr == nullptr) {
throw Error::New(env, "readPointer: Cannot read from nullptr pointer");
}
int64_t size = args[2].ToNumber();
char* val = *reinterpret_cast<char**>(ptr);
return WrapPointer(env, val, size);
}
/**
* Creates an ArrayBuffer from the given address without allocating new memory,
* using napi_create_external_arraybuffer.
*
* args[0] - Number/String - the memory address
* args[1] - Number - the length in bytes of the returned ArrayBuffer instance
*/
Value ReadExternalArrayBuffer(const CallbackInfo& args) {
Env env = args.Env();
Value in = args[0];
int64_t address;
if (in.IsNumber()) {
address = in.As<Number>();
} else if (in.IsString()) {
char* endptr;
char* str;
int base = 0;
std::string _str = in.As<String>();
str = &_str[0];
errno = 0; /* To distinguish success/failure after call */
address = strtoll(str, &endptr, base);
if (endptr == str) {
throw TypeError::New(env, "readExternalArrayBuffer: no digits we found in input String");
} else if (errno == ERANGE && (address == INT64_MAX || address == INT64_MIN)) {
throw TypeError::New(env, "readExternalArrayBuffer: input String numerical value out of range");
} else if (errno != 0 && address == 0) {
char errmsg[200];
snprintf(errmsg, sizeof(errmsg), "readExternalArrayBuffer: %s", strerror(errno));
throw TypeError::New(env, errmsg);
}
}
int64_t length = args[1].ToNumber();
return ArrayBuffer::New(env, (void*) address, (size_t) length);
}
/**
* Writes the memory address of the "input" buffer (and optional offset) to the
* specified "buf" buffer and offset. Essentially making "buf" hold a reference
* to the "input" Buffer.
*
* args[0] - Buffer - the "buf" Buffer instance to write to
* args[1] - Number - the offset from the "buf" buffer's address to write to
* args[2] - Buffer - the "input" Buffer whose memory address will be written
*/
void WritePointer(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
Value input = args[2];
if (!input.IsNull() && !input.IsBuffer()) {
throw TypeError::New(env, "writePointer: Buffer instance expected as third argument");
}
if (input.IsNull()) {
*reinterpret_cast<char**>(ptr) = nullptr;
} else {
if ((args.Length() == 4) && (args[3].As<Boolean>() == true)) {
// create a node-api reference and finalizer to ensure that
// the buffer whoes pointer is written can only be
// collected after the finalizers for the buffer
// to which the pointer was written have already run
Reference<Value>* ref = new Reference<Value>;
*ref = Persistent(args[2]);
args[0].As<Object>().AddFinalizer([](Env env, Reference<Value>* ref) {
delete ref;
}, ref);
}
char* input_ptr = GetBufferData(input);
*reinterpret_cast<char**>(ptr) = input_ptr;
}
}
/**
* Reads a machine-endian int64_t from the given Buffer at the given offset.
*
* args[0] - Buffer - the "buf" Buffer instance to read from
* args[1] - Number - the offset from the "buf" buffer's address to read from
*/
Value ReadInt64(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
if (ptr == nullptr) {
throw TypeError::New(env, "readInt64: Cannot read from nullptr pointer");
}
int64_t val = *reinterpret_cast<int64_t*>(ptr);
if (val < JS_MIN_INT || val > JS_MAX_INT) {
char strbuf[128];
snprintf(strbuf, 128, "%" PRId64, val);
return String::New(env, strbuf);
} else {
return Number::New(env, val);
}
}
/**
* Writes the input Number/String int64 value as a machine-endian int64_t to
* the given Buffer at the given offset.
*
* args[0] - Buffer - the "buf" Buffer instance to write to
* args[1] - Number - the offset from the "buf" buffer's address to write to
* args[2] - String/Number - the "input" String or Number which will be written
*/
void WriteInt64(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
Value in = args[2];
int64_t val;
if (in.IsNumber()) {
val = in.As<Number>();
} else if (in.IsString()) {
char* endptr;
char* str;
int base = 0;
std::string _str = in.As<String>();
str = &_str[0];
errno = 0; /* To distinguish success/failure after call */
val = strtoll(str, &endptr, base);
if (endptr == str) {
throw TypeError::New(env, "writeInt64: no digits we found in input String");
} else if (errno == ERANGE && (val == INT64_MAX || val == INT64_MIN)) {
throw TypeError::New(env, "writeInt64: input String numerical value out of range");
} else if (errno != 0 && val == 0) {
char errmsg[200];
snprintf(errmsg, sizeof(errmsg), "writeInt64: %s", strerror(errno));
throw TypeError::New(env, errmsg);
}
} else {
throw TypeError::New(env, "writeInt64: Number/String 64-bit value required");
}
*reinterpret_cast<int64_t*>(ptr) = val;
}
/**
* Reads a machine-endian uint64_t from the given Buffer at the given offset.
*
* args[0] - Buffer - the "buf" Buffer instance to read from
* args[1] - Number - the offset from the "buf" buffer's address to read from
*/
Value ReadUInt64(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
if (ptr == nullptr) {
throw TypeError::New(env, "readUInt64: Cannot read from nullptr pointer");
}
uint64_t val = *reinterpret_cast<uint64_t*>(ptr);
if (val > JS_MAX_INT) {
char strbuf[128];
snprintf(strbuf, 128, "%" PRIu64, val);
return String::New(env, strbuf);
} else {
return Number::New(env, val);
}
}
/**
* Writes the input Number/String uint64 value as a machine-endian uint64_t to
* the given Buffer at the given offset.
*
* args[0] - Buffer - the "buf" Buffer instance to write to
* args[1] - Number - the offset from the "buf" buffer's address to write to
* args[2] - String/Number - the "input" String or Number which will be written
*/
void WriteUInt64(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
Value in = args[2];
uint64_t val;
if (in.IsNumber()) {
val = static_cast<int64_t>(in.As<Number>());
} else if (in.IsString()) {
char* endptr;
char* str;
int base = 0;
std::string _str = in.As<String>();
str = &_str[0];
errno = 0; /* To distinguish success/failure after call */
val = strtoull(str, &endptr, base);
if (endptr == str) {
throw TypeError::New(env, "writeUInt64: no digits we found in input String");
} else if (errno == ERANGE && (val == UINT64_MAX)) {
throw TypeError::New(env, "writeUInt64: input String numerical value out of range");
} else if (errno != 0 && val == 0) {
char errmsg[200];
snprintf(errmsg, sizeof(errmsg), "writeUInt64: %s", strerror(errno));
throw TypeError::New(env, errmsg);
}
} else {
throw TypeError::New(env, "writeUInt64: Number/String 64-bit value required");
}
*reinterpret_cast<uint64_t*>(ptr) = val;
}
/**
* Reads a Utf8 C String from the given pointer at the given offset (or 0).
* I didn't want to add this function but it ends up being necessary for reading
* past a 0 or 1 length Buffer's boundary in node-ffi :\
*
* args[0] - Buffer - the "buf" Buffer instance to read from
* args[1] - Number - the offset from the "buf" buffer's address to read from
*/
Value ReadCString(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args);
if (ptr == nullptr) {
throw Error::New(env, "readCString: Cannot read from nullptr pointer");
}
return String::New(env, ptr);
}
/**
* Returns a new Buffer instance that has the same memory address
* as the given buffer, but with the specified size.
*
* args[0] - Buffer - the "buf" Buffer instance to read the address from
* args[1] - Number - the size in bytes that the returned Buffer should be
* args[2] - Number - the offset from the "buf" buffer's address to read from
*/
Value ReinterpretBuffer(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args, 2);
if (ptr == nullptr) {
throw Error::New(env, "reinterpret: Cannot reinterpret from nullptr pointer");
}
int64_t size = args[1].ToNumber();
return WrapPointer(env, ptr, size);
}
/**
* Returns a new Buffer instance that has the same memory address
* as the given buffer, but with a length up to the first aligned set of values of
* 0 in a row for the given length.
*
* args[0] - Buffer - the "buf" Buffer instance to read the address from
* args[1] - Number - the number of sequential 0-byte values that need to be read
* args[2] - Number - the offset from the "buf" buffer's address to read from
*/
Value ReinterpretBufferUntilZeros(const CallbackInfo& args) {
Env env = args.Env();
char* ptr = AddressForArgs(args, 2);
if (ptr == nullptr) {
throw Error::New(env, "reinterpretUntilZeros: Cannot reinterpret from nullptr pointer");
}
uint32_t numZeros = args[1].ToNumber();
uint32_t i = 0;
size_t size = 0;
bool end = false;
while (!end && size < kMaxLength) {
end = true;
for (i = 0; i < numZeros; i++) {
if (ptr[size + i] != 0) {
end = false;
break;
}
}
if (!end) {
size += numZeros;
}
}
return WrapPointer(env, ptr, size);
}
} // anonymous namespace
Object Init(Env env, Object exports) {
InstanceData* data = new InstanceData(env);
{
Value buffer_ctor = env.Global()["Buffer"];
Value buffer_from = buffer_ctor.As<Object>()["from"];
data->buffer_from.Reset(buffer_from.As<Function>(), 1);
assert(!data->buffer_from.IsEmpty());
napi_status status = napix_set_instance_data(
env, data, [](napi_env env, void* data, void* hint) {
delete static_cast<InstanceData*>(data);
}, nullptr);
if (status != napi_ok) {
delete data;
data = nullptr;
} else {
// Hack around the fact that we can't reset buffer_from from the
// InstanceData dtor.
buffer_from.As<Object>().AddFinalizer([](Env env, InstanceData* data) {
data->buffer_from.Reset();
}, data);
}
}
exports["instance"] = External<RefNapi::Instance>::New(env, data);
// "sizeof" map
Object smap = Object::New(env);
// fixed sizes
#define SET_SIZEOF(name, type) \
smap[ #name ] = Number::New(env, sizeof(type));
SET_SIZEOF(int8, int8_t);
SET_SIZEOF(uint8, uint8_t);
SET_SIZEOF(int16, int16_t);
SET_SIZEOF(uint16, uint16_t);
SET_SIZEOF(int32, int32_t);
SET_SIZEOF(uint32, uint32_t);
SET_SIZEOF(int64, int64_t);
SET_SIZEOF(uint64, uint64_t);
SET_SIZEOF(float, float);
SET_SIZEOF(double, double);
// (potentially) variable sizes
SET_SIZEOF(bool, bool);
SET_SIZEOF(byte, unsigned char);
SET_SIZEOF(char, char);
SET_SIZEOF(uchar, unsigned char);
SET_SIZEOF(short, short);
SET_SIZEOF(ushort, unsigned short);
SET_SIZEOF(int, int);
SET_SIZEOF(uint, unsigned int);
SET_SIZEOF(long, long);
SET_SIZEOF(ulong, unsigned long);
SET_SIZEOF(longlong, long long);
SET_SIZEOF(ulonglong, unsigned long long);
SET_SIZEOF(pointer, char *);
SET_SIZEOF(size_t, size_t);
// size of a weak handle to a JS object
SET_SIZEOF(Object, Reference<Object>);
// "alignof" map
Object amap = Object::New(env);
#define SET_ALIGNOF(name, type) \
struct s_##name { type a; }; \
amap[ #name ] = Number::New(env, alignof(struct s_##name));
SET_ALIGNOF(int8, int8_t);
SET_ALIGNOF(uint8, uint8_t);
SET_ALIGNOF(int16, int16_t);
SET_ALIGNOF(uint16, uint16_t);
SET_ALIGNOF(int32, int32_t);
SET_ALIGNOF(uint32, uint32_t);
SET_ALIGNOF(int64, int64_t);
SET_ALIGNOF(uint64, uint64_t);
SET_ALIGNOF(float, float);
SET_ALIGNOF(double, double);
SET_ALIGNOF(bool, bool);
SET_ALIGNOF(char, char);
SET_ALIGNOF(uchar, unsigned char);
SET_ALIGNOF(short, short);
SET_ALIGNOF(ushort, unsigned short);
SET_ALIGNOF(int, int);
SET_ALIGNOF(uint, unsigned int);
SET_ALIGNOF(long, long);
SET_ALIGNOF(ulong, unsigned long);
SET_ALIGNOF(longlong, long long);
SET_ALIGNOF(ulonglong, unsigned long long);
SET_ALIGNOF(pointer, char *);
SET_ALIGNOF(size_t, size_t);
SET_ALIGNOF(Object, Reference<Object>);
// exports
exports["sizeof"] = smap;
exports["alignof"] = amap;
exports["nullptr"] = exports["NULL"] = WrapPointer(env, nullptr, 0);
exports["address"] = Function::New(env, Address);
exports["hexAddress"] = Function::New(env, HexAddress);
exports["isNull"] = Function::New(env, IsNull);
exports["readObject"] = Function::New(env, ReadObject);
exports["_writeObject"] = Function::New(env, WriteObject);
exports["readPointer"] = Function::New(env, ReadPointer);
exports["_writePointer"] = Function::New(env, WritePointer);
exports["readInt64"] = Function::New(env, ReadInt64);
exports["writeInt64"] = Function::New(env, WriteInt64);
exports["readUInt64"] = Function::New(env, ReadUInt64);
exports["writeUInt64"] = Function::New(env, WriteUInt64);
exports["readCString"] = Function::New(env, ReadCString);
exports["_reinterpret"] = Function::New(env, ReinterpretBuffer);
exports["_reinterpretUntilZeros"] = Function::New(env, ReinterpretBufferUntilZeros);
exports["readExternalArrayBuffer"] = Function::New(env, ReadExternalArrayBuffer);
return exports;
}
NODE_API_MODULE(binding, Init)