How to forward declare functions in a Lua module? - lua

I have a Lua module written in C compiled as a shared library, but when I use the module, editors cannot autocomplete it because they cannot see the content of the library.
In C, editors can deduce from the header files, how do you do that in Lua?
The Lua standard library does that but I can't figure out how. Every lua editor can tell me about standard library functions (their signature and documentation). How does it get this info from? How do I add such documentation to my own lua module?

It depends on editor you use. Each editor has own implementation, and you must check documentation how to extend autocomplete.
For notepad++ documentation about autocomplete:
https://npp-user-manual.org/docs/auto-completion/,
and file for this: Notepad++\autoCompletion\lua.xml

Related

How to write F# Type Provider to generate P/Invoke code?

I was trying to develop a native C interop library in F#.
I wonder if there is a way to leverage F# type provider to read that C library header file, parse it and use the AST to generate the boilerplate P/Invoke binding code.
I read several type provider projects code, but there still things unclear to me, specifically the following:
How to mark a ProvidedMethod as extern?
How to generate a plain struct (value type) with StructLayout(LayoutKind.Explicit) attributes?
Please give any suggestion to me, thanks!

How to get autocompletion for custom modules in Lua

Whatever the setup I use for coding in Lua is always the same thing: autocompletion works for the standard libraries but not for the 3rd parties or my own libraries.
I tried ZeroBrane studio, VSCode with Lua plugin and Vim with lua ftplugin, exact same behaviour in all 3. I start typing a standard library symbol such as
io.w
And I do get the autocompletion popup showing everything in the io module, and showing the closest method to io.w which would be io.write, with the signature and documentation.
Now I try a 3rd party or my own library such as
require("wx"); wx.
or
require("my_module"); my_module.
Either nothing happens at all, or I get a warning "undefined" on the module name.
If I run the code with the interpreter, it does work. It will call the function in the module just fine. But in the editor, warning and no autocompletion.
Am I missing something?
wxwidgets API comes prepackaged with ZeroBrane Studio, but it needs to be explicitly enabled (you can add api = {"wxwidgets"} to the config file to do that; see Custom APIs section in the documentation). Any other (non-packaged) API would need to be added to the IDE as documented here. There are several popular APIs already provided as plugins; for example, for Redis, Urho3d, openRA and others.

How can you link against (non-standard) libraries from Terra?

I'd like to use raylib with Terra/Lua (better metaprogramming) but the official examples only show how to interface with the C standard library, which I assume Terra is linked against already.
But raylib has a header (raylib.h, I know how to include it in terra) and a so/dll file (the actual code of the library).
How can I link my Terra cide against raylib?
P.S. I'm interested in writing mostly Terra and just doing macros in Lua which is the third use case listed on the Terra website:
A stand-alone low-level language. Terra was designed so that it can
run independently from Lua. In fact, if your final program doesn’t
need Lua, you can save Terra code into a .o file or executable. In
addition to ensuring a clean separation between high- and low-level
code, this design lets you use Terra as a stand-alone low-level
language. In this use-case, Lua serves as a powerful meta-programming
language. Here it serves as a replacement for C++ template
metaprogramming or C preprocessor X-Macros with better syntax and
nicer properties such as hygiene. Since Terra exists only as code
embedded in a Lua meta-program, features that are normally built into
low-level languages can be implemented as Lua libraries. This design
keeps the core of Terra simple, while enabling powerful behavior such
as conditional compilation, namespaces, templating, and even class
systems implemented as libraries.
Nevermind, I think I found what I need.
terralib.linklibrary(filename)
Load the dynamic library in file
filename. If header files imported with includec contain declarations
whose definitions are not linked into the executable in which Terra is
run, then it is necessary to dynamically load the definitions with
linklibrary. This situation arises when using external libraries with
the terra REPL/driver application.
Source: Terra docs

Get clipboard string and set it

I need to get the clipboard into string,
After I getting the string of the clipboard I want to set the clipboard to something else.
Love2D doesn't have functions for accessing clip-board data. It's too platform-specific for it.
One of the strongest points of Lua is its ability to interface easily with C and C++ libraries through bindings. What you would have to do here is to write your own library in C (or find an existing one) that does what you want to do, write a binding for it for Lua and then use it from your love2d code.
Unfortunately, like Nicol said, this is likely going to be platform specific which means you'll need to write a library for each platform you want your game to run on and then use the love._os field to split your game logic.

Bundling additional Lua libraries for embedded and statically linked Lua runtime

I have embedded Lua on Win32 in my project by means of statically linking it in (no, I can't switch to DLL). I would like to bundle more Lua extensions that use native code - not just pure .lua files. Specifically, I want to bundle Steve Donovan's winapi which comes as some lua files and some .c files.
How to do it?
You need to do two things. First, you have to compile the Lua DLL projects into non-DLL projects. Since they're intended to be DLL modules, they probably won't have provisions for this in their build systems. That means you have to do it yourself. Get rid of the DLL main functions and other specialized DLL functions (but take note of what they do and make sure you replicate it if it's important). And make sure that you change any #defines that try to include Lua with dynamic linking.
All Lua module DLLs export one or more functions of the form luaopen_*, where * is the name of the module to load. This function will likely be decorated with declspec() notation. Typically, the notation is done via a preprocessor macro, but it may not be. Either way, remove it, turning it into a normal function declaration.
Now, once you have created your lua_State object, just call that luaopen_* function with your lua_State.

Resources