Class: Peruby::ModuleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/peruby/runtime/module_loader.rb

Overview

Reentrant require/use loader with user @INC taking precedence.

Constant Summary collapse

INTERNAL_MODULES =
%w[
  strict warnings utf8 feature integer bytes vars lib parent base Exporter constant Carp Scalar::Util List::Util
  POSIX Data::Dumper Test::Builder Test::More Test::Simple File::Basename File::Spec File::Path File::Temp Cwd
  Getopt::Long JSON::PP Encode Time::HiRes Digest::MD5 Digest::SHA Storable Fcntl Errno overload mro
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime) ⇒ ModuleLoader

Returns a new instance of ModuleLoader.



55
56
57
58
# File 'lib/peruby/runtime/module_loader.rb', line 55

def initialize(runtime)
  @runtime = runtime
  @loading = []
end

Class Attribute Details

.modulesObject (readonly)

Returns the value of attribute modules.



39
40
41
# File 'lib/peruby/runtime/module_loader.rb', line 39

def modules
  @modules
end

Class Method Details

.define(name, &block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/peruby/runtime/module_loader.rb', line 41

def define(name, &block)
  return @modules[name] = block unless block.arity.zero?

  definition = ModuleDefinition.new
  definition.instance_eval(&block)
  @modules[name] = definition
end

.prototype(module_name, sub_name) ⇒ Object



49
50
51
52
# File 'lib/peruby/runtime/module_loader.rb', line 49

def prototype(module_name, sub_name)
  implementation = @modules[module_name]
  implementation.functions.dig(sub_name, 0) if implementation.is_a?(ModuleDefinition)
end

Instance Method Details

#require_module(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/peruby/runtime/module_loader.rb', line 60

def require_module(name)
  key = filename(name)
  return 1 if @runtime.stash.glob('INC').hash.exists?(key)
  raise PerlError, "Recursive require of #{key}" if @loading.include?(key)

  @loading << key
  kind, content = find(key)
  source = if kind == :path
             load_file(content, key)
           elsif kind == :source
             load_source(content, key)
           else
             load_internal(name, key)
           end
  @runtime.stash.glob('INC').hash.slot(key).set(source)
  1
ensure
  @loading.delete(key) if key
end