I'm trying to stringify an integer in v8.
The nearest I've come to success so far is using String::Concat. I tried writing this method (in a node.js 9.11.1 native addon), but it doesn't compile.
void Method(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
const int num = 42;
args.GetReturnValue().Set(
String::Concat(String::NewFromUtf8(isolate, "The num is: "),
Integer::New(isolate, num)));
}
The compile error is:
'=': cannot convert from 'v8::Integer *' to 'v8::String *volatile '
I haven't been able to figure out the right parts of the v8 API to make use of to format an integer into a string. I'm (perhaps obviously) not familiar with the v8 API, and I'm having trouble finding good examples to learn from.
I was going by the answer to this question: How to convert an Integer to a String in V8? although it appears to be stale compared to the modern v8 API. The example appears to be pre-"isolate" for example.
I was under the impression that Concat would accept this because in JavaScript it just coerces the int to a string (that seems to be the gist of that question I referenced). But I guess maybe I have to be explicit about that when coding with v8?
I'm sure it's something simple I'm missing. I'd appreciate someone suggesting a better way.
Try calling ToString on the Integer you just created.
See the function declaration here: https://chromium.googlesource.com/v8/v8/+/6.5.254.41/include/v8.h#2333
The magic where "JavaScript just coerces the int" must be implemented somewhere -- namely, on the C++ side, where all such conversions are done manually ;-)
Related
I am making a c++ addon for node.js and I am struggling with passing and getting data. I understood how to transform v8::Number to double, double to v8::Number and int to v8:Number, but I need some more. Mainly, v8::String to std::string and back, v8::Number to int and v8::Array to Array and back. Also it would be great to transfer js objects to some c++ variables, but it is less necessary. Does someone know, how to do that?
P.S. I looked over docs and I found nothing about arrays and objects and only this string a (*v8::String::Utf8Value(args[0]->ToString())) according to strings. But it does not work, I get an error error C2660: v8::Value::ToString: function does not get 0 arguments and error C2512: v8::String::Utf8Value: no suitable default constructor. I do not have any more ideas how to implement that. Can someone help?
And also I tried to do something with returning data from c++. In this way args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world").ToLocalChecked()); it works, but if I make like this:
string s = "world";
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, s).ToLocalChecked());
I get an error error C2664: "v8::MaybeLocal<v8::String> v8::String::NewFromUtf8(v8::Isolate *,const char *,v8::NewStringType,int)": cannot convert argument 2 from "std::string" to "const char *"
And I have no idea what is wrong here. Did someone has exprience working with c++ addons and v8 types, How to do that?
For understanding v8::String, the latest version of the docs help: https://v8docs.nodesource.com/node-14.1/d4/d1b/classv8_1_1_string_1_1_utf8_value.html (some signatures have changed a bit since the days of Node 0.8 -- learning how to make sense of the compiler's error messages is highly recommended if you want to develop with C++!).
For understanding std::string, see some C++ documentation, e.g.: https://en.cppreference.com/w/cpp/string/basic_string
If you need an actual example, you can look at V8's samples/process.cc, which has a function ObjectToString that converts any JS object (e.g. a v8::String) to a std::string. It's only two lines!
using b_arr = bool [10];
i have written this code in code::blocks on windows. i have seen many other posts on the same error but do not understand what is wrong in my code.
i actually want to use the resulting user-defined data type for a function to return a boolean array created inside the function. i also do not understand how i will do that(return a boolean array). please help.
i am a novice in programming and coding, so it would be nice have some explanations
Is it possible to specify parameters to an pointer with something called & in C/C++?
Basically let's say I have a function:
int vba (unsigned long *a, unsigned long *b){.....}
Can I declare non-pointer values to the parameters using this mysterious & call?
I am reading this book and the author is using it to do so, but he doesn't declare what
it is, or what it is in general. I googled "& C++ call" and I didn't get any results.
So then he specifies a non ptr assignment using it in anothe function like so:
int vca ()
{
unsigned long c, d;
vba(&c, &d);
//etc ...
}
This is where I am confused, I don't know what just happened here, or what that call does, how did he assign a pointer to a non-pointer like that ... seems quite awesome though I never heard of it before. Anyone mind elaborating on what this is, or how it is possible?
Also when I put this into my code it says in the error log: "Intellisense: identifier "amp is undefined"? This is where I got really lost because the compiler highlights the amp call in blue, so how is it undefined?
That is just HTML encoding for the & character. The website you got this from clearly got something wrong. Hm, I just reread your question and noticed that you're talking about a book. Doesn't really change anything other than make the mistake even more appalling.
Anyway, replace the & with & and you'll be good to go.
I am trying to do a unit test on some c++ code but am running into some trouble.
I have something similar to the following lines of code...
std::string s1 = obj->getName();
std::string s2 = "ExpectedName";
Assert::AreEqual(s1, s2, "Unexpected Object Name");
And I'm getting the following compiler error...
error C2665: 'Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual' :
none of the 15 overloads could convert all the argument types
It seems like it should be a match with the following overload:
AreEqual<(Of <(T>)>)(T, T, String)
Isn't the above overload a template overload that should support any object, as long as arguments 1 and 2 are of the same type? Or am I missing something?
Is there some other way that I can accomplish this Assert?
You're attempting to use the managed unit testing framework with native types – this simply isn't going to work without marshaling the objects into managed types first.
VS2012 now comes with a native C++ unit testing framework; using this framework instead, your code could work by changing "Unexpected Object Name" to a wide string (prefix with L) and calling the following overload:
template<typename T>
static void AreEqual(
const T& expected,
const T& actual,
const wchar_t* message = NULL,
const __LineInfo* pLineInfo = NULL)
If we are trying to stay in un-managed C++, and we don't care what the error message looks like, this is probably a better option than the accepted answer:
Assert::IsTrue(s1==s2)
By better, I mean it is at least easy to read.
I hacked up a bit of a workaround so that integers are compared instead of strings:
Assert::AreEqual(0, s1.compare(s2), "Unexpected Object Name");
In the future, we will likely switch to native C++ unit testing, but in the meantime, this does the trick. Obviously the messaging for this isn't very helpful
Assert.AreEqual failed. Expected:<0>. Actual:<1>. Unexpected Trajectory Name
But it's better than nothing.
I believe solution is to us L prefix before string
Assert::AreEqual<bool>(true, dict->IsBeginWith("", ""), L"Empty");
You can also try case it like this, which give you wrong results, but lead to right direction of understanding of issue
Assert::AreEqual<bool>(true, dict->IsBeginWith("", ""), (wchar_t*)"Empty"); //Empty
Assert::AreEqual(true, dict->IsBeginWith("A", "A"), (wchar_t*)"Empty2");
Assert::AreEqual(true, dict->IsBeginWith("A", "a"), (wchar_t*)""); //CAPITAL LETTER Check
There's a web api which output json format:
{"ret":0}
c++ program could get the value of "ret", and it's a INT type.
but if modify the api, output to this:
{"ret":"0"}
c++ program runs error.
what if the value of "ret" is uncertain type, maybe INT or maybe STRING?
is there a way to process the uncertain type value in c++?
No, C++ is a statically-typed language. It's my opinion that you should code against the datatypes of the API which should not change. It is commonly accepted that if the API chagnges, then the code calling that API has to change as well.
You could just use Regex for different cases.
Like checking if there are two " around your input