koffi 0.9.26 → 0.9.27

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.
package/README.md CHANGED
@@ -261,30 +261,30 @@ At this stage, two benchmarks are implemented:
261
261
 
262
262
  In order to run it, go to `koffi/benchmark` and run `../../cnoke/cnoke.js` (or `node ..\..\cnoke\cnoke.js` on Windows) before doing anything else.
263
263
 
264
- Once this is done, you can execute each implementation, e.g. `build/atoi_cc 20000000` or `./atoi_koffi.js 20000000`.
264
+ Once this is done, you can execute each implementation, e.g. `build/atoi_cc` or `./atoi_koffi.js`. You can optionally define a custom number of iterations, e.g. `./atoi_koffi.js 10000000`.
265
265
 
266
266
  ## atoi results
267
267
 
268
268
  Here are some results from 2022-04-24 on my Linux machine (AMD® Ryzen™ 7 5800H 16G):
269
269
 
270
270
  ```sh
271
- $ build/atoi_cc 20000000
271
+ $ build/atoi_cc
272
272
  Iterations: 20000000
273
273
  Time: 0.24s
274
274
 
275
- $ ./atoi_napi.js 20000000
275
+ $ ./atoi_napi.js
276
276
  Iterations: 20000000
277
- Time: 1.56s
277
+ Time: 1.10s
278
278
 
279
- $ ./atoi_koffi.js 20000000
279
+ $ ./atoi_koffi.js
280
280
  Iterations: 20000000
281
- Time: 2.41s
281
+ Time: 2.34s
282
282
 
283
283
  # Note: the Node-FFI version does a few setTimeout calls to force the GC to run (around 20
284
284
  # for the example below), without which Node will consume all memory because the GC never appears
285
285
  # to run, or not enough. It's not ideal but on the other hand it counts as another limitation
286
286
  # to Node-FFI performance.
287
- $ ./atoi_node_ffi.js 20000000
287
+ $ ./atoi_node_ffi.js
288
288
  Iterations: 20000000
289
289
  Time: 640.49s
290
290
  ```
@@ -292,19 +292,19 @@ Time: 640.49s
292
292
  And on my Windows machine (Intel® Core™ i5-4460 16G):
293
293
 
294
294
  ```sh
295
- $ build\atoi_cc.exe 20000000
295
+ $ build\atoi_cc.exe
296
296
  Iterations: 20000000
297
297
  Time: 0.66s
298
298
 
299
- $ node atoi_napi.js 20000000
299
+ $ node atoi_napi.js
300
300
  Iterations: 20000000
301
- Time: 3.52s
301
+ Time: 3.23s
302
302
 
303
- $ node atoi_koffi.js 20000000
303
+ $ node atoi_koffi.js
304
304
  Iterations: 20000000
305
305
  Time: 4.81s
306
306
 
307
- $ node atoi_node_ffi.js 20000000
307
+ $ node atoi_node_ffi.js
308
308
  Iterations: 20000000
309
309
  Time: 491.99s
310
310
  ```
@@ -314,15 +314,15 @@ Time: 491.99s
314
314
  Here are some results from 2022-04-24 on my Linux machine (AMD® Ryzen™ 7 5800H 16G):
315
315
 
316
316
  ```sh
317
- $ build/raylib_cc 100
317
+ $ build/raylib_cc
318
318
  Iterations: 100
319
319
  Time: 4.14s
320
320
 
321
- $ ./raylib_koffi.js 100
321
+ $ ./raylib_koffi.js
322
322
  Iterations: 100
323
323
  Time: 6.25s
324
324
 
325
- $ ./raylib_node_ffi.js 100
325
+ $ ./raylib_node_ffi.js
326
326
  Iterations: 100
327
327
  Time: 27.13s
328
328
  ```
@@ -330,15 +330,15 @@ Time: 27.13s
330
330
  And on my Windows machine (Intel® Core™ i5-4460 16G):
331
331
 
332
332
  ```sh
333
- $ build\raylib_cc.exe 100
333
+ $ build\raylib_cc.exe
334
334
  Iterations: 100
335
335
  Time: 10.53s
336
336
 
337
- $ node raylib_koffi.js 100
337
+ $ node raylib_koffi.js
338
338
  Iterations: 100
339
339
  Time: 14.60s
340
340
 
341
- $ node raylib_node_ffi.js 100
341
+ $ node raylib_node_ffi.js
342
342
  Iterations: 100
343
343
  Time: 44.97s
344
344
  ```
@@ -18,7 +18,9 @@ find_package(CNoke)
18
18
 
19
19
  set(CMAKE_CXX_STANDARD 17)
20
20
 
21
- add_subdirectory(.. koffi)
21
+ if(NOT TARGET koffi)
22
+ add_subdirectory(.. koffi)
23
+ endif()
22
24
  add_subdirectory(../test test)
23
25
 
24
26
  # ---- atoi ----
@@ -25,15 +25,12 @@ volatile uint64_t sum = 0;
25
25
 
26
26
  int Main(int argc, char **argv)
27
27
  {
28
- if (argc < 2) {
29
- LogError("Missing number of iterations");
30
- LogInfo("Usage: atoi_cc <iterations>");
31
- return 1;
32
- }
28
+ int iterations = 20000000;
33
29
 
34
- int iterations = 0;
35
- if (!ParseInt(argv[1], &iterations))
36
- return 1;
30
+ if (argc >= 2) {
31
+ if (!ParseInt(argv[1], &iterations))
32
+ return 1;
33
+ }
37
34
  LogInfo("Iterations: %1", iterations);
38
35
 
39
36
  int64_t start = GetMonotonicTime();
@@ -13,7 +13,7 @@
13
13
  // You should have received a copy of the GNU Affero General Public License
14
14
  // along with this program. If not, see https://www.gnu.org/licenses/.
15
15
 
16
- const koffi = require('../build/koffi.node');
16
+ const koffi = require('./build/koffi.node');
17
17
 
18
18
  const strings = [
19
19
  '424242',
@@ -26,14 +26,15 @@ let sum = 0;
26
26
  main();
27
27
 
28
28
  function main() {
29
- if (process.argv.length < 3)
30
- throw new Error('Missing number of iterations');
31
-
32
- let iterations = parseInt(process.argv[2], 10);
33
- if (Number.isNaN(iterations))
34
- throw new Error('Not a valid number');
35
- if (iterations < 1)
36
- throw new Error('Value must be positive');
29
+ let iterations = 20000000;
30
+
31
+ if (process.argv.length >= 3) {
32
+ iterations = parseInt(process.argv[2], 10);
33
+ if (Number.isNaN(iterations))
34
+ throw new Error('Not a valid number');
35
+ if (iterations < 1)
36
+ throw new Error('Value must be positive');
37
+ }
37
38
  console.log('Iterations:', iterations);
38
39
 
39
40
  let lib = koffi.load(process.platform == 'win32' ? 'msvcrt.dll' : null);
@@ -30,20 +30,26 @@ static Napi::Value RunAtoi(const Napi::CallbackInfo &info)
30
30
  {
31
31
  Napi::Env env = info.Env();
32
32
 
33
- // We want maximum performance here, this is a benchmark
34
- #if 0
35
- if (info.Length() < 1) {
33
+ if (RG_UNLIKELY(info.Length() < 1)) {
36
34
  ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
37
35
  return env.Null();
38
36
  }
39
- if (!info[0].IsString()) {
40
- ThrowError<Napi::TypeError>(env, "Unexpected value for str, expected string");
41
- return env.Null();
37
+
38
+ char str[64];
39
+ {
40
+ napi_status status = napi_get_value_string_utf8(env, info[0], str, RG_SIZE(str), nullptr);
41
+
42
+ if (RG_UNLIKELY(status != napi_ok)) {
43
+ if (status == napi_string_expected) {
44
+ ThrowError<Napi::TypeError>(env, "Unexpected value for str, expected string");
45
+ } else {
46
+ ThrowError<Napi::TypeError>(env, "Failed to read JS string");
47
+ }
48
+ return env.Null();
49
+ }
42
50
  }
43
- #endif
44
51
 
45
- std::string name = info[0].As<Napi::String>();
46
- int value = atoi(name.c_str());
52
+ int value = atoi(str);
47
53
 
48
54
  return Napi::Number::New(env, value);
49
55
  }
@@ -26,14 +26,15 @@ let sum = 0;
26
26
  main();
27
27
 
28
28
  function main() {
29
- if (process.argv.length < 3)
30
- throw new Error('Missing number of iterations');
31
-
32
- let iterations = parseInt(process.argv[2], 10);
33
- if (Number.isNaN(iterations))
34
- throw new Error('Not a valid number');
35
- if (iterations < 1)
36
- throw new Error('Value must be positive');
29
+ let iterations = 20000000;
30
+
31
+ if (process.argv.length >= 3) {
32
+ iterations = parseInt(process.argv[2], 10);
33
+ if (Number.isNaN(iterations))
34
+ throw new Error('Not a valid number');
35
+ if (iterations < 1)
36
+ throw new Error('Value must be positive');
37
+ }
37
38
  console.log('Iterations:', iterations);
38
39
 
39
40
  let start = performance.now();
@@ -16,7 +16,6 @@
16
16
  const ref = require('ref-napi');
17
17
  const ffi = require('ffi-napi');
18
18
  const struct = require('ref-struct-di')(ref);
19
- const path = require('path');
20
19
 
21
20
  const strings = [
22
21
  '424242',
@@ -29,14 +28,15 @@ let sum = 0;
29
28
  main();
30
29
 
31
30
  async function main() {
32
- if (process.argv.length < 3)
33
- throw new Error('Missing number of iterations');
34
-
35
- let iterations = parseInt(process.argv[2], 10);
36
- if (Number.isNaN(iterations))
37
- throw new Error('Not a valid number');
38
- if (iterations < 1)
39
- throw new Error('Value must be positive');
31
+ let iterations = 20000000;
32
+
33
+ if (process.argv.length >= 3) {
34
+ iterations = parseInt(process.argv[2], 10);
35
+ if (Number.isNaN(iterations))
36
+ throw new Error('Not a valid number');
37
+ if (iterations < 1)
38
+ throw new Error('Value must be positive');
39
+ }
40
40
  console.log('Iterations:', iterations);
41
41
 
42
42
  const lib = ffi.Library(process.platform == 'win32' ? 'msvcrt.dll' : null, {
@@ -18,15 +18,12 @@ namespace RG {
18
18
 
19
19
  int Main(int argc, char **argv)
20
20
  {
21
- if (argc < 2) {
22
- LogError("Missing number of iterations");
23
- LogInfo("Usage: raylib_cc <iterations>");
24
- return 1;
25
- }
21
+ int iterations = 100;
26
22
 
27
- int iterations = 0;
28
- if (!ParseInt(argv[1], &iterations))
29
- return 1;
23
+ if (argc >= 2) {
24
+ if (!ParseInt(argv[1], &iterations))
25
+ return 1;
26
+ }
30
27
  LogInfo("Iterations: %1", iterations);
31
28
 
32
29
  // We need to call InitWindow before using anything else (such as fonts)
@@ -13,7 +13,7 @@
13
13
  // You should have received a copy of the GNU Affero General Public License
14
14
  // along with this program. If not, see https://www.gnu.org/licenses/.
15
15
 
16
- const koffi = require('../build/koffi.node');
16
+ const koffi = require('./build/koffi.node');
17
17
  const path = require('path');
18
18
 
19
19
  const Color = koffi.struct('Color', {
@@ -71,17 +71,18 @@ const Font = koffi.struct('Font', {
71
71
  main();
72
72
 
73
73
  function main() {
74
- if (process.argv.length < 3)
75
- throw new Error('Missing number of iterations');
76
-
77
- let iterations = parseInt(process.argv[2], 10);
78
- if (Number.isNaN(iterations))
79
- throw new Error('Not a valid number');
80
- if (iterations < 1)
81
- throw new Error('Value must be positive');
74
+ let iterations = 100;
75
+
76
+ if (process.argv.length >= 3) {
77
+ iterations = parseInt(process.argv[2], 10);
78
+ if (Number.isNaN(iterations))
79
+ throw new Error('Not a valid number');
80
+ if (iterations < 1)
81
+ throw new Error('Value must be positive');
82
+ }
82
83
  console.log('Iterations:', iterations);
83
84
 
84
- let lib_filename = path.dirname(__filename) + '/../test/build/raylib' + koffi.extension;
85
+ let lib_filename = path.dirname(__filename) + '/test/build/raylib' + koffi.extension;
85
86
  let lib = koffi.load(lib_filename);
86
87
 
87
88
  const InitWindow = lib.cdecl('InitWindow', 'void', ['int', 'int', 'string']);
@@ -16,7 +16,7 @@
16
16
  const ref = require('ref-napi');
17
17
  const ffi = require('ffi-napi');
18
18
  const struct = require('ref-struct-di')(ref);
19
- const koffi = require('../build/koffi.node');
19
+ const koffi = require('./build/koffi.node');
20
20
  const path = require('path');
21
21
 
22
22
  const Color = struct({
@@ -87,8 +87,16 @@ const Font = struct({
87
87
  main();
88
88
 
89
89
  function main() {
90
- if (process.argv.length < 3)
91
- throw new Error('Missing number of iterations');
90
+ let iterations = 100;
91
+
92
+ if (process.argv.length >= 3) {
93
+ iterations = parseInt(process.argv[2], 10);
94
+ if (Number.isNaN(iterations))
95
+ throw new Error('Not a valid number');
96
+ if (iterations < 1)
97
+ throw new Error('Value must be positive');
98
+ }
99
+ console.log('Iterations:', iterations);
92
100
 
93
101
  let iterations = parseInt(process.argv[2], 10);
94
102
  if (Number.isNaN(iterations))
@@ -97,7 +105,7 @@ function main() {
97
105
  throw new Error('Value must be positive');
98
106
  console.log('Iterations:', iterations);
99
107
 
100
- let lib_filename = path.dirname(__filename) + '/../test/build/raylib' + koffi.extension;
108
+ let lib_filename = path.dirname(__filename) + '/test/build/raylib' + koffi.extension;
101
109
 
102
110
  const r = ffi.Library(lib_filename, {
103
111
  InitWindow: ['void', ['int', 'int', 'string']],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "0.9.26",
3
+ "version": "0.9.27",
4
4
  "description": "Fast and simple FFI (foreign function interface) for Node.js",
5
5
  "keywords": [
6
6
  "foreign",
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "license": "AGPL-3.0",
25
25
  "dependencies": {
26
- "cnoke": "^0.9.26"
26
+ "cnoke": "^1.0.1"
27
27
  },
28
28
  "devDependencies": {
29
29
  "chalk": "^4.1.2",
package/src/ffi.cc CHANGED
@@ -116,7 +116,7 @@ static Napi::Value CreateStructType(const Napi::CallbackInfo &info)
116
116
  if (!member.type)
117
117
  return env.Null();
118
118
 
119
- type->size = AlignLen(type->size, member.type->align) + member.type->size;
119
+ type->size = (int16_t)(AlignLen(type->size, member.type->align) + member.type->size);
120
120
  type->align = std::max(type->align, member.type->align);
121
121
 
122
122
  type->members.Append(member);
@@ -14,6 +14,10 @@
14
14
  cmake_minimum_required(VERSION 3.12)
15
15
  project(koffi C CXX)
16
16
 
17
+ if(NOT TARGET koffi)
18
+ add_subdirectory(.. koffi)
19
+ endif()
20
+
17
21
  # ---- Raylib ----
18
22
 
19
23
  add_library(raylib SHARED
@@ -25,9 +25,7 @@
25
25
  "Linux ARM32": {
26
26
  "directory": "/home/debian/luigi",
27
27
  "build": {
28
- "Install": "npm install --production --ignore-scripts",
29
- "Build Koffi": "node ../cnoke/cnoke.js",
30
- "Build dependencies": "node ../cnoke/cnoke.js -C test"
28
+ "Build": "node ../cnoke/cnoke.js -C test"
31
29
  },
32
30
  "commands": {
33
31
  "Test Raylib": "xvfb-run node test/tests/raylib.js",
@@ -64,9 +62,7 @@
64
62
  "Linux ARM64": {
65
63
  "directory": "/home/debian/luigi",
66
64
  "build": {
67
- "Install": "npm install --production --ignore-scripts",
68
- "Build Koffi": "node ../cnoke/cnoke.js",
69
- "Build dependencies": "node ../cnoke/cnoke.js -C test"
65
+ "Build": "node ../cnoke/cnoke.js -C test"
70
66
  },
71
67
  "commands": {
72
68
  "Test Raylib": "xvfb-run node test/tests/raylib.js",
@@ -103,9 +99,7 @@
103
99
  "Linux i386": {
104
100
  "directory": "/home/debian/luigi",
105
101
  "build": {
106
- "Install": "npm install --production --ignore-scripts",
107
- "Build Koffi": "node ../cnoke/cnoke.js",
108
- "Build dependencies": "node ../cnoke/cnoke.js -C test"
102
+ "Build": "node ../cnoke/cnoke.js -C test"
109
103
  },
110
104
  "commands": {
111
105
  "Test Raylib": "xvfb-run node test/tests/raylib.js",
@@ -142,9 +136,7 @@
142
136
  "Linux x64": {
143
137
  "directory": "/home/debian/luigi",
144
138
  "build": {
145
- "Install": "npm install --production --ignore-scripts",
146
- "Build Koffi": "node ../cnoke/cnoke.js",
147
- "Build dependencies": "node ../cnoke/cnoke.js -C test"
139
+ "Build": "node ../cnoke/cnoke.js -C test"
148
140
  },
149
141
  "commands": {
150
142
  "Test Raylib": "xvfb-run node test/tests/raylib.js",
@@ -181,9 +173,7 @@
181
173
  "Windows i386": {
182
174
  "directory": "C:/Users/windows/Desktop/luigi32",
183
175
  "build": {
184
- "Install": "C:\\Node32\\node32.cmd npm install --production --ignore-scripts",
185
- "Build Koffi": "C:\\Node32\\node32.cmd node ../cnoke/cnoke.js",
186
- "Build dependencies": "C:\\Node32\\node32.cmd node ../cnoke/cnoke.js -C test"
176
+ "Build": "C:\\Node32\\node32.cmd node ../cnoke/cnoke.js -C test"
187
177
  },
188
178
  "commands": {
189
179
  "Test Raylib": "seatsh C:\\Node32\\node32.cmd node test/tests/raylib.js",
@@ -195,9 +185,7 @@
195
185
  "Windows x64": {
196
186
  "directory": "C:/Users/windows/Desktop/luigi64",
197
187
  "build": {
198
- "Install": "C:\\Node64\\node64.cmd npm install --production --ignore-scripts",
199
- "Build Koffi": "C:\\Node64\\node64.cmd node ../cnoke/cnoke.js",
200
- "Build dependencies": "C:\\Node64\\node64.cmd node ../cnoke/cnoke.js -C test"
188
+ "Build": "C:\\Node64\\node64.cmd node ../cnoke/cnoke.js -C test"
201
189
  },
202
190
  "commands": {
203
191
  "Test Raylib": "seatsh C:\\Node64\\node64.cmd node test/tests/raylib.js",
@@ -234,9 +222,7 @@
234
222
  "FreeBSD x64": {
235
223
  "directory": "/home/freebsd/luigi",
236
224
  "build": {
237
- "Install": "npm install --production --ignore-scripts",
238
- "Build Koffi": "node ../cnoke/cnoke.js",
239
- "Build dependencies": "node ../cnoke/cnoke.js -C test"
225
+ "Build": "node ../cnoke/cnoke.js -C test"
240
226
  },
241
227
  "commands": {
242
228
  "Test Raylib": "xvfb-run node test/tests/raylib.js",
@@ -273,9 +259,7 @@
273
259
  "FreeBSD i386": {
274
260
  "directory": "/home/freebsd/luigi",
275
261
  "build": {
276
- "Install": "npm install --production --ignore-scripts",
277
- "Build Koffi": "node ../cnoke/cnoke.js",
278
- "Build dependencies": "node ../cnoke/cnoke.js -C test"
262
+ "Build": "node ../cnoke/cnoke.js -C test"
279
263
  },
280
264
  "commands": {
281
265
  "Test Raylib": "xvfb-run node test/tests/raylib.js",
@@ -312,9 +296,7 @@
312
296
  "FreeBSD ARM64": {
313
297
  "directory": "/home/freebsd/luigi",
314
298
  "build": {
315
- "Install": "npm install --production --ignore-scripts",
316
- "Build Koffi": "node ../cnoke/cnoke.js",
317
- "Build dependencies": "node ../cnoke/cnoke.js -C test"
299
+ "Build": "node ../cnoke/cnoke.js -C test"
318
300
  },
319
301
  "commands": {
320
302
  "Test Raylib": "xvfb-run node test/tests/raylib.js",
@@ -351,9 +333,7 @@
351
333
  "macOS x64": {
352
334
  "directory": "/Users/macos/luigi",
353
335
  "build": {
354
- "Install": "PATH=/usr/local/bin:/usr/bin:/bin npm install --production --ignore-scripts",
355
- "Build Koffi": "PATH=/usr/local/bin:/usr/bin:/bin SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk node ../cnoke/cnoke.js",
356
- "Build dependencies": "PATH=/usr/local/bin:/usr/bin:/bin SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk node ../cnoke/cnoke.js -C test"
336
+ "Build": "PATH=/usr/local/bin:/usr/bin:/bin SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk node ../cnoke/cnoke.js -C test"
357
337
  },
358
338
  "commands": {
359
339
  "Test Misc": "PATH=/usr/local/bin:/usr/bin:/bin node test/tests/misc.js",
@@ -13,7 +13,7 @@
13
13
  // You should have received a copy of the GNU Affero General Public License
14
14
  // along with this program. If not, see https://www.gnu.org/licenses/.
15
15
 
16
- const koffi = require('../../build/koffi.node');
16
+ const koffi = require('../build/koffi.node');
17
17
  const assert = require('assert');
18
18
  const path = require('path');
19
19
 
@@ -13,7 +13,7 @@
13
13
  // You should have received a copy of the GNU Affero General Public License
14
14
  // along with this program. If not, see https://www.gnu.org/licenses/.
15
15
 
16
- const koffi = require('../../build/koffi.node');
16
+ const koffi = require('../build/koffi.node');
17
17
  const crypto = require('crypto');
18
18
  const fs = require('fs');
19
19
  const os = require('os');
@@ -14,7 +14,7 @@
14
14
  // along with this program. If not, see https://www.gnu.org/licenses/.
15
15
 
16
16
  const crypto = require('crypto');
17
- const koffi = require('../../build/koffi.node');
17
+ const koffi = require('../build/koffi.node');
18
18
  const assert = require('assert');
19
19
  const fs = require('fs');
20
20
  const os = require('os');
Binary file