arraybuffer 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/ext/arraybuffer/dataview.c +56 -0
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19ea4341e8a445c5f0f7fb037201da95de458b1c598b19dfcdede555a7ada4af
4
- data.tar.gz: 55c21ac531706d232aeb9997c214ea99124e5c5d72ca94266abcf91b47431408
3
+ metadata.gz: 54a97a398cfc2dba3f1e7be91ad4270c5cda76d80d3a537e928f55c69a5a884d
4
+ data.tar.gz: 89a6d372984df0af25cc325f9df08819eb28f1b04a49998226f4efccb425ff4c
5
5
  SHA512:
6
- metadata.gz: 761a077323636bb899b3c764f1b6225e0af328468d70a82a8d2cad42e70c585eeda68c6bbb8bf23c2ba3b80f495fc93402ef9b7e92cea77936d1e22a7a8b48fc
7
- data.tar.gz: c1f26cdd5a15d3cb1068be495f8b1aee915b29a647fec7fc146420f0b39bf67a186a64b7b7fd52d58e744a380c3a5426ec0a906f0d71ac87313dca883093883a
6
+ metadata.gz: 4cc867ac7554a8da0df51f40609f53e3fef3ab4b41d3b53252910330cc12f80959d41cfeaab2b1ac64476eaa48da7df1664f9d01f54928d67a7f6d2b0d172bb2
7
+ data.tar.gz: 9db2cf2cde11394abd9cb850d02a156658e0a770ae230f2104fce0697382459199b040829b54f9dff37e31409e2c2944883201c9a014b47b41f24e3f74836c24
@@ -434,6 +434,60 @@ t_dv_setu32(VALUE self, VALUE index, VALUE value) {
434
434
  return self;
435
435
  }
436
436
 
437
+ static VALUE
438
+ t_dv_setbytes(VALUE self, VALUE index, VALUE bytes) {
439
+ DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
440
+ unsigned int idx0 = dv->offset + (unsigned int)idx;
441
+ CHECKBOUNDSBB(idx0);
442
+
443
+ if (RB_TYPE_P(bytes, T_ARRAY)) {
444
+ const unsigned int length = (unsigned int)rb_array_len(bytes);
445
+ const VALUE* items = rb_array_const_ptr(bytes);
446
+ CHECKBOUNDSBB(idx0 + length);
447
+
448
+ for (unsigned int i = 0; i < length; i++) {
449
+ if (!RB_FIXNUM_P(items[i]))
450
+ rb_raise(rb_eRuntimeError, "array contains non fixnum value at index %d", i);
451
+ int num = NUM2INT(items[i]);
452
+ ADJUSTBOUNDS(num, 0xFF);
453
+ bb->ptr[idx0 + i] = (unsigned char)num;
454
+ }
455
+ } else if (RB_TYPE_P(bytes, T_STRING)) {
456
+ const char *str_ptr = RSTRING_PTR(bytes);
457
+ const unsigned int length = (unsigned int)RSTRING_LEN(bytes);
458
+ CHECKBOUNDSBB(idx0 + length);
459
+
460
+ for (unsigned int i = 0; i < length; i++) {
461
+ bb->ptr[idx0 + i] = (unsigned char)str_ptr[i];
462
+ }
463
+ } else if (RB_TYPE_P(bytes, T_DATA) &&
464
+ (CLASS_OF(bytes) == cArrayBuffer || CLASS_OF(bytes) == cDataView)) {
465
+ unsigned int length;
466
+ const char *src_bytes;
467
+ if (CLASS_OF(bytes) == cArrayBuffer) {
468
+ struct LLC_ArrayBuffer *src_bb = (struct LLC_ArrayBuffer*)rb_data_object_get(bytes);
469
+ length = src_bb->size;
470
+ src_bytes = (const char*)src_bb->ptr;
471
+ } else {
472
+ struct LLC_DataView *src_dv = (struct LLC_DataView*)rb_data_object_get(bytes);
473
+ struct LLC_ArrayBuffer *src_bb = (struct LLC_ArrayBuffer*)rb_data_object_get(src_dv->bb_obj);
474
+ length = src_dv->size;
475
+ src_bytes = (const char*)(src_bb->ptr + (size_t)src_dv->offset);
476
+ if (src_dv->offset >= src_bb->size)
477
+ rb_raise(rb_eRuntimeError, "offset exceeds the underlying source buffer size");
478
+ if (src_dv->offset + length >= src_bb->size)
479
+ rb_raise(rb_eRuntimeError, "offset + size exceeds the underlying source buffer size");
480
+ }
481
+
482
+ CHECKBOUNDSBB(idx0 + length);
483
+ memcpy((void*)(bb->ptr + (size_t)idx0), src_bytes, (size_t)length);
484
+ } else {
485
+ rb_raise(rb_eArgError, "Invalid type: %+"PRIsVALUE, CLASS_OF(bytes));
486
+ }
487
+
488
+ return self;
489
+ }
490
+
437
491
  void
438
492
  Init_dataview() {
439
493
  idEndianess = rb_intern("endianess");
@@ -456,6 +510,8 @@ Init_dataview() {
456
510
  rb_define_method(cDataView, "setU24", t_dv_setu24, 2);
457
511
  rb_define_method(cDataView, "setU32", t_dv_setu32, 2);
458
512
 
513
+ rb_define_method(cDataView, "setBytes", t_dv_setbytes, 2);
514
+
459
515
  rb_define_method(cDataView, "endianess", t_dv_endianess, 0);
460
516
  rb_define_method(cDataView, "offset=", t_dv_setoffset, 1);
461
517
  rb_define_method(cDataView, "offset", t_dv_offset, 0);
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arraybuffer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - André Diego Piske
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-17 00:00:00.000000000 Z
11
+ date: 2020-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -63,5 +63,5 @@ requirements: []
63
63
  rubygems_version: 3.1.2
64
64
  signing_key:
65
65
  specification_version: 4
66
- summary: Low level byte operators and buffers
66
+ summary: An array buffer (a.k.a. byte array) implementation forRuby, implemented natively.
67
67
  test_files: []