sweet-moon 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +77 -1
- data/config/tests.sample.yml +1 -1
- data/dsl/fennel.rb +8 -4
- data/dsl/state.rb +27 -4
- data/logic/spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 880cceae5913b9a748b5b318bfc27c56ccf58d7ae54b7f31f8aeea943c524aac
|
4
|
+
data.tar.gz: ab2abc87ef9ead4ea7be474963a92ad647326ec6d4ad1bfec62ed20d103d4e4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b613dba97c24527e3e71a8bd7c6d14cd98edcd9a0083a85463a3300f05886a81da33ea5c515108fe523ed43ab012760954edd40dacec4aa38a250e6846b3906
|
7
|
+
data.tar.gz: c8c09f0f1bc7cde58c789975b84491399af843b8d95cb69bd3600dbb4c22a056f232e99eea50d3af969ebea6fa298bd86214a596d7ab67afe5c4a209d8a43fa3
|
data/README.md
CHANGED
@@ -28,6 +28,7 @@ _Sweet Moon_ is a resilient solution that makes working with [Lua](https://www.l
|
|
28
28
|
- [Fennel](#fennel)
|
29
29
|
- [Fennel Usage](#fennel-usage)
|
30
30
|
- [Fennel Global vs Local Variables](#fennel-global-vs-local-variables)
|
31
|
+
- [allowedGlobals and options](#allowedglobals-and-options)
|
31
32
|
- [Fennel Setup](#fennel-setup)
|
32
33
|
- [Integration with fnx](#integration-with-fnx)
|
33
34
|
- [Fennel REPL](#fennel-repl)
|
@@ -74,7 +75,7 @@ gem install sweet-moon
|
|
74
75
|
> **Disclaimer:** It's an early-stage project, and you should expect breaking changes.
|
75
76
|
|
76
77
|
```ruby
|
77
|
-
gem 'sweet-moon', '~> 0.0.
|
78
|
+
gem 'sweet-moon', '~> 0.0.7'
|
78
79
|
```
|
79
80
|
|
80
81
|
```ruby
|
@@ -445,6 +446,16 @@ second = state.get(:second)
|
|
445
446
|
second.([%w[a b c]]) # => 'b'
|
446
447
|
```
|
447
448
|
|
449
|
+
Alternatively, you can send the `outputs` parameter:
|
450
|
+
|
451
|
+
```ruby
|
452
|
+
require 'sweet-moon'
|
453
|
+
|
454
|
+
state = SweetMoon::State.new
|
455
|
+
|
456
|
+
state.eval('return "a", "b"', { outputs: 2 }) # => ['a', 'b']
|
457
|
+
```
|
458
|
+
|
448
459
|
You can call Ruby _Lambdas_ from _Lua_ as well:
|
449
460
|
|
450
461
|
```ruby
|
@@ -821,6 +832,71 @@ fennel.eval('var-b') # => nil
|
|
821
832
|
fennel.eval('_G.var-b') # => 35
|
822
833
|
```
|
823
834
|
|
835
|
+
### allowedGlobals and options
|
836
|
+
|
837
|
+
As Lua, Fennel functions may return [multiple results](https://www.lua.org/pil/5.1.html), so `eval` and `load` accept a second parameter to indicate the expected number of outputs:
|
838
|
+
|
839
|
+
```fnl
|
840
|
+
; source.fnl
|
841
|
+
|
842
|
+
(fn multi [] (values "c" "d"))
|
843
|
+
|
844
|
+
(multi)
|
845
|
+
```
|
846
|
+
|
847
|
+
```ruby
|
848
|
+
require 'sweet-moon'
|
849
|
+
|
850
|
+
fennel = SweetMoon::State.new.fennel
|
851
|
+
|
852
|
+
fennel.eval('(values "a" "b")', 2) # => ['a', 'b']
|
853
|
+
fennel.load('source.fnl', 2) # => ['c', 'd']
|
854
|
+
```
|
855
|
+
|
856
|
+
The Fennel API offers [some options](https://fennel-lang.org/api) that `eval` and `load` accept as a third parameter:
|
857
|
+
|
858
|
+
```ruby
|
859
|
+
require 'sweet-moon'
|
860
|
+
|
861
|
+
fennel = SweetMoon::State.new.fennel
|
862
|
+
|
863
|
+
fennel.eval('(print (+ 2 3))', 1, { allowedGlobals: ['print'] }) # => 5
|
864
|
+
|
865
|
+
fennel.eval('(print (+ 2 3))', 1, { allowedGlobals: [] })
|
866
|
+
# Compile error in unknown:1 (SweetMoon::Errors::LuaRuntimeError)
|
867
|
+
# unknown identifier in strict mode: print
|
868
|
+
|
869
|
+
# (print (+ 2 3))
|
870
|
+
# ^^^^^
|
871
|
+
# * Try looking to see if there's a typo.
|
872
|
+
# * Try using the _G table instead, eg. _G.print if you really want a global.
|
873
|
+
# * Try moving this code to somewhere that print is in scope.
|
874
|
+
# * Try binding print as a local in the scope of this code.
|
875
|
+
```
|
876
|
+
|
877
|
+
Alternatively, you can use the second parameter for options as well:
|
878
|
+
|
879
|
+
```ruby
|
880
|
+
require 'sweet-moon'
|
881
|
+
|
882
|
+
fennel = SweetMoon::State.new.fennel
|
883
|
+
|
884
|
+
fennel.eval('(print (+ 2 3))', { allowedGlobals: ['print'] }) # => 5
|
885
|
+
```
|
886
|
+
|
887
|
+
You can also specify the expected outputs in the options parameter (it will be removed and not forwarded to Fennel):
|
888
|
+
|
889
|
+
```ruby
|
890
|
+
require 'sweet-moon'
|
891
|
+
|
892
|
+
fennel = SweetMoon::State.new.fennel
|
893
|
+
|
894
|
+
fennel.eval(
|
895
|
+
'(values "a" "b")',
|
896
|
+
{ allowedGlobals: ['values'], outputs: 2 }
|
897
|
+
) # => ['a', 'b']
|
898
|
+
```
|
899
|
+
|
824
900
|
### Fennel Setup
|
825
901
|
|
826
902
|
To ensure that the Fennel module is available, you can set up the [_LuaRocks_](#integration-withluarocks) integration or manually add the `package_path` for the module.
|
data/config/tests.sample.yml
CHANGED
data/dsl/fennel.rb
CHANGED
@@ -20,12 +20,16 @@ module DSL
|
|
20
20
|
build_meta
|
21
21
|
end
|
22
22
|
|
23
|
-
def eval(input,
|
24
|
-
|
23
|
+
def eval(input, first = nil, second = nil)
|
24
|
+
options = _build_options(first, second)
|
25
|
+
|
26
|
+
@eval.([input, options[:options]], options[:outputs])
|
25
27
|
end
|
26
28
|
|
27
|
-
def load(path,
|
28
|
-
|
29
|
+
def load(path, first = nil, second = nil)
|
30
|
+
options = _build_options(first, second)
|
31
|
+
|
32
|
+
@dofile.([path, options[:options]], options[:outputs])
|
29
33
|
end
|
30
34
|
|
31
35
|
def build_meta
|
data/dsl/state.rb
CHANGED
@@ -28,11 +28,19 @@ module DSL
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def eval(input, outputs = 1)
|
31
|
-
|
31
|
+
options = _build_options(outputs, nil)
|
32
|
+
|
33
|
+
@controller[:eval!].(
|
34
|
+
@api, @interpreter, state, input, options[:outputs]
|
35
|
+
)[:output]
|
32
36
|
end
|
33
37
|
|
34
38
|
def load(path, outputs = 1)
|
35
|
-
|
39
|
+
options = _build_options(outputs, nil)
|
40
|
+
|
41
|
+
@controller[:load!].(
|
42
|
+
@api, @interpreter, state, path, options[:outputs]
|
43
|
+
)[:output]
|
36
44
|
end
|
37
45
|
|
38
46
|
def get(variable, key = nil)
|
@@ -56,6 +64,22 @@ module DSL
|
|
56
64
|
nil
|
57
65
|
end
|
58
66
|
|
67
|
+
def _build_options(first, second)
|
68
|
+
options = { outputs: 1 }
|
69
|
+
|
70
|
+
options = options.merge(first) if first.is_a? Hash
|
71
|
+
options = options.merge(second) if second.is_a? Hash
|
72
|
+
|
73
|
+
options[:outputs] = first if first.is_a? Numeric
|
74
|
+
options[:outputs] = second if second.is_a? Numeric
|
75
|
+
|
76
|
+
outputs = options[:outputs] if options[:outputs]
|
77
|
+
|
78
|
+
options.delete(:outputs)
|
79
|
+
|
80
|
+
{ options: options, outputs: outputs }
|
81
|
+
end
|
82
|
+
|
59
83
|
def _ensure_min_version!(purpose, lua, jit = nil)
|
60
84
|
version = lua
|
61
85
|
version = jit if meta.interpreter[/jit/] && jit
|
@@ -113,8 +137,7 @@ module DSL
|
|
113
137
|
|
114
138
|
def state
|
115
139
|
unless @state
|
116
|
-
raise SweetMoon::Errors::SweetMoonError,
|
117
|
-
'The state no longer exists.'
|
140
|
+
raise SweetMoon::Errors::SweetMoonError, 'The state no longer exists.'
|
118
141
|
end
|
119
142
|
|
120
143
|
@state
|
data/logic/spec.rb
CHANGED
@@ -2,7 +2,7 @@ module Logic
|
|
2
2
|
Spec = {
|
3
3
|
name: 'sweet-moon',
|
4
4
|
command: 'sweet-moon',
|
5
|
-
version: '0.0.
|
5
|
+
version: '0.0.7',
|
6
6
|
author: 'gbaptista',
|
7
7
|
summary: 'Lua / Fennel from Ruby and vice versa. Support to LuaJIT, Lua 5.0, ' \
|
8
8
|
'and 5.1. Lua C API for Lua 5, 4, and 3. LuaRocks and fnx integration.',
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sweet-moon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gbaptista
|
8
8
|
autorequire:
|
9
9
|
bindir: ports/in/shell
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|