crystalruby 0.1.3 → 0.1.5

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: 157d03e574c1ef5e06073e71c29e259741c26373113f2ae51c692b4aaef429f9
4
- data.tar.gz: 94c1f670b52a39b78c961a3050c2587c540a1cb1593c30e87d6bd05d90a4d736
3
+ metadata.gz: e8dc48667be65ee6e6d36ba7efcfc87dfefb133a06cb375a22297db9643221e3
4
+ data.tar.gz: 40753b3741ae9fb5dec86019d35968027517280a08ae6bffbcf8f2e207fe96b3
5
5
  SHA512:
6
- metadata.gz: 1b7972639977a290fd9a42b737edd1f3eec28a240a6e0e773619190bf646ec0707520d317ddf6e6152f507753b16fa66db0d2903c18fd23e58ebce4422e02555
7
- data.tar.gz: bc3d03de05c21a14a6d6d9b6de76ff04efcd660999c0dd2c166cc306707d0f93be01053e34ba8a531c9e0a9154e887bb1c6796b141564e290e25d537eeab88b4
6
+ metadata.gz: 1682a4405fd2baf66a93fc913af2146fa11e9806b90c8d6ac6d0eebc496b30415204474bd841377e3ea409401608f2907f58d7b717b21520cd4b1fe04d825d86
7
+ data.tar.gz: 8682ebdcdd83bda6ab29261cdfc627afe1bd211343bc4caa95c9f08cbb30fba663f1aebcf4df1535e27d7a84377449d5028afcce145cc0a91c153721e2d261f1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.4] - 2024-04-10
4
+
5
+ - Fix bug in type checking on deserialization of union types
6
+
3
7
  ## [0.1.3] - 2024-04-10
4
8
 
5
9
  - Support exceptions thrown in Crystal being caught in Ruby
data/README.md CHANGED
@@ -1,4 +1,19 @@
1
- # crystalruby
1
+ <table>
2
+ <tr>
3
+ <td><img src="logo.png" alt="logo" width="150"></td>
4
+ <td>
5
+ <h1 align="center">crystalruby</h1>
6
+ <p align="center">
7
+ <a href="https://rubygems.org/gems/crystalruby">
8
+ <img alt="GEM Version" src="https://img.shields.io/gem/v/crystalruby?color=168AFE&include_prereleases&logo=ruby&logoColor=FE1616">
9
+ </a><br>
10
+ <a href="https://rubygems.org/gems/crystalruby">
11
+ <img alt="GEM Downloads" src="https://img.shields.io/gem/dt/crystalruby?color=168AFE&logo=ruby&logoColor=FE1616">
12
+ </a>
13
+ </p>
14
+ </td>
15
+ </tr>
16
+ </table>
2
17
 
3
18
  `crystalruby` is a gem that allows you to write Crystal code, inlined in Ruby. All you need is a modern crystal compiler installed on your system.
4
19
 
@@ -196,6 +211,22 @@ ArgumentError: Expected Bool but was Int at line 1, column 15
196
211
  from crystalruby.rb:303:in `block in compile!'
197
212
  ```
198
213
 
214
+ ## Named Types
215
+
216
+ You can name your types, for more succinct method signatures.
217
+ The type names will be mirrored in the generated Crystal code.
218
+ E.g.
219
+
220
+ ```ruby
221
+
222
+ IntArrOrBoolArr = crtype{ Array(Bool) | Array(Int32) }
223
+
224
+ crystalize [a: IntArrOrBoolArr] => json{ IntArrOrBoolArr }
225
+ def method_with_named_types(a)
226
+ return a
227
+ end
228
+ ```
229
+
199
230
  ## Exceptions
200
231
 
201
232
  Exceptions thrown in Crystal code can be caught in Ruby.
@@ -294,7 +325,6 @@ It should support escape hatches to allow it to coexist with code that performs
294
325
  The library is currently in its infancy. Planned additions are:
295
326
 
296
327
  - Replace existing checksum process, with one that combines results of inline and external crystal to more accurately detect when recompilation is necessary.
297
- - Support for automatic serialization of nested data structures (holding _ONLY_ primitives), using JSON as our serialization protocol (prioritizing portability over raw serialization performance. JSON generation and parsing is bundled into the stdlib in both languages).
298
328
  - Simple mixin/concern that utilises `FFI::Struct` for bi-directional passing of Ruby objects and Crystal objects (by value).
299
329
  - Install command to generate a sample build script, and supports build command (which simply verifies then invokes this script)
300
330
  - Call Ruby from Crystal using FFI callbacks (implement `.expose_to_crystal`)
@@ -1,4 +1,3 @@
1
- require 'pry-byebug'
2
1
  module CrystalRuby
3
2
  module Template
4
3
  Dir[File.join(File.dirname(__FILE__), "templates", "*.cr")].each do |file|
@@ -65,115 +65,3 @@ module CrystalRuby
65
65
  end
66
66
  end
67
67
  end
68
-
69
- # class UnionType
70
- # attr_reader :inner
71
-
72
- # def initialize(*inner)
73
- # @inner = inner
74
- # end
75
-
76
- # def |(other)
77
- # UnionType.new(*inner, *other.inner)
78
- # end
79
-
80
- # def inspect
81
- # elements = inner.map(&:inspect).join(" | ")
82
- # end
83
- # end
84
-
85
- # class Type
86
- # attr_reader :inner, :contains
87
-
88
- # def initialize(name)
89
- # @name = name
90
- # @contains = contains
91
- # @inner = [self]
92
- # end
93
-
94
- # def |(other)
95
- # UnionType.new(*inner, *other.inner)
96
- # end
97
-
98
- # def inspect
99
- # if @contains
100
- # "#{@name}(#{@contains.inspect})"
101
- # else
102
- # @name
103
- # end
104
- # end
105
- # end
106
-
107
- # module_function
108
-
109
- # %w[
110
- # Bool Uint8 Uint16 Uint32 Uint64 Int8 Int16 Int32 Int64 Float32 Float64 String Time Symbol
111
- # Null
112
- # ].map do |t|
113
- # cls = Class.new(Type)
114
- # const_set(t, cls)
115
- # define_method(t.downcase) do
116
- # cls.new(t)
117
- # end
118
- # end
119
-
120
- # def build(&blk)
121
- # instance_exec(&blk)
122
- # end
123
-
124
- # def hash(key_type, value_type)
125
- # Hash.new(key_type, value_type)
126
- # end
127
-
128
- # def array(type)
129
- # Array.new(type)
130
- # end
131
-
132
- # def tuple(*types)
133
- # Tuple.new(*types)
134
- # end
135
-
136
- # def named_tuple(type_hash)
137
- # NamedTuple.new(type_hash)
138
- # end
139
-
140
- # def NamedTuple(type_hash)
141
- # NamedTuple.new(type_hash)
142
- # end
143
-
144
- # class Hash < Type
145
- # HASH_KEY_TYPES = %w[String Symbol].freeze
146
- # def initialize(key_type, value_type)
147
- # super("Hash")
148
- # @key_type = key_type
149
- # @value_type = value_type
150
- # raise "Invalid key type" unless [Uint8, Uint16, Uint32, Uint64, Int8, Int16, Int32, Int64,
151
- # String].include?(key_type)
152
- # raise "Invalid value type" unless value_type.is_a?(Type)
153
- # end
154
- # end
155
-
156
- # class Array < Type
157
- # def initialize(value_type)
158
- # super("Array")
159
- # @value_type = value_type
160
- # raise "Invalid value type" unless value_type.is_a?(Type)
161
- # end
162
- # end
163
-
164
- # class NamedTuple < Type
165
- # def initialize(types_hash)
166
- # raise "keys must be symbols" unless types_hash.keys.all? { |k| k.is_a?(Symbol) }
167
- # raise "Invalid value type" unless types_hash.values.all? { |v| v.is_a?(Type) }
168
-
169
- # super("NamedTuple")
170
- # @types_hash = types_hash
171
- # end
172
- # end
173
-
174
- # class Tuple < Type
175
- # def initialize(*value_types)
176
- # super("Tuple")
177
- # raise "Invalid value type" unless value_types.all? { |v| v.is_a?(Type) }
178
- # end
179
- # end
@@ -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
- return type.interpret!(raw)
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}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Crystalruby
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
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: arg_types.map{|k, arg_type| "#{k} = #{arg_type[:convert_lib_to_crystal_type]["_#{k}"]}" }.join("\n "),
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("#{config.crystal_src_dir}/#{config.crystal_main_file}", "require \"./#{config.crystal_codegen_dir}/index\"\n")
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, [:string, :string]) do |error_type, message|
303
+ const_set(:ErrorCallback, FFI::Function.new(:void, %i[string string]) do |error_type, message|
299
304
  error_type = error_type.to_sym
300
- error_type = Object.const_defined?(error_type) && Object.const_get(error_type).ancestors.include?(Exception) ?
301
- Object.const_get(error_type) :
302
- RuntimeError
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
- @compiled = File.exist?("#{config.crystal_src_dir}/#{config.crystal_codegen_dir}/index.cr") unless defined?(@compiled)
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}")
data/logo.png ADDED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crystalruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wouter Coppieters
@@ -107,6 +107,7 @@ files:
107
107
  - lib/crystalruby/types/union_type.rb
108
108
  - lib/crystalruby/version.rb
109
109
  - lib/module.rb
110
+ - logo.png
110
111
  - sig/crystalruby.rbs
111
112
  homepage: https://github.com/wouterken/crystalruby
112
113
  licenses: