Where is namespace `i` defined - v8

I am studying the source of the V8 engine. There is a namespace named i, which I guess should be an alias of v8::internal, but I could not find where it (or the alias) is defined.
I have grepped the whole source tree using namespace i, or using i =, but I could not find anything.
As shown in the following code snippet:
if (!context.IsEmpty()) {
i::Handle<i::Context> env = Utils::OpenHandle(*context);
i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
......
as shown https://github.com/v8/v8/blob/master/src/api/api.cc#L277
Could anyone who is familiar with the code base tell me where it is defined?

It can be found at the end of globals.h:
namespace i = v8::internal;

Related

Change prefix to per-function sections generated by -ffunction-sections

If I have a function foo() and use -ffunction-sections, gcc will place foo() inside its own .text.foo section. Is it possible to change the prefix of .text? Such that I get .customName.foo instead of text.foo.
I have the same problem, and I solved it using the section attribute.
The following solution is the simplest but does not create a section for each function (as the -ffunction-section parameter allows to do)
#define AT_FLASH_TEXT_SECTION(var) \
__attribute__((section(".text.flash"))) var
AT_FLASH_TEXT_SECTION(int myFunction(float param1, long param2));
So the function myFunction will appear in the section .text.flash, but also all other functions that are declared using the macro AT_FLASH_TEXT_SECTION.
To get the desired behavior I modified the macro as follows:
#define AT_FLASH_TEXT_SECTION_SYM(var, subsectionName) \
__attribute__((section(".text.flash." #subsectionName))) var
AT_FLASH_TEXT_SECTION_SYM(int myNewFunction(float param1, long param2), myNewFunction);
This is the best solution I've found so far.
Unfortunately, it is error-prone: the function name must be repeated identically in the subsectionName parameter of the AT_FLASH_TEXT_SECTION_SYM macro.
Furthermore, if two c modules contain two static functions with the same name, they will be emitted in the same section, going back to the previous problem.
I hope this helps, and maybe you can find a better solution starting from this.
No, that does not seem possible. See gcc/varasm.c (I haven't run the debugger, but am fairly sure this is the code that computes section names.)
void
default_unique_section (tree decl, int reloc)
{
[...]
switch (categorize_decl_for_section (decl, reloc))
{
case SECCAT_TEXT:
prefix = one_only ? ".t" : ".text";
break;
[...]
name = IDENTIFIER_POINTER (id);
name = targetm.strip_name_encoding (name);
[...]
string = ACONCAT ((linkonce, prefix, ".", name, NULL));
set_decl_section_name (decl, string);
}
Besides, that might be a bad idea, as e.g. linker scripts treat sections based on their names (see ld --verbose). Something like .text.customprefix.foo might be a better choice, but I don't know why you want custom prefices.
As a workaround, you can assign sections manually with the section attribute.
'section ("SECTION-NAME")'
Normally, the compiler places the code it generates in the 'text'
section. Sometimes, however, you need additional sections, or you
need certain particular functions to appear in special sections.
The 'section' attribute specifies that a function lives in a
particular section. For example, the declaration:
extern void foobar (void) __attribute__ ((section ("bar")));
puts the function 'foobar' in the 'bar' section.

Code explanation of the json11 library about implicit constructor

I'm reading the source code of the main json11 header file.
It contains the following declaration:
template <class T, class = decltype(&T::to_json)>
Json(const T & t) : Json(t.to_json()) {}
I'm trying to find some documentation about this usage of decltype and class inside a template declaration but no success.
Does this construction/usage has a name in C++? Any good reference about it?
It's using SFINAE ("Substitution Failure Is Not An Error"), a common technique for advanced template stuff. In this case, it's used as a crude(1) test whether the type T has a function named to_json.
How it works: if the expression T::to_json is well-formed (there is something named to_json inside the type T), decltype(T::to_json) denotes a valid type and the constructor template can be used normally.
However, if T::to_json is ill-formed (i.e. if there is no to_json member inside T), it means substituting the template argument for T has failed. Per SFINAE, this is not an error of the entire program; it just means that the template is removed from further consideration (as if it was never part of the class).
The effect is thus that if type T has a member to_json, you can use an object of type T to initialise a Json object. If there is no such member in T, the constructor will not exist.
(1) I'm saying crude test because this only checks that T has such a member. It doesn't check that the member is a function which can be invoked without arguments and returns something another constructor of Json can accept. A tighter-fitting test might look something like this:
template <class T, class = std::enable_if_t<std::is_constructible<Json, decltype(std::declval<const T>().to_json())>::value>>
Json(const T & t) : Json(t.to_json()) {}
[Live example]

OMNET++ how to access function or variables in another class

I have modified inet NodeStatus.cc with a customized function that return the variable value as follows:
int NodeStatus::getValueA()
{
return ValueA;
}
Then, I created another simple module called simpleNodeB.cc and I wanted to retrieve ValueA from NodeStatus.cc. I tried the following code in simpleNodeB.cc but didn't work:
if(getParentModule()->getSubModule(NodeStatus).getValueA()==test1)
bubble("Value is the same");
The error message I got -> error: expected expected primary-expression before ')' token. I'm not sure if I used the correct way to call getValueA() function. Please enlighten me. thanks a lot.
There are many errors in your code.
The method getSubmodule requires a name of module, not a name of class. Look at your NED file and check the actual name of this module.
getSubmodule returns a pointer to the cModule object. It has to be manually cast into another class.
Assuming that an NodeStatus module in your NED is named fooStatus the correct code should look like:
cModule *mod = getParentModule()->getSubmodule("fooStatus");
NodeStatus *status = check_and_cast<NodeStatus*>(mod);
if(status->getValueA() == test1)
bubble("Value is the same");
Reference: OMNeT++ Manual.

Does RStudio auto generated "Package with Rcpp" allow Rcpp modules?

I was trying to expose my C++ class through Rcpp by building a package. It seems that module is not allowed in RStudio auto-generated template. For example, if we compare the NAMESPACE file generated by Rcpp.package.skeleton(myPackage, module=TRUE) and by RStudio, importFrom(Rcpp, loadModule) is not in the RStudio NAMESPACE file. Did I miss something? How can I enable RStudio to generate module allowed package template?
Here is a minimal example to show my C++ codes in case someone would like to try it in RStudio
class Student{
private:
double age;
double GPA;
public:
Student(double age_, double GPA_):age(age_),GPA(GPA_){}
double sum(double x, double myGPA){
GPA = myGPA;
return GPA + x;
}
double times(double x, double myage){
age = myage;
return age*GPA*x;
}
};
RCPP_MODULE(my_module){
class_<Student>("Student")
.constructor<double, double>()
.method("sum", &Student::sum)
.method("times",&Student::times);
}
In very, very short: No. Or maybe "sort of; not completely".
A bit more expanded: The auto-package stub creates a working basic Rcpp package. But eg not an RcppArmadillo package (and this has bitten people before).
And we do include a complete example for Rcpp Modules in the package in this directory (which is used by the unit tests) so you may have to do a few steps by hand if you go that route.
You can also try the helper function Rcpp.package.skeleton() with the option module=TRUE which the other formal option.
So in sum you cannot reasonably expect the RStudio GUI to support every available permutation.
Simply put, RStudio is limited in generating Rcpp project types for this version (1.0). Per a PR, custom package templates are incoming!
To get around this limitation, try the following:
Close all open projects.
Run the following: Rcpp.package.skeleton("myPackage", module=TRUE)
New Project -> From Existing Directory or use devtools::use_rstudio() to generate the .Rproj.
Edit 1
Per the comment, what really is happening is two fold:
First, the module definition needs to be updated to be:
#include <Rcpp.h>
class Student{
private:
double age;
double GPA;
public:
Student(double age_, double GPA_):age(age_),GPA(GPA_){}
double sum(double x, double myGPA){
GPA = myGPA;
return GPA + x;
}
double times(double x, double myage){
age = myage;
return age*GPA*x;
}
};
RCPP_MODULE(my_module){
using namespace Rcpp ; // Added (if not done globally)
class_<Student>("Student")
.constructor<double, double>()
.method("sum", &Student::sum, "Sum") // Add some documentation (optional)
.method("times",&Student::times, "Times");
}
Next, in some R code file add:
loadModule("my_module", TRUE)
Build and Reload
Then, you have:
s <- new( Student, 1, 2 )
s$sum(2,4)
# [1] 6
s$times(5,6)
# [1] 120

Unit testing with generics

I have something like:
class BackupList : List<Backup> {}
and the tests that VS2010 has generated for me look somewhat like:
[TestMethod()]
[DeploymentItem("[...].exe")]
public void AddBackupNormal()
{
SqlServer_Accessor.BackupList ls = new SqlServer_Accessor.BackupList("", "");
SqlServer_Accessor.Backup bk = new SqlServer_Accessor.Backup();
ls.Add(bk);
Assert.IsTrue(ls.Count == 0); // won't compile
List<SqlServer_Accessor.Backup> x = new List<SqlServer_Accessor.Backup>();
Assert.IsTrue(x.Count == 1); // compiles fine
}
however, in the above, the .Count reference fails to compile with:
Error 1 'xxx.SqlServer_Accessor.BackupList'
does not contain a definition for 'Count' and no extension method
'Count' accepting a first argument of type
'xxx.SqlServer_Accessor.BackupList'
could be found (are you missing a using directive or an assembly
reference?) C:[...]Tests\SqlServer_BackupListTest.cs
interestingly, a reference to the original type seems to contain a .Count property as I would expect... so the issue is that the _accessor seems to be casting to something other than a List<>.
how does one approach this?
TIA - e!
My first guess would be that your test class needs a reference...
using System.Collections.Generic;

Resources