Using Lua from C++, Fails to open standard libs with Nil error - c++

I've been writing Lua integration into a game project, and frustratingly, every time I try and use lua_pcall, I get the error "attempting to call nil value."
When I try to look up the error, most of the examples are of people who have forgotten to use pcall after loading their lua file, or some error down the line. But for me, it's as soon as I even attempt to load the lua standard libs. I don't understand what's going wrong with so little code to show for it. Other things I've tried are skipping openLibs and going straight to loading my file with doFile, but even in that case, anything I do at all will result in a nil error.
Any help is greatly appreciated.
EDIT:
Thanks to some help I determined that I didn't need to do a pcall for luaL_openlibs, but as I wrote above, trying to run any function at all results in errors. In this case, it can't find "init" in the global lua namespace
c++
void LuaScriptingInterface::init()
{
m_luaState = std::shared_ptr<lua_State>(luaL_newstate());
luaL_openlibs(m_luaState.get());
if (luaL_dostring(m_luaState.get(), m_script.c_str())) // script below
{
const char* errStr = lua_tostring(m_luaState.get(), -1);
ASSERT(false, errStr ); // not hitting this case
}
lua_getglobal(m_luaState.get(), "init");
if (!lua_isfunction(m_luaState.get(), -1))
{
ASSERT(false); // hit this case! Why can't we find "init"?
}
}
lua
init function ()
end
update function (dt)
end
It doesn't do anything yet of course, but I expect it should at least be able to find the function.

Hilariously the issue was in my FileReader code, there was an omitted newline character

Related

Unidentifiable error in parallel_for

I'm trying to learn how to use a parallel_for loop for simple tasks. I am trying to use the following code:
Concurrency::parallel_for(int(0), 50, [&](int i)
{
try
{
printf("%d,", i);
}
catch (exception e)
{
printf("error\n");
}
});
Now, if I put it in main, it works fine, and prints out what I expect appropriately. However, if I put it in a .lib I am linking against, it compiles, but I receive the following error:
"Unhandled exception at 0x00007FFA8541A020 (ntdll.dll) in main.exe: RangeChecks instrumentation code detected an out of range array access."
So my questions:
1) Why would this work in the main method directly, but not in my .lib? I feel like it wouldn't compile if I was missing headers- how can I fix this?
2) No amount of Googling has led me to a solution on what exactly this error means and how I can resolve it. More confusing is the fact that all fifty valid numbers print before failure. Has anyone come across this issue?
Thanks for the insight- I'll keep looking myself!

C++ user defined function is not invoking in vs2010 while debugging in some C++ function

Actually here i am tring to invoke some user defined function from 1 function. But i couldnt step-in in to that function, i dont know what could be the reason.
below is the code snippet for calling function
if (!m_pCSock->ConnectTo(aszTWinServerName, m_szServicePort, false))
and below is the defination for the above call
int CSocketClient::ConnectTo(const char *szSuppliedHostname, const char *Suppliedport, bool ShowErrors)
{
if (m_Connected)
{
return m_Connected;
}
char szHostname[128] = "";
char port[128] = "";
The above 2 instances are in different projects and different files.
I tried a lot, I coudn't invoke into this function.
While debugging, when I try to invoke the below line
if (!m_pCSock->ConnectTo(aszTWinServerName, m_szServicePort, false))
it is not moving inside this, it is just moving to next statement.
I am suspecting here it might be some setting issue, I am actually to Windows.
Please help me. Thanks in advance.
maybe the file that contains the function is not built in "debug" mode.
If you perform a "clean and build" operation over the library or proyect, forces the file to be compiled with debug symbols and then debugger can visit that code.

Regex for JavaScript source

XPages application fails with following stack trace:
com.ibm.jscript.InterpretException: Script interpreter error, line=30, col=43: 'component' is null
at com.ibm.jscript.ASTTree.ASTMember.interpret(ASTMember.java:153)
at com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:88)
at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
at com.ibm.jscript.ASTTree.ASTIf.interpret(ASTIf.java:85)
at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
at com.ibm.jscript.ASTTree.ASTIf.interpret(ASTIf.java:85)
at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
at com.ibm.jscript.ASTTree.ASTTry.interpret(ASTTry.java:109)
at com.ibm.jscript.ASTTree.ASTIf.interpret(ASTIf.java:85)
at com.ibm.jscript.ASTTree.ASTProgram.interpret(ASTProgram.java:119)
at com.ibm.jscript.ASTTree.ASTProgram.interpretEx(ASTProgram.java:139)
From this I know, that there is problem with variable "component" nested inside hierarchy of blocks:
if -> try -> { -> if -> { -> if -> { -> method call with invalid argument.
I don't know what to look for exactly, search for "component" yields too many results.
What regex should I use to find the right spot based on code hierarchy?
In this case I see a good chance that you have not put all your SSJS code into try/catch blocks. The bad news: searching the cause of this error is extremely cumbersome as close to all SSJS blocks may be the root cause of this error.
For that reason I placed my own rule (and ignore it every now and then) to put EVERY SSJS block into a try/catch like this:
try {
// ... do fancy stuff here
} catch (e) {
print(e.toString());
}
The toString() call is used for some special cases where the error object appears to no automatically convert into an object that can be handled by the print method.
If it is the case, you have not put all SSJS blocks into a try/catch, this is exactly the right time to do so and keep that coding pattern for the future. It really helps every now and then ;-)
Instead of printStackTrace and toString() , you could just say print(e), which will output only the error message(should be the same as e.message). The error object if passed to a java routine, you could get the error line.
variable "component" nested inside hierarchy of blocks ==> We have made this working without issues.

Segmentation Fault when wrapping V8 in a class?

I want to use Google's Javascript Engine V8 in a project, and attempted to write a wrapper class for the engine. Parts of the Code are copied from samples/shell.cc, from the V8 Distribution.
However, it just aborts with a Segmentation fault, and I can't figure out why, although the problem is happening around v8::internal::Top::global_context() (due to an invalid context, which appears to be NULL).. The code itself looks fine to me, but maybe I did something incredibly stupid :-).
The Segmentation fault in my Code happens in v8::Script::Compile.
Code in Question (Updated): https://gist.github.com/4c28227185a14bb6288c
Thanks to Luis G. Costantini R.'s Answer, there is no longer a problem in Set (It doesn't abort anymore), however, exposed names are still not available and will result in a ReferenceError...
Thy to change v8::Context::Scope context_scope(context); from the constructor (line 134) to internal_executeString (before script = v8::Script::Compile(source, name);). That because the destructor of the class v8::Context::Scope exits from the context.
I changed the method addFunction:
void addFunction(const std::string& fname, v8::InvocationCallback func)
{
v8::HandleScope handle_scope;
std::cout << "before ::Set()" << std::endl;
v8::Context::Scope context_scope(context);
context->Global()->Set(v8::String::New(fname.c_str()),
v8::FunctionTemplate::New(func)->GetFunction());
std::cout << "after ::Set()" << std::endl;
}
The function must be added to the global object of the context used to execute the script. There is an excellent tutorial (in two parts) of V8:
http://www.homepluspower.info/2010/06/v8-javascript-engine-tutorial-part-1.html
and
http://www.homepluspower.info/2010/06/v8-javascript-engine-tutorial-part-2.html
If you try to create an instance of JavaScript Function (FunctionTemplate::GetFunction()) or JavaScript Object (ObjectTemplate::NewInstance()) before entering the context (via Context::Scope), you get the segmentation fault. The reason: there is no JavaScript context available and both Function and Object always exist in a JavaScript execution context only. As per V8 documentation:
Function:
A JavaScript function object (ECMA-262, 15.3).
Object:
A JavaScript object (ECMA-262, 4.3.3).
The stack backtrace is almost useless unless I download all the source and try to build it myself, so... :)
Change js.executeString("1+1", true, false); to js.executeString("1+1", true, true); and see what the exception handler tells you?
Looks like you just got stung by this bug, that is if you have not already taken note of it. Perhaps submit another report since the referenced one looks old. Perhaps dig a little deeper and investigate the stack frame at every function call until the Segmentation Fault is received, you could either find a work around or the fix for this bug :)
I had a similar segmentation fault and the problem turned out to be the following. I was creating a new thread and attempting to create an object template and object in that thread. Unfortunately it seems that if you create a thread, you need to make sure that you enter a v8::Context again in order to do such things.
I got it working by passing a Handle to the v8::Context::Calling to the newly created thread and entered it in the new thread by using a scope.
I wrote this here as it is the only useful thing that comes up when I do a google search for the segmentation fault.

What has to be Glib::init()'ed in order to use Glib::wrap?

So I'm trying to make use of a GtkSourceView in C++ using GtkSourceViewmm, whose documentation and level of support give me the impression that it hasn't been very carefully looked at in a long time. But I'm always an optimist :)
I'm trying to add a SourceView using some code similar to the following:
Glib::RefPtr<gtksourceview::SourceLanguageManager> source_language_manager = gtksourceview::SourceLanguageManager::create();
Glib::RefPtr<gtksourceview::SourceLanguage> source_language = Glib::wrap(gtk_source_language_manager_guess_language(source_language_manager->gobj(), file, NULL));
Glib::RefPtr<gtksourceview::SourceBuffer> source_buffer = gtksourceview::SourceBuffer::create(source_language);
gtksourceview::SourceView* = m_source_view = new gtksourceview::SourceView(source_buffer);
m_vbox.pack_start(*m_source_view);
Unfortunately, it spits out the warning
(algoviz:4992): glibmm-WARNING **:
Failed to wrap object of type
'GtkSourceLanguage'. Hint: this error
is commonly caused by failing to call
a library init() function.
and when I look at it in a debugger, indeed the second line above (the one with the Glib::wrap()) is returning NULL. I have no idea why this is, but I tried to heed the warning by adding Glib::init() to the begining of the program, but that didn't seem to help at all either.
I've tried Google'ing around, but have been unsuccessful. Does anyone know what Glib wants me to init in order to be able to make that wrap call? Or, even better, does anyone know of any working sample code that uses GtkSourceViewmm (not just regular GtkSourceView)? I haven't been able to find any actual sample code, not even on Google Code Search.
Thanks!
It turns out, perhaps not surprisingly, that what I needed to init was:
gtksourceview::init();
After this, I ran into another problem with one of the parameter to gtksourceview::SourceLanguageManager, but this was caused by a genuine bug which I subsequently reported and was promptly fixed. So everything's working great now!
I use gtkmm. Typically you have to initialize things with something like :
_GTKMain = new Gtk::Main(0, 0, false);
Of course do not forget :
delete _GTKMain;
Check here for details :
http://library.gnome.org/devel/gtkmm/2.19/classGtk_1_1Main.html
(Sorry but the link option does not work ...)