im 0.1.1 → 0.1.3

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: 844192c05246f7a6fdf91f2999e1af62ffa0124fafc26666b0215bebbb276645
4
- data.tar.gz: a077e568115fc13153de58c087288c465c1f7be1851499f511c2a5372e83d5ca
3
+ metadata.gz: 3f60f56fa26101e6aeec8d3a9123f1ce8196a60ff559931ec91418316cef13e2
4
+ data.tar.gz: 445bf0961717077dd35e5ab900b1ab235b25a834a0b1123ed6936c2770439872
5
5
  SHA512:
6
- metadata.gz: a6769bba616d834f2869366bdd072120cacf0e596e59a390433dd3b8380ab14e501e1e88254ff11f281e8a7df7e5a48706712669d01fef711ed3832c84ad6af8
7
- data.tar.gz: 8857482df7b59722dae014fa5f5b789e16f5407dae62643907cd9578329fcc4c83bc0cdca817c74d712479401562ad615c84599c2a987e5c97f981c0a1c17730
6
+ metadata.gz: d0c2063a12bc66f6e86565a27d1e58e2079a88466a61c42a9830fbb0705864ee33f1c780f367dbe61e55b1e21a741cd6c05fc2e4cb7c50ee2bca1625096898c1
7
+ data.tar.gz: 4ba1c608ba252e26625ddd136fcaddb3ac8b9f6a5f79db80796ce82ebd8b96da1560b946c79d31a8e658c1fbe5e727d6e68c793dd6a8944e6ceb60b62dd01182
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.2] - 2022-09-16
4
+ - Fix gemspec
5
+
6
+ ## [0.1.1] - 2022-09-16
7
+
8
+ - Fix TracePoint event
9
+ - Simplify import inspect
10
+ - Remove unnecessary Kernel#autoload patch
11
+ - Encode comprehensive list of top-level constants
12
+
13
+ ## [0.1.0] - 2022-09-15
14
+
15
+ - Initial release, requires patched version of Ruby.
16
+
17
+ ## [0.0.1] - 2022-07-13
18
+
19
+ - Stub
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in im.gemspec
6
+ gemspec
7
+
8
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ im (0.1.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (13.0.6)
11
+ rake-compiler (1.2.0)
12
+ rake
13
+ rspec (3.11.0)
14
+ rspec-core (~> 3.11.0)
15
+ rspec-expectations (~> 3.11.0)
16
+ rspec-mocks (~> 3.11.0)
17
+ rspec-core (3.11.0)
18
+ rspec-support (~> 3.11.0)
19
+ rspec-expectations (3.11.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.11.0)
22
+ rspec-mocks (3.11.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.11.0)
25
+ rspec-support (3.11.0)
26
+
27
+ PLATFORMS
28
+ x86_64-linux
29
+
30
+ DEPENDENCIES
31
+ im!
32
+ rake (~> 13.0)
33
+ rake-compiler
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 2.4.0.dev
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Chris Salzberg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Im
2
+
3
+ Im is a (currently) experimental implementation of import modules in Ruby,
4
+ similar to systems like CommonJS, ESmodules, and RequireJS in Javascript. It
5
+ requires a patched version of Ruby which you will need to build in order to use
6
+ the gem.
7
+
8
+ ## Installation
9
+
10
+ Im requires a patched version of Ruby to use; if not installed, the gem will raise and exit.
11
+
12
+ You can find the patched version of Ruby here:
13
+
14
+ https://github.com/shioyama/ruby/tree/import_modules
15
+
16
+ Once installed, install the gem and the error should go away.
17
+
18
+ ## Usage
19
+
20
+ Extend `Im` and use `import` to import files or gems under an anonymous module namespace:
21
+
22
+ ```ruby
23
+ require "im"
24
+
25
+ extend Im
26
+
27
+ mod = import "activemodel"
28
+ #=> #<Im::Import:0x00007f2d34dfd0c8 root: active_model>
29
+ ```
30
+
31
+ Constants in the imported files are under the returned module, not the root namespace:
32
+
33
+ ```ruby
34
+ ActiveModel
35
+ #=> uninitialized constant ActiveModel (NameError)
36
+
37
+ mod::ActiveModel
38
+ #=> ActiveModel
39
+ ```
40
+
41
+ You can now assign your custom parent namespace and use the gem constants as always:
42
+
43
+ ```ruby
44
+ MyRails = mod
45
+
46
+ class EmailContact
47
+ include MyRails::ActiveModel::API
48
+
49
+ attr_accessor :name
50
+ validates :name, presence: true
51
+ end
52
+
53
+ contact = EmailContact.new
54
+ contact.valid?
55
+ #=> false
56
+
57
+ contact.name = "foo"
58
+ contact.valid?
59
+ #=> true
60
+ ```
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Im
4
+ module Autoload
5
+ def autoload(name, path)
6
+ Im.autoloads[path] = Im.current_import if Im.current_import
7
+ super(name, path)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,103 @@
1
+ module Im
2
+ # It's faster to just hard-code constants than to introspect them at runtime
3
+ CONSTANTS = [
4
+ :ArgumentError,
5
+ :Array,
6
+ :BasicObject,
7
+ :Binding,
8
+ :Class,
9
+ :ClosedQueueError,
10
+ :Comparable,
11
+ :Complex,
12
+ :ConditionVariable,
13
+ :DidYouMean,
14
+ :Dir,
15
+ :Encoding,
16
+ :EncodingError,
17
+ :Enumerable,
18
+ :Enumerator,
19
+ :EOFError,
20
+ :Errno,
21
+ :ErrorHighlight,
22
+ :Exception,
23
+ :FalseClass,
24
+ :Fiber,
25
+ :FiberError,
26
+ :File,
27
+ :FileTest,
28
+ :Float,
29
+ :FloatDomainError,
30
+ :FrozenError,
31
+ :GC,
32
+ :Gem,
33
+ :Hash,
34
+ :IndexError,
35
+ :Integer,
36
+ :Interrupt,
37
+ :IO,
38
+ :IOError,
39
+ :Kernel,
40
+ :KeyError,
41
+ :LoadError,
42
+ :LocalJumpError,
43
+ :Marshal,
44
+ :MatchData,
45
+ :Math,
46
+ :Method,
47
+ :Module,
48
+ :Monitor,
49
+ :MonitorMixin,
50
+ :Mutex,
51
+ :NameError,
52
+ :NilClass,
53
+ :NoMatchingPatternError,
54
+ :NoMatchingPatternKeyError,
55
+ :NoMemoryError,
56
+ :NoMethodError,
57
+ :NotImplementedError,
58
+ :Numeric,
59
+ :Object,
60
+ :ObjectSpace,
61
+ :Proc,
62
+ :Process,
63
+ :Queue,
64
+ :Ractor,
65
+ :Random,
66
+ :Range,
67
+ :RangeError,
68
+ :Rational,
69
+ :RbConfig,
70
+ :Refinement,
71
+ :Regexp,
72
+ :RegexpError,
73
+ :RubyVM,
74
+ :RuntimeError,
75
+ :ScriptError,
76
+ :SecurityError,
77
+ :Set,
78
+ :Signal,
79
+ :SignalException,
80
+ :SizedQueue,
81
+ :StandardError,
82
+ :StopIteration,
83
+ :String,
84
+ :Struct,
85
+ :Symbol,
86
+ :SyntaxError,
87
+ :SystemCallError,
88
+ :SystemExit,
89
+ :SystemStackError,
90
+ :Thread,
91
+ :ThreadError,
92
+ :ThreadGroup,
93
+ :Time,
94
+ :TracePoint,
95
+ :TrueClass,
96
+ :TypeError,
97
+ :UnboundMethod,
98
+ :UncaughtThrowError,
99
+ :UnicodeNormalize,
100
+ :Warning,
101
+ :ZeroDivisionError,
102
+ ]
103
+ end
data/lib/im/kernel.rb ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kernel
4
+ alias_method :im_original_require, :require
5
+
6
+ def require(path)
7
+ if Im.autoloads.key?(path)
8
+ return Im.with_context(Im.autoloads.delete(path)) do |_context|
9
+ !!Im.import(path)
10
+ end
11
+ end
12
+
13
+ loaded = im_original_require(path)
14
+ return loaded unless Im.current_import
15
+
16
+ if resolved = $LOAD_PATH.resolve_feature_path(path)
17
+ if Im.registry.key?(resolved_path = resolved[1])
18
+ _, modules = Im.registry[resolved_path]
19
+
20
+ modules.each do |m|
21
+ next unless name = m.name
22
+
23
+ # Do not assign constants that are aliased to root namespace
24
+ root = name.split("::", 2)[0]
25
+ next if Object.const_defined?(root) &&
26
+ Im.current_import.const_defined?(root, false) &&
27
+ Object.const_get(root) == Im.current_import.const_get(root)
28
+
29
+ Im.current_import.const_set(name, m) unless Im.current_import.const_defined?(name, false)
30
+ end
31
+ else
32
+ Im.registry[resolved_path] ||= [Im.current_import, []] if Im.current_import
33
+ end
34
+ end
35
+
36
+ return loaded
37
+ end
38
+ end
data/lib/im/module.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Module
2
+ alias_method :im_original_autoload, :autoload
3
+
4
+ def autoload(name, path)
5
+ Im.autoloads[path] = Im.current_import if Im.current_import
6
+ im_original_autoload(name, path)
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ RubyVersionCheck = Module.new
2
+
3
+ unless RubyVersionCheck.name == "RubyVersionCheck"
4
+ raise RuntimeError, <<~MSG
5
+
6
+
7
+ \e[33m!!! RUNNING INCOMPATIBLE VERSION OF RUBY !!!\e[0m
8
+
9
+ In order to implement module imports, Im depends on features not currently
10
+ available in Ruby. To use it, you must build a custom version of Ruby,
11
+ currently available at:
12
+
13
+ https://github.com/shioyama/ruby/tree/import_modules
14
+
15
+ Install this Ruby following the instructions here:
16
+
17
+ https://docs.ruby-lang.org/en/master/contributing/building_ruby_md.html
18
+
19
+ Once installed, ensure you are using this patched version and then gem install Im,
20
+ and this message should no longer appear.
21
+ MSG
22
+ end
data/lib/im/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Im
4
+ VERSION = "0.1.3"
5
+ end
data/lib/im.rb ADDED
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Im
4
+ load $LOAD_PATH.resolve_feature_path("im/ruby_version_check")[1], true
5
+ require "im/version"
6
+ require "im/kernel"
7
+
8
+ class << self
9
+ attr_reader :current_import
10
+ attr_reader :tracer
11
+
12
+ def with_context(context)
13
+ original_context = current_import
14
+ @current_import = context
15
+ yield(context)
16
+ ensure
17
+ @current_import = original_context
18
+ end
19
+
20
+ def registry
21
+ @registry ||= {}
22
+ end
23
+
24
+ def autoloads
25
+ @autoloads ||= {}
26
+ end
27
+ end
28
+
29
+ class Import < Module
30
+ def initialize(root)
31
+ @root = root
32
+ super()
33
+ Im::CONSTANTS.each do |const|
34
+ self.const_set(const, Object.const_get(const))
35
+ end
36
+ end
37
+
38
+ def inspect
39
+ @inspect ||= "<#Im::Import root: #{@root}>"
40
+ end
41
+ end
42
+
43
+ def import(path)
44
+ if resolved = $LOAD_PATH.resolve_feature_path(path)
45
+ resolved_path = resolved[1]
46
+ raise LoadError, "import can only import ruby files" unless resolved[0] == :rb
47
+ else
48
+ raise LoadError, "cannot load such file -- #{path}"
49
+ end
50
+
51
+ Im.registry.fetch(resolved_path) do
52
+ # handle autoload within import
53
+ return nil if $LOADED_FEATURES.include?(resolved_path)
54
+
55
+ $LOADED_FEATURES << resolved_path
56
+
57
+ context = Im.current_import || Import.new(path)
58
+
59
+ Im.with_context(context) do
60
+ originally_enabled = Im.tracer.enabled?
61
+ Im.tracer.enable
62
+ load resolved_path, context
63
+ Im.tracer.disable unless originally_enabled
64
+ end
65
+
66
+ Im.registry[resolved_path] ||= [context, []]
67
+ end[0]
68
+ end
69
+
70
+ module RootNamespaceRedirect
71
+ def const_missing(name)
72
+ if Im.current_import && Im.current_import.const_defined?(name)
73
+ Im.current_import.const_get(name)
74
+ else
75
+ super
76
+ end
77
+ end
78
+ end
79
+
80
+ @tracer = TracePoint.new(:class) do |event|
81
+ if resolved = $LOAD_PATH.resolve_feature_path(event.path)
82
+ resolved_path = resolved[1]
83
+ if Im.registry.key?(resolved_path)
84
+ (Im.registry[resolved_path][1] << event.self).uniq!
85
+ else
86
+ Im.registry[resolved_path] = [Im.current_import, [event.self]]
87
+ end
88
+ end
89
+ end
90
+
91
+ require "im/module"
92
+ require "im/autoload"
93
+ require "im/constants"
94
+
95
+ extend self
96
+ end
97
+
98
+ Object.extend Im::RootNamespaceRedirect
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: im
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2022-09-16 00:00:00.000000000 Z
12
12
  dependencies:
@@ -58,14 +58,29 @@ email:
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
- files: []
61
+ files:
62
+ - CHANGELOG.md
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/im.rb
69
+ - lib/im/autoload.rb
70
+ - lib/im/constants.rb
71
+ - lib/im/kernel.rb
72
+ - lib/im/module.rb
73
+ - lib/im/ruby_version_check.rb
74
+ - lib/im/version.rb
62
75
  homepage: https://github.com/shioyama/im
63
76
  licenses:
64
77
  - MIT
65
78
  metadata:
66
79
  homepage_uri: https://github.com/shioyama/im
67
80
  source_code_uri: https://github.com/shioyama/im
81
+ bug_tracker_uri: https://github.com/shioyama/im/issues
68
82
  changelog_uri: https://github.com/shioyama/im/CHANGELOG.md
83
+ rubygems_mfa_required: 'true'
69
84
  post_install_message:
70
85
  rdoc_options: []
71
86
  require_paths: