• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env ruby
2# Copyright (c) 2021 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15require 'yaml'
16
17class String
18  def snakecase
19    self.gsub(/::/, '/').
20    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
21    gsub(/([a-z\d])([A-Z])/,'\1_\2').
22    tr("-", "_").
23    downcase
24  end
25end
26
27class Entrypoint
28  def initialize(dscr)
29    @dscr = dscr
30  end
31
32  def name
33    @dscr['name']
34  end
35
36  def enum_name
37    @dscr['name'].snakecase.upcase
38  end
39
40  def bridge_name
41    @dscr.entrypoint.nil? ? "#{name}Bridge" : @dscr.entrypoint
42  end
43
44  def entrypoint_name
45    @dscr.entrypoint.nil? ? "#{name}Entrypoint" : @dscr.entrypoint
46  end
47
48  def get_entry
49    "#{name}Entrypoint"
50  end
51
52  def signature
53    @dscr['signature']
54  end
55
56  def external?
57    has_property? 'external'
58  end
59
60  def has_property? prop
61    @dscr['properties']&.include? prop
62  end
63
64end
65
66module Compiler
67  module_function
68
69  def entrypoints
70    @entrypoints ||= @data['entrypoints'].map {|x| Entrypoint.new x }
71  end
72
73  def entrypoints_crc32
74    require "zlib"
75    Zlib.crc32(entrypoints.map(&:signature).join)
76  end
77
78  def wrap_data(data)
79    @data = data
80  end
81end
82
83def Gen.on_require(data)
84  Compiler.wrap_data(data)
85end
86