|
16 | 16 | #include <string.h> |
17 | 17 | #include <node.h> |
18 | 18 | #include <nan.h> |
| 19 | +#include <ArrayBuffer.h> |
| 20 | +#include "memcpy.h" |
19 | 21 |
|
20 | 22 | using namespace v8; |
21 | 23 |
|
22 | 24 | // Copies data between kExternalUnsignedByteArrays like node Buffers and ArrayBuffers. |
23 | 25 | // memcpy(target[, targetStart], source[, sourceStart[, sourceEnd]]):bytesCopied |
24 | 26 | NAN_METHOD(memcpy){ |
25 | 27 | NanScope(); |
| 28 | + if (sizeof(unsigned char) != 1) { |
| 29 | + NanThrowError("sizeof(unsigned char) != 1"); |
| 30 | + NanReturnUndefined(); |
| 31 | + } |
26 | 32 |
|
27 | | - // Parse arguments |
| 33 | + unsigned char* targetData; |
| 34 | + size_t targetStart = 0, |
| 35 | + targetLength; |
| 36 | + |
| 37 | + unsigned char* sourceData; |
| 38 | + size_t sourceStart = 0, |
| 39 | + sourceLength, |
| 40 | + sourceEnd; |
| 41 | + |
| 42 | + // Requires at least two arguments: target and source |
28 | 43 | if (args.Length() < 2) { |
29 | | - NanThrowTypeError("Illegal number of arguments"); |
| 44 | + NanThrowTypeError("illegal number of arguments"); |
30 | 45 | NanReturnUndefined(); |
31 | 46 | } |
32 | 47 | int i = 0; |
| 48 | + |
| 49 | + // Target must be an object |
33 | 50 | if (!args[i]->IsObject()) { |
34 | | - NanThrowTypeError("Illegal target: Not an object"); |
| 51 | + NanThrowTypeError("illegal target: not an object"); |
35 | 52 | NanReturnUndefined(); |
36 | 53 | } |
37 | | - Local<Object> target = args[i++]->ToObject(); |
38 | | - if (target->GetIndexedPropertiesExternalArrayDataType() != kExternalUnsignedByteArray) { |
39 | | - NanThrowTypeError("Illegal target: Not a valid kExternalUnsignedByteArray"); |
| 54 | + Local<Object> target = args[i]->ToObject(); |
| 55 | + |
| 56 | + // Target must reference an unsigned byte array (or be an ArrayBuffer) |
| 57 | + if (target->GetIndexedPropertiesExternalArrayDataType() == kExternalUnsignedByteArray) { |
| 58 | + targetData = static_cast<unsigned char*>(target->GetIndexedPropertiesExternalArrayData()); |
| 59 | + targetLength = target->GetIndexedPropertiesExternalArrayDataLength(); |
| 60 | + } else { |
| 61 | +#if NODE_MAJOR_VERSION > 0 || NODE_MINOR_VERSION >= 12 |
| 62 | + if (!args[i]->IsArrayBuffer()) { |
| 63 | + NanThrowTypeError("illegal target: not an ArrayBuffer"); |
| 64 | + NanReturnUndefined(); |
| 65 | + } |
| 66 | + node::ArrayBuffer* ab = node::ArrayBuffer::New(args[i]); |
| 67 | + targetData = static_cast<unsigned char*>(ab->Data()); |
| 68 | + targetLength = ab->ByteLength(); |
| 69 | +#else |
| 70 | + NanThrowTypeError("illegal target: not a kExternalUnsignedByteArray"); |
40 | 71 | NanReturnUndefined(); |
| 72 | +#endif |
41 | 73 | } |
42 | | - int targetStart = 0; |
43 | | - int targetLength = target->GetIndexedPropertiesExternalArrayDataLength(); |
44 | | - if (args[i]->IsUint32()) { |
| 74 | + // If specified, targetStart must be an unsigned integer in [0,targetLength] |
| 75 | + if (args[++i]->IsUint32()) { |
45 | 76 | targetStart = args[i++]->ToUint32()->Value(); |
46 | 77 | if (targetStart < 0 || targetStart > targetLength) { |
47 | | - NanThrowTypeError("Illegal targetStart: Less than 0 or bigger than length"); |
| 78 | + NanThrowRangeError("illegal targetStart: out of bounds"); |
48 | 79 | NanReturnUndefined(); |
49 | 80 | } |
50 | 81 | } |
| 82 | + // Requires at least one additional argument: source |
51 | 83 | if (i >= args.Length()) { |
52 | | - NanThrowTypeError("Illegal number of arguments"); |
| 84 | + NanThrowTypeError("illegal number of arguments"); |
53 | 85 | NanReturnUndefined(); |
54 | 86 | } |
| 87 | + // Source must be an object |
55 | 88 | if (!args[i]->IsObject()) { |
56 | | - NanThrowTypeError("Illegal source: Not an object"); |
| 89 | + NanThrowTypeError("illegal source: not an object"); |
57 | 90 | NanReturnUndefined(); |
58 | 91 | } |
59 | | - Local<Object> source = args[i++]->ToObject(); |
60 | | - if (source->GetIndexedPropertiesExternalArrayDataType() != kExternalUnsignedByteArray) { |
61 | | - NanThrowTypeError("Illegal source: Not a valid kExternalUnsignedByteArray"); |
| 92 | + Local<Object> source = args[i]->ToObject(); |
| 93 | + |
| 94 | + // Source must reference an unsigned byte array (or be an ArrayBuffer) |
| 95 | + if (source->GetIndexedPropertiesExternalArrayDataType() == kExternalUnsignedByteArray) { |
| 96 | + sourceData = static_cast<unsigned char*>(source->GetIndexedPropertiesExternalArrayData()); |
| 97 | + sourceEnd = sourceLength = source->GetIndexedPropertiesExternalArrayDataLength(); |
| 98 | + } else { |
| 99 | +#if NODE_MAJOR_VERSION > 0 || NODE_MINOR_VERSION >= 12 |
| 100 | + if (!args[i]->IsArrayBuffer()) { |
| 101 | + NanThrowTypeError("illegal source: not an ArrayBuffer"); |
| 102 | + NanReturnUndefined(); |
| 103 | + } |
| 104 | + node::ArrayBuffer* ab = node::ArrayBuffer::New(args[i]); |
| 105 | + sourceData = static_cast<unsigned char*>(ab->Data()); |
| 106 | + sourceEnd = sourceLength = ab->ByteLength(); |
| 107 | +#else |
| 108 | + NanThrowTypeError("illegal target: not a kExternalUnsignedByteArray"); |
62 | 109 | NanReturnUndefined(); |
| 110 | +#endif |
63 | 111 | } |
64 | | - int sourceStart = 0; |
65 | | - int sourceLength = source->GetIndexedPropertiesExternalArrayDataLength(); |
66 | | - int sourceEnd = sourceLength; |
67 | | - if (i < args.Length()) { |
| 112 | + // If specified, sourceStart must be an unsigned integer in [0,sourceLength] |
| 113 | + if (++i < args.Length()) { |
68 | 114 | if (!args[i]->IsUint32()) { |
69 | | - NanThrowTypeError("Illegal sourceStart: Not an uint32"); |
| 115 | + NanThrowTypeError("illegal sourceStart: not an uint32"); |
70 | 116 | NanReturnUndefined(); |
71 | 117 | } |
72 | 118 | sourceStart = args[i++]->ToUint32()->Value(); |
73 | 119 | if (sourceStart < 0 || sourceStart > sourceLength) { |
74 | | - NanThrowTypeError("Illegal sourceStart: Less than 0 or bigger than length"); |
| 120 | + NanThrowTypeError("illegal sourceStart: out of bounds"); |
75 | 121 | NanReturnUndefined(); |
76 | 122 | } |
77 | 123 | } |
| 124 | + // If specified, sourceEnd must be an unsigned integer in [sourceStart,sourceLength] |
78 | 125 | if (i < args.Length()) { |
79 | 126 | if (!args[i]->IsUint32()) { |
80 | | - NanThrowTypeError("Illegal sourceEnd: Not an uint32"); |
| 127 | + NanThrowTypeError("illegal sourceEnd: not an uint32"); |
81 | 128 | NanReturnUndefined(); |
82 | 129 | } |
83 | 130 | sourceEnd = args[i++]->ToUint32()->Value(); |
84 | 131 | if (sourceEnd < sourceStart || sourceEnd > sourceLength) { |
85 | | - NanThrowTypeError("Illegal sourceEnd: Less than sourceStart or bigger than length"); |
| 132 | + NanThrowTypeError("illegal sourceEnd: out of bounds"); |
86 | 133 | NanReturnUndefined(); |
87 | 134 | } |
88 | 135 | } |
89 | | - if (i /* still */ < args.Length()) { |
90 | | - NanThrowTypeError("Illegal number of arguments"); |
| 136 | + // Additional arguments are invalid |
| 137 | + if (i < args.Length()) { |
| 138 | + NanThrowTypeError("illegal number of arguments"); |
91 | 139 | NanReturnUndefined(); |
92 | 140 | } |
93 | | - |
94 | | - // Perform sanity checks |
95 | | - int len = sourceEnd - sourceStart; |
96 | | - if (len == 0) { |
97 | | - NanReturnUndefined(); |
| 141 | + // Determine number of bytes to copy |
| 142 | + int length = sourceEnd - sourceStart; |
| 143 | + if (length == 0) { |
| 144 | + NanReturnValue(NanNew<Number>(length)); |
98 | 145 | } |
99 | | - if (targetStart + len > targetLength) { |
100 | | - NanThrowTypeError("Illegal source range: Target capacity overrun"); |
101 | | - NanReturnUndefined(); |
102 | | - } |
103 | | - if (sizeof(unsigned char) != 1) { |
104 | | - NanThrowTypeError("sizeof(unsigned char) != 1"); |
| 146 | + // Perform sanity checks |
| 147 | + if (targetStart + length > targetLength) { |
| 148 | + NanThrowTypeError("illegal source range: target capacity overrun"); |
105 | 149 | NanReturnUndefined(); |
106 | 150 | } |
107 | | - |
108 | 151 | // Do the thing (memmove to be compatible with native Buffer#copy) |
109 | 152 | memmove( |
110 | | - static_cast<unsigned char*>(target->GetIndexedPropertiesExternalArrayData()) + targetStart, |
111 | | - static_cast<unsigned char*>(source->GetIndexedPropertiesExternalArrayData()) + sourceStart, |
112 | | - len |
| 153 | + targetData + targetStart, |
| 154 | + sourceData + sourceStart, |
| 155 | + length |
113 | 156 | ); |
114 | | - |
115 | | - NanReturnValue(NanNew<Number>(len)); |
| 157 | + NanReturnValue(NanNew<Number>(length)); |
116 | 158 | } |
117 | 159 |
|
118 | 160 | void init(Handle<Object> exports) { |
|
0 commit comments