class Puppet::Pops::Loader::PuppetFunctionInstantiator
The PuppetFunctionInstantiator
instantiates a Puppet::Functions::PuppetFunction
given a Puppet
Programming language source that when called evaluates the Puppet
logic it contains.
Public Class Methods
Produces an instance of the Function class with the given typed_name, or fails with an error if the given puppet source does not produce this instance when evaluated.
@param loader [Loader] The loader the function is associated with @param typed_name [TypedName] the type / name of the function to load @param source_ref [URI, String] a reference to the source / origin of the puppet code to evaluate @param pp_code_string [String] puppet code in a string
@return [Functions::Function] - an instantiated function with global scope closure associated with the given loader
# File lib/puppet/pops/loader/puppet_function_instantiator.rb 17 def self.create(loader, typed_name, source_ref, pp_code_string) 18 parser = Parser::EvaluatingParser.new() 19 20 # parse and validate 21 result = parser.parse_string(pp_code_string, source_ref) 22 # Only one function is allowed (and no other definitions) 23 case result.definitions.size 24 when 0 25 raise ArgumentError, _("The code loaded from %{source_ref} does not define the function '%{func_name}' - it is empty.") % { source_ref: source_ref, func_name: typed_name.name } 26 when 1 27 # ok 28 else 29 raise ArgumentError, _("The code loaded from %{source_ref} must contain only the function '%{type_name}' - it has additional definitions.") % { source_ref: source_ref, type_name: typed_name.name } 30 end 31 the_function_definition = result.definitions[0] 32 33 unless the_function_definition.is_a?(Model::FunctionDefinition) 34 raise ArgumentError, _("The code loaded from %{source_ref} does not define the function '%{type_name}' - no function found.") % { source_ref: source_ref, type_name: typed_name.name } 35 end 36 unless the_function_definition.name == typed_name.name 37 expected = typed_name.name 38 actual = the_function_definition.name 39 raise ArgumentError, _("The code loaded from %{source_ref} produced function with the wrong name, expected %{expected}, actual %{actual}") % { source_ref: source_ref, expected: expected, actual: actual } 40 end 41 unless result.body == the_function_definition 42 raise ArgumentError, _("The code loaded from %{source} contains additional logic - can only contain the function %{name}") % { source: source_ref, name: typed_name.name } 43 end 44 45 # Adapt the function definition with loader - this is used from logic contained in it body to find the 46 # loader to use when making calls to the new function API. Such logic have a hard time finding the closure (where 47 # the loader is known - hence this mechanism 48 private_loader = loader.private_loader 49 Adapters::LoaderAdapter.adapt(the_function_definition).loader_name = private_loader.loader_name 50 51 # Cannot bind loaded functions to global scope, that must be done without binding that scope as 52 # loaders survive a compilation. 53 closure_scope = nil # Puppet.lookup(:global_scope) { {} } 54 55 created = create_function_class(the_function_definition) 56 # create the function instance - it needs closure (scope), and loader (i.e. where it should start searching for things 57 # when calling functions etc. 58 # It should be bound to global scope 59 60 created.new(closure_scope, private_loader) 61 end
Creates Function class and instantiates it based on a FunctionDefinition model @return [Array<TypedName, Functions
.Function>] - array of
typed name, and an instantiated function with global scope closure associated with the given loader
# File lib/puppet/pops/loader/puppet_function_instantiator.rb 67 def self.create_from_model(function_definition, loader) 68 created = create_function_class(function_definition) 69 typed_name = TypedName.new(:function, function_definition.name) 70 [typed_name, created.new(nil, loader)] 71 end
# File lib/puppet/pops/loader/puppet_function_instantiator.rb 73 def self.create_function_class(function_definition) 74 # Create a 4x function wrapper around a named closure 75 Puppet::Functions.create_function(function_definition.name, Puppet::Functions::PuppetFunction) do 76 # TODO: should not create a new evaluator per function 77 init_dispatch(Evaluator::Closure::Named.new( 78 function_definition.name, 79 Evaluator::EvaluatorImpl.new(), function_definition)) 80 end 81 end