crystalruby 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +16 -0
- data/lib/crystalruby/types/union_type.rb +5 -1
- data/lib/crystalruby/version.rb +1 -1
- data/lib/crystalruby.rb +14 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26b2e0d15fd3b320f700a1f0571b8bb5d8991a4ed1c60cfcd9f003b48bb9f891
|
4
|
+
data.tar.gz: 27243d104172eef50adfc46524a0b9052410636dc0b39e142fc3e53c04ded1f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a54c1cf883a7dea97d66e9c18903139713aaec0b965026a21fc2e14ced85c13fc676f7d8e76ecc9a2e6ca2fc69034185a9fa4eb6fc66a6986cc9bb2d037738fb
|
7
|
+
data.tar.gz: 0f29fe7bad266af6593d0e4533af27a9be925372befe40ed0e4ee5ea8c9293c7ea62aab4b1664eaf3d2202eb3000b3dda1dfbdc44ae9e924e2de017f5516a6bc
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -196,6 +196,22 @@ ArgumentError: Expected Bool but was Int at line 1, column 15
|
|
196
196
|
from crystalruby.rb:303:in `block in compile!'
|
197
197
|
```
|
198
198
|
|
199
|
+
## Named Types
|
200
|
+
|
201
|
+
You can name your types, for more succinct method signatures.
|
202
|
+
The type names will be mirrored in the generated Crystal code.
|
203
|
+
E.g.
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
|
207
|
+
IntArrOrBoolArr = crtype{ Array(Bool) | Array(Int32) }
|
208
|
+
|
209
|
+
crystalize [a: IntArrOrBoolArr] => json{ IntArrOrBoolArr }
|
210
|
+
def method_with_named_types(a)
|
211
|
+
return a
|
212
|
+
end
|
213
|
+
```
|
214
|
+
|
199
215
|
## Exceptions
|
200
216
|
|
201
217
|
Exceptions thrown in Crystal code can be caught in Ruby.
|
@@ -27,7 +27,11 @@ module CrystalRuby
|
|
27
27
|
def interpret!(raw)
|
28
28
|
union_types.each do |type|
|
29
29
|
if type.interprets?(raw)
|
30
|
-
|
30
|
+
begin
|
31
|
+
return type.interpret!(raw)
|
32
|
+
rescue
|
33
|
+
# Pass
|
34
|
+
end
|
31
35
|
end
|
32
36
|
end
|
33
37
|
raise "Invalid deserialized value #{raw} for type #{inspect}"
|
data/lib/crystalruby/version.rb
CHANGED
data/lib/crystalruby.rb
CHANGED
@@ -112,7 +112,9 @@ module CrystalRuby
|
|
112
112
|
fn_ret_type: return_type[:crystal_type],
|
113
113
|
lib_fn_args: arg_types.map { |k, arg_type| "_#{k}: #{arg_type[:lib_type]}" }.join(","),
|
114
114
|
lib_fn_ret_type: return_type[:lib_type],
|
115
|
-
convert_lib_args:
|
115
|
+
convert_lib_args: arg_types.map do |k, arg_type|
|
116
|
+
"#{k} = #{arg_type[:convert_lib_to_crystal_type]["_#{k}"]}"
|
117
|
+
end.join("\n "),
|
116
118
|
arg_names: args.keys.join(","),
|
117
119
|
convert_return_type: return_type[:convert_crystal_to_lib_type]["return_value"],
|
118
120
|
error_value: return_type[:error_value]
|
@@ -212,7 +214,10 @@ module CrystalRuby
|
|
212
214
|
FileUtils.mkdir_p "#{config.crystal_src_dir}/#{config.crystal_codegen_dir}"
|
213
215
|
FileUtils.mkdir_p "#{config.crystal_lib_dir}"
|
214
216
|
unless File.exist?("#{config.crystal_src_dir}/#{config.crystal_main_file}")
|
215
|
-
IO.write(
|
217
|
+
IO.write(
|
218
|
+
"#{config.crystal_src_dir}/#{config.crystal_main_file}",
|
219
|
+
"require \"./#{config.crystal_codegen_dir}/index\"\n"
|
220
|
+
)
|
216
221
|
end
|
217
222
|
return if File.exist?("#{config.crystal_src_dir}/shard.yml")
|
218
223
|
|
@@ -295,12 +300,11 @@ module CrystalRuby
|
|
295
300
|
extend FFI::Library
|
296
301
|
ffi_lib "#{config.crystal_lib_dir}/#{config.crystal_lib_name}"
|
297
302
|
attach_function :attach_rb_error_handler, [:pointer], :int
|
298
|
-
const_set(:ErrorCallback, FFI::Function.new(:void, [
|
303
|
+
const_set(:ErrorCallback, FFI::Function.new(:void, %i[string string]) do |error_type, message|
|
299
304
|
error_type = error_type.to_sym
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
raise error_type.new(message)
|
305
|
+
is_exception_type = Object.const_defined?(error_type) && Object.const_get(error_type).ancestors.include?(Exception)
|
306
|
+
error_type = is_exception_type ? Object.const_get(error_type) : RuntimeError
|
307
|
+
raise error_type.new(message)
|
304
308
|
end)
|
305
309
|
attach_rb_error_handler(ErrorCallback)
|
306
310
|
end
|
@@ -313,7 +317,9 @@ module CrystalRuby
|
|
313
317
|
end
|
314
318
|
|
315
319
|
def self.write_function(owner, name:, body:, &compile_callback)
|
316
|
-
|
320
|
+
unless defined?(@compiled)
|
321
|
+
@compiled = File.exist?("#{config.crystal_src_dir}/#{config.crystal_codegen_dir}/index.cr")
|
322
|
+
end
|
317
323
|
@block_store ||= []
|
318
324
|
@block_store << { owner: owner, name: name, body: body, compile_callback: compile_callback }
|
319
325
|
FileUtils.mkdir_p("#{config.crystal_src_dir}/#{config.crystal_codegen_dir}")
|