This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBuffer.cs
More file actions
662 lines (585 loc) · 26.1 KB
/
Copy pathBuffer.cs
File metadata and controls
662 lines (585 loc) · 26.1 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
/*
Copyright 2014 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Jurassic;
using Jurassic.Library;
using SharpJS.Helpers;
using System;
using System.Text;
namespace SharpJS.Libraries
{
// TODO: Think about a proper CLR-way to optimize memory allocation.
public class Buffer : Module
{
public readonly BufferConstructor Constructor;
private int InspectMaxBytes = 50;
public Buffer(Context context) : base(context) {
Constructor = new BufferConstructor(context);
SetPropertyValue("Buffer", Constructor, true);
SetPropertyValue("SlowBuffer", Constructor, true);
}
public override Module RegisterGlobals() {
Engine.SetGlobalValue("Buffer", Constructor);
return this;
}
[JSProperty(Name = "INSPECT_MAX_BYTES", IsEnumerable = true)]
public int INSPECT_MAX_BYTES {
get {
return InspectMaxBytes;
}
set {
InspectMaxBytes = value;
}
}
}
public class BufferConstructor : ConstructorFunction
{
public BufferConstructor(Context context) : base(context, "Buffer", new BufferInstance(context.Engine.Object.InstancePrototype, false)) {
Populate();
}
[JSConstructorFunction]
public BufferInstance Construct(object subject, [DefaultParameterValue(null)] object encoding = null) {
if (subject is byte[]) {
byte[] b = subject as byte[];
return new BufferInstance(Context, this.InstancePrototype, b, 0, b.Length);
} else if (TypeUtil.IsNumber(subject)) {
int size = TypeUtil.ToInteger(subject);
if (size < 0)
throw new JavaScriptException(Engine, "RangeError", "size < 0");
return new BufferInstance(Context, this.InstancePrototype, new byte[size], 0, size);
} else if (TypeUtil.IsArray(subject)) {
ArrayInstance array = TypeUtil.ToArray(subject);
byte[] b = new byte[array.Length];
int i = 0;
foreach (object v in array.ElementValues) {
try {
b[i++] = unchecked((byte)TypeConverter.ToUint32(v));
} catch (Exception ex) {
throw new JavaScriptException(Engine, "TypeError", ex.Message);
}
}
return new BufferInstance(Context, this.InstancePrototype, b, 0, b.Length);
} else if (TypeUtil.IsString(subject)) {
string str = TypeUtil.ToString(subject);
Encoding enc = Context.GetEncoding(encoding as string);
if (enc == null)
throw new JavaScriptException(Engine, "TypeError", "Unknown encoding: " + encoding);
try {
byte[] b = enc.GetBytes(str);
return new BufferInstance(Context, this.InstancePrototype, b, 0, b.Length);
} catch (Exception ex) {
throw new JavaScriptException(Engine, "TypeError", ex.Message);
}
} else {
throw new JavaScriptException(Engine, "TypeError", "subject is not a number, array or string");
}
}
[JSFunction(Name = "isBuffer", IsEnumerable = true)]
public bool IsBuffer(object obj) {
return obj != null && obj is BufferInstance;
}
[JSFunction(Name = "isEncoding", IsEnumerable = true)]
public bool IsEncoding(object encoding) {
return Context.GetEncoding(encoding as string) != null;
}
[JSFunction(Name = "byteLength", IsEnumerable = true)]
public int ByteLength(string str) {
return ByteLength(str, "utf8");
}
[JSFunction(Name = "byteLength", IsEnumerable = true)]
public int ByteLength(string str, object encoding) {
if (encoding == Undefined.Value || encoding == null || encoding == Null.Value)
encoding = "utf8";
Encoding enc = Context.GetEncoding(encoding as string);
if (enc == null)
throw new JavaScriptException(Engine, "TypeError", "Unknown encoding: " + enc);
return enc.GetByteCount(str);
}
}
public class BufferInstance : ContextObjectInstance
{
internal byte[] Buffer;
internal int Offset;
internal int Limit;
// Prototype constructor
public BufferInstance(ObjectInstance nextPrototype, bool extended) : base(nextPrototype, true) {
if (!extended)
Populate();
}
// Instance constructor
public BufferInstance(Context context, ObjectInstance thisPrototype, byte[] buffer, int offset, int limit) : base(context, thisPrototype) {
Buffer = buffer;
Offset = offset;
Limit = limit;
}
// Array access
protected override object GetMissingPropertyValue(string propertyName) {
try {
return (uint)Buffer[Offset + Convert.ToUInt32(propertyName)];
} catch (Exception ex) {
return null;
}
}
public override void SetPropertyValue(uint index, object value, bool throwOnError) {
try {
Buffer[Offset + index] = unchecked((byte)TypeConverter.ToInt32(value));
} catch (Exception) {
}
}
[JSProperty(Name = "length", IsEnumerable = false)]
public int Length {
get {
return Limit - Offset;
}
}
// NOTE: This does not implement noAssert behaviour. It's cumbersome, has a huge overhad and has no real value.
// Instead values will always be converted unchecked to the target type and an exception will be thrown when
// the offset is out of range.
// Unsigned
[JSFunction(Name = "readUInt8", IsEnumerable = true)]
public uint ReadUInt8(int offset) {
int o = offset + Offset;
if (o < Offset || o + 1 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
return Buffer[o];
}
[JSFunction(Name = "writeUInt8", IsEnumerable = true)]
public int WriteUInt8(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 1 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
uint v = TypeConverter.ToUint32(value);
unchecked {
Buffer[o] = (byte)v;
}
return o + 1;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
} catch (OverflowException) {
throw new JavaScriptException(Engine, "RangeError", "index out of range");
}
}
[JSFunction(Name = "readUInt16LE", IsEnumerable = true)]
public uint ReadUInt16LE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 2 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
return unchecked((ushort)((Buffer[o + 1] << 8) | Buffer[o]));
}
[JSFunction(Name = "writeUInt16LE", IsEnumerable = true)]
public int WriteUInt16LE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 2 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
uint v = TypeConverter.ToUint32(value);
unchecked {
Buffer[o + 1] = (byte)((v >> 8) & 0xFF);
Buffer[o ] = (byte)( v & 0xFF);
}
return o + 2;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
[JSFunction(Name = "readUInt16BE", IsEnumerable = true)]
public uint ReadUInt16BE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 2 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
return unchecked((uint)(
(Buffer[o ] << 8) |
Buffer[o + 1]
));
}
[JSFunction(Name = "writeUInt16BE", IsEnumerable = true)]
public int WriteUInt16BE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 2 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
uint v = TypeConverter.ToUint32(value);
unchecked {
Buffer[o ] = (byte)((v >> 8) & 0xFF);
Buffer[o + 1] = (byte)( v & 0xFF);
}
return o + 2;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
[JSFunction(Name = "readUInt32LE", IsEnumerable = true)]
public uint ReadUInt32LE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 2 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
return unchecked((uint)(
(Buffer[o + 3] << 24) |
(Buffer[o + 2] << 16) |
(Buffer[o + 1] << 8) |
Buffer[o]
));
}
[JSFunction(Name = "writeUInt32LE", IsEnumerable = true)]
public int WriteUInt32LE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 4 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
uint v = TypeConverter.ToUint32(value);
unchecked {
Buffer[o + 3] = (byte)((v >> 24) & 0xFF);
Buffer[o + 2] = (byte)((v >> 16) & 0xFF);
Buffer[o + 1] = (byte)((v >> 8) & 0xFF);
Buffer[o ] = (byte)( v & 0xFF);
}
return o + 4;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
[JSFunction(Name = "readUInt32BE", IsEnumerable = true)]
public uint ReadUInt32BE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 4 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
return unchecked((uint)(
(Buffer[o ] << 24) |
(Buffer[o + 1] << 16) |
(Buffer[o + 2] << 8) |
Buffer[o + 3]
));
}
[JSFunction(Name = "writeUInt32BE", IsEnumerable = true)]
public int WriteUInt32BE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 4 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
uint v = TypeConverter.ToUint32(value);
unchecked {
Buffer[o ] = (byte)((v >> 24) & 0xFF);
Buffer[o + 1] = (byte)((v >> 16) & 0xFF);
Buffer[o + 2] = (byte)((v >> 8) & 0xFF);
Buffer[o + 3] = (byte)( v & 0xFF);
}
return o + 4;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
// Signed
[JSFunction(Name = "readInt8", IsEnumerable = true)]
public int ReadInt8(int offset) {
return unchecked((sbyte)ReadUInt8(offset));
}
[JSFunction(Name = "writeInt8", IsEnumerable = true)]
public int WriteInt8(object value, int offset) {
return WriteUInt8(value, offset);
}
[JSFunction(Name = "readInt16LE", IsEnumerable = true)]
public int ReadInt16LE(int offset) {
return unchecked((short)ReadUInt16LE(offset));
}
[JSFunction(Name = "writeInt16LE", IsEnumerable = true)]
public int WriteInt16LE(object value, int offset) {
return WriteUInt16LE(value, offset);
}
[JSFunction(Name = "readInt16BE", IsEnumerable = true)]
public int ReadInt16BE(int offset) {
return unchecked((short)ReadUInt16BE(offset));
}
[JSFunction(Name = "writeInt16BE", IsEnumerable = true)]
public int WriteInt16BE(object value, int offset) {
return WriteUInt16BE(value, offset);
}
[JSFunction(Name = "readInt32LE", IsEnumerable = true)]
public int ReadInt32LE(int offset) {
return unchecked((int)ReadUInt32LE(offset));
}
[JSFunction(Name = "writeInt32LE", IsEnumerable = true)]
public int WriteInt32LE(object value, int offset) {
return WriteUInt32LE(value, offset);
}
[JSFunction(Name = "readInt32BE", IsEnumerable = true)]
public int ReadInt32BE(int offset) {
return unchecked((int)ReadUInt32BE(offset));
}
[JSFunction(Name = "writeInt32BE", IsEnumerable = true)]
public int WriteInt32BE(object value, int offset) {
return WriteUInt32BE(value, offset);
}
// Floats
private byte[] FourBytes = new byte[4];
private byte[] EightBytes = new byte[8];
[JSFunction(Name = "readFloatLE", IsEnumerable = true)]
public float ReadFloatLE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 4 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
if (BitConverter.IsLittleEndian)
return BitConverter.ToSingle(Buffer, o);
else {
Buffer.CopyTo(FourBytes, o);
Array.Reverse(FourBytes);
return BitConverter.ToSingle(FourBytes, o);
}
}
[JSFunction(Name = "writeFloatLE", IsEnumerable = true)]
public int WriteFloatLE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 4 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
float v = Convert.ToSingle(TypeConverter.ToNumber(value));
if (BitConverter.IsLittleEndian) {
BitConverter.GetBytes(v).CopyTo(Buffer, o);
} else {
byte[] vb = BitConverter.GetBytes(v);
Array.Reverse(vb);
vb.CopyTo(Buffer, o);
}
return o + 4;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
[JSFunction(Name = "readFloatBE", IsEnumerable = true)]
public float ReadFloatBE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 4 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
if (!BitConverter.IsLittleEndian)
return BitConverter.ToSingle(Buffer, o);
else {
Buffer.CopyTo(FourBytes, o);
Array.Reverse(FourBytes);
return BitConverter.ToSingle(FourBytes, o);
}
}
[JSFunction(Name = "writeFloatBE", IsEnumerable = true)]
public int WriteFloatBE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 4 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
float v = Convert.ToSingle(TypeConverter.ToNumber(value));
if (!BitConverter.IsLittleEndian) {
BitConverter.GetBytes(v).CopyTo(Buffer, o);
} else {
byte[] vb = BitConverter.GetBytes(v);
Array.Reverse(vb);
vb.CopyTo(Buffer, o);
}
return o + 4;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
// Doubles
[JSFunction(Name = "readDoubleLE", IsEnumerable = true)]
public double ReadDoubleLE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 8 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
if (BitConverter.IsLittleEndian)
return BitConverter.ToDouble(Buffer, o);
else {
Buffer.CopyTo(EightBytes, o);
Array.Reverse(EightBytes);
return BitConverter.ToDouble(EightBytes, o);
}
}
[JSFunction(Name = "writeDoubleLE", IsEnumerable = true)]
public int WriteDoubleLE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 8 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
double v = TypeConverter.ToNumber(value);
if (BitConverter.IsLittleEndian) {
BitConverter.GetBytes(v).CopyTo(Buffer, o);
} else {
byte[] vb = BitConverter.GetBytes(v);
Array.Reverse(vb);
vb.CopyTo(Buffer, o);
}
return o + 8;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
[JSFunction(Name = "readDoubleBE", IsEnumerable = true)]
public double ReadDoubleBE(int offset) {
int o = offset + Offset;
if (o < Offset || o + 8 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
if (!BitConverter.IsLittleEndian)
return BitConverter.ToDouble(Buffer, o);
else {
Buffer.CopyTo(EightBytes, o);
Array.Reverse(EightBytes);
return BitConverter.ToDouble(EightBytes, o);
}
}
[JSFunction(Name = "writeDoubleBE", IsEnumerable = true)]
public int WriteDoubleBE(object value, int offset) {
int o = offset + Offset;
if (o < Offset || o + 8 > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
try {
double v = TypeConverter.ToNumber(value);
if (!BitConverter.IsLittleEndian) {
BitConverter.GetBytes(v).CopyTo(Buffer, o);
} else {
byte[] vb = BitConverter.GetBytes(v);
Array.Reverse(vb);
vb.CopyTo(Buffer, o);
}
return o + 8;
} catch (ArgumentException) {
throw new JavaScriptException(Engine, "TypeError", "illegal value");
}
}
// Strings
[JSFunction(Name = "write", IsEnumerable = true)]
public int Write(string str, object encoding) {
return Write(str, 0, "utf8");
}
[JSFunction(Name = "write", IsEnumerable = true)]
public int Write(string str, int offset, object encoding) {
return Write(str, 0, Limit - Offset, encoding);
}
[JSFunction(Name = "write", IsEnumerable = true)]
public int Write(string str, int offset, int length, object encoding) {
if (encoding == Undefined.Value || encoding == null || encoding == Null.Value) {
encoding = "utf8";
}
Encoding enc = Context.GetEncoding(encoding as string);
if (enc == null)
throw new JavaScriptException(Engine, "TypeError", "Unknown encoding: "+encoding);
byte[] b = enc.GetBytes(str);
int o = offset + Offset;
int e = offset + Offset + length;
if (o < Offset || e > Limit || e < o)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
for (int i = o, j = 0; i < e; ++i, ++j) {
Buffer[i] = b[j];
}
return offset + length;
}
[JSFunction(Name = "toString", IsEnumerable = false)]
public string toString([DefaultParameterValue("utf8")] string encoding = "utf8", [DefaultParameterValue(0)] int start = 0) {
return toString(encoding, start, Limit - Offset);
}
[JSFunction(Name = "toString", IsEnumerable = false)]
public string toString(string encoding, int start, int end) {
if (start == end)
return "";
int s = start + Offset;
int e = end + Offset;
if (s < Offset || e > Limit || e < s)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
Encoding enc = Context.GetEncoding(encoding);
if (enc == null)
throw new JavaScriptException(Engine, "TypeError", "Unknown encoding: " + encoding);
try {
return enc.GetString(Buffer, s, e - s);
} catch (Exception ex) {
throw new JavaScriptException(Engine, "TypeError", ex.Message);
}
}
[JSFunction(Name = "inspect", IsEnumerable = true)]
public string Inspect() {
StringBuilder sb = new StringBuilder();
sb.Append("Buffer");
for (var i = Offset; i < Math.Min(Limit, Offset + Context.Buffer.INSPECT_MAX_BYTES); ++i) {
sb.Append(" " + Buffer[i].ToString("X"));
}
return sb.ToString();
}
// Methods
[JSFunction(Name = "slice", IsEnumerable = true)]
public BufferInstance Slice() {
return new BufferInstance(Context, Context.Buffer.Constructor.InstancePrototype, Buffer, Offset, Limit);
}
[JSFunction(Name = "slice", IsEnumerable = true)]
public BufferInstance Slice(int start) {
if (start < Offset || start > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
BufferInstance buffer = Slice();
buffer.Offset = start;
return buffer;
}
[JSFunction(Name = "slice", IsEnumerable = true)]
public BufferInstance Slice(int start, int end) {
if (end < start || end > Limit)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
BufferInstance buffer = Slice(start);
buffer.Limit = end;
return buffer;
}
[JSFunction(Name = "copy", IsEnumerable = true)]
public void Copy(BufferInstance targetBuffer) {
Copy(targetBuffer, 0);
}
[JSFunction(Name = "copy", IsEnumerable = true)]
public void Copy(BufferInstance targetBuffer, int targetStart) {
Copy(targetBuffer, targetStart, 0);
}
[JSFunction(Name = "copy", IsEnumerable = true)]
public void Copy(BufferInstance targetBuffer, int targetStart, int sourceStart) {
Copy(targetBuffer, targetStart, sourceStart, Limit - Offset);
}
[JSFunction(Name = "copy", IsEnumerable = true)]
public void Copy(BufferInstance targetBuffer, int targetStart, int sourceStart, int sourceEnd) {
int len = sourceEnd - sourceStart;
if (targetStart < targetBuffer.Offset || targetStart + len > targetBuffer.Limit)
throw new JavaScriptException(Engine, "RangeError", "target index out of range");
if (sourceStart < Offset || sourceEnd > Limit || sourceEnd < sourceStart)
throw new JavaScriptException(Engine, "RangeError", "source index out of range");
for (int i = sourceStart, j = targetStart; i < sourceEnd; ++i, ++j) {
targetBuffer.Buffer[j] = Buffer[i];
}
}
[JSFunction(Name = "fill", IsEnumerable = true)]
public void Fill(object value) {
Fill(value, 0, Limit - Offset);
}
[JSFunction(Name = "fill", IsEnumerable = true)]
public void Fill(object value, int offset) {
Fill(value, offset, Limit - Offset);
}
[JSFunction(Name = "fill", IsEnumerable = true)]
public /* is this */ void /* ? */ Fill(object value, int offset, int end) {
int o = offset + Offset;
int e = end + Offset;
if (o < Offset || e > Limit || e < o)
throw new JavaScriptException(Engine, "RangeError", "index out of range");
byte val;
if (value is string) {
if ((value as string).Length < 1)
throw new JavaScriptException(Engine, "TypeError", "empty string");
val = BitConverter.GetBytes((value as string)[0])[0];
} else if (value is int) {
val = unchecked(BitConverter.GetBytes((sbyte)value)[0]);
} else if (value is uint) {
val = unchecked(BitConverter.GetBytes((byte)value)[0]);
} else
throw new JavaScriptException(Engine, "TypeError", "illegal value");
for (var i = offset; i < end; i++) {
Buffer[i] = val;
}
}
}
}