usamin-js 1.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +20 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/usamin_js/extconf.rb +27 -0
- data/ext/usamin_js/generator.cpp +279 -0
- data/ext/usamin_js/generator.hpp +17 -0
- data/ext/usamin_js/rb270_fix.hpp +17 -0
- data/ext/usamin_js/rb_common.cpp +22 -0
- data/ext/usamin_js/rb_common.hpp +9 -0
- data/ext/usamin_js/usamin_js.cpp +40 -0
- data/lib/usamin/js/version.rb +7 -0
- data/lib/usamin/js.rb +9 -0
- data/usamin.gemspec +31 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bd8d94c422e7c611281569bb7f6e4925b1c6e399f148ce96303379b18dcd3a2a
|
4
|
+
data.tar.gz: 1a375b7a3b2f58b00f114a93527b9c4913fb6ca72c57fcd80dc2d2f09850f908
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 959f0490d6a235b87bcc72eabfb374f15abbef9b072c060bb7e0d58f050e86b0d8fcde5b88fdcff9cb2bd1e7e9039177ebf6f2f7bfeb1a3e6a4b3b1d649665b1
|
7
|
+
data.tar.gz: a22bd778d426aeab77a1185b6fd7ef0a6540e5e876adf04e5c3066857601d7887502acba084909506c714426afa609e4996fe7ecdf1fc5eb419b8706f45e7d54
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Ishotihadus
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# UsaminJs
|
2
|
+
|
3
|
+
JavaScript generator based on RapidJSON.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install RapidJSON beforehand. Only header files are necessary, and no need to build.
|
8
|
+
|
9
|
+
Next, add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'usamin-js'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install usamin-js
|
22
|
+
|
23
|
+
The directory of RapidJSON can be explicitly specified with `--with-rapidjson-dir` option.
|
24
|
+
|
25
|
+
$ gem install usamin-js -- --with-rapidjson-dir=/usr/local/opt/rapidjson
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'usamin/js'
|
31
|
+
|
32
|
+
puts Usamin::Js.generate({ abe: 'nana', time: Time.now, "3": 5 })
|
33
|
+
#=> {"abe":"nana","time":new Date(1655617665882),"3":5}
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Ishotihadus/usamin-js.
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT) at the request of RapidJSON.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rake/extensiontask'
|
13
|
+
|
14
|
+
task build: :compile
|
15
|
+
|
16
|
+
Rake::ExtensionTask.new('usamin_js') do |ext|
|
17
|
+
ext.lib_dir = 'lib/usamin/js'
|
18
|
+
end
|
19
|
+
|
20
|
+
task default: %i[clobber compile test]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'usamin'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'pry'
|
14
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mkmf'
|
4
|
+
|
5
|
+
RbConfig::MAKEFILE_CONFIG['CXX'] = ENV['CXX'] if ENV['CXX']
|
6
|
+
have_library('stdc++')
|
7
|
+
have_library('m')
|
8
|
+
dir_config('rapidjson')
|
9
|
+
append_cppflags('-O3')
|
10
|
+
append_cppflags('-Wall')
|
11
|
+
append_cppflags('-Wextra')
|
12
|
+
append_cppflags('-Wno-missing-field-initializers')
|
13
|
+
append_cppflags('-Wvla')
|
14
|
+
|
15
|
+
if checking_for('whether -march=native is accepted as CPPFLAGS') {try_cppflags('-march=native')}
|
16
|
+
if checking_for('whether -msse4.2 is accepted as CPPFLAGS') {try_cppflags('-msse4.2')}
|
17
|
+
$CPPFLAGS << ' -msse4.2 -march=native'
|
18
|
+
elsif checking_for('whether -msse2 is accepted as CPPFLAGS') {try_cppflags('-msse2')}
|
19
|
+
$CPPFLAGS << ' -msse2 -march=native'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
$CPPFLAGS << ' -std=c++11 -fvisibility=hidden'
|
24
|
+
|
25
|
+
$CPPFLAGS << ' -DRAPIDJSON_IS_HEAD' if arg_config('--rapidjson-is-head')
|
26
|
+
|
27
|
+
create_makefile('usamin/js/usamin_js')
|
@@ -0,0 +1,279 @@
|
|
1
|
+
#include <sstream>
|
2
|
+
#include "rb270_fix.hpp"
|
3
|
+
#include <rapidjson/prettywriter.h>
|
4
|
+
#include <rapidjson/writer.h>
|
5
|
+
#include <ruby.h>
|
6
|
+
#include <ruby/version.h>
|
7
|
+
#include <iomanip>
|
8
|
+
#include "rb_common.hpp"
|
9
|
+
#include "generator.hpp"
|
10
|
+
|
11
|
+
#define WRITER_CONFIGS rapidjson::UTF8<>, rapidjson::UTF8<>, RubyCrtAllocator, rapidjson::kWriteNanAndInfFlag
|
12
|
+
|
13
|
+
void *RubyCrtAllocator::Malloc(size_t size) {
|
14
|
+
if (size)
|
15
|
+
return ruby_xmalloc(size);
|
16
|
+
else
|
17
|
+
return nullptr;
|
18
|
+
}
|
19
|
+
|
20
|
+
void *RubyCrtAllocator::Realloc(void *originalPtr, size_t, size_t newSize) {
|
21
|
+
if (newSize == 0) {
|
22
|
+
ruby_xfree(originalPtr);
|
23
|
+
return nullptr;
|
24
|
+
}
|
25
|
+
return ruby_xrealloc(originalPtr, newSize);
|
26
|
+
}
|
27
|
+
|
28
|
+
void RubyCrtAllocator::Free(void *ptr) {
|
29
|
+
ruby_xfree(ptr);
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
template <class Writer>
|
34
|
+
static inline void write_hash(Writer &, const VALUE);
|
35
|
+
template <class Writer>
|
36
|
+
static inline void write_array(Writer &, const VALUE);
|
37
|
+
template <class Writer>
|
38
|
+
static inline void write_struct(Writer &, const VALUE);
|
39
|
+
template <class Writer>
|
40
|
+
static inline void write_usamin(Writer &, const VALUE);
|
41
|
+
|
42
|
+
template <class Writer>
|
43
|
+
static inline void write_str(Writer &writer, const VALUE value) {
|
44
|
+
VALUE v = get_utf8_str(value);
|
45
|
+
writer.String(RSTRING_PTR(v), RSTRING_LENINT(v));
|
46
|
+
}
|
47
|
+
|
48
|
+
template <class Writer>
|
49
|
+
static inline void write_to_s(Writer &writer, const VALUE value) {
|
50
|
+
extern ID id_to_s;
|
51
|
+
write_str(writer, rb_funcall(value, id_to_s, 0));
|
52
|
+
}
|
53
|
+
|
54
|
+
template <class Writer>
|
55
|
+
static inline void write_time(Writer &writer, const VALUE value) {
|
56
|
+
extern ID id_to_f;
|
57
|
+
std::ostringstream stream;
|
58
|
+
stream << "new Date(" << std::fixed << std::setprecision(0) << NUM2DBL(rb_funcall(value, id_to_f, 0)) * 1000 << ")";
|
59
|
+
auto str = stream.str();
|
60
|
+
writer.RawValue(str.c_str(), str.length(), rapidjson::kStringType);
|
61
|
+
}
|
62
|
+
|
63
|
+
template <class Writer>
|
64
|
+
static inline void write_date(Writer &writer, const VALUE value) {
|
65
|
+
extern ID id_to_time;
|
66
|
+
write_time(writer, rb_funcall(value, id_to_time, 0));
|
67
|
+
}
|
68
|
+
|
69
|
+
template <class Writer>
|
70
|
+
static void write(Writer &writer, const VALUE value) {
|
71
|
+
extern VALUE rb_cTime, rb_cDate, rb_cDateTime, rb_cUsaminValue;
|
72
|
+
switch (TYPE(value)) {
|
73
|
+
case RUBY_T_NONE:
|
74
|
+
case RUBY_T_NIL:
|
75
|
+
case RUBY_T_UNDEF:
|
76
|
+
writer.Null();
|
77
|
+
break;
|
78
|
+
case RUBY_T_TRUE:
|
79
|
+
writer.Bool(true);
|
80
|
+
break;
|
81
|
+
case RUBY_T_FALSE:
|
82
|
+
writer.Bool(false);
|
83
|
+
break;
|
84
|
+
case RUBY_T_FIXNUM:
|
85
|
+
writer.Int64(FIX2LONG(value));
|
86
|
+
break;
|
87
|
+
case RUBY_T_FLOAT:
|
88
|
+
case RUBY_T_RATIONAL:
|
89
|
+
writer.Double(NUM2DBL(value));
|
90
|
+
break;
|
91
|
+
case RUBY_T_STRING:
|
92
|
+
write_str(writer, value);
|
93
|
+
break;
|
94
|
+
case RUBY_T_ARRAY:
|
95
|
+
write_array(writer, value);
|
96
|
+
break;
|
97
|
+
case RUBY_T_HASH:
|
98
|
+
write_hash(writer, value);
|
99
|
+
break;
|
100
|
+
case RUBY_T_STRUCT:
|
101
|
+
write_struct(writer, value);
|
102
|
+
break;
|
103
|
+
case RUBY_T_BIGNUM: {
|
104
|
+
VALUE v = rb_big2str(value, 10);
|
105
|
+
writer.RawValue(RSTRING_PTR(v), RSTRING_LEN(v), rapidjson::kNumberType);
|
106
|
+
} break;
|
107
|
+
default:
|
108
|
+
if (rb_cUsaminValue && rb_obj_is_kind_of(value, rb_cUsaminValue))
|
109
|
+
write_usamin(writer, value);
|
110
|
+
else if ((rb_cDate && rb_obj_is_kind_of(value, rb_cDate)) || (rb_cDateTime && rb_obj_is_kind_of(value, rb_cDateTime)))
|
111
|
+
write_date(writer, value);
|
112
|
+
else if (rb_cTime && rb_obj_is_kind_of(value, rb_cTime))
|
113
|
+
write_time(writer, value);
|
114
|
+
else
|
115
|
+
write_to_s(writer, value);
|
116
|
+
break;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
template <class Writer>
|
121
|
+
static inline void write_key_str(Writer &writer, const VALUE value) {
|
122
|
+
VALUE v = get_utf8_str(value);
|
123
|
+
writer.Key(RSTRING_PTR(v), RSTRING_LENINT(v));
|
124
|
+
}
|
125
|
+
|
126
|
+
template <class Writer>
|
127
|
+
static inline void write_key_to_s(Writer &writer, const VALUE value) {
|
128
|
+
extern ID id_to_s;
|
129
|
+
write_key_str(writer, rb_funcall(value, id_to_s, 0));
|
130
|
+
}
|
131
|
+
|
132
|
+
template <class Writer>
|
133
|
+
static inline int write_hash_each(const VALUE key, const VALUE value, VALUE writer_v) {
|
134
|
+
Writer *writer = reinterpret_cast<Writer *>(writer_v);
|
135
|
+
if (RB_TYPE_P(key, T_STRING))
|
136
|
+
write_key_str(*writer, key);
|
137
|
+
else if (RB_TYPE_P(key, T_SYMBOL))
|
138
|
+
write_key_str(*writer, rb_sym_to_s(key));
|
139
|
+
else
|
140
|
+
write_key_to_s(*writer, key);
|
141
|
+
write(*writer, value);
|
142
|
+
return ST_CONTINUE;
|
143
|
+
}
|
144
|
+
|
145
|
+
template <class Writer>
|
146
|
+
static inline void write_hash(Writer &writer, const VALUE hash) {
|
147
|
+
writer.StartObject();
|
148
|
+
#if RUBY_API_VERSION_CODE < 20700
|
149
|
+
rb_hash_foreach(hash, (int (*)(ANYARGS))write_hash_each<Writer>, reinterpret_cast<VALUE>(&writer));
|
150
|
+
#else
|
151
|
+
rb_hash_foreach(hash, write_hash_each<Writer>, reinterpret_cast<VALUE>(&writer));
|
152
|
+
#endif
|
153
|
+
writer.EndObject();
|
154
|
+
}
|
155
|
+
|
156
|
+
template <class Writer>
|
157
|
+
static inline void write_array(Writer &writer, const VALUE value) {
|
158
|
+
writer.StartArray();
|
159
|
+
const VALUE *ptr = rb_array_const_ptr(value);
|
160
|
+
for (long i = 0; i < rb_array_len(value); i++, ptr++)
|
161
|
+
write(writer, *ptr);
|
162
|
+
writer.EndArray();
|
163
|
+
}
|
164
|
+
|
165
|
+
template <class Writer>
|
166
|
+
static inline void write_struct(Writer &writer, const VALUE value) {
|
167
|
+
writer.StartObject();
|
168
|
+
VALUE members = rb_struct_members(value);
|
169
|
+
const VALUE *ptr = rb_array_const_ptr(members);
|
170
|
+
for (long i = 0; i < rb_array_len(members); i++, ptr++) {
|
171
|
+
if (RB_TYPE_P(*ptr, T_SYMBOL))
|
172
|
+
write_key_str(writer, rb_sym_to_s(*ptr));
|
173
|
+
else if (RB_TYPE_P(*ptr, T_STRING))
|
174
|
+
write_key_str(writer, *ptr);
|
175
|
+
else
|
176
|
+
write_key_to_s(writer, *ptr);
|
177
|
+
write(writer, rb_struct_aref(value, *ptr));
|
178
|
+
}
|
179
|
+
writer.EndObject();
|
180
|
+
}
|
181
|
+
|
182
|
+
template <class Writer>
|
183
|
+
static inline void write_usamin(Writer &writer, const VALUE self) {
|
184
|
+
extern VALUE rb_mUsamin;
|
185
|
+
extern ID id_generate;
|
186
|
+
VALUE str = rb_funcall(rb_mUsamin, id_generate, 1, self);
|
187
|
+
VALUE v = get_utf8_str(str);
|
188
|
+
writer.RawValue(RSTRING_PTR(v), RSTRING_LENINT(v), rapidjson::kObjectType);
|
189
|
+
}
|
190
|
+
|
191
|
+
static void prepare_generate() {
|
192
|
+
extern VALUE rb_mUsamin, rb_cTime, rb_cDate, rb_cDateTime, rb_cUsaminValue;
|
193
|
+
extern ID id_Time, id_Date, id_DateTime, id_Value;
|
194
|
+
rb_cTime = rb_const_defined(rb_cObject, id_Time) ? rb_const_get(rb_cObject, id_Time) : 0;
|
195
|
+
rb_cDate = rb_const_defined(rb_cObject, id_Date) ? rb_const_get(rb_cObject, id_Date) : 0;
|
196
|
+
rb_cDateTime = rb_const_defined(rb_cObject, id_DateTime) ? rb_const_get(rb_cObject, id_DateTime) : 0;
|
197
|
+
rb_cUsaminValue = rb_const_defined(rb_mUsamin, id_Value) ? rb_const_get(rb_mUsamin, id_Value) : 0;
|
198
|
+
}
|
199
|
+
|
200
|
+
/*
|
201
|
+
* Generate the JSON string from Ruby data structures.
|
202
|
+
*
|
203
|
+
* @overload generate(obj)
|
204
|
+
* @param [Object] obj an object to serialize
|
205
|
+
* @return [String]
|
206
|
+
*/
|
207
|
+
VALUE w_generate(const VALUE, const VALUE value) {
|
208
|
+
rapidjson::StringBuffer buf;
|
209
|
+
rapidjson::Writer<rapidjson::StringBuffer, WRITER_CONFIGS> writer(buf);
|
210
|
+
prepare_generate();
|
211
|
+
write(writer, value);
|
212
|
+
return new_utf8_str(buf.GetString(), buf.GetSize());
|
213
|
+
}
|
214
|
+
|
215
|
+
/*
|
216
|
+
* Generate the prettified JSON string from Ruby data structures.
|
217
|
+
*
|
218
|
+
* @overload pretty_generate(obj, opts = {})
|
219
|
+
* @param [Object] obj an object to serialize
|
220
|
+
* @param [::Hash] opts options
|
221
|
+
* @option opts [String] :indent (' ') a string used to indent
|
222
|
+
* @option opts [Boolean] :single_line_array (false)
|
223
|
+
* @return [String]
|
224
|
+
*/
|
225
|
+
VALUE w_pretty_generate(const int argc, const VALUE *argv, const VALUE) {
|
226
|
+
extern VALUE rb_eUsaminJsError;
|
227
|
+
extern VALUE sym_indent, sym_single_line_array;
|
228
|
+
|
229
|
+
VALUE value, options;
|
230
|
+
rb_scan_args(argc, argv, "1:", &value, &options);
|
231
|
+
rapidjson::StringBuffer buf;
|
232
|
+
#if RAPIDJSON_VERSION_CODE(RAPIDJSON_MAJOR_VERSION, RAPIDJSON_MINOR_VERSION, RAPIDJSON_PATCH_VERSION) > RAPIDJSON_VERSION_CODE(1, 1, 0) || defined(RAPIDJSON_IS_HEAD)
|
233
|
+
rapidjson::PrettyWriter<rapidjson::StringBuffer, WRITER_CONFIGS> writer(buf);
|
234
|
+
#else
|
235
|
+
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
|
236
|
+
#endif
|
237
|
+
|
238
|
+
char indent_char = ' ';
|
239
|
+
unsigned int indent_count = 2;
|
240
|
+
if (!NIL_P(options)) {
|
241
|
+
VALUE v_indent = rb_hash_lookup(options, sym_indent);
|
242
|
+
if (RTEST(v_indent)) {
|
243
|
+
if (RB_FIXNUM_P(v_indent)) {
|
244
|
+
int l = FIX2INT(v_indent);
|
245
|
+
indent_count = l > 0 ? l : 0;
|
246
|
+
} else {
|
247
|
+
int vlen = RSTRING_LENINT(v_indent);
|
248
|
+
if (vlen == 0) {
|
249
|
+
indent_count = 0;
|
250
|
+
} else {
|
251
|
+
const char *indent_str = RSTRING_PTR(v_indent);
|
252
|
+
switch (indent_str[0]) {
|
253
|
+
case ' ':
|
254
|
+
case '\t':
|
255
|
+
case '\r':
|
256
|
+
case '\n':
|
257
|
+
indent_char = indent_str[0];
|
258
|
+
break;
|
259
|
+
default:
|
260
|
+
rb_raise(rb_eUsaminJsError,
|
261
|
+
":indent must be a repetation of \" \", \"\\t\", \"\\r\" or \"\\n\".");
|
262
|
+
}
|
263
|
+
for (long i = 1; i < vlen; i++)
|
264
|
+
if (indent_str[0] != indent_str[i])
|
265
|
+
rb_raise(rb_eUsaminJsError,
|
266
|
+
":indent must be a repetation of \" \", \"\\t\", \"\\r\" or \"\\n\".");
|
267
|
+
indent_count = vlen;
|
268
|
+
}
|
269
|
+
}
|
270
|
+
}
|
271
|
+
if (RTEST(rb_hash_lookup(options, sym_single_line_array)))
|
272
|
+
writer.SetFormatOptions(rapidjson::kFormatSingleLineArray);
|
273
|
+
}
|
274
|
+
writer.SetIndent(indent_char, indent_count);
|
275
|
+
|
276
|
+
prepare_generate();
|
277
|
+
write(writer, value);
|
278
|
+
return new_utf8_str(buf.GetString(), buf.GetSize());
|
279
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#ifndef USAMIN_GENERATOR_HPP
|
2
|
+
#define USAMIN_GENERATOR_HPP
|
3
|
+
|
4
|
+
#include <ruby.h>
|
5
|
+
|
6
|
+
class RubyCrtAllocator {
|
7
|
+
public:
|
8
|
+
static const bool kNeedFree = true;
|
9
|
+
void *Malloc(size_t size);
|
10
|
+
void *Realloc(void *originalPtr, size_t, size_t newSize);
|
11
|
+
static void Free(void *ptr);
|
12
|
+
};
|
13
|
+
|
14
|
+
VALUE w_generate(const VALUE self, const VALUE value);
|
15
|
+
VALUE w_pretty_generate(const int argc, const VALUE *argv, const VALUE self);
|
16
|
+
|
17
|
+
#endif
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#ifndef RB270_FIX_HPP
|
2
|
+
#define RB270_FIX_HPP
|
3
|
+
|
4
|
+
#include <cstring>
|
5
|
+
#include <ruby/version.h>
|
6
|
+
|
7
|
+
#if RUBY_API_VERSION_CODE >= 20700
|
8
|
+
|
9
|
+
namespace std {
|
10
|
+
static inline void *ruby_nonempty_memcpy(void *dest, const void *src, size_t n) {
|
11
|
+
return (n ? ::memcpy(dest, src, n) : dest);
|
12
|
+
}
|
13
|
+
} // namespace std
|
14
|
+
|
15
|
+
#endif
|
16
|
+
|
17
|
+
#endif
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <ruby/encoding.h>
|
3
|
+
|
4
|
+
VALUE get_utf8_str(VALUE str) {
|
5
|
+
extern int utf8index;
|
6
|
+
extern VALUE utf8value;
|
7
|
+
extern rb_encoding *utf8;
|
8
|
+
|
9
|
+
Check_Type(str, T_STRING);
|
10
|
+
int encoding = rb_enc_get_index(str);
|
11
|
+
if (encoding == utf8index || rb_enc_compatible(str, utf8value) == utf8)
|
12
|
+
return str;
|
13
|
+
else
|
14
|
+
return rb_str_conv_enc(str, rb_enc_from_index(encoding), utf8);
|
15
|
+
}
|
16
|
+
|
17
|
+
VALUE new_utf8_str(const char *cstr, const long len) {
|
18
|
+
extern int utf8index;
|
19
|
+
VALUE ret = rb_str_new(cstr, len);
|
20
|
+
rb_enc_set_index(ret, utf8index);
|
21
|
+
return ret;
|
22
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <ruby/encoding.h>
|
3
|
+
#include "generator.hpp"
|
4
|
+
|
5
|
+
#if SIZEOF_VALUE < SIZEOF_VOIDP
|
6
|
+
#error SIZEOF_VOIDP must not be greater than SIZEOF_VALUE.
|
7
|
+
#endif
|
8
|
+
|
9
|
+
rb_encoding *utf8;
|
10
|
+
int utf8index;
|
11
|
+
ID id_to_s, id_to_f, id_to_time, id_generate, id_Time, id_Date, id_DateTime, id_Value;
|
12
|
+
VALUE rb_mUsamin, rb_mUsaminJs, rb_eUsaminJsError;
|
13
|
+
VALUE utf8value, sym_indent, sym_single_line_array, sym_symbolize_names;
|
14
|
+
VALUE rb_cUsaminValue, rb_cTime, rb_cDate, rb_cDateTime;
|
15
|
+
|
16
|
+
extern "C" {
|
17
|
+
void __attribute__((visibility("default"))) Init_usamin_js() {
|
18
|
+
utf8 = rb_utf8_encoding();
|
19
|
+
utf8index = rb_enc_to_index(utf8);
|
20
|
+
utf8value = rb_enc_from_encoding(utf8);
|
21
|
+
sym_indent = rb_id2sym(rb_intern("indent"));
|
22
|
+
sym_single_line_array = rb_id2sym(rb_intern("single_line_array"));
|
23
|
+
sym_symbolize_names = rb_id2sym(rb_intern("symbolize_names"));
|
24
|
+
id_to_s = rb_intern("to_s");
|
25
|
+
id_to_f = rb_intern("to_f");
|
26
|
+
id_to_time = rb_intern("to_time");
|
27
|
+
id_generate = rb_intern("generate");
|
28
|
+
id_Time = rb_intern("Time");
|
29
|
+
id_Date = rb_intern("Date");
|
30
|
+
id_DateTime = rb_intern("DateTime");
|
31
|
+
id_Value = rb_intern("Value");
|
32
|
+
|
33
|
+
rb_mUsamin = rb_define_module("Usamin");
|
34
|
+
rb_mUsaminJs = rb_define_module_under(rb_mUsamin, "Js");
|
35
|
+
rb_define_module_function(rb_mUsaminJs, "generate", RUBY_METHOD_FUNC(w_generate), 1);
|
36
|
+
rb_define_module_function(rb_mUsaminJs, "pretty_generate", RUBY_METHOD_FUNC(w_pretty_generate), -1);
|
37
|
+
|
38
|
+
rb_eUsaminJsError = rb_define_class_under(rb_mUsaminJs, "UsaminJsError", rb_eStandardError);
|
39
|
+
}
|
40
|
+
}
|
data/lib/usamin/js.rb
ADDED
data/usamin.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$:.unshift(lib) unless $:.include?(lib)
|
5
|
+
require 'usamin/js/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'usamin-js'
|
9
|
+
spec.version = Usamin::Js::VERSION
|
10
|
+
spec.authors = ['Ishotihadus']
|
11
|
+
spec.email = ['hanachan.pao@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'A RapidJSON-based JavaScript object generator for Ruby'
|
14
|
+
spec.description = 'A RapidJSON-based JavaScript object generator for Ruby.'
|
15
|
+
spec.homepage = 'https://github.com/Ishotihadus/usamin-js'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f)}
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
spec.extensions = ['ext/usamin_js/extconf.rb']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler'
|
27
|
+
spec.add_development_dependency 'pry'
|
28
|
+
spec.add_development_dependency 'rake'
|
29
|
+
spec.add_development_dependency 'rake-compiler'
|
30
|
+
spec.add_development_dependency 'usamin'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: usamin-js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ishotihadus
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake-compiler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: usamin
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A RapidJSON-based JavaScript object generator for Ruby.
|
84
|
+
email:
|
85
|
+
- hanachan.pao@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions:
|
88
|
+
- ext/usamin_js/extconf.rb
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- ext/usamin_js/extconf.rb
|
100
|
+
- ext/usamin_js/generator.cpp
|
101
|
+
- ext/usamin_js/generator.hpp
|
102
|
+
- ext/usamin_js/rb270_fix.hpp
|
103
|
+
- ext/usamin_js/rb_common.cpp
|
104
|
+
- ext/usamin_js/rb_common.hpp
|
105
|
+
- ext/usamin_js/usamin_js.cpp
|
106
|
+
- lib/usamin/js.rb
|
107
|
+
- lib/usamin/js/version.rb
|
108
|
+
- usamin.gemspec
|
109
|
+
homepage: https://github.com/Ishotihadus/usamin-js
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.3.3
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: A RapidJSON-based JavaScript object generator for Ruby
|
132
|
+
test_files: []
|