Class: Peruby::ModuleDefinition

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

Overview

Definition collected by Peruby.define_module's small Ruby DSL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModuleDefinition

Returns a new instance of ModuleDefinition.



9
10
11
12
# File 'lib/peruby/runtime/module_loader.rb', line 9

def initialize
  @exports = []
  @functions = {}
end

Instance Attribute Details

#exportsObject (readonly)

Returns the value of attribute exports.



7
8
9
# File 'lib/peruby/runtime/module_loader.rb', line 7

def exports
  @exports
end

#functionsObject (readonly)

Returns the value of attribute functions.



7
8
9
# File 'lib/peruby/runtime/module_loader.rb', line 7

def functions
  @functions
end

Instance Method Details

#export_ok(*names) ⇒ Object



14
# File 'lib/peruby/runtime/module_loader.rb', line 14

def export_ok(*names) = @exports.concat(names.map(&:to_s))

#install(name, runtime) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/peruby/runtime/module_loader.rb', line 20

def install(name, runtime)
  @functions.each do |sub_name, (prototype, implementation)|
    code = Code.new("#{name}::#{sub_name}", NativeBody.new(&implementation), runtime.env, name, prototype)
    runtime.stash.glob(code.name).code = code
  end
  runtime.stash.touch
end

#sub(name, proto: nil, &implementation) ⇒ Object



16
17
18
# File 'lib/peruby/runtime/module_loader.rb', line 16

def sub(name, proto: nil, &implementation)
  @functions[name.to_s] = [proto, implementation]
end