Skip to content

Add a way to call luaL_loadstring .#109

Open
Zardoz89 wants to merge 4 commits intojeremyong:masterfrom
Zardoz89:develop
Open

Add a way to call luaL_loadstring .#109
Zardoz89 wants to merge 4 commits intojeremyong:masterfrom
Zardoz89:develop

Conversation

@Zardoz89
Copy link

Why ? Well, on some case, would be interesting to handle strings on memory instead loading/saving directly to HDD, in special when there is some kind of filesystem abstraction, or Selene it's being used inside a script editor.

LoadString looks that collides with some Windows API macro, so using LoadStr
instead of LoadString.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this->

@jeremyong
Copy link
Owner

Thanks for the PR! This code is very similar to the code for loading from a file. Can any of the code be consolidated or factored out in a more generic way?

@Zardoz89
Copy link
Author

Comes to my head a few methods :

  • A single method with an boolean flag to chose between filename or string (Java-ish)
  • Define a LoadFrom<T>(T var) were on function of T type :
    • T is char* -> Call to LoadString(var)
    • T is a string& -> Call to LoadString(var.c_str())
    • T is a FILE* -> Use a defined lua_Reader to read from C file handler with luaL_Load
    • T is an ostream -> Use a defined lua_Reader to read from C++ stream with luaL_Load
    • T is a lambda function -> Use the lambda function on a lua_Reader function wrapper.

The second case is the most flexible, and Load(filename) could become a simple tiny wrapper around LoadFrom that opens the file, pass the ostream to LoadFrom, and closes it.

Sadly my C++ template skills are actually poor. I could try, but actually I'm trying more to get working a lua scripting system integrated on Trillek.

@jeremyong
Copy link
Owner

I was thinking something with less machinery. Really the only difference is a call to loadFile vs loadString, and the rest could be lifted to a separate common helper function (unless I missed something)

@Zardoz89
Copy link
Author

It could work better. I think that over-engineered the problem.

LoadString looks that collides with some Windows API macro, so using LoadStr
instead of LoadString
Shared code moved to a private inlined method : PostLoad

Also, explicit check of pointers against nullptr
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be return PostLoad(status)

@jeremyong
Copy link
Owner

I think removing all the uses of this-> which isn't good idiomatic C++ and making things const correct (the status for example) should make this good to go.

@jhanssen
Copy link

I'd really like to see this supported, I'm currently working on a project where lua code arrives over the network and no files ever touch the disk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants