vox-etf 0.1.1 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 814a39714371d1d129be3606ee5f4f00045c6f6a35fdbcae83bec672cec55d33
4
- data.tar.gz: f2cd32adb4fa957dd6bf692babe956f7f8569251bac15658ae1445d476c342d3
3
+ metadata.gz: 4435ae55a1631c64aa810ea8f6f0beb899f1e3363498f36cd13b2cd661704a97
4
+ data.tar.gz: 23df68f172a4d60529e72b40d02c74fbe96a342bacf534aad606047c54543167
5
5
  SHA512:
6
- metadata.gz: 7fc627a3904d13c023aa85092010b11ea4a281a2ca93992c39a1e13293b9d1768362a12114c6cefa6c9afae1075bc1902fbcee337f3a8a852ec8fbe75754a5b7
7
- data.tar.gz: d26faa973c98490470b43735e5f45c0dbe53759499def683ac6d80fd662af94e158951702014ca6a55bafed2f2a2839226b5b67709d28a9f2adfc62a09d3f807
6
+ metadata.gz: ca076026d2625686aa7f8f6428ff33a6a7b40571eecb68478ed4684883658854e749e123053544d1861f9bff13b9c8f7d310047afa95ad70ed059036a1b0e4a3
7
+ data.tar.gz: b708e0871cfae5fb34b74f28024e607684ca386eaf0bc9c9ec9d668a900ab7b3e89f3d2a1b101d4b7fffd3dc870f91c4fb1b11101b01d6ac4e40d0de1b856998
@@ -4,6 +4,33 @@
4
4
  #include "erlpack/sysdep.h"
5
5
  #include "erlpack/constants.h"
6
6
 
7
+ /* This code is highly derivative of discord's erlpack decoder
8
+ * targeting Javascript.
9
+ *
10
+ *
11
+ * MIT License
12
+ *
13
+ * Copyright (c) 2017 Discord
14
+ *
15
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ * of this software and associated documentation files (the "Software"), to deal
17
+ * in the Software without restriction, including without limitation the rights
18
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ * copies of the Software, and to permit persons to whom the Software is
20
+ * furnished to do so, subject to the following conditions:
21
+ *
22
+ * The above copyright notice and this permission notice shall be included in all
23
+ * copies or substantial portions of the Software.
24
+ *
25
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ * SOFTWARE.
32
+ */
33
+
7
34
  namespace etf
8
35
  {
9
36
  class decoder
@@ -131,12 +158,12 @@ namespace etf
131
158
  return val;
132
159
  }
133
160
 
134
- VALUE decode_small_integer(void)
161
+ VALUE decode_small_integer()
135
162
  {
136
163
  return INT2FIX(read8());
137
164
  }
138
165
 
139
- VALUE decode_integer(void)
166
+ VALUE decode_integer()
140
167
  {
141
168
  return INT2NUM(read32());
142
169
  }
@@ -156,7 +183,7 @@ namespace etf
156
183
  VALUE array = decode_array(length);
157
184
  const uint8_t tail = read8();
158
185
 
159
- if (tail != term::nil)
186
+ if (tail != NIL_EXT)
160
187
  {
161
188
  rb_raise(rb_eArgError, "List doesn't end with `NIL`, but it must!");
162
189
  return Qnil;
@@ -278,7 +305,7 @@ namespace etf
278
305
  const uint8_t sign = read8();
279
306
  const char *buff = read_string(length);
280
307
 
281
- auto flags = INTEGER_PACK_LITTLE_ENDIAN | (sign * INTEGER_PACK_NEGATIVE);
308
+ int flags = INTEGER_PACK_LITTLE_ENDIAN | (sign * INTEGER_PACK_NEGATIVE);
282
309
  return rb_integer_unpack(buff, length, 1, 0, flags);
283
310
  }
284
311
 
@@ -1,5 +1,7 @@
1
1
  #include "erlpack/encoder.h"
2
+ #include "erlpack/constants.h"
2
3
  #include "etf.hpp"
4
+ #include "ruby.h"
3
5
 
4
6
  namespace etf
5
7
  {
@@ -56,13 +58,23 @@ namespace etf
56
58
  encode_hash(input);
57
59
  break;
58
60
  default:
59
- rb_raise(rb_eArgError, "Unsupported serialization type");
61
+ if (rb_respond_to(input, rb_intern("to_hash")))
62
+ {
63
+ VALUE hash = rb_funcall(input, rb_intern("to_hash"), 0);
64
+ Check_Type(hash, T_HASH);
65
+ encode_hash(hash);
66
+ }
67
+ else
68
+ {
69
+ rb_raise(rb_eArgError, "Unsupported serialization type");
70
+ }
71
+
60
72
  break;
61
73
  }
62
74
  }
63
75
 
64
76
  VALUE
65
- r_string(void)
77
+ r_string()
66
78
  {
67
79
  return rb_str_new(erl_buff->buf, erl_buff->length);
68
80
  }
@@ -101,7 +113,7 @@ namespace etf
101
113
  {
102
114
  // id byte | n byte | sign byte | data
103
115
  uint8_t buff[3 + byte_count];
104
- buff[0] = term::small_big;
116
+ buff[0] = SMALL_BIG_EXT;
105
117
  buff[1] = byte_count;
106
118
  buff[2] = FIX2LONG(bignum) >= 0 ? 0 : 1;
107
119
  rb_integer_pack(bignum, buff + 3, byte_count, sizeof(uint8_t), 0, INTEGER_PACK_LITTLE_ENDIAN);
@@ -111,7 +123,7 @@ namespace etf
111
123
  {
112
124
  // id byte | 4 byte n | sign byte | data
113
125
  uint8_t buff[6 + byte_count];
114
- buff[0] = term::large_big;
126
+ buff[0] = LARGE_BIG_EXT;
115
127
  _erlpack_store32(buff + 1, byte_count);
116
128
  buff[5] = RBIGNUM_SIGN(bignum) ? 0 : 1;
117
129
  rb_integer_pack(bignum, buff + 6, byte_count, sizeof(uint8_t), 0, INTEGER_PACK_LITTLE_ENDIAN);
@@ -126,18 +138,25 @@ namespace etf
126
138
 
127
139
  void encode_array(VALUE array)
128
140
  {
129
- erlpack_append_list_header(erl_buff, RARRAY_LEN(array));
130
141
  uint32_t size = RARRAY_LEN(array);
142
+ if (size == 0)
143
+ {
144
+ erlpack_append_nil_ext(erl_buff);
145
+ return;
146
+ }
147
+
148
+ erlpack_append_list_header(erl_buff, size);
131
149
  for (uint32_t index = 0; index < size; index++)
132
150
  {
133
151
  encode_object(RARRAY_AREF(array, index));
134
152
  }
153
+
154
+ erlpack_append_nil_ext(erl_buff);
135
155
  }
136
156
 
137
157
  void encode_symbol(VALUE symbol)
138
158
  {
139
- VALUE str = rb_sym2str(symbol);
140
- erlpack_append_atom_utf8(erl_buff, RSTRING_PTR(str), RSTRING_LEN(str));
159
+ encode_string(rb_sym2str(symbol));
141
160
  }
142
161
 
143
162
  void encode_string(VALUE string)
@@ -154,6 +173,7 @@ namespace etf
154
173
  for (uint32_t index = 0; index < size * 2; index += 2)
155
174
  {
156
175
  VALUE key = RARRAY_AREF(keys, index / 2);
176
+ // Normalize keys to strings because discord
157
177
  encode_object(key);
158
178
  encode_object(rb_hash_aref(hash, key));
159
179
  }
@@ -3,10 +3,6 @@
3
3
  #include "decoder.hpp"
4
4
  #include "etf.hpp"
5
5
 
6
- // Decode an ETF term from a ruby string
7
- // @param self [Object] Vox::ETF
8
- // @param input [Rice::String] The ETF term to be decoded.
9
- // @return [Rice::Object] Whatever the term is, as a ruby object.
10
6
  VALUE decode(VALUE self, VALUE input)
11
7
  {
12
8
  Check_Type(input, T_STRING);
@@ -2,42 +2,6 @@
2
2
 
3
3
  #define ETF_VERSION 131
4
4
 
5
- // Term IDs, first byte of any term.
6
- enum term
7
- {
8
- new_float = 70,
9
- bit_binary = 77,
10
- atom_cache = 82,
11
- new_pid = 88,
12
- new_port = 89,
13
- newer_reference = 90,
14
- small_integer = 97,
15
- integer = 98,
16
- // annoying
17
- etf_float = 99,
18
- atom = 100,
19
- reference = 101,
20
- port = 102,
21
- pid = 103,
22
- small_tuple = 104,
23
- large_tuple = 105,
24
- nil = 106,
25
- string = 107,
26
- list = 108,
27
- binary = 109,
28
- small_big = 110,
29
- large_big = 111,
30
- new_fun = 112,
31
- // annoying
32
- etf_export = 113,
33
- new_reference = 114,
34
- small_atom = 115,
35
- map = 116,
36
- fun = 117,
37
- atom_utf8 = 118,
38
- small_atom_utf8 = 119
39
- };
40
-
41
5
  VALUE decode(VALUE self, VALUE input);
42
6
  VALUE encode(VALUE self, VALUE input);
43
7
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require('mkmf-rice')
3
+ require('mkmf')
4
4
 
5
5
  find_header('ruby.h')
6
6
  find_header('zlib.h')
@@ -1,7 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Container for Vox components.
3
4
  module Vox
5
+ # Default ETF adapter for vox's gateway component.
4
6
  module ETF
5
- VERSION = '0.1.1'
7
+ # @!parse [ruby]
8
+ # # Encode an object to an ETF term. This method accepts, `Integer`, `Float`,
9
+ # # `String`, `Symbol`, `Hash`, `Array`, `nil`, `true`, and `false` objects.
10
+ # # It also allows any object that responds to `#to_hash => Hash`.
11
+ # # @param input [Object, #to_hash] The object to be encoded as an ETF term.
12
+ # # @return [String] The ETF term encoded as a packed string.
13
+ # def self.encode(input)
14
+ # end
15
+
16
+ # @!parse [ruby]
17
+ # # Decode an ETF term from a string.
18
+ # # @param input [String] The ETF term to be decoded.
19
+ # # @return [Object] The ETF term decoded to an object.
20
+ # def self.decode(input)
21
+ # end
22
+
23
+ # Gem version
24
+ VERSION = '0.1.6'
6
25
  end
7
26
  end
@@ -30,7 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.require_paths = ['lib']
31
31
  spec.extensions << 'ext/vox/extconf.rb'
32
32
 
33
- spec.add_dependency 'rice', '~> 2.2.0'
34
33
  spec.add_development_dependency 'rake', '~> 12.3.3'
35
34
  spec.add_development_dependency 'rake-compiler', '~> 1.1.1'
36
35
  spec.add_development_dependency 'rspec', '~> 3.9.0'
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vox-etf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Carey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-11 00:00:00.000000000 Z
11
+ date: 2020-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rice
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 2.2.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 2.2.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -160,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
146
  - !ruby/object:Gem::Version
161
147
  version: '0'
162
148
  requirements: []
163
- rubygems_version: 3.1.2
149
+ rubygems_version: 3.0.3
164
150
  signing_key:
165
151
  specification_version: 4
166
152
  summary: ETF decoding/encoding for vox.