How to run a whole js files multiple times using duk_pcall? - duktape

I want to use duk_pcall to do it, here is what I tried:
Firstly, I load the script file into a char* string.
Secondly, use duk_pcompile_string(ctx, 0, programBody) to compile it.
Then, I can use duk_pcall(ctx, 0) to call it once, and call duk_pop(ctx).
But when I try to use duk_pcall a second time, I failed.
Can you give me some advice about how to do it the right way?

duk_pcompile_string places the result on the TOS and duk_pcall executes the TOS (+ eventual parameters) and replaces all of that by the return value of the call.
In order to make the function callable multiple times you have to duplicate the TOS insert the required parameter and then call that using the pcall. After the call remove the result from the stack. The original function should now be at the TOS again. Start over with what is written in this paragraph, to call it again.

Related

Shouldn't "toHaveBeenNthCalledWith" test only one instance of the mocked function

From my understanding, toHaveBeenNthCalledWith within a jest test should test only the "Nth" call with "N" being a non-zero number. My test below returns the Nth call and the one after it. In the test, I give N a value of one and it is showing two. Now I can replaced the one with a two and then it will show 2 and 3. This is causing my test to fail. Are there any suggestions to why it is behaving like this??
Probably your mock is saving state between tests, this means that it "holds" previous calls, therefore, you see 4 calls.
Try to use new mock for each test, usually done by configuring the mock in a beforeEach hook.
Edit
You can use .toHaveBeenNthCalledWith which specifies which call is under test.
My overall problem was whitespace. Plus I had to change the way the underlining executeQuery function was being called in the actual controller. So the expect statements in the third image are actually correct. I copied the sql statement from the controller and pasted in the test. I did NOT add tabs and it worked.

Pandas applymap loops twice, apply once?

Want to only use the first email if multiple are added. Made a function which looks for ',', if it finds it, displays a message and returns the first email.
For a strange reason, it seems to loop through the dataframe twice when using 'applymap', because it prints the message twice.
When I use the 'apply' function on the series, it -as expected-prints out once. Any idea why is this discrepancy?
From the documentation, version 0.25.0, I quote :
Notes
In the current implementation applymap calls func twice on the first column/row to decide whether it can take a fast or slow code path. This can lead to unexpected behavior if func has side-effects, as they will take effect twice for the first column/row.

Added an extra variable to a VBA function - now has compile error stating Expected:=

I have a VBA function that worked fine, until I tried to pass an extra variable to it. Now the code won't run, and I get an error stating Expected:=, I've tried renaming the function, but no help.
Was - Function GetData(site_add)
Changed to Function GetData(site_add, temporary) and failed - despite changing the call to the function accordingly...!?!
Is it possible that the compiler is glitching and I should focus on that? I have other functions in the code that use 5 call 5 variables and don't even call/use them all...!? Help...
By adding the second parameter, you are effectively telling the compiler that every call to this method now requires two parameters instead of one. So you have to find everywhere you call the GetData() function and make sure it now passes two parameters instead of one, even if the second parameter is Nothing. Now, if you want it to default to nothing so you don't need to pass it you can rewrite it as
GetData(site_add, Optional temporary)
*my vb is rusty, so take my example with a grain of salt please.

Executing functions stored in a string

Lets say that there is a function in my Delphi app:
MsgBox
and there is a string which has MsgBox in it.
I know what most of you are going to say is that its possible, but I think it is possible because I opened the compiled exe(compiled using delphi XE2) using a Resource Editor, and that resource editor was built for Delphi. In that, I could see most of the code I wrote, as I wrote it. So since the variables names, function names etc aren't changed during compile, there should a way to execute the functions from a string, but how? Any help will be appreciated.
EDIT:
What I want to do is to create a simple interpreter/scripting engine. And this is how its supposed to work:
There are two files, scr.txt and arg.txt
scr.txt contains:
msg_show
0
arg.txt contains:
"Message"
And now let me explain what that 0 is:
First, scr.txt's first line is function name
second line tells that at which line its arguments are in the arg.txt, i.e 0 tells that "Message" is the argument for msg_show.
I hope my question is now clear.
I want to make a simple scripting engine.
In order to execute arbitrary code stored as text, you need a compiler or an interpreter. Either you need to write one yourself, or embed one that already exists. Realistically, the latter option is your best option. There are a number available but in my view it's hard to look past dwscript.
I think I've already solved my problem! The answer is in this question's first answer.
EDIT:
But with that, as for a workaround of the problem mentioned in first comment, I have a very easy solution.
You don't need to pass all the arguments/parameters to it. Just take my example:
You have two files, as mentioned in the question. Now you need to execute the files. It is as simple as that:
read the first line of scr.txt
check if it's a function. If not, skip the line
If yes, read the next line which tells the index where it's arguments are in arg.txt
pass on the index(an integer) to the "Call" function.
Now to the function which has to be executed, it should know how many arguments it needs. i.e 2
Lets say that the function is "Sum(a,b : integer)".It needs 2 arguments
Now let the function read the two arguments from arg.txt.
And its done!
I hope it will help you all.
And I can get some rep :)

Get the end address of Linux kernel function on run-time

I am trying to get the boundary for a kernel function (system calls for example). Now, if I understand correctly, I can get the start address of the interested function by reading /proc/kallsyms or System.map but I dont know how to get the end address of this function.
As you may know, /proc/kallsyms allow us to view the symbol table for Linux kernel so we can see the start address of all exported symbols. Can we use the start address of the next function to calculate the end address of the previous function? If we cannot do like this, could you suggest me another ways?
Generally, executables store only the start address of a function, as it is all that is required to call the function. You will have to infer the end address, rather than simply looking it up.
You could try to find the start address of the subsequent function, but that wouldn't always work either. Imagine the following:
void func_a() {
// do something
}
static void helper_function() {
// do something else
}
void func_b() {
// ...
helper_function();
// ...
}
You could get the address of func_a and func_b, but helper_function would not show up, because nothing needs to link to it. If you tried to use func_b as the end of func_a (assuming that the order in the compiled code in equivalent to the order in the source code, which is not guaranteed), you would end up accidentally including code that you didn't need to include - and might not find code that you need to find when inlining other functions into func_b.
So, how do we find this information? Well, if you think about it - the information does exist - all of the paths within func_a will eventually terminate (in a loop, return statement, tail call, etc), probably before helper_function begins.
You would need to parse out the code of func_a and build up a map of all of the possible code paths within it. Of course, you would need to do this anyway to inline other functions into it - so it shouldn't be too much harder to simply not care about the end address of the function.
One final note: in this example, you would have trouble finding helper_function in order to know to inline it, because the symbol wouldn't show up in kallsyms. The solution here is that you can track the call instructions in individual functions to determine what hidden functions exist that you didn't know about otherwise.
TL;DR: You can only find the end address by parsing the compiled code. You have to parse this anyway, so just do it once.

Resources