1#!/usr/bin/env ruby 2 3# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16require 'yaml' 17require 'json' 18 19module Gen; end 20 21class Module 22 def cached(method_name) 23 definer = instance_methods.include?(method_name) ? :define_method : :define_singleton_method 24 noncached_method = instance_method(method_name) 25 send(definer, method_name) do 26 unless instance_variable_defined? "@#{method_name}" 27 instance_variable_set("@#{method_name}", noncached_method.bind(self).call) 28 end 29 instance_variable_get("@#{method_name}").freeze 30 end 31 end 32end 33 34class ISA 35 def self.setup(isa_filename, isapi_filename) 36 isa = YAML.load_file(File.expand_path(isa_filename)) 37 isa = JSON.parse(isa.to_json, object_class: OpenStruct).freeze 38 require isapi_filename 39 Gen.on_require(isa) 40 end 41end 42