class Puppet::Pops::Loader::RubyLegacyFunctionInstantiator
Constants
- UNKNOWN
Public Class Methods
Produces an instance of the Function class with the given typed_name, or fails with an error if the given ruby source does not produce this instance when evaluated.
@param loader [Puppet::Pops::Loader::Loader] The loader the function is associated with @param typed_name [Puppet::Pops::Loader::TypedName] the type / name of the function to load @param source_ref [URI, String] a reference to the source / origin of the ruby code to evaluate @param ruby_code_string [String] ruby code in a string
@return [Puppet::Pops::Functions.Function] - an instantiated function with global scope closure associated with the given loader
# File lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb 18 def self.create(loader, typed_name, source_ref, ruby_code_string) 19 # Assert content of 3x function by parsing 20 assertion_result = [] 21 if assert_code(ruby_code_string, assertion_result) 22 unless ruby_code_string.is_a?(String) && assertion_result.include?(:found_newfunction) 23 raise ArgumentError, _("The code loaded from %{source_ref} does not seem to be a Puppet 3x API function - no 'newfunction' call.") % { source_ref: source_ref } 24 end 25 end 26 27 # make the private loader available in a binding to allow it to be passed on 28 loader_for_function = loader.private_loader 29 here = get_binding(loader_for_function) 30 31 # Avoid reloading the function if already loaded via one of the APIs that trigger 3x function loading 32 # Check if function is already loaded the 3x way (and obviously not the 4x way since we would not be here in the 33 # first place. 34 environment = Puppet.lookup(:current_environment) 35 func_info = Puppet::Parser::Functions.environment_module(environment).get_function_info(typed_name.name.to_sym) 36 if func_info.nil? 37 # This will do the 3x loading and define the "function_<name>" and "real_function_<name>" methods 38 # in the anonymous module used to hold function definitions. 39 # 40 func_info = eval(ruby_code_string, here, source_ref, 1) 41 42 # Validate what was loaded 43 unless func_info.is_a?(Hash) 44 # TRANSLATORS - the word 'newfunction' should not be translated as it is a method name. 45 raise ArgumentError, _("Illegal legacy function definition! The code loaded from %{source_ref} did not return the result of calling 'newfunction'. Got '%{klass}'") % { source_ref: source_ref, klass: func_info.class } 46 end 47 unless func_info[:name] == "function_#{typed_name.name()}" 48 raise ArgumentError, _("The code loaded from %{source_ref} produced mis-matched name, expected 'function_%{type_name}', got '%{created_name}'") % { 49 source_ref: source_ref, type_name: typed_name.name, created_name: func_info[:name] } 50 end 51 end 52 53 created = Puppet::Functions::Function3x.create_function(typed_name.name(), func_info, loader_for_function) 54 55 # create the function instance - it needs closure (scope), and loader (i.e. where it should start searching for things 56 # when calling functions etc. 57 # It should be bound to global scope 58 59 # Sets closure scope to nil, to let it be picked up at runtime from Puppet.lookup(:global_scope) 60 # If function definition used the loader from the binding to create a new loader, that loader wins 61 created.new(nil, loader_for_function) 62 end
Private Class Methods
# File lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb 72 def self.assert_code(code_string, result) 73 ripped = Ripper.sexp(code_string) 74 return false if ripped.nil? # Let the next real parse crash and tell where and what is wrong 75 ripped.each {|x| walk(x, result) } 76 true 77 end
Extracts the method name and line number from the Ripper Rast for an id entry. The expected input (a result from Ripper :@ident entry) is an array with:
Returns an Array; a tuple with method name and line number or “<unknown>” if either is missing, or format is not the expected
# File lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb 115 def self.extract_name_line(x) 116 (x.is_a?(Array) ? [ x[1], x[2].is_a?(Array) ? x[2][1] : nil] : [nil, nil]).map {|v| v.nil? ? UNKNOWN : v } 117 end
# File lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb 102 def self.find_identity(rast) 103 rast.find{|x| x.is_a?(Array) && x[0] == :@ident } 104 end
Produces a binding where the given loader is bound as a local variable (loader_injected_arg). This variable can be used in loaded ruby code - e.g. to call Puppet::Function.create_loaded_function(:name, loader,…)
# File lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb 67 def self.get_binding(loader_injected_arg) 68 binding 69 end
# File lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb 80 def self.walk(x, result) 81 return unless x.is_a?(Array) 82 first = x[0] 83 case first 84 when :fcall, :call 85 # Ripper returns a :fcall for a function call in a module (want to know there is a call to newfunction()). 86 # And it returns :call for a qualified named call 87 identity_part = find_identity(x) 88 result << :found_newfunction if identity_part.is_a?(Array) && identity_part[1] == 'newfunction' 89 when :def, :defs 90 # There should not be any calls to def in a 3x function 91 mname, mline = extract_name_line(find_identity(x)) 92 raise SecurityError, _("Illegal method definition of method '%{method_name}' on line %{line} in legacy function. See %{url} for more information") % { 93 method_name: mname, 94 line: mline, 95 url: "https://puppet.com/docs/puppet/latest/functions_refactor_legacy.html" 96 } 97 end 98 x.each {|v| walk(v, result) } 99 end