koffi 2.6.10 → 2.6.12

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.
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ # this software and associated documentation files (the “Software”), to deal in
7
+ # the Software without restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
9
+ # Software, and to permit persons to whom the Software is furnished to do so,
10
+ # subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ # This script uses the database of mimetypes distributed here: https://github.com/jshttp/mime-db
25
+ # to produce the X-header file mimetypes.inc
26
+
27
+ import os
28
+ import argparse
29
+ import json
30
+
31
+ if __name__ == "__main__":
32
+ parser = argparse.ArgumentParser(description = 'Update mimetypes include file')
33
+ parser.add_argument('-O', '--output_file', dest = 'output_file', action = 'store', help = 'Output file')
34
+ parser.add_argument('json', help = 'Source JSON database')
35
+ args = parser.parse_args()
36
+
37
+ if args.output_file is None:
38
+ output_file = os.path.join(os.path.dirname(__file__), 'mimetypes.inc')
39
+ else:
40
+ output_file = args.output_file
41
+
42
+ with open(args.json) as f:
43
+ db = json.load(f)
44
+
45
+ extensions = {}
46
+ for (k, v) in db.items():
47
+ if 'extensions' not in v:
48
+ continue
49
+
50
+ for ext in v['extensions']:
51
+ if not ext in extensions:
52
+ extensions[ext] = 'application/x-'
53
+ if extensions[ext].startswith('application/x-'):
54
+ extensions[ext] = k
55
+
56
+ extensions = [(k, extensions[k]) for k in sorted(extensions.keys())]
57
+ extensions.sort()
58
+
59
+ with open(output_file) as f:
60
+ lines = f.readlines()
61
+ with open(output_file, 'w') as f:
62
+ for line in lines:
63
+ if not line.startswith('//'):
64
+ break
65
+ f.write(line)
66
+
67
+ print('', file = f)
68
+ print('#ifndef MIMETYPE', file = f)
69
+ print(' #error Please define MIMETYPE() before including mimetypes.inc', file = f)
70
+ print('#endif', file = f)
71
+ print('', file = f)
72
+
73
+ for k, v in extensions:
74
+ print(f'MIMETYPE(".{k}", "{v}")', file = f)
75
+
76
+ print('', file = f)
77
+ print('#undef MIMETYPE', file = f)
@@ -1966,7 +1966,7 @@ static Napi::Value EncodeValue(const Napi::CallbackInfo &info)
1966
1966
  Napi::Env env = info.Env();
1967
1967
 
1968
1968
  bool has_offset = (info.Length() >= 2 && info[1].IsNumber());
1969
- bool has_len = (info.Length() >= 4u + has_offset && info[2u + has_offset].IsNumber());
1969
+ bool has_len = (info.Length() >= 4u + has_offset && info[3u + has_offset].IsNumber());
1970
1970
 
1971
1971
  if (info.Length() < 3u + has_offset) [[unlikely]] {
1972
1972
  ThrowError<Napi::TypeError>(env, "Expected %1 to 5 arguments, got %2", 3 + has_offset, info.Length());
@@ -1979,10 +1979,10 @@ static Napi::Value EncodeValue(const Napi::CallbackInfo &info)
1979
1979
 
1980
1980
  Napi::Value ref = info[0];
1981
1981
  int64_t offset = has_offset ? info[1].As<Napi::Number>().Int64Value() : 0;
1982
- Napi::Value value = info[2u + has_offset + has_len];
1982
+ Napi::Value value = info[2u + has_offset];
1983
1983
 
1984
1984
  if (has_len) {
1985
- Size len = info[2u + has_offset].As<Napi::Number>();
1985
+ Size len = info[3u + has_offset].As<Napi::Number>();
1986
1986
 
1987
1987
  if (!Encode(ref, offset, value, type, &len))
1988
1988
  return env.Null();
@@ -117,13 +117,30 @@ const TypeInfo *ResolveType(Napi::Value value, int *out_directions)
117
117
 
118
118
  if (value.IsString()) {
119
119
  std::string str = value.As<Napi::String>();
120
- const TypeInfo *type = ResolveType(env, str.c_str(), out_directions);
121
120
 
122
- if (!type) {
123
- if (!env.IsExceptionPending()) {
124
- ThrowError<Napi::TypeError>(env, "Unknown or invalid type name '%1'", str.c_str());
121
+ // Quick path for known types (int, float *, etc.)
122
+ const TypeInfo *type = instance->types_map.FindValue(str.c_str(), nullptr);
123
+
124
+ if (!type || (type->flags & (int)TypeFlag::IsIncomplete)) {
125
+ type = ResolveType(env, str.c_str(), out_directions);
126
+
127
+ if (!type) {
128
+ if (!env.IsExceptionPending()) {
129
+ ThrowError<Napi::TypeError>(env, "Unknown or invalid type name '%1'", str.c_str());
130
+ }
131
+ return nullptr;
125
132
  }
126
- return nullptr;
133
+
134
+ // Cache for quick future access
135
+ bool inserted;
136
+ auto bucket = instance->types_map.TrySetDefault(str.c_str(), &inserted);
137
+
138
+ if (inserted) {
139
+ bucket->key = DuplicateString(str.c_str(), &instance->str_alloc).ptr;
140
+ bucket->value = type;
141
+ }
142
+ } else if (out_directions) {
143
+ *out_directions = 1;
127
144
  }
128
145
 
129
146
  return type;
@@ -200,48 +217,54 @@ const TypeInfo *ResolveType(Napi::Env env, Span<const char> str, int *out_direct
200
217
  }
201
218
  }
202
219
 
203
- // Skip initial const qualifiers
204
- str = TrimStr(str);
205
- while (SplitIdentifier(str) == "const") {
206
- str = str.Take(6, str.len - 6);
207
- str = TrimStr(str);
208
- }
209
- str = TrimStr(str);
210
-
211
- Span<const char> remain = str;
220
+ Span<const char> name;
221
+ Span<const char> after;
222
+ {
223
+ Span<const char> remain = str;
212
224
 
213
- // Consume one or more identifiers (e.g. unsigned int)
214
- for (;;) {
225
+ // Skip initial const qualifiers
226
+ remain = TrimStr(remain);
227
+ while (SplitIdentifier(remain) == "const") {
228
+ remain = remain.Take(6, remain.len - 6);
229
+ remain = TrimStr(remain);
230
+ }
215
231
  remain = TrimStr(remain);
216
232
 
217
- Span<const char> token = SplitIdentifier(remain);
218
- if (!token.len)
219
- break;
220
- remain = remain.Take(token.len, remain.len - token.len);
221
- }
233
+ after = remain;
222
234
 
223
- Span<const char> name = TrimStr(MakeSpan(str.ptr, remain.ptr - str.ptr));
235
+ // Consume one or more identifiers (e.g. unsigned int)
236
+ for (;;) {
237
+ after = TrimStr(after);
238
+
239
+ Span<const char> token = SplitIdentifier(after);
240
+ if (!token.len)
241
+ break;
242
+ after = after.Take(token.len, after.len - token.len);
243
+ }
244
+
245
+ name = TrimStr(MakeSpan(remain.ptr, after.ptr - remain.ptr));
246
+ }
224
247
 
225
248
  // Consume pointer indirections
226
- while (remain.len) {
227
- if (remain[0] == '*') {
228
- remain = remain.Take(1, remain.len - 1);
249
+ while (after.len) {
250
+ if (after[0] == '*') {
251
+ after = after.Take(1, after.len - 1);
229
252
  indirect++;
230
253
 
231
254
  if (indirect >= RG_SIZE(disposables) * 8) [[unlikely]] {
232
255
  ThrowError<Napi::Error>(env, "Too many pointer indirections");
233
256
  return nullptr;
234
257
  }
235
- } else if (remain[0] == '!') {
236
- remain = remain.Take(1, remain.len - 1);
258
+ } else if (after[0] == '!') {
259
+ after = after.Take(1, after.len - 1);
237
260
  disposables |= (1u << indirect);
238
- } else if (SplitIdentifier(remain) == "const") {
239
- remain = remain.Take(6, remain.len - 6);
261
+ } else if (SplitIdentifier(after) == "const") {
262
+ after = after.Take(6, after.len - 6);
240
263
  } else {
241
264
  break;
242
265
  }
243
266
 
244
- remain = TrimStr(remain);
267
+ after = TrimStr(after);
245
268
  }
246
269
 
247
270
  const TypeInfo *type = instance->types_map.FindValue(name, nullptr);
Binary file