@@ -5,9 +5,9 @@ const kMaxLength = smalloc.kMaxLength;
55const util = require ( 'util' ) ;
66
77exports . alloc = alloc ;
8- exports . copyOnto = smalloc . copyOnto ;
8+ exports . copyOnto = copyOnto ;
99exports . dispose = dispose ;
10- exports . hasExternalData = smalloc . hasExternalData ;
10+ exports . hasExternalData = hasExternalData ;
1111
1212// don't allow kMaxLength to accidentally be overwritten. it's a lot less
1313// apparent when a primitive is accidentally changed.
@@ -50,13 +50,21 @@ function alloc(n, obj, type) {
5050 throw new TypeError ( 'obj must be an Object' ) ;
5151 }
5252
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+
5362 // 1 == v8::kExternalUint8Array, 9 == v8::kExternalUint8ClampedArray
5463 if ( type < 1 || type > 9 )
5564 throw new TypeError ( 'unknown external array type: ' + type ) ;
56- if ( Array . isArray ( obj ) )
57- throw new TypeError ( 'Arrays are not supported' ) ;
5865 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' ) ;
6068
6169 return smalloc . alloc ( obj , n , type ) ;
6270}
@@ -70,7 +78,29 @@ function dispose(obj) {
7078 if ( smalloc . isTypedArray ( obj ) )
7179 throw new TypeError ( 'obj cannot be a typed array' ) ;
7280 if ( ! smalloc . hasExternalData ( obj ) )
73- throw new Error ( 'obj has no external array data' ) ;
81+ throw new TypeError ( 'obj has no external array data' ) ;
7482
7583 smalloc . dispose ( obj ) ;
7684}
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