sweet-moon 0.0.3 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/dsl/cache.rb CHANGED
@@ -7,112 +7,126 @@ require_relative '../controllers/state'
7
7
  require_relative 'api'
8
8
  require_relative 'sweet_moon'
9
9
 
10
- class Cache
11
- include Singleton
10
+ module DSL
11
+ class Cache
12
+ include Singleton
12
13
 
13
- def clear_global!
14
- @cache[:global_state]&._unsafely_destroy
14
+ API_KEYS = %i[shared_objects api_reference global_ffi]
15
+ STATE_KEYS = %i[interpreter package_path package_cpath]
15
16
 
16
- @cache.each_key { |key| @cache.delete(key) if key[/^global/] }
17
- end
17
+ def api_keys?(options)
18
+ API_KEYS.each { |key| return true if options.key?(key) }
19
+ false
20
+ end
18
21
 
19
- def keys
20
- @cache.keys
21
- end
22
+ def state_keys?(options)
23
+ STATE_KEYS.each { |key| return true if options.key?(key) }
24
+ false
25
+ end
22
26
 
23
- def initialize
24
- @cache = {}
25
- end
27
+ def clear_global!
28
+ @cache[:global_state]&._unsafely_destroy
26
29
 
27
- def global_state(options = {}, recreate: false)
28
- key = :global_state
30
+ @cache.each_key { |key| @cache.delete(key) if key[/^global/] }
31
+ end
29
32
 
30
- clear_global_state_cache!(options) if recreate
33
+ def keys
34
+ @cache.keys
35
+ end
31
36
 
32
- return @cache[key] if @cache[key]
37
+ def initialize
38
+ @cache = {}
39
+ end
33
40
 
34
- api = Cache.instance.api_module(options, :global_api_module)
35
- interpreter = Cache.instance.interpreter_module(
36
- api, options, :global_interpreter_module
37
- )
41
+ def global_state(options = {}, recreate: false)
42
+ key = :global_state
38
43
 
39
- @cache[key] = DSL::State.new(api, interpreter, Controller::State, options)
40
- @cache[key].instance_eval('undef :destroy', __FILE__, __LINE__)
44
+ clear_global_state_cache!(options) if recreate
41
45
 
42
- @cache[key]
43
- end
46
+ return @cache[key] if @cache[key]
44
47
 
45
- def global_api(options = {}, recreate: false)
46
- key = :global_api
48
+ api = Cache.instance.api_module(options, :global_api_module)
49
+ interpreter = Cache.instance.interpreter_module(
50
+ api, options, :global_interpreter_module
51
+ )
47
52
 
48
- clear_global_api_cache! if recreate
53
+ @cache[key] = DSL::State.new(api, interpreter, Controller::State, options)
54
+ @cache[key].instance_eval('undef :destroy', __FILE__, __LINE__)
49
55
 
50
- @cache[key] ||= api(options, :global_api, :global_api_module)
51
- end
56
+ @cache[key]
57
+ end
52
58
 
53
- def api(options = {}, key = nil, api_module_key = nil)
54
- key ||= cache_key_for(:api, options, %i[shared_objects api_reference])
55
- @cache[key] ||= DSL::Api.new(api_module(options, api_module_key))
56
- end
59
+ def global_api(options = {}, recreate: false)
60
+ key = :global_api
57
61
 
58
- def api_module(options = {}, key = nil)
59
- key ||= cache_key_for(:api_module, options, %i[shared_objects api_reference])
60
- @cache[key] ||= Controller::API[:handle!].(options)
61
- end
62
+ clear_global_api_cache! if recreate
62
63
 
63
- def interpreter_module(api, options = {}, key = nil)
64
- key ||= cache_key_for(
65
- :interpreter_module,
66
- { shared_objects: api[:meta][:elected][:shared_objects],
67
- api_reference: api[:meta][:elected][:api_reference],
68
- interpreter: options[:interpreter], package_path: options[:package_path],
69
- package_cpath: options[:package_cpath] },
70
- %i[shared_objects api_reference interpreter package_path package_cpath]
71
- )
72
-
73
- @cache[key] ||= Controller::Interpreter[:handle!].(api, options)
74
- end
64
+ @cache[key] ||= api(options, :global_api, :global_api_module)
65
+ end
75
66
 
76
- private
67
+ def api(options = {}, key = nil, api_module_key = nil)
68
+ key ||= cache_key_for(:api, options, API_KEYS)
69
+ @cache[key] ||= DSL::Api.new(api_module(options, api_module_key))
70
+ end
77
71
 
78
- def clear_global_api_cache!
79
- @cache[:global_state]&._unsafely_destroy
72
+ def api_module(options = {}, key = nil)
73
+ key ||= cache_key_for(:api_module, options, API_KEYS)
74
+ @cache[key] ||= Controller::API[:handle!].(options)
75
+ end
80
76
 
81
- %i[global_api global_api_module
82
- global_interpreter_module
83
- global_state].each do |key|
84
- @cache.delete(key)
77
+ def interpreter_module(api, options = {}, key = nil)
78
+ key ||= cache_key_for(
79
+ :interpreter_module,
80
+ { shared_objects: api[:meta][:elected][:shared_objects],
81
+ api_reference: api[:meta][:elected][:api_reference],
82
+ global_ffi: api[:meta][:global_ffi],
83
+ interpreter: options[:interpreter], package_path: options[:package_path],
84
+ package_cpath: options[:package_cpath] },
85
+ API_KEYS.concat(STATE_KEYS)
86
+ )
87
+
88
+ @cache[key] ||= Controller::Interpreter[:handle!].(api, options)
85
89
  end
86
- end
87
90
 
88
- def clear_global_state_cache!(options)
89
- @cache[:global_state]&._unsafely_destroy
91
+ def cache_key_for(prefix, options = {}, relevant_keys = [])
92
+ values = [prefix]
90
93
 
91
- %i[global_interpreter_module global_state].each do |key|
92
- @cache.delete(key)
93
- end
94
+ relevant_keys.each do |key|
95
+ value = options[key]
96
+ value = options[key.to_s] if value.nil?
94
97
 
95
- return unless options.key?(:shared_objects) || options.key?(:api_reference)
98
+ values << (value.is_a?(Array) ? value.sort.join(':') : value.inspect)
99
+ end
96
100
 
97
- %i[global_api global_api_module].each do |key|
98
- @cache.delete(key)
101
+ values.join('|')
99
102
  end
100
103
 
101
- global_api(options, recreate: true)
102
- end
104
+ private
103
105
 
104
- def cache_key_for(prefix, options = {}, relevant_keys = [])
105
- values = [prefix]
106
+ def clear_global_api_cache!
107
+ @cache[:global_state]&._unsafely_destroy
106
108
 
107
- relevant_keys.each do |key|
108
- value = options[key] || options[key.to_s]
109
- if value.is_a?(Array)
110
- values << value.sort.join(':')
111
- elsif value
112
- values << value
109
+ %i[global_api global_api_module
110
+ global_interpreter_module
111
+ global_state].each do |key|
112
+ @cache.delete(key)
113
113
  end
114
114
  end
115
115
 
116
- values.join('|')
116
+ def clear_global_state_cache!(options)
117
+ @cache[:global_state]&._unsafely_destroy
118
+
119
+ %i[global_interpreter_module global_state].each do |key|
120
+ @cache.delete(key)
121
+ end
122
+
123
+ return unless api_keys?(options)
124
+
125
+ %i[global_api global_api_module].each do |key|
126
+ @cache.delete(key)
127
+ end
128
+
129
+ global_api(options, recreate: true)
130
+ end
117
131
  end
118
132
  end
data/dsl/errors.rb CHANGED
@@ -10,6 +10,14 @@ module SweetMoon
10
10
  class LuaFileError < LuaError; end
11
11
 
12
12
  module SweetMoonErrorHelper
13
+ def merge_traceback!(ruby_error, lua_traceback)
14
+ ruby_error.set_backtrace(
15
+ ruby_error.backtrace.concat(lua_traceback.split("\n"))
16
+ )
17
+
18
+ ruby_error
19
+ end
20
+
13
21
  def for(status)
14
22
  case status
15
23
  when :runtime then LuaRuntimeError
@@ -22,7 +30,7 @@ module SweetMoon
22
30
  end
23
31
  end
24
32
 
25
- module_function :for
33
+ module_function :for, :merge_traceback!
26
34
  end
27
35
  end
28
36
  end
data/dsl/fennel.rb CHANGED
@@ -11,6 +11,8 @@ module DSL
11
11
  'table.insert(package.loaders or package.searchers, fennel.searcher)'
12
12
  )
13
13
 
14
+ @state.eval('debug.traceback = fennel.traceback')
15
+
14
16
  @eval = @state.get(:fennel, :eval)
15
17
  @dofile = @state.get(:fennel, :dofile)
16
18
  @version = @state.get(:fennel, :version)
data/dsl/global.rb CHANGED
@@ -2,41 +2,40 @@ require_relative '../logic/options'
2
2
 
3
3
  require_relative 'cache'
4
4
 
5
- module Global
6
- def api
7
- Cache.instance.global_api
8
- end
5
+ module DSL
6
+ module Global
7
+ def api
8
+ Cache.instance.global_api
9
+ end
9
10
 
10
- def state
11
- Cache.instance.global_state
12
- end
11
+ def state
12
+ Cache.instance.global_state
13
+ end
13
14
 
14
- def config(options = {})
15
- options = Logic::Options[:normalize].(options)
15
+ def config(options = {})
16
+ options = Logic::Options[:normalize].(options)
16
17
 
17
- if options.key?(:shared_objects) || options.key?(:api_reference)
18
- Cache.instance.global_api(options, recreate: true)
19
- end
18
+ if Cache.instance.api_keys?(options)
19
+ Cache.instance.global_api(options, recreate: true)
20
+ end
20
21
 
21
- return unless
22
- options.key?(:interpreter) ||
23
- options.key?(:package_path) ||
24
- options.key?(:package_cpath)
22
+ return unless Cache.instance.state_keys?(options)
25
23
 
26
- Cache.instance.global_state(options, recreate: true)
24
+ Cache.instance.global_state(options, recreate: true)
27
25
 
28
- nil
29
- end
26
+ nil
27
+ end
30
28
 
31
- def cached(all: false)
32
- return Cache.instance.keys if all
29
+ def cached(all: false)
30
+ return Cache.instance.keys if all
33
31
 
34
- Cache.instance.keys.select { |key| key[/^global/] }
35
- end
32
+ Cache.instance.keys.select { |key| key[/^global/] }
33
+ end
36
34
 
37
- def clear
38
- Cache.instance.clear_global!
39
- end
35
+ def clear
36
+ Cache.instance.clear_global!
37
+ end
40
38
 
41
- module_function :api, :state, :config, :cached, :clear
39
+ module_function :api, :state, :config, :cached, :clear
40
+ end
42
41
  end
data/dsl/state.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  require_relative 'errors'
2
2
  require_relative 'concerns/packages'
3
3
  require_relative 'concerns/fennel'
4
+ require_relative '../components/default'
4
5
 
5
6
  module DSL
6
7
  class State
7
8
  include DSL::Concerns::Packages
8
9
  include DSL::Concerns::Fennel
9
10
 
10
- attr_reader :meta
11
+ attr_reader :meta, :api
11
12
 
12
13
  def initialize(api_component, interpreter_component, controller, options = {})
13
14
  @api = api_component[:api]
@@ -22,6 +23,10 @@ module DSL
22
23
  add_package_cpath(options[:package_cpath]) if options[:package_cpath]
23
24
  end
24
25
 
26
+ def raw
27
+ @state
28
+ end
29
+
25
30
  def eval(input, outputs = 1)
26
31
  @controller[:eval!].(@api, @interpreter, state, input, outputs)[:output]
27
32
  end
@@ -34,8 +39,10 @@ module DSL
34
39
  @controller[:get!].(@api, @interpreter, state, variable, key)[:output]
35
40
  end
36
41
 
37
- def set(variable, value)
38
- @controller[:set!].(@api, @interpreter, state, variable, value)[:output]
42
+ def set(variable, key_or_value, value = nil)
43
+ @controller[:set!].(
44
+ @api, @interpreter, state, variable, key_or_value, value
45
+ )[:output]
39
46
  end
40
47
 
41
48
  def destroy
@@ -81,13 +88,25 @@ module DSL
81
88
 
82
89
  private
83
90
 
84
- def build_meta(api_component, interpreter_component)
85
- meta_data = {
91
+ def build_api_meta(api_component)
92
+ global_ffi = Component::Default.instance.options[:global_ffi]
93
+
94
+ unless api_component[:meta][:options][:global_ffi].nil?
95
+ global_ffi = api_component[:meta][:options][:global_ffi]
96
+ end
97
+
98
+ {
86
99
  api_reference: api_component[:meta][:elected][:api_reference],
87
100
  shared_objects: api_component[:meta][:elected][:shared_objects],
88
- interpreter: interpreter_component[:meta][:elected][:interpreter],
89
- runtime: interpreter_component[:meta][:runtime][:lua]
101
+ global_ffi: global_ffi
90
102
  }
103
+ end
104
+
105
+ def build_meta(api_component, interpreter_component)
106
+ meta_data = build_api_meta(api_component)
107
+
108
+ meta_data[:interpreter] = interpreter_component[:meta][:elected][:interpreter]
109
+ meta_data[:runtime] = interpreter_component[:meta][:runtime][:lua]
91
110
 
92
111
  @meta = Struct.new(*meta_data.keys).new(*meta_data.values)
93
112
  end
data/dsl/sweet_moon.rb CHANGED
@@ -11,7 +11,7 @@ require_relative '../logic/interpreter'
11
11
  module SweetMoon
12
12
  module API
13
13
  def new(options = {})
14
- Cache.instance.api(Logic::Options[:normalize].(options))
14
+ DSL::Cache.instance.api(Logic::Options[:normalize].(options))
15
15
  end
16
16
 
17
17
  module_function :new
@@ -21,9 +21,9 @@ module SweetMoon
21
21
  def new(options = {})
22
22
  options = Logic::Options[:normalize].(options)
23
23
 
24
- api = Cache.instance.api_module(options)
24
+ api = DSL::Cache.instance.api_module(options)
25
25
 
26
- interpreter = Cache.instance.interpreter_module(api, options)
26
+ interpreter = DSL::Cache.instance.interpreter_module(api, options)
27
27
 
28
28
  DSL::State.new(api, interpreter, Controller::State, options)
29
29
  end
@@ -46,7 +46,7 @@ module SweetMoon
46
46
  end
47
47
 
48
48
  def global
49
- Global
49
+ DSL::Global
50
50
  end
51
51
 
52
52
  module_function :meta, :global
@@ -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.6',
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.6
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-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -48,6 +48,7 @@ files:
48
48
  - LICENSE
49
49
  - README.md
50
50
  - components/api.rb
51
+ - components/default.rb
51
52
  - components/injections.rb
52
53
  - components/injections/injections_503.rb
53
54
  - components/injections/injections_514.rb