Edit: apparently it's not clear, guess I'll make it more concise.
Node application is built, uses a native addon. I need to pass in a Javascript function from this project through node-addon-api into my C++ addon. Then, I need to be able to call that function from C++ multiple times. The issue arose when I found out I am unable to save the reference to the Javascript function due to the napi_env (required for calling the function) being protected from caching.
Could not find any clear answers or examples on the internet regarding how to do this, looking for tips.
Original:
as the title describes, I need to figure out a way to call a JS function multiple times in my addon. Generic use case is that my addon does some long running commands and needs to periodically push a status update back to the javascript.
I thought the best approach would be to have the user pass in a function (which just appends to a text block) for my addon to call (so it can write the updates), this way the javascript side can decide where it gets displayed.
I have experimented to get this working. Found out that my original way of saving the function in a persistent napi_value doesn't work since you cannot save napi_env as well.
I found this thread, which I think is the closest to what I need, but I can't manage to translate the Nan to napi_ so it would work with what I'm using. Callback NodeJS Javascript function from multithreaded C++ addon
Also attempted passing in an EventEmitter, but similar problem as above.
Can anyone give some pointers on if I am heading in the right direction? Perhaps help me dig up a few examples on how to accomplish this?
Your question is not clear. Assuming you are using Javascript in Node, have a look at FFI which allows one to loading and calling dynamic libraries using Javascript.
Alternatively one can just execute a function as follows from the command line:
/usr/bin/node yourjsfunctionfilehere.js
You can also pass command line parameters to the called JS function.
Related
I want to expose a native object or function to javascript so that i can have a callback function in C when the particular function is called in browser.
From this example, it seems it is possible in WebKitGTK1 using the signal "window-object-cleared".
How can i achieve the same in WebKitGTK2, i hope it has something to do with WebKitWebExtension. But i am not sure and also there is not clear guidelines or examples for implementing WebKitWebExtension. Can anyone help me with a solution for my request ?
Yes, WebKitWebExtension is the way to do this. Currently the best documentation on how to write one of those, is this blog post. There is a window-object-cleared signal similar to WebKit1 that you can connect to in your web extension and there use the JavaScriptCore API to define Javascript functions that are visible to your web page.
Good day all.
I would like to count the js functions present on a given page, and then send this number via ajax (the ajax part is the simple part) do you think is it possible to achieve that in javascript? what should be the best way to do it?
thanks in advance.
explanation:
I'm trying to figure out how to counter measure some fraud attempts on some subscription pages, I suspect that some javascript is injected on the page before the user click, so having the number of functions present at the load event, and then the number of those present on the submit event, should lead me in the right direction.
Well, if someone is injecting code to your site, they could just as easily use that code to turn off your code counting functions. You can never trust anything that happens on the client side and must validate everything on the server.
As for the technical side, you'd use a tool like acorn to traverse the syntax tree and find all FunctionDeclaration and FunctionExpressions (and arrows, concise method definitions and methods). That would not find all functions, but it would find all statically created ones.
Once the code started executing it's impossible since it's easily reducable to the halting problem. You don't know if a code will create a function at some point in the future.
[I'm a total newbie to Javascript so take it slow.]
I'm working on an app in node.js which will have a list of plugins. Each "plugin" should be a function (or 2) that can take a string and do something with it. The app should call each plugin in turn and pass it the string. Depending on the result it might call another function in that plugin etc...
What's the best way to design this in Javascript? I don't mind having to modify the app to add every plugin as its developed but would rather avoid having to do this a lot.
Right now I'm just thinking created a module for every module then "require" it within the primary app and call the function but that seems cludgy for a few reasons (a) I need to change the parent app quiet a bit for every new plugin, (b) there's no interface I can enforce on the plugins. I was kind of hoping there was some sort of contract I could force the plugins to respect.
You can list the plugin directory, and require each file. If each file adds a function to an existing object (say, myapp.plugins), you can just forEach the object and invoke each function. You can't really enforce a contract in the code without doing weird things like invoking an AST parser; you're better off doing unit testing on plugins to make sure they work in isolation.
EDIT:
Can you elaborate a bit on this part: "If each file adds a function to an existing object (say, myapp.plugins), you can just forEach the object and invoke each function."?
var invokePlugins = function() {
var args = arguments.slice(0);
myapp.plugins.forEach(function(plugin) {
plugin.apply(plugin, args);
});
}
invokePlugins("this", "gets", "passed to", "each plugin");
Your architecture is unclear, so this is just a very rough idea of what you could do.
I am in the middle of a project which basically allows other users to install "add-ons" for my script. In general it will be something like the following:
function widget () {
this.addControl = function (type,control) {
control.call({ Object With Some Un-thoughtout Vars });
}
}
Very basic example with pretty much everything removed
The users who will be using this trust myself fully. Now I want to make sure any scripts other people make will be ran with limited permissions. So they can't hijack sessions or redirect the user or upright steal their passwords/keystrokes.
I looked at a few results on Google and they basically suggest that you shouldn't really do this as its more trouble than its worth. But in my opinion I'd much rather limiting it than someone installing a widget that takes their information (which they more than likely will). So does anyone know of a way of breaking down the callback function into individual lines of code so I can check everything's fine before allowing it to run?
Or will I have to go down some horrid string then eval method?
I've got a question about data flow that is summarized best by the image below:
I've got the data path from the UI (WaveMaker) down to the hardware working perfectly. The question I have is whether I'm missing something in the connection from the Java Service to Wavemaker.
I'm trying to provide information back to Wavemaker from the HW. The specifics of shared memory and semaphore signaling are worked out already. Where I'm running into a problem is how to get the data from the Java Service back to WaveMaker, when it hasn't specifically requested it. My plan was to generate events when the Java Service returned, but another engineer here insists that it won't work, since there's no direct call from Wavemaker and we don't want to poll.
What I proposed was to call the function after the page loaded, allow the blocking to occur at the .so level, as shown below, and then handle the return string when the call returned. We would then call the function again. That has the serious flaw of blocking out interaction with the user interface.
Another option put forth would be to use a hidden control, somehow pass it into Java, and invoke an event on it from Java, which could then be made to execute a script to update the UI with the HW response. That keeps the option of using threads alive, and possibly resolves the issue. Is there some more elementary way of getting information from Java->JavaScript->UI without it having been asked for?