Skip to content

Commit

Permalink
[web] Switch from ffi-napi to koffi
Browse files Browse the repository at this point in the history
  • Loading branch information
sidi762 committed Nov 6, 2024
1 parent 44e7077 commit d3d204a
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions nasal-web-app/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const express = require('express');
const ffi = require('ffi-napi');
const path = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const koffi = require('koffi');

// Parse command line arguments
const argv = yargs(hideBin(process.argv))
Expand Down Expand Up @@ -33,12 +33,23 @@ const app = express();
app.use(express.json());
app.use(express.static('public'));

const nasalLib = ffi.Library(path.join(__dirname, '../module/libnasal-web'), {
'nasal_init': ['pointer', []],
'nasal_cleanup': ['void', ['pointer']],
'nasal_eval': ['string', ['pointer', 'string', 'int']],
'nasal_get_error': ['string', ['pointer']]
});
let nasalLib;
try {
// First load the library
const lib = koffi.load(path.join(__dirname, '../module/libnasal-web.dylib'));

// Then declare the functions explicitly
nasalLib = {
nasal_init: lib.func('nasal_init', 'void*', []),
nasal_cleanup: lib.func('nasal_cleanup', 'void', ['void*']),
nasal_eval: lib.func('nasal_eval', 'const char*', ['void*', 'const char*', 'int']),
nasal_get_error: lib.func('nasal_get_error', 'const char*', ['void*'])
};

} catch (err) {
console.error('Failed to load nasal library:', err);
process.exit(1);
}

app.post('/eval', (req, res) => {
const { code, showTime = false } = req.body;
Expand Down

0 comments on commit d3d204a

Please sign in to comment.