koffi 1.3.12 → 2.1.0-beta.1

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.
Files changed (104) hide show
  1. package/CMakeLists.txt +8 -10
  2. package/ChangeLog.md +48 -16
  3. package/README.md +6 -0
  4. package/benchmark/atoi_koffi.js +12 -8
  5. package/benchmark/atoi_napi.js +12 -8
  6. package/benchmark/atoi_node_ffi.js +11 -10
  7. package/benchmark/raylib_cc.cc +12 -9
  8. package/benchmark/raylib_koffi.js +15 -13
  9. package/benchmark/raylib_node_ffi.js +15 -13
  10. package/benchmark/raylib_node_raylib.js +14 -11
  11. package/build/qemu/2.1.0-beta.1/koffi_darwin_arm64.tar.gz +0 -0
  12. package/build/qemu/2.1.0-beta.1/koffi_darwin_x64.tar.gz +0 -0
  13. package/build/qemu/2.1.0-beta.1/koffi_freebsd_arm64.tar.gz +0 -0
  14. package/build/qemu/2.1.0-beta.1/koffi_freebsd_ia32.tar.gz +0 -0
  15. package/build/qemu/2.1.0-beta.1/koffi_freebsd_x64.tar.gz +0 -0
  16. package/build/qemu/2.1.0-beta.1/koffi_linux_arm32hf.tar.gz +0 -0
  17. package/build/qemu/2.1.0-beta.1/koffi_linux_arm64.tar.gz +0 -0
  18. package/build/qemu/2.1.0-beta.1/koffi_linux_ia32.tar.gz +0 -0
  19. package/build/qemu/2.1.0-beta.1/koffi_linux_riscv64hf64.tar.gz +0 -0
  20. package/build/qemu/2.1.0-beta.1/koffi_linux_x64.tar.gz +0 -0
  21. package/build/qemu/2.1.0-beta.1/koffi_openbsd_ia32.tar.gz +0 -0
  22. package/build/qemu/2.1.0-beta.1/koffi_openbsd_x64.tar.gz +0 -0
  23. package/build/qemu/2.1.0-beta.1/koffi_win32_arm64.tar.gz +0 -0
  24. package/build/qemu/2.1.0-beta.1/koffi_win32_ia32.tar.gz +0 -0
  25. package/build/qemu/2.1.0-beta.1/koffi_win32_x64.tar.gz +0 -0
  26. package/doc/changes.md +160 -1
  27. package/doc/conf.py +14 -1
  28. package/doc/contribute.md +0 -1
  29. package/doc/dist/doctrees/benchmarks.doctree +0 -0
  30. package/doc/dist/doctrees/changes.doctree +0 -0
  31. package/doc/dist/doctrees/environment.pickle +0 -0
  32. package/doc/dist/doctrees/functions.doctree +0 -0
  33. package/doc/dist/doctrees/index.doctree +0 -0
  34. package/doc/dist/doctrees/types.doctree +0 -0
  35. package/doc/dist/html/.buildinfo +1 -1
  36. package/doc/dist/html/_sources/benchmarks.md.txt +2 -2
  37. package/doc/dist/html/_sources/changes.md.txt +160 -1
  38. package/doc/dist/html/_sources/functions.md.txt +17 -13
  39. package/doc/dist/html/_sources/types.md.txt +87 -35
  40. package/doc/dist/html/benchmarks.html +7 -3
  41. package/doc/dist/html/changes.html +241 -14
  42. package/doc/dist/html/contribute.html +5 -1
  43. package/doc/dist/html/functions.html +30 -23
  44. package/doc/dist/html/genindex.html +5 -1
  45. package/doc/dist/html/index.html +13 -19
  46. package/doc/dist/html/memory.html +7 -3
  47. package/doc/dist/html/objects.inv +0 -0
  48. package/doc/dist/html/platforms.html +6 -2
  49. package/doc/dist/html/search.html +5 -1
  50. package/doc/dist/html/searchindex.js +1 -1
  51. package/doc/dist/html/start.html +5 -1
  52. package/doc/dist/html/types.html +104 -43
  53. package/doc/functions.md +139 -15
  54. package/doc/templates/badges.html +5 -0
  55. package/doc/types.md +108 -40
  56. package/package.json +2 -2
  57. package/qemu/qemu.js +1 -1
  58. package/qemu/registry/machines.json +5 -5
  59. package/qemu/registry/sha256sum.txt +16 -16
  60. package/src/abi_arm32.cc +91 -19
  61. package/src/abi_arm32_fwd.S +121 -57
  62. package/src/abi_arm64.cc +91 -19
  63. package/src/abi_arm64_fwd.S +96 -0
  64. package/src/abi_arm64_fwd.asm +128 -0
  65. package/src/abi_riscv64.cc +89 -19
  66. package/src/abi_riscv64_fwd.S +96 -0
  67. package/src/abi_x64_sysv.cc +94 -22
  68. package/src/abi_x64_sysv_fwd.S +96 -0
  69. package/src/abi_x64_win.cc +89 -19
  70. package/src/abi_x64_win_fwd.asm +128 -0
  71. package/src/abi_x86.cc +94 -19
  72. package/src/abi_x86_fwd.S +96 -0
  73. package/src/abi_x86_fwd.asm +128 -0
  74. package/src/call.cc +128 -78
  75. package/src/call.hh +17 -4
  76. package/src/ffi.cc +514 -145
  77. package/src/ffi.hh +30 -9
  78. package/src/index.js +4 -2
  79. package/src/parser.cc +19 -44
  80. package/src/util.cc +160 -27
  81. package/src/util.hh +7 -2
  82. package/test/async.js +1 -2
  83. package/test/callbacks.js +56 -11
  84. package/test/misc.c +50 -15
  85. package/test/raylib.js +2 -2
  86. package/test/sqlite.js +27 -19
  87. package/test/sync.js +71 -35
  88. package/vendor/libcc/libcc.cc +18 -5
  89. package/vendor/libcc/libcc.hh +70 -23
  90. package/build/qemu/1.3.12/koffi_darwin_arm64.tar.gz +0 -0
  91. package/build/qemu/1.3.12/koffi_darwin_x64.tar.gz +0 -0
  92. package/build/qemu/1.3.12/koffi_freebsd_arm64.tar.gz +0 -0
  93. package/build/qemu/1.3.12/koffi_freebsd_ia32.tar.gz +0 -0
  94. package/build/qemu/1.3.12/koffi_freebsd_x64.tar.gz +0 -0
  95. package/build/qemu/1.3.12/koffi_linux_arm32hf.tar.gz +0 -0
  96. package/build/qemu/1.3.12/koffi_linux_arm64.tar.gz +0 -0
  97. package/build/qemu/1.3.12/koffi_linux_ia32.tar.gz +0 -0
  98. package/build/qemu/1.3.12/koffi_linux_riscv64hf64.tar.gz +0 -0
  99. package/build/qemu/1.3.12/koffi_linux_x64.tar.gz +0 -0
  100. package/build/qemu/1.3.12/koffi_openbsd_ia32.tar.gz +0 -0
  101. package/build/qemu/1.3.12/koffi_openbsd_x64.tar.gz +0 -0
  102. package/build/qemu/1.3.12/koffi_win32_arm64.tar.gz +0 -0
  103. package/build/qemu/1.3.12/koffi_win32_ia32.tar.gz +0 -0
  104. package/build/qemu/1.3.12/koffi_win32_x64.tar.gz +0 -0
package/CMakeLists.txt CHANGED
@@ -15,11 +15,6 @@ cmake_minimum_required(VERSION 3.6)
15
15
  project(koffi C CXX ASM)
16
16
 
17
17
  include(CheckCXXCompilerFlag)
18
- if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.9.0")
19
- cmake_policy(SET CMP0069 NEW)
20
- include(CheckIPOSupported)
21
- check_ipo_supported(RESULT USE_LTO)
22
- endif()
23
18
 
24
19
  find_package(CNoke)
25
20
 
@@ -113,14 +108,17 @@ endif()
113
108
  if(NOT MSVC OR CMAKE_C_COMPILER_ID MATCHES "[Cc]lang")
114
109
  # Restore C/C++ compiler sanity
115
110
 
116
- target_compile_options(koffi PRIVATE -fno-exceptions -fno-strict-aliasing -fwrapv
117
- -fno-delete-null-pointer-checks)
111
+ if(NOT MSVC)
112
+ target_compile_options(koffi PRIVATE -fno-exceptions -fno-strict-aliasing -fwrapv
113
+ -fno-delete-null-pointer-checks)
114
+ else()
115
+ target_compile_options(koffi PRIVATE -fno-strict-aliasing /clang:-fwrapv
116
+ -fno-delete-null-pointer-checks)
117
+ endif()
118
118
 
119
119
  check_cxx_compiler_flag(-fno-finite-loops use_no_finite_loops)
120
120
  if(use_no_finite_loops)
121
121
  target_compile_options(koffi PRIVATE -fno-finite-loops)
122
122
  endif()
123
123
  endif()
124
- if(USE_LTO)
125
- set_target_properties(koffi PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
126
- endif()
124
+ enable_unity_build(koffi)
package/ChangeLog.md CHANGED
@@ -1,18 +1,50 @@
1
1
  # Changelog
2
2
 
3
- ## Koffi 1.3.12
3
+ ## History
4
+
5
+ ### Koffi 2.0.1
6
+
7
+ **Main changes:**
8
+
9
+ - Return `undefined` (instead of null) for `void` functions
10
+
11
+ ### Koffi 2.0.0
12
+
13
+ **Major new features:**
14
+
15
+ - Add [disposable types](functions.md#heap-allocated-values) for automatic disposal of C values (such as heap-allocated strings)
16
+ - Add support for [registered callbacks](functions.md#registered-callbacks), that can be called after the initial FFI call
17
+ - Support named pointer types
18
+ - Support complex type specifications outside of prototype parser
19
+
20
+ **Minor new features:**
21
+
22
+ - Support type aliases with `koffi.alias()`
23
+ - Add `koffi.resolve()` to resolve type strings
24
+ - Expose all primitive type aliases in `koffi.types`
25
+ - Correctly pass exceptions thrown in JS callbacks
26
+
27
+ **Breaking API changes:**
28
+
29
+ - Change handling of callback types, which must be used through pointers
30
+ - Change handling of opaque handles, which must be used through pointers
31
+ - Support all types in `koffi.introspect(type)`
32
+
33
+ Consult the [migration guide](changes.md#migration-guide) for more information.
34
+
35
+ ### Koffi 1.3.12
4
36
 
5
37
  **Main fixes:**
6
38
 
7
39
  - Fix support for Yarn package manager
8
40
 
9
- ## Koffi 1.3.11
41
+ ### Koffi 1.3.11
10
42
 
11
43
  **Main fixes:**
12
44
 
13
45
  - Fix broken parsing of `void *` when used for first parameter
14
46
 
15
- ## Koffi 1.3.10
47
+ ### Koffi 1.3.10
16
48
 
17
49
  **Main fixes:**
18
50
 
@@ -24,13 +56,13 @@
24
56
 
25
57
  - Various documentation fixes and improvements
26
58
 
27
- ## Koffi 1.3.9
59
+ ### Koffi 1.3.9
28
60
 
29
61
  **Main fixes:**
30
62
 
31
63
  - Fix prebuild compatibility with Electron on Windows x64
32
64
 
33
- ## Koffi 1.3.8
65
+ ### Koffi 1.3.8
34
66
 
35
67
  **Main changes:**
36
68
 
@@ -41,7 +73,7 @@
41
73
 
42
74
  - Fix and harmonize a few error messages
43
75
 
44
- ## Koffi 1.3.7
76
+ ### Koffi 1.3.7
45
77
 
46
78
  **Main fixes:**
47
79
 
@@ -54,7 +86,7 @@
54
86
  - Add str/str16 type aliases for string/string16
55
87
  - Various documentation fixes and improvements
56
88
 
57
- ## Koffi 1.3.6
89
+ ### Koffi 1.3.6
58
90
 
59
91
  **Main fixes:**
60
92
 
@@ -66,7 +98,7 @@
66
98
  - Prebuild with Clang for Windows x64 and Linux x64 binaries
67
99
  - Various documentation improvements
68
100
 
69
- ## Koffi 1.3.5
101
+ ### Koffi 1.3.5
70
102
 
71
103
  **Main changes:**
72
104
 
@@ -78,13 +110,13 @@
78
110
  - Reduce default async memory stack and heap size
79
111
  - Various documentation improvements
80
112
 
81
- ## Koffi 1.3.4
113
+ ### Koffi 1.3.4
82
114
 
83
115
  **Main fixes:**
84
116
 
85
117
  - Fix possible OpenBSD i386 crash with `(void)` functions
86
118
 
87
- ## Koffi 1.3.3
119
+ ### Koffi 1.3.3
88
120
 
89
121
  **Main fixes:**
90
122
 
@@ -96,20 +128,20 @@
96
128
  - Disable unsafe compiler optimizations
97
129
  - Various documentation improvements
98
130
 
99
- ## Koffi 1.3.2
131
+ ### Koffi 1.3.2
100
132
 
101
133
  **Main fixes:**
102
134
 
103
135
  - Support compilation in C++14 mode (graceful degradation)
104
136
  - Support older toolchains on Linux (tested on Debian 9)
105
137
 
106
- ## Koffi 1.3.1
138
+ ### Koffi 1.3.1
107
139
 
108
140
  **Main fixes:**
109
141
 
110
142
  - The prebuilt binary is tested when Koffi is installed, and a rebuild happens if it fails to load
111
143
 
112
- ## Koffi 1.3.0
144
+ ### Koffi 1.3.0
113
145
 
114
146
  **Major changes:**
115
147
 
@@ -125,19 +157,19 @@
125
157
  - Detect floating-point ABI before using prebuilt binaries (ARM32, RISC-V)
126
158
  - Forbid duplicate member names in struct types
127
159
 
128
- ## Koffi 1.2.4
160
+ ### Koffi 1.2.4
129
161
 
130
162
  **New features:**
131
163
 
132
164
  - Windows ARM64 is now supported
133
165
 
134
- ## Koffi 1.2.3
166
+ ### Koffi 1.2.3
135
167
 
136
168
  **New features:**
137
169
 
138
170
  - A prebuilt binary for macOS ARM64 (M1) is now included
139
171
 
140
- ## Koffi 1.2.1
172
+ ### Koffi 1.2.1
141
173
 
142
174
  This entry documents changes since version 1.1.0.
143
175
 
package/README.md CHANGED
@@ -23,6 +23,12 @@ RISC-V 64 [^3] | ⬜️ *N/A* | ✅ Yes | ⬜️ *N/A* | 🟨 Probab
23
23
 
24
24
  Go to the web site for more information: https://koffi.dev/
25
25
 
26
+ # Project history
27
+
28
+ You can consult the [changelog](https://koffi.dev/changes) on the official website.
29
+
30
+ Major version increments can include breaking API changes, use the [migration guide](https://koffi.dev/changes#migration-guide) for more information.
31
+
26
32
  # License
27
33
 
28
34
  This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
@@ -26,14 +26,14 @@ let sum = 0;
26
26
  main();
27
27
 
28
28
  function main() {
29
- let iterations = 20000000;
29
+ let time = 5000;
30
30
 
31
31
  if (process.argv.length >= 3) {
32
- iterations = parseInt(process.argv[2], 10);
33
- if (Number.isNaN(iterations))
32
+ time = parseFloat(process.argv[2]) * 1000;
33
+ if (Number.isNaN(time))
34
34
  throw new Error('Not a valid number');
35
- if (iterations < 1)
36
- throw new Error('Value must be positive');
35
+ if (time < 0)
36
+ throw new Error('Time must be positive');
37
37
  }
38
38
 
39
39
  let lib = koffi.load(process.platform == 'win32' ? 'msvcrt.dll' : null);
@@ -41,11 +41,15 @@ function main() {
41
41
  const atoi = lib.cdecl('atoi', 'int', ['str']);
42
42
 
43
43
  let start = performance.now();
44
+ let iterations = 0;
44
45
 
45
- for (let i = 0; i < iterations; i++) {
46
- sum += atoi(strings[i % strings.length]);
46
+ while (performance.now() - start < time) {
47
+ for (let i = 0; i < 1000000; i++)
48
+ sum += atoi(strings[i % strings.length]);
49
+
50
+ iterations += 1000000;
47
51
  }
48
52
 
49
- let time = performance.now() - start;
53
+ time = performance.now() - start;
50
54
  console.log(JSON.stringify({ iterations: iterations, time: Math.round(time) }));
51
55
  }
@@ -26,22 +26,26 @@ let sum = 0;
26
26
  main();
27
27
 
28
28
  function main() {
29
- let iterations = 20000000;
29
+ let time = 5000;
30
30
 
31
31
  if (process.argv.length >= 3) {
32
- iterations = parseInt(process.argv[2], 10);
33
- if (Number.isNaN(iterations))
32
+ time = parseFloat(process.argv[2]) * 1000;
33
+ if (Number.isNaN(time))
34
34
  throw new Error('Not a valid number');
35
- if (iterations < 1)
36
- throw new Error('Value must be positive');
35
+ if (time < 0)
36
+ throw new Error('Time must be positive');
37
37
  }
38
38
 
39
39
  let start = performance.now();
40
+ let iterations = 0;
40
41
 
41
- for (let i = 0; i < iterations; i++) {
42
- sum += atoi.atoi(strings[i % strings.length]);
42
+ while (performance.now() - start < time) {
43
+ for (let i = 0; i < 1000000; i++)
44
+ sum += atoi.atoi(strings[i % strings.length]);
45
+
46
+ iterations += 1000000;
43
47
  }
44
48
 
45
- let time = performance.now() - start;
49
+ time = performance.now() - start;
46
50
  console.log(JSON.stringify({ iterations: iterations, time: Math.round(time) }));
47
51
  }
@@ -28,14 +28,14 @@ let sum = 0;
28
28
  main();
29
29
 
30
30
  async function main() {
31
- let iterations = 200000;
31
+ let time = 5000;
32
32
 
33
33
  if (process.argv.length >= 3) {
34
- iterations = parseInt(process.argv[2], 10);
35
- if (Number.isNaN(iterations))
34
+ time = parseFloat(process.argv[2]) * 1000;
35
+ if (Number.isNaN(time))
36
36
  throw new Error('Not a valid number');
37
- if (iterations < 1)
38
- throw new Error('Value must be positive');
37
+ if (time < 0)
38
+ throw new Error('Time must be positive');
39
39
  }
40
40
 
41
41
  const lib = ffi.Library(process.platform == 'win32' ? 'msvcrt.dll' : null, {
@@ -43,14 +43,15 @@ async function main() {
43
43
  });
44
44
 
45
45
  let start = performance.now();
46
+ let iterations = 0;
46
47
 
47
- for (let i = 0; i < iterations; i++) {
48
- if (i % 1000000 == 0)
49
- await new Promise(resolve => setTimeout(resolve, 0));
48
+ while (performance.now() - start < time) {
49
+ for (let i = 0; i < 1000000; i++)
50
+ sum += lib.atoi(strings[i % strings.length]);
50
51
 
51
- sum += lib.atoi(strings[i % strings.length]);
52
+ iterations += 1000000;
52
53
  }
53
54
 
54
- let time = performance.now() - start;
55
+ time = performance.now() - start;
55
56
  console.log(JSON.stringify({ iterations: iterations, time: Math.round(time) }));
56
57
  }
@@ -18,10 +18,10 @@ namespace RG {
18
18
 
19
19
  int Main(int argc, char **argv)
20
20
  {
21
- int iterations = 360000;
21
+ int time = 5000;
22
22
 
23
23
  if (argc >= 2) {
24
- if (!ParseInt(argv[1], &iterations))
24
+ if (!ParseInt(argv[1], &time))
25
25
  return 1;
26
26
  }
27
27
 
@@ -34,15 +34,16 @@ int Main(int argc, char **argv)
34
34
  Font font = GetFontDefault();
35
35
 
36
36
  int64_t start = GetMonotonicTime();
37
+ int64_t iterations = 0;
37
38
 
38
- for (int i = 0; i < iterations; i += 3600) {
39
+ while (GetMonotonicTime() - start < time) {
39
40
  ImageClearBackground(&img, Color { 0, 0, 0, 255 });
40
41
 
41
- for (int j = 0; j < 3600; j++) {
42
+ for (int i = 0; i < 3600; i++) {
42
43
  const char *text = "Hello World!";
43
44
  float text_width = MeasureTextEx(font, text, 10, 1).x;
44
45
 
45
- double angle = (j * 7) * PI / 180;
46
+ double angle = (i * 7) * PI / 180;
46
47
  Color color = {
47
48
  (unsigned char)(127.5 + 127.5 * sin(angle)),
48
49
  (unsigned char)(127.5 + 127.5 * sin(angle + PI / 2)),
@@ -50,16 +51,18 @@ int Main(int argc, char **argv)
50
51
  255
51
52
  };
52
53
  Vector2 pos = {
53
- (float)((img.width / 2 - text_width / 2) + j * 0.1 * cos(angle - PI / 2)),
54
- (float)((img.height / 2 - 16) + j * 0.1 * sin(angle - PI / 2))
54
+ (float)((img.width / 2 - text_width / 2) + i * 0.1 * cos(angle - PI / 2)),
55
+ (float)((img.height / 2 - 16) + i * 0.1 * sin(angle - PI / 2))
55
56
  };
56
57
 
57
58
  ImageDrawTextEx(&img, font, text, pos, 10, 1, color);
58
59
  }
60
+
61
+ iterations += 3600;
59
62
  }
60
63
 
61
- int64_t time = GetMonotonicTime() - start;
62
- PrintLn("{\"iterations\": %1,\"time\": %2}", iterations, time);
64
+ time = GetMonotonicTime() - start;
65
+ PrintLn("{\"iterations\": %1, \"time\": %2}", iterations, time);
63
66
 
64
67
  return 0;
65
68
  }
@@ -14,7 +14,6 @@
14
14
  // along with this program. If not, see https://www.gnu.org/licenses/.
15
15
 
16
16
  const koffi = require('./build/koffi.node');
17
- const path = require('path');
18
17
 
19
18
  const Color = koffi.struct('Color', {
20
19
  r: 'uchar',
@@ -71,17 +70,17 @@ const Font = koffi.struct('Font', {
71
70
  main();
72
71
 
73
72
  function main() {
74
- let iterations = 360000;
73
+ let time = 5000;
75
74
 
76
75
  if (process.argv.length >= 3) {
77
- iterations = parseInt(process.argv[2], 10);
78
- if (Number.isNaN(iterations))
76
+ time = parseFloat(process.argv[2]) * 1000;
77
+ if (Number.isNaN(time))
79
78
  throw new Error('Not a valid number');
80
- if (iterations < 1)
81
- throw new Error('Value must be positive');
79
+ if (time < 0)
80
+ throw new Error('Time must be positive');
82
81
  }
83
82
 
84
- let lib_filename = path.dirname(__filename) + '/build/raylib' + koffi.extension;
83
+ let lib_filename = __dirname + '/build/raylib' + koffi.extension;
85
84
  let lib = koffi.load(lib_filename);
86
85
 
87
86
  const InitWindow = lib.cdecl('InitWindow', 'void', ['int', 'int', 'str']);
@@ -103,15 +102,16 @@ function main() {
103
102
  let font = GetFontDefault();
104
103
 
105
104
  let start = performance.now();
105
+ let iterations = 0;
106
106
 
107
- for (let i = 0; i < iterations; i += 3600) {
107
+ while (performance.now() - start < time) {
108
108
  ImageClearBackground(img, { r: 0, g: 0, b: 0, a: 255 });
109
109
 
110
- for (let j = 0; j < 3600; j++) {
110
+ for (let i = 0; i < 3600; i++) {
111
111
  let text = 'Hello World!';
112
112
  let text_width = MeasureTextEx(font, text, 10, 1).x;
113
113
 
114
- let angle = (j * 7) * Math.PI / 180;
114
+ let angle = (i * 7) * Math.PI / 180;
115
115
  let color = {
116
116
  r: 127.5 + 127.5 * Math.sin(angle),
117
117
  g: 127.5 + 127.5 * Math.sin(angle + Math.PI / 2),
@@ -119,14 +119,16 @@ function main() {
119
119
  a: 255
120
120
  };
121
121
  let pos = {
122
- x: (img.width / 2 - text_width / 2) + j * 0.1 * Math.cos(angle - Math.PI / 2),
123
- y: (img.height / 2 - 16) + j * 0.1 * Math.sin(angle - Math.PI / 2)
122
+ x: (img.width / 2 - text_width / 2) + i * 0.1 * Math.cos(angle - Math.PI / 2),
123
+ y: (img.height / 2 - 16) + i * 0.1 * Math.sin(angle - Math.PI / 2)
124
124
  };
125
125
 
126
126
  ImageDrawTextEx(img, font, text, pos, 10, 1, color);
127
127
  }
128
+
129
+ iterations += 3600;
128
130
  }
129
131
 
130
- let time = performance.now() - start;
132
+ time = performance.now() - start;
131
133
  console.log(JSON.stringify({ iterations: iterations, time: Math.round(time) }));
132
134
  }
@@ -17,7 +17,6 @@ const ref = require('ref-napi');
17
17
  const ffi = require('ffi-napi');
18
18
  const struct = require('ref-struct-di')(ref);
19
19
  const koffi = require('./build/koffi.node');
20
- const path = require('path');
21
20
 
22
21
  const Color = struct({
23
22
  r: 'uchar',
@@ -87,17 +86,17 @@ const Font = struct({
87
86
  main();
88
87
 
89
88
  function main() {
90
- let iterations = 180000;
89
+ let time = 5000;
91
90
 
92
91
  if (process.argv.length >= 3) {
93
- iterations = parseInt(process.argv[2], 10);
94
- if (Number.isNaN(iterations))
92
+ time = parseFloat(process.argv[2]) * 1000;
93
+ if (Number.isNaN(time))
95
94
  throw new Error('Not a valid number');
96
- if (iterations < 1)
97
- throw new Error('Value must be positive');
95
+ if (time < 0)
96
+ throw new Error('Time must be positive');
98
97
  }
99
98
 
100
- let lib_filename = path.dirname(__filename) + '/build/raylib' + koffi.extension;
99
+ let lib_filename = __dirname + '/build/raylib' + koffi.extension;
101
100
 
102
101
  const r = ffi.Library(lib_filename, {
103
102
  InitWindow: ['void', ['int', 'int', 'string']],
@@ -121,15 +120,16 @@ function main() {
121
120
  let font = r.GetFontDefault();
122
121
 
123
122
  let start = performance.now();
123
+ let iterations = 0;
124
124
 
125
- for (let i = 0; i < iterations; i += 3600) {
125
+ while (performance.now() - start < time) {
126
126
  r.ImageClearBackground(imgp, new Color({ r: 0, g: 0, b: 0, a: 255 }));
127
127
 
128
- for (let j = 0; j < 3600; j++) {
128
+ for (let i = 0; i < 3600; i++) {
129
129
  let text = 'Hello World!';
130
130
  let text_width = r.MeasureTextEx(font, text, 10, 1).x;
131
131
 
132
- let angle = (j * 7) * Math.PI / 180;
132
+ let angle = (i * 7) * Math.PI / 180;
133
133
  let color = new Color({
134
134
  r: 127.5 + 127.5 * Math.sin(angle),
135
135
  g: 127.5 + 127.5 * Math.sin(angle + Math.PI / 2),
@@ -137,14 +137,16 @@ function main() {
137
137
  a: 255
138
138
  });
139
139
  let pos = new Vector2({
140
- x: (img.width / 2 - text_width / 2) + j * 0.1 * Math.cos(angle - Math.PI / 2),
141
- y: (img.height / 2 - 16) + j * 0.1 * Math.sin(angle - Math.PI / 2)
140
+ x: (img.width / 2 - text_width / 2) + i * 0.1 * Math.cos(angle - Math.PI / 2),
141
+ y: (img.height / 2 - 16) + i * 0.1 * Math.sin(angle - Math.PI / 2)
142
142
  });
143
143
 
144
144
  r.ImageDrawTextEx(imgp, font, text, pos, 10, 1, color);
145
145
  }
146
+
147
+ iterations += 3600;
146
148
  }
147
149
 
148
- let time = performance.now() - start;
150
+ time = performance.now() - start;
149
151
  console.log(JSON.stringify({ iterations: iterations, time: Math.round(time) }));
150
152
  }
@@ -18,14 +18,14 @@ const r = require('raylib');
18
18
  main();
19
19
 
20
20
  function main() {
21
- let iterations = 360000;
21
+ let time = 5000;
22
22
 
23
23
  if (process.argv.length >= 3) {
24
- iterations = parseInt(process.argv[2], 10);
25
- if (Number.isNaN(iterations))
24
+ time = parseFloat(process.argv[2]) * 1000;
25
+ if (Number.isNaN(time))
26
26
  throw new Error('Not a valid number');
27
- if (iterations < 1)
28
- throw new Error('Value must be positive');
27
+ if (time < 0)
28
+ throw new Error('Time must be positive');
29
29
  }
30
30
 
31
31
  // We need to call InitWindow before using anything else (such as fonts)
@@ -37,15 +37,16 @@ function main() {
37
37
  let font = r.GetFontDefault();
38
38
 
39
39
  let start = performance.now();
40
+ let iterations = 0;
40
41
 
41
- for (let i = 0; i < iterations; i += 3600) {
42
+ while (performance.now() - start < time) {
42
43
  r.ImageClearBackground(img, { r: 0, g: 0, b: 0, a: 255 });
43
44
 
44
- for (let j = 0; j < 3600; j++) {
45
+ for (let i = 0; i < 3600; i++) {
45
46
  let text = 'Hello World!';
46
47
  let text_width = r.MeasureTextEx(font, text, 10, 1).x;
47
48
 
48
- let angle = (j * 7) * Math.PI / 180;
49
+ let angle = (i * 7) * Math.PI / 180;
49
50
  let color = {
50
51
  r: 127.5 + 127.5 * Math.sin(angle),
51
52
  g: 127.5 + 127.5 * Math.sin(angle + Math.PI / 2),
@@ -53,14 +54,16 @@ function main() {
53
54
  a: 255
54
55
  };
55
56
  let pos = {
56
- x: (img.width / 2 - text_width / 2) + j * 0.1 * Math.cos(angle - Math.PI / 2),
57
- y: (img.height / 2 - 16) + j * 0.1 * Math.sin(angle - Math.PI / 2)
57
+ x: (img.width / 2 - text_width / 2) + i * 0.1 * Math.cos(angle - Math.PI / 2),
58
+ y: (img.height / 2 - 16) + i * 0.1 * Math.sin(angle - Math.PI / 2)
58
59
  };
59
60
 
60
61
  r.ImageDrawTextEx(img, font, text, pos, 10, 1, color);
61
62
  }
63
+
64
+ iterations += 3600;
62
65
  }
63
66
 
64
- let time = performance.now() - start;
67
+ time = performance.now() - start;
65
68
  console.log(JSON.stringify({ iterations: iterations, time: Math.round(time) }));
66
69
  }