How to call C functions from a .js file - javascript

I have a website(client) with a bunch of buttons, the site sends the value of those controls to a .js file(sever) through a websocket. Both are connected through the same port I'm using node.js and ws.
How do I call a C function from the .js file in order to perform server side calculations? Is that even possible?

The key word here is “Foreign Function Interface”, or FFI for short. It turns out that there's a package on NPM called ffi that would probably do what you want.

V8 (which node.js uses) has functionality that may help.
https://code.google.com/p/v8-juice/wiki/BindingFunctions

Related

How do I use node.js on a localhost:8000 server?

I have some pure JS code and HTML and CSS on my localhost:8000 server. I have installed node.js and I am able to use it without problem testing it on the VS code terminal. However, I want to integrate the file reading mechanic of node.js into the code that I am running on the localhost:8000 server. I want to put this node.js code into my webpage on localhost:8000
const {readFileSync, promises: fsPromises} = require('fs');
function syncReadFile(filename) {
const contents = readFileSync(filename, 'utf-8');
const arr = contents.split(/\r?\n/);
console.log(arr); // 👉️ ['One', 'Two', 'Three', 'Four']
return arr;
}
syncReadFile('./locations.txt');
I have tried copy and pasting it into the js file for the webpage, however when I run it, the console says
Uncaught ReferenceError: require is not defined
at window.onload (index.js:23:46)
index.js:23:46 is the line where const {readFileSync, promises: fsPromises} = require('fs'); is.
How do I fix this?
I think you need a better understanding of how NodeJS works:
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
A JavaScript runtime is (in this context) a program that runs in a machine, with tools to interact with that machine. In development, the context of that machine is the context of a Server.
Now, JavaScript found it's use in the HTML script tag, that's the most "vanilla" way to execute JS in it's home, what it's most used for. In this context, JavaScript is running as a Client.
What makes NodeJS, different from executing the same code in an HTML file you can already execute without installing NodeJS?
It's as vanilla as you can get after you "force" JavaScript to be executed in the Backend, but for it to be used as a webserver, there are some tools that need to be ported, converted or even created, some of these tools, like the File System (fs) are specific to NodeJS.
That's it! TLDR is that your code won't work because it's being executed in the wrong place. You can fix that in many ways, like this one, but maybe you can find a better path understanding how NodeJS works
Browsers cannot access file systems like that. Nodefs will not work within the browser
The NodeJS modules only works on the server side, you can't use it client side (that also include the require() syntax).
If you want to read a file from the server, you might want to use an AJAX directed to the route that has a controller to read the targeted file, which then the result of the AJAX is handled client side with vanilla JS.

Photoshop 8800 Error only after python creates the file

I have a bit of a weird situation that I will try to explain the best I can.
I am using Python to launch photoshop and run a javascript file. But my goal is for python to generate the javascript first, then run it in photoshop.
In order to do that I have python copying the javascript file, then replacing a single line of code and running this new copy.
When I run the original javascript file it works as intended with no problems.
When I run the copied javascript file it works as intended with no problems.
When I run the copied javascript file that has the line replaced, it gives me an 8800 error.
At this point, even if I manually type the replaced line to match the original javascript file. I will still get an 8800 error.
Does python somehow write files differently?
Here is the code I am using to replace the copy and replace the javascript contents:
from shutil import copyfile
jsx_file = r'E:\PS\_javascript_constructor_template.jsx'
jsx_file_new = r'E:\PS\_javascript_constructor_template_new.jsx'
copyfile(jsx_file, jsx_file_new)
with open(jsx_file_new, "r") as fin:
data = fin.read()
with open(jsx_file_new, "w") as fout:
fout.write(data.replace("!REPLACEME!",'"E:\PS\MockVar.csv"'))
Any ideas?
SIDE NOTE: I am only doing this because I have no idea how to pass an argument from python into the javascript file I am subprocess calling.
I would much rather send an argument to the javascript file than build new files enitrely.
If you'd rather send an argument to the Photoshop script, I'd recommend using interprocess communication. You can use the socket module in Python and the Socket object in Extendscript to send messages back and forth. Check out External Communications Tools in the Adobe Tools Guide for more information.

Passing Value from C++ to Javascript

i have a c++ file which reads values from a sensor and I want to display those values on a website dynamically. So Im looking for a way to pass these values(integers) from my cpp file to an javascript which displays them on the site.
My first, simple try was to write the values into a js file as variables every second from my cpp script. The Js then uses this file as a source and displays its variables on the site:
cpp:
fprintf(file, "var mx=%d, my=%d, mz=%d, ax=%d, ay=%d, az=%d, gx=%d, gy=%d, gz=%d;\n",
imu.raw_m[0], imu.raw_m[1], imu.raw_m[2], // M = Magnetometer
imu.raw_a[0], imu.raw_a[1], imu.raw_a[2], // A = Accelerometer
imu.raw_g[0], imu.raw_g[1], imu.raw_g[2] // G = Gyroscope
);
html/js:
<script src="./imu.js" type="text/javascript"></script>
The Problem now is of course, that I need to refresh the page all the time, because the imu.js file is cached by the website.
I'd rather have a way to directly pass to integers from the cpp file to the js script. I read something about json or Googles V8 script. But I'd like to hear your suggestions first.
By the way, Im running this on a raspi, if this is important.
Thanks for your help
EDIT:
I'm goning to try it with a mysql database, in which my cpp file writes the data from the sensor with Connector/c++ from http://dev.mysql.com/doc/connector-cpp/en/ and my website reads them.
You could compile your C++ code into a Node.js plugin, you can then register a JavaScript function with your plugin which the C++ calls when it updates the value. That way you can pass values directly from C++ into Javascript in a managed and controlled way.
Node.js has the added benefit of being able to host your webpage and do all the Websocket and HTTP stuff that can be a pain in C++.
You do not have to refresh if your script is smart about how to access the data file! In case you do have a webserver at hand: Take care that your data file is accessible by your webserver and then let your script request the file via ajax (link to w3schools)
I'm doing something similar on a BeagleBone Black. With websocketd you can turn pretty much any program into a websocket endpoint and then send data via stdin and stdout commands. This would be a particularly good solution for you since websockets are designed to handle information that's constantly changing.

node.js how to use a minified library used in client side

Basically, I am encoding a wav file using libopus.js on the client side, now on the server side(node.js) I have to decode it using the same library, this is where I am getting lost, not sure how adapt the libopus.js for the server, to make things easier, it is present in minified form.
so what are the steps I have to follow to port a client side library to node.js.
I can't comment due to low reputation, the usual porting to node js usually means exposing the variable via exports instead of on the window, for example for the Opus which is defined in https://github.com/kazuki/opus.js-sample/blob/master/opus.js
you'd do exports.opus = Opus; and then in node you'd have:
var opus = require('./libopus').Opus and you will have that object.
However, from a fast glance it seems that it works with buffers etc which I am not sure how they port from browser to node js but give it a try I guess.

Count number of files in a folder through javascript

I have folder in which there are no. of html files. Can i count the no. of files using javascript? Please help
Thanks
No, this is not possible using JavaScript only.
You can make an Ajax call to a page and from the server side code you can find the number of files in a folder and then return the result to the callback function.
I assume the files are in not in the client machine.
Javascript is a client side language. This means that it has no interaction at all with the server. It get sends unprocessed by the server to the client and the client executes it.
However, what you could do is learn AJAX - if you don't know it yet - and create a script (PHP, ASP, Perl, SSI, etc.) that counts the files in the directory and prints the number - I recommend Perl - .
Each minute, for example, the AJAX page would fetch the response of the script and display it.

Categories

Resources