Skip to content

Commit 8f5f12b

Browse files
committed
smalloc: export constants from C++
PR-URL: #920 Reviewed-By: Chris Dickinson <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 0697f8b commit 8f5f12b

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

lib/smalloc.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const smalloc = process.binding('smalloc');
44
const kMaxLength = smalloc.kMaxLength;
5+
const kMinType = smalloc.kMinType;
6+
const kMaxType = smalloc.kMaxType;
57
const util = require('util');
68

79
exports.alloc = alloc;
@@ -15,24 +17,8 @@ Object.defineProperty(exports, 'kMaxLength', {
1517
enumerable: true, value: kMaxLength, writable: false
1618
});
1719

18-
// enumerated values for different external array types
19-
var Types = {};
20-
21-
// Must match enum v8::ExternalArrayType.
22-
Object.defineProperties(Types, {
23-
'Int8': { enumerable: true, value: 1, writable: false },
24-
'Uint8': { enumerable: true, value: 2, writable: false },
25-
'Int16': { enumerable: true, value: 3, writable: false },
26-
'Uint16': { enumerable: true, value: 4, writable: false },
27-
'Int32': { enumerable: true, value: 5, writable: false },
28-
'Uint32': { enumerable: true, value: 6, writable: false },
29-
'Float': { enumerable: true, value: 7, writable: false },
30-
'Double': { enumerable: true, value: 8, writable: false },
31-
'Uint8Clamped': { enumerable: true, value: 9, writable: false }
32-
});
33-
3420
Object.defineProperty(exports, 'Types', {
35-
enumerable: true, value: Types, writable: false
21+
enumerable: true, value: Object.freeze(smalloc.types), writable: false
3622
});
3723

3824

@@ -59,8 +45,7 @@ function alloc(n, obj, type) {
5945
if (smalloc.hasExternalData(obj))
6046
throw new TypeError('object already has external array data');
6147

62-
// 1 == v8::kExternalUint8Array, 9 == v8::kExternalUint8ClampedArray
63-
if (type < 1 || type > 9)
48+
if (type < kMinType || type > kMaxType)
6449
throw new TypeError('unknown external array type: ' + type);
6550
if (n > kMaxLength)
6651
throw new RangeError('Attempt to allocate array larger than maximum ' +

src/smalloc.cc

+34
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
#include <string.h>
1111

1212
#define ALLOC_ID (0xA10C)
13+
#define EXTERNAL_ARRAY_TYPES(V) \
14+
V(Int8, kExternalInt8Array) \
15+
V(Uint8, kExternalUint8Array) \
16+
V(Int16, kExternalInt16Array) \
17+
V(Uint16, kExternalUint16Array) \
18+
V(Int32, kExternalInt32Array) \
19+
V(Uint32, kExternalUint32Array) \
20+
V(Float, kExternalFloat32Array) \
21+
V(Double, kExternalFloat64Array) \
22+
V(Uint8Clamped, kExternalUint8ClampedArray)
23+
24+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
25+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
26+
1327

1428
namespace node {
1529
namespace smalloc {
@@ -559,6 +573,7 @@ void Initialize(Handle<Object> exports,
559573
Handle<Value> unused,
560574
Handle<Context> context) {
561575
Environment* env = Environment::GetCurrent(context);
576+
Isolate* isolate = env->isolate();
562577

563578
env->SetMethod(exports, "copyOnto", CopyOnto);
564579
env->SetMethod(exports, "sliceOnto", SliceOnto);
@@ -573,6 +588,25 @@ void Initialize(Handle<Object> exports,
573588
exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"),
574589
Uint32::NewFromUnsigned(env->isolate(), kMaxLength));
575590

591+
Local<Object> types = Object::New(isolate);
592+
593+
uint32_t kMinType = ~0;
594+
uint32_t kMaxType = 0;
595+
#define V(name, value) \
596+
types->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
597+
Uint32::NewFromUnsigned(env->isolate(), v8::value)); \
598+
kMinType = MIN(kMinType, v8::value); \
599+
kMaxType = MAX(kMinType, v8::value);
600+
601+
EXTERNAL_ARRAY_TYPES(V)
602+
#undef V
603+
604+
exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "types"), types);
605+
exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kMinType"),
606+
Uint32::NewFromUnsigned(env->isolate(), kMinType));
607+
exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxType"),
608+
Uint32::NewFromUnsigned(env->isolate(), kMaxType));
609+
576610
HeapProfiler* heap_profiler = env->isolate()->GetHeapProfiler();
577611
heap_profiler->SetWrapperClassInfoProvider(ALLOC_ID, WrapperInfo);
578612
}

test/parallel/test-smalloc.js

+20
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,23 @@ assert.throws(function() {
309309
assert.throws(function() {
310310
smalloc.dispose({});
311311
});
312+
313+
314+
// Types should be immutable
315+
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(smalloc, 'Types'), {
316+
value: smalloc.Types,
317+
writable: false,
318+
enumerable: true,
319+
configurable: false
320+
});
321+
322+
var types = Object.keys(smalloc.Types);
323+
var Types = smalloc.Types;
324+
325+
for (var i = 0; i < types.length; i++)
326+
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(Types, types[i]), {
327+
value: Types[types[i]],
328+
writable: false,
329+
enumerable: true,
330+
configurable: false
331+
});

0 commit comments

Comments
 (0)