wrapture 0.2.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,33 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+
1
3
  # frozen_string_literal: true
2
4
 
5
+ #--
6
+ # Copyright 2019-2020 Joel E. Anderson
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #++
20
+
3
21
  module Wrapture
4
- VERSION = '0.2.2'
22
+ # the current version of Wrapture
23
+ VERSION = '0.5.0'
24
+
25
+ # Returns true if the version of the spec is supported by this version of
26
+ # Wrapture. Otherwise returns false.
27
+ def self.supports_version?(version)
28
+ wrapture_version = Gem::Version.new(Wrapture::VERSION)
29
+ spec_version = Gem::Version.new(version)
30
+
31
+ spec_version <= wrapture_version
32
+ end
5
33
  end
@@ -0,0 +1,134 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+
3
+ # frozen_string_literal: true
4
+
5
+ #--
6
+ # Copyright 2019-2020 Joel E. Anderson
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #++
20
+
21
+ module Wrapture
22
+ # A description of a function to be wrapped by another language.
23
+ class WrappedFunctionSpec
24
+ # Normalizes a hash specification of a wrapped function. Normalization will
25
+ # check for things like invalid keys, duplicate entries in include lists,
26
+ # and will set missing keys to their default values (for example, an empty
27
+ # list if no includes are given).
28
+ def self.normalize_spec_hash(spec)
29
+ normalized = spec.dup
30
+
31
+ normalized['params'] ||= []
32
+ normalized['params'].each do |param_spec|
33
+ param_spec['value'] = param_spec['name'] if param_spec['value'].nil?
34
+ end
35
+
36
+ normalized['includes'] = Wrapture.normalize_includes(spec['includes'])
37
+
38
+ normalized['error-check'] ||= {}
39
+ normalized['error-check']['rules'] ||= []
40
+
41
+ unless spec.key?('return')
42
+ normalized['return'] = {}
43
+ normalized['return']['type'] = 'void'
44
+ end
45
+
46
+ normalized
47
+ end
48
+
49
+ # Creates a wrapped function spec based on the provided spec.
50
+ #
51
+ # The hash must have the following keys:
52
+ # name:: the name of the wrapped function
53
+ # params:: a list of parameters to supply when calling
54
+ #
55
+ # Each member of the params list must be a hash, with a mandatory key of
56
+ # 'value' holding the value to be supplied as the parameter. If only a
57
+ # 'name' key is provided, this will be used as the value. A 'type' may be
58
+ # supplied as well, and is necessary if an equivalent struct or pointer is
59
+ # to be supplied as the value so that casting can be performed correctly.
60
+ #
61
+ # The following key is optional:
62
+ # includes:: a list of includes needed for this function
63
+ def initialize(spec)
64
+ @spec = self.class.normalize_spec_hash(spec)
65
+
66
+ check = @spec['error-check']
67
+
68
+ @error_rules = check['rules'].map do |rule_spec|
69
+ RuleSpec.new(rule_spec)
70
+ end
71
+
72
+ action = check['error-action']
73
+ @error_action = ActionSpec.new(action) unless @error_rules.empty?
74
+ end
75
+
76
+ # Generates a function call from a provided FunctionSpec. Paremeters and
77
+ # types are resolved using this function's context.
78
+ def call_from(function_spec)
79
+ resolved_params = []
80
+
81
+ @spec['params'].each do |param|
82
+ resolved_params << function_spec.resolve_wrapped_param(param)
83
+ end
84
+
85
+ "#{@spec['name']}( #{resolved_params.join(', ')} )"
86
+ end
87
+
88
+ # Yields each line of the error check and any actions taken for this wrapped
89
+ # function. If this function does not have any error check defined, then
90
+ # this function returns without yielding anything.
91
+ #
92
+ # +return_val+ is used as the replacement for a return value signified by
93
+ # the use of RETURN_VALUE_KEYWORD in the spec. If not specified it defaults
94
+ # to +'return_val'+. This parameter was added in release 0.4.2.
95
+ def error_check(return_val: 'return_val')
96
+ return if @error_rules.empty?
97
+
98
+ checks = @error_rules.map { |rule| rule.check(return_val: return_val) }
99
+ yield "if( #{checks.join(' && ')} ){"
100
+ yield " #{@error_action.take};"
101
+ yield '}'
102
+ end
103
+
104
+ # True if the wrapped function has an error check associated with it.
105
+ def error_check?
106
+ !@error_rules.empty?
107
+ end
108
+
109
+ # A list of includes required for this function call.
110
+ def includes
111
+ includes = @spec['includes'].dup
112
+
113
+ includes.concat(@error_action.includes) if error_check?
114
+
115
+ includes
116
+ end
117
+
118
+ # A TypeSpec describing the type of the return value.
119
+ #
120
+ # Changed in release 0.4.2 to return a TypeSpec instead of a String.
121
+ def return_val_type
122
+ TypeSpec.new(@spec['return']['type'])
123
+ end
124
+
125
+ # True if calling this wrapped function needs to save/use the return value
126
+ # for error checking. This is equivalent to checking all error rules for the
127
+ # use of RETURN_VALUE_KEYWORD.
128
+ #
129
+ # This method was added in release 0.4.2.
130
+ def use_return?
131
+ @error_rules.any?(&:use_return?)
132
+ end
133
+ end
134
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrapture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-01 00:00:00.000000000 Z
11
+ date: 2020-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -17,13 +17,19 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.6.4
20
- type: :runtime
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.3'
23
+ type: :development
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: 1.6.4
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.3'
27
33
  description: Wraps C code in C++.
28
34
  email: joelanderson333@gmail.com
29
35
  executables:
@@ -33,16 +39,30 @@ extra_rdoc_files: []
33
39
  files:
34
40
  - bin/wrapture
35
41
  - lib/wrapture.rb
42
+ - lib/wrapture/action_spec.rb
36
43
  - lib/wrapture/class_spec.rb
44
+ - lib/wrapture/comment.rb
37
45
  - lib/wrapture/constant_spec.rb
46
+ - lib/wrapture/constants.rb
47
+ - lib/wrapture/enum_spec.rb
48
+ - lib/wrapture/errors.rb
38
49
  - lib/wrapture/function_spec.rb
50
+ - lib/wrapture/normalize.rb
51
+ - lib/wrapture/param_spec.rb
52
+ - lib/wrapture/rule_spec.rb
53
+ - lib/wrapture/scope.rb
54
+ - lib/wrapture/struct_spec.rb
55
+ - lib/wrapture/template_spec.rb
56
+ - lib/wrapture/type_spec.rb
39
57
  - lib/wrapture/version.rb
40
- homepage: http://rubygems.org/gems/wrapture
58
+ - lib/wrapture/wrapped_function_spec.rb
59
+ homepage: https://goatshriek.github.io/wrapture/
41
60
  licenses:
42
61
  - Apache-2.0
43
62
  metadata:
44
63
  bug_tracker_uri: https://github.com/goatshriek/wrapture/issues
45
- changelog_uri: https://github.com/goatshriek/wrapture/blob/master/ChangeLog.md
64
+ changelog_uri: https://github.com/goatshriek/wrapture/blob/latest/ChangeLog.md
65
+ documentation_uri: https://goatshriek.github.io/wrapture/rdoc/
46
66
  source_code_uri: https://github.com/goatshriek/wrapture/
47
67
  post_install_message:
48
68
  rdoc_options: []
@@ -52,14 +72,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
72
  requirements:
53
73
  - - ">="
54
74
  - !ruby/object:Gem::Version
55
- version: '2.3'
75
+ version: '2.4'
56
76
  required_rubygems_version: !ruby/object:Gem::Requirement
57
77
  requirements:
58
78
  - - ">="
59
79
  - !ruby/object:Gem::Version
60
80
  version: '0'
61
81
  requirements: []
62
- rubygems_version: 3.0.3
82
+ rubygems_version: 3.2.1
63
83
  signing_key:
64
84
  specification_version: 4
65
85
  summary: wrap C in C++