sweet-moon 0.0.3 → 0.0.4

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: 79d7b00c236a8ce4b85d6cb4230d7e4e9714b0e537199fa3445254c0577b5bfa
4
- data.tar.gz: befd1ae3a4bcec8a274a256b5aab3c690cf876df1167531a896650b956e7dc58
3
+ metadata.gz: bdfabe100158948ea2c29fadca09e82cd9567d71230627f6bd0e8ba3a04f523d
4
+ data.tar.gz: 3bb2925b0ef437c3942a837a67bb94261d89c9aaf5a011973dcf4b98db07e6b9
5
5
  SHA512:
6
- metadata.gz: 05ff924f37bab331deb03a55e21b968384fd7755cc38d8b4780b45ec5a14d21487df64df1dea2122c41ad4dedfb4e8013964e926a826537e713aa253b923be90
7
- data.tar.gz: 8d1266390a693ef67f8bf5209eb5b2cf2b997bddb1e9f90ba5e2030fadd9ddf5819f2411099173b7a4528eafdcd09b7157675bcff77e3ca864fe600018085355
6
+ metadata.gz: 582a792771b05e59a577349070709e78bb2a569ef14cdb955fb2666e826eb401ba4aa4e7b19297c30fd64f98eda244d656caf2e904f9c1af95e971807aed8820
7
+ data.tar.gz: 38307987e564f998b40b2792fd97a3e107095d6e46992689663d63d67bf248dfc16c3a8838f8461bf7a8765857e7b5c04da4e4ac3c636cd6a419abb85bab2e5b
data/README.md CHANGED
@@ -21,11 +21,13 @@ _Sweet Moon_ is a resilient solution that makes working with [Lua](https://www.l
21
21
  - [Tables, Arrays, and Hashes](#tables-arrays-and-hashes)
22
22
  - [Functions](#functions)
23
23
  - [Other Types](#other-types)
24
+ - [Lua Global vs Local Variables](#lua-global-vs-local-variables)
24
25
  - [_destroy_ and _clear_](#destroy-and-clear)
25
26
  - [Modules, Packages and LuaRocks](#modules-packages-and-luarocks)
26
27
  - [Integration with LuaRocks](#integration-with-luarocks)
27
28
  - [Fennel](#fennel)
28
29
  - [Fennel Usage](#fennel-usage)
30
+ - [Fennel Global vs Local Variables](#fennel-global-vs-local-variables)
29
31
  - [Fennel Setup](#fennel-setup)
30
32
  - [Integration with fnx](#integration-with-fnx)
31
33
  - [Global vs Isolated](#global-vs-isolated)
@@ -69,7 +71,7 @@ gem install sweet-moon
69
71
  > **Disclaimer:** It's an early-stage project, and you should expect breaking changes.
70
72
 
71
73
  ```ruby
72
- gem 'sweet-moon', '~> 0.0.3'
74
+ gem 'sweet-moon', '~> 0.0.4'
73
75
  ```
74
76
 
75
77
  ```ruby
@@ -209,6 +211,7 @@ Compared to: [rufus-lua](https://github.com/jmettraux/rufus-lua), [YAML](https:/
209
211
  - [Tables, Arrays, and Hashes](#tables-arrays-and-hashes)
210
212
  - [Functions](#functions)
211
213
  - [Other Types](#other-types)
214
+ - [Lua Global vs Local Variables](#lua-global-vs-local-variables)
212
215
  - [_destroy_ and _clear_](#destroy-and-clear)
213
216
 
214
217
  ### Setup
@@ -389,6 +392,20 @@ state.eval('lua_value = {a = "text", b = 1.5, c = true}') # => nil
389
392
  state.get(:lua_value, :b) # => 1.5
390
393
  ```
391
394
 
395
+ With `set`, you can use a second parameter to set a field:
396
+
397
+ ```ruby
398
+ require 'sweet-moon'
399
+
400
+ state = SweetMoon::State.new
401
+
402
+ state.set(:myTable, {}) # => nil
403
+
404
+ state.set(:myTable, :a, 3) # => nil
405
+
406
+ state.eval('return myTable["a"]') # => 3
407
+ ```
408
+
392
409
  Caveats:
393
410
 
394
411
  - Ruby `Symbol` (e.g. `:value`) is converted to Lua `string`.
@@ -512,6 +529,24 @@ fennel_eval = state.get(:fennel_eval)
512
529
  fennel_eval.(['(+ 1 1)']) # => 2
513
530
  ```
514
531
 
532
+ #### Lua Global vs Local Variables
533
+
534
+ You can't exchange _local_ variables, only [_global_](https://www.lua.org/pil/1.2.html) ones:
535
+
536
+ ```ruby
537
+ require 'sweet-moon'
538
+
539
+ state = SweetMoon::State.new
540
+
541
+ state.eval('lua_value = "Lua Text"') # => nil
542
+
543
+ state.get('lua_value') # => 'Lua Text'
544
+
545
+ state.eval('local lua_b = "b"') # => nil
546
+
547
+ state.get('lua_b') # => nil
548
+ ```
549
+
515
550
  ## _destroy_ and _clear_
516
551
 
517
552
  You can destroy a state:
@@ -714,8 +749,8 @@ state = SweetMoon::State.new
714
749
 
715
750
  state.fennel.eval('(+ 1 2)') # => 3
716
751
 
717
- state.fennel.eval('(global mySum (fn [a b] (+ a b)))')
718
- state.fennel.eval('(mySum 2 3)') # => 5
752
+ state.fennel.eval('(set _G.mySum (fn [a b] (+ a b)))')
753
+ state.fennel.eval('(_G.mySum 2 3)') # => 5
719
754
 
720
755
  mySum = state.fennel.get(:mySum)
721
756
 
@@ -725,7 +760,7 @@ sum_list = -> (list) { list.sum }
725
760
 
726
761
  state.set('sumList', sum_list) # => nil
727
762
 
728
- state.fennel.eval('(sumList [2 3 5])') # => 10
763
+ state.fennel.eval('(_G.sumList [2 3 5])') # => 10
729
764
 
730
765
  state.fennel.load('file.fnl')
731
766
  ```
@@ -740,6 +775,49 @@ state = SweetMoon::State.new.fennel
740
775
  state.eval('(+ 1 2)') # => 3
741
776
  ```
742
777
 
778
+ ### Fennel Global vs Local Variables
779
+
780
+ Fennel encourages you to explicitly use the [_`_G`_](https://www.lua.org/manual/5.4/manual.html#pdf-_G) table to access global variables:
781
+
782
+ ```ruby
783
+ require 'sweet-moon'
784
+
785
+ fennel = SweetMoon::State.new.fennel
786
+
787
+ fennel.eval('(set _G.a? 2)')
788
+
789
+ fennel.get('a?') # => 2
790
+ fennel.get('_G', 'a?') # => 2
791
+
792
+ fennel.set('b', 3)
793
+
794
+ fennel.eval('(print _G.b)') # => 3
795
+ ```
796
+
797
+ Although older versions have the expression `(global name "value")`, it's deprecated, and you should avoid using that. _Sweet Moon_ has no commitments in supporting this deprecated expression, and you should prefer the `_G` way.
798
+
799
+ As is [true for Lua](#lua-global-vs-local-variables), you can't exchange _local_ variables, only [_global_](https://www.lua.org/pil/1.2.html) ones:
800
+
801
+ ```ruby
802
+ require 'sweet-moon'
803
+
804
+ fennel = SweetMoon::State.new.fennel
805
+
806
+ fennel.eval('(local name "value")')
807
+
808
+ fennel.get('name') # => nil
809
+
810
+ fennel.eval('(set _G.name "value")')
811
+
812
+ fennel.get('name') # => "value"
813
+
814
+ fennel.set('var-b', 35) # => nil
815
+
816
+ fennel.eval('var-b') # => nil
817
+
818
+ fennel.eval('_G.var-b') # => 35
819
+ ```
820
+
743
821
  ### Fennel Setup
744
822
 
745
823
  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.
@@ -38,8 +38,13 @@ module Component
38
38
  { state: state, error: Interpreter[:_error].(api, state, result, pull: true) }
39
39
  },
40
40
 
41
- set_table!: ->(api, state, variable, value) {
42
- Table[:set!].(api, state, variable, value)
41
+ set_table!: ->(api, state) {
42
+ result = api.lua_settable(state[:lua], -3)
43
+
44
+ api.lua_settop(state[:lua], -2)
45
+
46
+ { state: state,
47
+ error: Interpreter[:_error].(api, state, result, pull: false) }
43
48
  },
44
49
 
45
50
  push_value!: ->(api, state, value) {
@@ -55,12 +60,19 @@ module Component
55
60
  },
56
61
 
57
62
  get_variable_and_push!: ->(api, state, variable, key = nil) {
63
+ extra_pop = true
64
+ if variable == '_G'
65
+ variable = key
66
+ key = nil
67
+ extra_pop = false
68
+ end
69
+
58
70
  api.lua_pushstring(state[:lua], variable.to_s)
59
71
  api.lua_gettable(state[:lua], Logic::V50::Interpreter[:LUA_GLOBALSINDEX])
60
72
 
61
73
  Table[:read_field_and_push!].(api, state, key, -1) unless key.nil?
62
74
 
63
- { state: state }
75
+ { state: state, extra_pop: extra_pop }
64
76
  },
65
77
 
66
78
  call!: ->(api, state, inputs = 0, outputs = 1) {
@@ -45,7 +45,7 @@ module Component
45
45
 
46
46
  tuples << [key[:value], value[:value]]
47
47
 
48
- break if value[:type] == 'no value'
48
+ break if value[:type] == 'no value' || key[:value].instance_of?(Proc)
49
49
  end
50
50
 
51
51
  { value: Logic::Tables[:to_hash_or_array].(tuples), pop: true }
@@ -78,7 +78,7 @@ module Component
78
78
  break
79
79
  end
80
80
 
81
- break if value[:type] == 'no value'
81
+ break if value[:type] == 'no value' || key[:value].instance_of?(Proc)
82
82
  end
83
83
 
84
84
  api.lua_settop(state[:lua], -2)
@@ -31,8 +31,13 @@ module Component
31
31
  { state: state, error: Interpreter[:_error].(api, state, result, pull: true) }
32
32
  },
33
33
 
34
- set_table!: ->(api, state, variable, value) {
35
- Table[:set!].(api, state, variable, value)
34
+ set_table!: ->(api, state) {
35
+ result = api.lua_settable(state[:lua], -3)
36
+
37
+ api.lua_settop(state[:lua], -2)
38
+
39
+ { state: state,
40
+ error: Interpreter[:_error].(api, state, result, pull: false) }
36
41
  },
37
42
 
38
43
  push_value!: ->(api, state, value) {
@@ -60,7 +65,7 @@ module Component
60
65
  end
61
66
  end
62
67
 
63
- { state: state }
68
+ { state: state, extra_pop: true }
64
69
  },
65
70
 
66
71
  call!: ->(api, state, inputs = 0, outputs = 1) {
@@ -45,7 +45,7 @@ module Component
45
45
 
46
46
  tuples << [key[:value], value[:value]]
47
47
 
48
- break if value[:type] == 'no value'
48
+ break if value[:type] == 'no value' || key[:value].instance_of?(Proc)
49
49
  end
50
50
 
51
51
  { value: Logic::Tables[:to_hash_or_array].(tuples), pop: true }
@@ -30,8 +30,13 @@ module Component
30
30
  { state: state, error: Interpreter[:_error].(api, state, result, pull: true) }
31
31
  },
32
32
 
33
- set_table!: ->(api, state, variable, value) {
34
- Table[:set!].(api, state, variable, value)
33
+ set_table!: ->(api, state) {
34
+ result = api.lua_settable(state[:lua], -3)
35
+
36
+ api.lua_settop(state[:lua], -2)
37
+
38
+ { state: state,
39
+ error: Interpreter[:_error].(api, state, result, pull: false) }
35
40
  },
36
41
 
37
42
  push_value!: ->(api, state, value) {
@@ -56,7 +61,7 @@ module Component
56
61
  end
57
62
  end
58
63
 
59
- { state: state }
64
+ { state: state, extra_pop: true }
60
65
  },
61
66
 
62
67
  call!: ->(api, state, inputs = 0, outputs = 1) {
@@ -1,5 +1,7 @@
1
1
  # Available at https://github.com/gbaptista/sweet-moon-test
2
2
 
3
+ fennel-dev: '/home/me/sweet-moon-test/fennel-dev.lua'
4
+
3
5
  luarocks:
4
6
  path:
5
7
  - /home/me/.luarocks/share/lua/5.4/?.lua
data/controllers/state.rb CHANGED
@@ -26,23 +26,48 @@ module Controller
26
26
  },
27
27
 
28
28
  get!: ->(api, interpreter, state, variable, key = nil) {
29
+ if key.nil?
30
+ key = variable
31
+ variable = '_G'
32
+ end
33
+
34
+ State[:_get_key!].(api, interpreter, state, variable, key)
35
+ },
36
+
37
+ _get_key!: ->(api, interpreter, state, variable, key) {
29
38
  result = State[:_check!].(
30
39
  interpreter[:get_variable_and_push!].(api, state, variable, key)
31
40
  )
32
41
 
33
- result = State[:_check!].(interpreter[:read_and_pop!].(
34
- api, result[:state], -1, extra_pop: !key.nil?
35
- ))
42
+ result = State[:_check!].(
43
+ interpreter[:read_and_pop!].(
44
+ api, result[:state], -1, extra_pop: result[:extra_pop]
45
+ )
46
+ )
36
47
 
37
48
  { state: result[:state], output: result[:output] }
38
49
  },
39
50
 
40
- set!: ->(api, interpreter, state, variable, value) {
41
- result = State[:_check!].(interpreter[:push_value!].(api, state, value))
42
-
43
- result = State[:_check!].(
44
- interpreter[:pop_and_set_as!].(api, result[:state], variable.to_s)
45
- )
51
+ set!: ->(api, interpreter, state, variable, key_or_value, value = nil) {
52
+ if value.nil?
53
+ result = State[:_check!].(interpreter[:push_value!].(api, state,
54
+ key_or_value))
55
+
56
+ result = State[:_check!].(
57
+ interpreter[:pop_and_set_as!].(api, result[:state], variable.to_s)
58
+ )
59
+ else
60
+ result = State[:_check!].(
61
+ interpreter[:get_variable_and_push!].(api, state, variable)
62
+ )
63
+
64
+ result = State[:_check!].(interpreter[:push_value!].(api, result[:state],
65
+ key_or_value))
66
+ result = State[:_check!].(interpreter[:push_value!].(api, result[:state],
67
+ value))
68
+
69
+ result = State[:_check!].(interpreter[:set_table!].(api, result[:state]))
70
+ end
46
71
 
47
72
  { state: result[:state], output: result[:output] }
48
73
  },
data/dsl/fennel.rb CHANGED
@@ -11,6 +11,9 @@ module DSL
11
11
  'table.insert(package.loaders or package.searchers, fennel.searcher)'
12
12
  )
13
13
 
14
+ # TODO: Makes sense?
15
+ # debug.traceback = fennel.traceback
16
+
14
17
  @eval = @state.get(:fennel, :eval)
15
18
  @dofile = @state.get(:fennel, :dofile)
16
19
  @version = @state.get(:fennel, :version)
data/dsl/state.rb CHANGED
@@ -34,8 +34,10 @@ module DSL
34
34
  @controller[:get!].(@api, @interpreter, state, variable, key)[:output]
35
35
  end
36
36
 
37
- def set(variable, value)
38
- @controller[:set!].(@api, @interpreter, state, variable, value)[:output]
37
+ def set(variable, key_or_value, value = nil)
38
+ @controller[:set!].(
39
+ @api, @interpreter, state, variable, key_or_value, value
40
+ )[:output]
39
41
  end
40
42
 
41
43
  def destroy
@@ -6,6 +6,7 @@ module Logic
6
6
  LUA_REGISTRYINDEX: -10_000,
7
7
  LUA_GLOBALSINDEX: -10_001,
8
8
 
9
+ # lua_isinteger lua_pushinteger lua_tointeger
9
10
  requires: %i[
10
11
  lua_close lua_gettable lua_gettop lua_insert lua_newtable lua_next lua_open
11
12
  lua_pcall lua_pushboolean lua_pushcclosure lua_pushnil lua_pushnumber
@@ -12,11 +12,11 @@ module Logic
12
12
 
13
13
  # lua_isinteger lua_pushinteger lua_tointeger
14
14
  requires: %i[
15
- lua_close lua_createtable lua_getfield lua_getglobal lua_gettop lua_next
16
- lua_pcall lua_pushboolean lua_pushcclosure lua_pushnil lua_pushnumber
17
- lua_pushstring lua_rawgeti lua_setglobal lua_settable lua_settop lua_toboolean
18
- lua_tonumber lua_topointer lua_tostring lua_type lua_typename luaL_loadfile
19
- luaL_loadstring luaL_newstate luaL_openlibs luaL_ref
15
+ lua_close lua_createtable lua_getfield lua_gettop lua_next lua_pcall
16
+ lua_pushboolean lua_pushcclosure lua_pushnil lua_pushnumber lua_pushstring
17
+ lua_rawgeti lua_settable lua_settop lua_toboolean lua_tonumber lua_topointer
18
+ lua_tostring lua_type lua_typename luaL_loadfile luaL_loadstring luaL_newstate
19
+ luaL_openlibs luaL_ref lua_getglobal lua_setglobal
20
20
  ],
21
21
 
22
22
  status: {
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.3',
5
+ version: '0.0.4',
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.3
4
+ version: 0.0.4
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-23 00:00:00.000000000 Z
11
+ date: 2022-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi