Commit Graph

261 Commits

Author SHA1 Message Date
邵静
014cec9777 ziglua 0.2.0 2024-02-18 09:57:20 +08:00
邵静
82dc5f7b34 1 2024-02-15 22:07:32 +08:00
Nathan Craddock
a213c57a32 Simplify pushFunction and pushClosure
This makes the api work for all versions of Lua. If Luau users wish to
set the debugname for the function, they can use the pushFunctionNamed
and pushClosureNamed functions.
2024-01-31 18:24:20 -07:00
Nathan Craddock
4cbe5eff39 Run zig fmt 2024-01-29 18:07:25 -07:00
Nathan Craddock
bcdcb04b35 Merge liblua.zig into lib.zig 2024-01-29 18:07:25 -07:00
Nathan Craddock
b3e475f106 Merge lib51.zig into lib.zig
Now lua 5.1 and luajit work. The last thing is Luau
2024-01-29 18:07:25 -07:00
Nathan Craddock
286bd4d7b0 Merge lib52.zig into lib.zig 2024-01-29 18:07:25 -07:00
Nathan Craddock
b0fadc40b9 Combine Lua 54 and 53
The biggest change is that the Uservalue functions in lua 54 no longer
have Index in the name.
2024-01-29 18:07:25 -07:00
Nathan Craddock
db29119d23 Add initial LuaJIT support
Adds the latest release of LuaJIT v2.1 and all necessary build file
changes to build as a static library.

This has only been tested on aarch64 macos. Many LuaJIT flags are
missing from the build config. Even if it works, it may not be working
perfectly.

Uses the Lua 51 library bindings, so some additional LuaJIT functions
may be missing. These will be audited and added at a later time.

Part of #19
2024-01-20 21:06:27 -07:00
Nathan Craddock
f03efd286b Remove duplication in Lua source file arrays 2024-01-20 21:06:27 -07:00
Nathan Craddock
373fe7e5f6 Use addCSourceFiles
This is simpler than looping over the files
2024-01-20 21:06:27 -07:00
Nathan Craddock
168272d632 Remove shared from buildLuau function 2024-01-20 21:06:27 -07:00
Nathan Craddock
b4136fb94d Prevent building Luau as a shared library
Luau does not support loading native compiled modules. This adds a panic
at build time if lang == .luau and shared = true.

Also removes exportFn from the Luau module.
2024-01-18 21:13:02 -07:00
Nathan Craddock
dcff37b8c1 Add lua.allocator() function to get the Zig allocator
Adds a function to return the Zig allocator associated with a Lua state.
The function will fail if the Lua state was not created with a Zig
allocator, or using the returned Allocator will segfault.

Correct use of this function is trusted to the programmer. Though in the
normal/default use of Ziglua, this will always be safe.

Closes #40
2024-01-17 20:54:51 -07:00
Nathan Craddock
aca17d6bc8 Change Lua.init() to accept a pointer to an Allocator
The Lua VM uses a void pointer to store some user data that is passed to
the custom allocator function.

This modifies Ziglua to require a pointer to be passed to the library
rather than allocating the pointer internally. This is primarily
motivated by clarity. This way makes it obvious that Ziglua diverges
form the norm in Zig code and requires a pointer to an allocator.

An additional benefit is the lua.allocator field is no longer required.
This makes the struct smaller, and removes confusion (why is
lua.allocator not always set?).

Part of #40
2024-01-17 20:52:17 -07:00
Janne Hellsten
31721fed51 Add support for user atoms and namecalls
Namecall is a mechanism in Luau to speed up method invocations.
The basic idea is that the VM can cache method names (strings)
to integer indices the first time it executes a method call.
At this point it calls the "user atom callback" with the string.
The user callback is responsible for mapping the method string
to a unique 16-bit index that's returned to the VM.

Next time the VM encounters the same string, it already knows
how to map the string to an index as, so it will reuse the user's
16-bit index.

The above is the mechanism for quickly resolving function name
strings to integers.  The other part of the API is using the
indices.  This part is the __namecall function that's attached
to a (userdata) object's metatable.  On a method call, the VM
knows that the userdata has a registered __namecall, and calls
that to dispatch to the actual user's native function to handle
the native method.  The namecall dispatch routine uses lua.namecallAtom()
to retrieve the method name/index, which is used to select which
actual native method is called.

It's not very simple but it should be fast as the VM doesn't
need to do a string->function hash table lookup on every
method invocation.

I'm not 100% sure of the details, but I suspect that the VM may
also patch the bytecode (or some internal representation of it)
directly with the namecall indices rather than looking them up
from some string hash table.
2024-01-17 09:15:17 -07:00
Nathan Craddock
e489db4495 Update readme 2024-01-16 21:59:01 -07:00
Nathan Craddock
cb31623524
Experimenting with chat 2024-01-16 18:44:20 -07:00
Nathan Craddock
0a83e5a2f3 Fix ziglua.exportFn
Adds a wrapping struct to ensure the function is actually exported. Not
sure if this is needed or if it is a Zig bug/limitation.

Closes #7
2024-01-15 10:32:02 -07:00
Nathan Craddock
7448d830cf Fix docs building
After exposing compiler options as a module to the code, the doc build
was failing. It now needs to be part of the build.zig so the docs have
access to the config module.
2024-01-15 09:07:08 -07:00
Nathan Craddock
ffc0195a05 Add luau-bytecode examples to tests 2024-01-15 09:07:08 -07:00
Nathan Craddock
c216e0f8ee Expose Lua headers with the Lua artifact
Now the dependency.artifact("lua") includes the Lua headers so other
software (like LPeg for example) can link against the built Lua
artifact.

This also exposes the Luau headers, but those may not be as useful with
all of the changes. For example, there is no lauxlib.h header in Luau.
2024-01-15 09:07:08 -07:00
Janne Hellsten
d63c450302 Expose Luau built-in float vector support
The Luau VM supports native f32 3- or 4-vectors so that typical
linear algebra operations are fast in game code.

Both the 3- and 4-vector flavors are supported.  Use
-Dluau_vector_size=N to choose which.  This must be configured
at build time, as the native Luau VM must be re-compiled for this
setting.
2024-01-14 17:04:13 -07:00
Nathan Craddock
c143f56bb7 Expose the lua artifact
When I updated to the latest package manager APIs I removed the artifact
and header installation. It turns out there are some valid uses for
these. This exposes the artifact again. The headers will come later once
I figure things out.
2024-01-13 15:26:56 -07:00
Janne Hellsten
848190f20f Cleanup unnecessary use of @as(), drop testing. prefix from expectEqual 2024-01-12 11:01:14 -07:00
Janne Hellsten
a147cbc9b4 Add support for tagged userdata and userdata destructors
Luau doesn't support the usual metatable __gc method, instead
userdatadtors should be used.  There's more information
available about these differences here:

https://github.com/luau-lang/luau/issues/251#issuecomment-981817554
2024-01-12 11:01:14 -07:00
Janne Hellsten
3337d7e435 Expose Luau bytecode compiling and loading API
- Add ziglua.compile for a safer wrapper around c.luau_compile
- Add Lua.loadBytecode()
- Add example
2024-01-12 10:18:08 -07:00
Nathan Craddock
e6f7d2f698 Add documentation link to registerFns 2024-01-11 21:26:57 -07:00
Nathan Craddock
bbcadbbd47 Cleanup and document tests 2024-01-11 21:26:25 -07:00
Nathan Craddock
1f5b6de91f Move lib.zig out of subdirectories and rename 2024-01-11 20:04:39 -07:00
Nathan Craddock
a6562beb70 Combine all tests into one tests.zig file
This shows that using ziglua.lang for conditional compilation should be
a reasonable path forward. Also found a few bugs while doing this.
2024-01-11 20:04:39 -07:00
Nathan Craddock
69d305c7a5 Expose build config to ziglua module
Not sure if the top level config should be public. Then the use would be
ziglua.config.lang instead of ziglua.lang.

If build options grow this can be revisited.

Closes #44
2024-01-10 17:05:32 -07:00
Nathan Craddock
8f5ab076b5 Rename version build option to lang 2024-01-10 16:57:16 -07:00
Nathan Craddock
b71a42e831 docs: update dependency documentation 2024-01-09 22:54:41 -07:00
Nathan Craddock
744da91c36 Replace Lua 5.1.5 with patched dependency 2024-01-09 22:54:41 -07:00
Nathan Craddock
29518d2bf7 Remove vendored Luau library 2024-01-09 22:54:41 -07:00
Nathan Craddock
d8511141c5 Remove vendored lua libraries
Use the Zig package manager to download the sources for Lua 5.1 through
Lua 5.4. Luau still needs to be done this way.

The LuaVersion enum strings are changed to remove the _ characters, this
is a breaking change.

Also makes the tests rely on the module rather than the raw source file.
Not sure if this is a good idea yet, just testing some things.

This links the compiled lua library to the ziglua module so it doesn't
need to be linked manually in other projects.
2024-01-09 22:54:41 -07:00
Nathan Craddock
86ba04135e fix: update build.zig for module changes 2024-01-09 22:54:41 -07:00
Nathan Craddock
7f1e80c18f docs: add Luau to readme 2024-01-02 09:44:20 -07:00
Nathan Craddock
808f141b81 ci: add luau to documentation generation 2024-01-02 09:44:10 -07:00
Nathan Craddock
2c6a35e1bb cleanup: build.zig 2024-01-02 09:35:23 -07:00
Nathan Craddock
3e4612fa21 Fix freeing memory in Luau loadString on Windows
We shouldn't assume the c_allocator is the same as free()
2024-01-02 09:35:23 -07:00
Nathan Craddock
be2da11336 ci: Add windows test coverage
Really not sure why this wasn't here already
2024-01-02 09:35:23 -07:00
Nathan Craddock
e64a5b9e39 Add Luau AST source files 2024-01-02 09:35:23 -07:00
Nathan Craddock
52692f075e Add debug library tests 2024-01-02 09:35:23 -07:00
Nathan Craddock
d4cfdab5ef Remove load/dump tests and enable resume tests 2024-01-02 09:35:23 -07:00
Nathan Craddock
5b7b930485 Add more garbage collector functions 2024-01-02 09:35:23 -07:00
Nathan Craddock
9f63b1e90d Register and Buffers 2024-01-02 09:35:23 -07:00
Nathan Craddock
b1ca88c477 Add doString and enable more tests 2024-01-02 09:35:23 -07:00
Nathan Craddock
d8e1ac8195 Uncomment some Luau tests 2024-01-02 09:35:23 -07:00