javascript arguments are messed up when passed to NPAPI plugin function - plugins

I am using a simple NPAPI example from https://github.com/mikma/npsimple.
When I try to pass arguments from javascript to the NPAPI invoke function, the
parameters recieved by the NPAPI function are garbage, though argument count is
passed correctly. The following is the definition of the function in which I am trying to print the "args" array after converting them to char*:
invoke(NPObject* obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
Am I missing something here?

It is really hard to tell what you're trying to do based on what you have given us. Specifically, as smorgan requested, we need to know how you are trying to convert the args array to char*.
You are aware of how the NPVariant works? If it's a string, the NPVariant type will be NPVariantType_String and you will need to use both the UTF8Characters member of the NPString struct (which in turn is part of the NPVariant union) and the UTF8Length member, since the string may or may not be null terminated.
Also, keep in mind that depending on what you put in, it may or may not be valid to make your NPVariant a char*. If that helps, great; if it doesn't, please post the contents of the function in which you are trying to handle the input as well as the specific javascript calls that you are making. You haven't given us enough to work with to give you more than guesses as to what problem you may be having.

Related

XMLQUERY() WITHIN XMLATTRIBUTES()

I am doing some basic tasks using, sql/xml. I am currently working on an error message that I get when trying to compute a XMLQUERY() within a XMLATTRIBUTES() function. (See code below)
SELECT XMLELEMENT(NAME "Nodename",
XMLATTRIBUTES(XMLQUERY('$t//Element/text()' PASSING Info AS "t") AS "hello"))
FROM Kurs
The error message that I get, says that there is no qualified routine that can run the function. I cant copy-paste the error message because its in Swedish, but this should be enough.
Also this might help: SQLCODE=-440, SQLSTATE=42884, DRIVER=4.18.60
So my question is (I have been looking for the answer), why doesn't this work? I will always get a value from that XMLQUERY, and it should simply translate into a value and used by XMLATTRIBUTES()
Any documentation, or link, is welcomed as well!
Thank you in advance!
The scalar function XMLQUERY returns an XML value. The function XMLATTRIBUTES expects an expression that returns a value of any type, but XML and some other types.
Thus, the functions are not compatible the way you are using them. DB2 cannot find a routine with that function signature. It results in that error -440.
How about wrapping a CAST/XMLCAST around it...?

Set Matlab function parameter as uint8

Is it possible in Matlab to say what the function expects? something like this:
function functionA( obj, uint8(param) )
Here I am saying that the function expects one parameter of type uint8.
Not on the function signature. Typically, you do this via an assert block:
function (obj, param)
assert(isa(param, 'uint8'),...
[mfilename ':invalid_datatype'],...
'Parameter ''param'' must be of class ''uint8''; received ''%s''.',...
class(param));
To complement Rody's answer, there are four ways that you can do this:
Use a conditional and raise an exception if the argument is not of the expected type. The problem with this method is that you have to write a lot of code.
Use an assertion. See Rody's answer or here. One can argue that this is not what assertions are supposed to be used for, but you can certainly use them this way.
Use the validateattributesfunction. See here. This is a very good balance between simplicity and utility. It allows you to check for a number of properties in an argument (and generally, any variable at any part of code)
Use the inputParser class. See here. This is the most powerful method of parsing inputs, but may be overkill. Also, the cost of creating an inputParser object means that it may not be a good idea for functions that are called repeatedly. Nevertheless, it's very good for the public API.

What is the difference between ByRef and Output method argument modifiers?

All is in the subject, really.
I fail to see what the difference in behavior is between those two methods for x:
// first version
Method m(ByRef x As whatever)
{
// play with x
}
// second version
Method m(Output x As whatever)
{
// play with x
}
There must be some reason why both those modifiers exist, however my "mastery" (uhm) of the language is not enough to understand the difference. I have tried and read the documentation, search it etc, to no avail so far.
So, what is the difference between those two argument modifiers?
Well those are just "prettifiers", they don't do much in terms of actual language behaviour, and only used to provide documentation. Idea is that arguments documented as ByRef provide both input and output, for example you can pass an array to be sorted, and Output arguments only provide output, for example list of errors. Output modifier was introduced later, and a lot of system code still use ByRef for both use cases.
If argument is actually passed by reference is only determined by method caller, and keyword doesn't really matter. You will call your method as ..m(.parameter) to pass variable by reference, and ..m(parameter) to pass variable by value.

Can we say that using "pass by reference" is always better than "pass by value"?

In C# or php or other languages, there are 2 ways to pass a value to a function, pass it by value and pass it by referece.
Pass parameter by value make the value copied in the function, so this need a extra memory space although the memory space will be reclaimed after running outside the function.
But passing parameter by reference no need to copy a value, it's save the memory. From this perspective, can we say that using "pass by reference" is always better than "pass by value"?
Pass by reference and pass by value are semantically different and sometimes one is correct approach and sometimes the other one is. In many cases the task at hand already prescribes which approach is needed and in contexts where only one option is supported you often need to manually work around it (e.g., if you need a copy in Java you'll need to clone() the object).
In the context of generic functions the answer is rather the opposite way of your proposed preference: pass arguments of deduced type by value! The reason is that you can use something like std::ref() to obtain reference semantics but there is no way to get value semantics if the functions use reference semantics.
No.
There are tons of cases where you'd want to pass by value.
An example might be when you need both const Type& and Type&& overloads. Passing by value just handles both cases without having to duplicate any code:
void function(Object o) { do_something_with(std::move(o)); }
As opposed to:
void function(Object&& o) { do_something_with(std::move(o)); }
void function(const Object& o) { do_something_with(Object(o)); }
Of course there is much more to the subject, but since you're only asking for "is it always better?" I feel a single disproving example is enough. ;)
Edit: the question was originally tagged c++ hence my very specific answer.
Another, more language-agnostic example would be when you need to make a copy of your parameter because you don't want to modify the original object:
void function(int& val) { int v2 = val; modify(v2); use(v2); }
// vs
void function(int val) { modify(val); use(val); }
You get the idea...
Pass by reference requires copying a reference to the object. If that reference is comparable in cost to the object itself, then the benefit is illusory. Also, sometimes you need a copy of the object, and passing by value provides you one.
Also, there's a key error in the reasoning in the question. If passing by value, and there is no need to copy the value, nothing requires that the value actually be copied. Most languages have an "as-if" rule that states that the program only has to act as if the compiler did what you ask for. So if the copy can be avoided, the compiler is free to avoid it. If the copy can't be avoided, then you needed the copy.

Getting UTF-8 Request Parameter Strings in mod_perl2

I'm using mod_perl2 for a website and use CGI::Apache2::Wrapper to get the request parameters for the page (e.g. post data). I've noticed that the string the $req->param("parameter") function returns is not UTF-8. If I use the string as-is I can end up with garbled results, so I need to decode it using Encode::decode_utf8(). Is there anyway to either get the parameters already decoded into UTF-8 strings or loop through the parameters and safely decode them?
To get the parameters already decoded, we would need to override the behaviour of the underlying class Apache2::Request from libapreq2, thus losing its XS speed advantage. But that is not even straightforward possible, as unfortunately we are sabotaged by the CGI::Apache2::Wrapper constructor:
unless (defined $r and ref($r) and ref($r) eq 'Apache2::RequestRec') {
This is wrong OO programming, it should say
… $r->isa('Apache2::RequestRec')
or perhaps forego class names altogether and just test for behaviour (… $r->can('param')).
I say, with those obstacles, it's not worth it. I recommend to keep your existing solution that decodes parameters explicitly. It's clear enough.
To loop over the request parameters, simply do not pass an argument to the param method and you get a list of the names. This is documented (1, 2), please read more carefully.