crystalruby 0.1.4 → 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: 26b2e0d15fd3b320f700a1f0571b8bb5d8991a4ed1c60cfcd9f003b48bb9f891
4
- data.tar.gz: 27243d104172eef50adfc46524a0b9052410636dc0b39e142fc3e53c04ded1f0
3
+ metadata.gz: 7c552102391f468b4974cb4ac36c4876001743fdec5fa8f2a69577255de4c52d
4
+ data.tar.gz: f7c104b10cf70eb03ab064117be946d4268c97063ba9302a99613e9c473ded97
5
5
  SHA512:
6
- metadata.gz: a54c1cf883a7dea97d66e9c18903139713aaec0b965026a21fc2e14ced85c13fc676f7d8e76ecc9a2e6ca2fc69034185a9fa4eb6fc66a6986cc9bb2d037738fb
7
- data.tar.gz: 0f29fe7bad266af6593d0e4533af27a9be925372befe40ed0e4ee5ea8c9293c7ea62aab4b1664eaf3d2202eb3000b3dda1dfbdc44ae9e924e2de017f5516a6bc
6
+ metadata.gz: 0c01cefa9a6f820cd59aed62e02846bd90cd53a742c203b8de0023908c53e77ef2a558adf26023008dcd921e9c9cbc0466a63598361c18ec7cc6bee29a57ce2c
7
+ data.tar.gz: a9fe76d0b8a93dab4afd94d696d9955a81e75384fd1d4af8a0af5249c30fd51920d46de4728caab630862c889e799f1207f9915176fe861c6624e7a13d994e5a
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
 
@@ -118,6 +133,31 @@ def add(a, b)
118
133
  end
119
134
  ```
120
135
 
136
+ ## Getting Started
137
+
138
+ The below is a stand-alone one-file script that allows you to quickly see crystalruby in action.
139
+
140
+ ```ruby
141
+ # crystalrubytest.rb
142
+ require 'bundler/inline'
143
+
144
+ gemfile do
145
+ source 'https://rubygems.org'
146
+ gem 'crystalruby', path: '../crystalruby'
147
+ end
148
+
149
+ require 'crystalruby'
150
+
151
+ module Adder
152
+ crystalize [a: :int, b: :int] => :int
153
+ def add(a, b)
154
+ a + b * 3
155
+ end
156
+ end
157
+
158
+ puts Adder.add(1, 2)
159
+ ```
160
+
121
161
  ## Types
122
162
 
123
163
  Currently primitive types are supported.
@@ -234,7 +274,7 @@ Remember to require these installed shards after installing them. E.g. inside `.
234
274
 
235
275
  You can edit the default paths for crystal source and library files from within the `./crystalruby.yaml` config file.
236
276
 
237
- ### Wrapping Crystal code in Ruby
277
+ ## Wrapping Crystal code in Ruby
238
278
 
239
279
  Sometimes you may want to wrap a Crystal method in Ruby, so that you can use Ruby before the Crystal code to prepare arguments, or after the Crystal code, to apply transformations to the result. A real-life example of this might be an ActionController method, where you might want to use Ruby to parse the request, perform auth etc., and then use Crystal to perform some heavy computation, before returning the result from Ruby.
240
280
  To do this, you simply pass a block to the `crystalize` method, which will serve as the Ruby entry point to the function. From within this block, you can invoke `super` to call the Crystal method, and then apply any Ruby transformations to the result.
@@ -255,7 +295,7 @@ end
255
295
  MyModule.add("1", "2")
256
296
  ```
257
297
 
258
- ### Release Builds
298
+ ## Release Builds
259
299
 
260
300
  You can control whether CrystalRuby builds in debug or release mode by setting following config option
261
301
 
@@ -288,7 +328,7 @@ CrystalRuby.compile!
288
328
 
289
329
  Then you can run this file as part of your build step, to ensure all Crystal code is compiled ahead of time.
290
330
 
291
- ### Troubleshooting
331
+ ## Troubleshooting
292
332
 
293
333
  The logic to detect when to JIT recompile is not robust and can end up in an inconsistent state. To remedy this it is useful to clear out all generated assets and build from scratch.
294
334
 
@@ -310,7 +350,6 @@ It should support escape hatches to allow it to coexist with code that performs
310
350
  The library is currently in its infancy. Planned additions are:
311
351
 
312
352
  - Replace existing checksum process, with one that combines results of inline and external crystal to more accurately detect when recompilation is necessary.
313
- - 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).
314
353
  - Simple mixin/concern that utilises `FFI::Struct` for bi-directional passing of Ruby objects and Crystal objects (by value).
315
354
  - Install command to generate a sample build script, and supports build command (which simply verifies then invokes this script)
316
355
  - 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Crystalruby
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  end
data/lib/crystalruby.rb CHANGED
@@ -292,10 +292,11 @@ module CrystalRuby
292
292
  "crystal build --release --no-debug -o #{lib_target} #{config.crystal_main_file}"
293
293
  end
294
294
 
295
- raise "Error compiling crystal code" unless result = system(cmd)
296
-
295
+ unless result = system(cmd)
296
+ File.delete("#{config.crystal_codegen_dir}/index.cr") if File.exist?("#{config.crystal_codegen_dir}/index.cr")
297
+ raise "Error compiling crystal code"
298
+ end
297
299
  @compiled = true
298
- File.delete("#{config.crystal_codegen_dir}/index.cr") if File.exist?("#{config.crystal_codegen_dir}/index.cr")
299
300
  end
300
301
  extend FFI::Library
301
302
  ffi_lib "#{config.crystal_lib_dir}/#{config.crystal_lib_name}"
@@ -303,7 +304,7 @@ module CrystalRuby
303
304
  const_set(:ErrorCallback, FFI::Function.new(:void, %i[string string]) do |error_type, message|
304
305
  error_type = error_type.to_sym
305
306
  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
+ error_type = is_exception_type ? Object.const_get(error_type) : RuntimeError
307
308
  raise error_type.new(message)
308
309
  end)
309
310
  attach_rb_error_handler(ErrorCallback)
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.4
4
+ version: 0.1.6
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: