@@ -5,9 +5,9 @@ const kMaxLength = smalloc.kMaxLength;
5
5
const util = require ( 'util' ) ;
6
6
7
7
exports . alloc = alloc ;
8
- exports . copyOnto = smalloc . copyOnto ;
8
+ exports . copyOnto = copyOnto ;
9
9
exports . dispose = dispose ;
10
- exports . hasExternalData = smalloc . hasExternalData ;
10
+ exports . hasExternalData = hasExternalData ;
11
11
12
12
// don't allow kMaxLength to accidentally be overwritten. it's a lot less
13
13
// apparent when a primitive is accidentally changed.
@@ -50,13 +50,21 @@ function alloc(n, obj, type) {
50
50
throw new TypeError ( 'obj must be an Object' ) ;
51
51
}
52
52
53
+ if ( Array . isArray ( obj ) )
54
+ throw new TypeError ( 'obj cannot be an array' ) ;
55
+ if ( obj instanceof Buffer )
56
+ throw new TypeError ( 'obj cannot be a Buffer' ) ;
57
+ if ( smalloc . isTypedArray ( obj ) )
58
+ throw new TypeError ( 'obj cannot be a typed array' ) ;
59
+ if ( smalloc . hasExternalData ( obj ) )
60
+ throw new TypeError ( 'object already has external array data' ) ;
61
+
53
62
// 1 == v8::kExternalUint8Array, 9 == v8::kExternalUint8ClampedArray
54
63
if ( type < 1 || type > 9 )
55
64
throw new TypeError ( 'unknown external array type: ' + type ) ;
56
- if ( Array . isArray ( obj ) )
57
- throw new TypeError ( 'Arrays are not supported' ) ;
58
65
if ( n > kMaxLength )
59
- throw new RangeError ( 'n > kMaxLength' ) ;
66
+ throw new RangeError ( 'Attempt to allocate array larger than maximum ' +
67
+ 'size: 0x' + kMaxLength . toString ( 16 ) + ' elements' ) ;
60
68
61
69
return smalloc . alloc ( obj , n , type ) ;
62
70
}
@@ -70,7 +78,29 @@ function dispose(obj) {
70
78
if ( smalloc . isTypedArray ( obj ) )
71
79
throw new TypeError ( 'obj cannot be a typed array' ) ;
72
80
if ( ! smalloc . hasExternalData ( obj ) )
73
- throw new Error ( 'obj has no external array data' ) ;
81
+ throw new TypeError ( 'obj has no external array data' ) ;
74
82
75
83
smalloc . dispose ( obj ) ;
76
84
}
85
+
86
+
87
+ function copyOnto ( source , sourceStart , dest , destStart , copyLength ) {
88
+ if ( util . isPrimitive ( source ) )
89
+ throw new TypeError ( 'source must be an Object' ) ;
90
+ if ( util . isPrimitive ( dest ) )
91
+ throw new TypeError ( 'dest must be an Object' ) ;
92
+ if ( ! smalloc . hasExternalData ( source ) )
93
+ throw new TypeError ( 'source has no external array data' ) ;
94
+ if ( ! smalloc . hasExternalData ( dest ) )
95
+ throw new TypeError ( 'dest has no external array data' ) ;
96
+
97
+ return smalloc . copyOnto ( source , sourceStart , dest , destStart , copyLength ) ;
98
+ }
99
+
100
+
101
+ function hasExternalData ( obj ) {
102
+ if ( util . isPrimitive ( obj ) )
103
+ return false ;
104
+
105
+ return smalloc . hasExternalData ( obj ) ;
106
+ }
0 commit comments