Created
November 26, 2025 05:19
-
-
Save kjunichi/a307ac75ba442dd3e104fbec3b68716e to your computer and use it in GitHub Desktop.
npm install @napi-ffi/napi-ffiでいれたモジュール内のexamleを動かす修正
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This example creates an ffi.Callback from the "Math.abs()" JavaScript function | |
| * then creates a ffi.ForeignFunction from that callback function pointer. | |
| * | |
| * The result is basically the same as calling "Math.abs()" directly, haha! | |
| * | |
| * This example should basically just run forever, in an endless loop. | |
| * This file is a "pummel test" of sorts for: | |
| * https://github.com/rbranson/node-ffi/issues/74 | |
| * | |
| * We should run this file periodically for a period of ~10 minutes to make sure | |
| * it doesn't crash ever. | |
| */ | |
| var ref = require('@napi-ffi/ref-napi') | |
| , ffi = require('../') | |
| , assert = require('assert') | |
| var funcPtr = ffi.Callback('int', [ 'int' ], Math.abs) | |
| var func = ffi.ForeignFunction(funcPtr, 'int', [ 'int' ]) | |
| function loop () { | |
| for (var i = 0; i < 100; i++) { | |
| assert.equal(Math.abs(-i), func(-i)) | |
| } | |
| (typeof setImmediate != 'undefined' ? setImmediate : process.nextTick)(loop) | |
| } | |
| loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment