aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2024-07-24 21:45:40 +0100
committergingerBill <bill@gingerbill.org>2024-07-24 21:45:40 +0100
commit9b624ef9e158bde0fd610cbf9c79dce4fbe8acf0 (patch)
tree25b1ea1d6962400f69cbd9c4dca9d8d688e38ecb
parentf03c2b7783518f05f7804dfe51adaef4b5e5020e (diff)
parent33d6677514af5b4507d4c202a1e3fbea9cc3194e (diff)
Merge branch 'master' of https://github.com/odin-lang/Odin
-rw-r--r--.github/workflows/ci.yml5
-rw-r--r--tests/vendor/all.odin1
-rw-r--r--tests/vendor/lua/5.4/factorial.lua10
-rw-r--r--tests/vendor/lua/5.4/test_vendor_lua.5.4.odin71
-rw-r--r--vendor/lua/README.md48
5 files changed, 128 insertions, 7 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f576700b8..73cd3493b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -88,13 +88,13 @@ jobs:
- name: Download LLVM (MacOS Intel)
if: matrix.os == 'macos-13'
run: |
- brew install llvm@17
+ brew install llvm@17 lua@5.4
echo "/usr/local/opt/llvm@17/bin" >> $GITHUB_PATH
- name: Download LLVM (MacOS ARM)
if: matrix.os == 'macos-14'
run: |
- brew install llvm@17 wasmtime
+ brew install llvm@17 wasmtime lua@5.4
echo "/opt/homebrew/opt/llvm@17/bin" >> $GITHUB_PATH
- name: Build Odin
@@ -204,6 +204,7 @@ jobs:
shell: cmd
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat
+ copy vendor\lua\5.4\windows\*.dll .
odin test tests/vendor -all-packages -define:ODIN_TEST_FANCY=false
- name: Odin internals tests
shell: cmd
diff --git a/tests/vendor/all.odin b/tests/vendor/all.odin
index 1ce56e786..1abbc5d7f 100644
--- a/tests/vendor/all.odin
+++ b/tests/vendor/all.odin
@@ -1,3 +1,4 @@
package tests_vendor
@(require) import "glfw"
+@(require) import "lua/5.4" \ No newline at end of file
diff --git a/tests/vendor/lua/5.4/factorial.lua b/tests/vendor/lua/5.4/factorial.lua
new file mode 100644
index 000000000..00cfb20f7
--- /dev/null
+++ b/tests/vendor/lua/5.4/factorial.lua
@@ -0,0 +1,10 @@
+-- defines a factorial function
+function fact (n)
+ if n == 0 then
+ return 1
+ else
+ return n * fact(n-1)
+ end
+end
+
+return fact(10) \ No newline at end of file
diff --git a/tests/vendor/lua/5.4/test_vendor_lua.5.4.odin b/tests/vendor/lua/5.4/test_vendor_lua.5.4.odin
new file mode 100644
index 000000000..e331200ea
--- /dev/null
+++ b/tests/vendor/lua/5.4/test_vendor_lua.5.4.odin
@@ -0,0 +1,71 @@
+//+build windows, linux, darwin
+package test_vendor_lua_54
+
+import "core:testing"
+import "core:c"
+import lua "vendor:lua/5.4"
+import "base:runtime"
+
+@(test)
+// Test context.allocator and returning a string
+return_string_with_context_based_allocator :: proc(t: ^testing.T) {
+ _context := context
+
+ state: ^lua.State
+ state = lua.newstate(lua_context_allocator, &_context)
+ defer lua.close(state)
+
+ lua.L_dostring(state, "return 'somestring'")
+ str := lua.tostring(state, -1)
+
+ testing.expectf(
+ t, str == "somestring", "Expected Lua to return \"somestring\"",
+ )
+}
+
+@(test)
+// Test lua.dofile and returning an integer
+dofile_factorial :: proc(t: ^testing.T) {
+ state := lua.L_newstate()
+ defer lua.close(state)
+
+ FACT_10 :: 3628800
+
+ res := lua.L_dofile(state, #directory + "/factorial.lua")
+ testing.expectf(t, lua.Status(res) == .OK, "Expected L_dofile to return OKAY")
+
+ fact := lua.L_checkinteger(state, -1)
+
+ testing.expectf(t, fact == FACT_10, "Expected factorial(10) to return %v, got %v", FACT_10, fact)
+}
+
+@(test)
+// Test that our bindings didn't get out of sync with the API version
+verify_lua_api_version :: proc(t: ^testing.T) {
+ state := lua.L_newstate()
+ defer lua.close(state)
+
+ version := int(lua.version(state))
+
+ testing.expectf(t, version == lua.VERSION_NUM, "Expected lua.version to return %v, got %v", lua.VERSION_NUM, version)
+}
+
+// Simple context.allocator-based callback for Lua. Use `lua.newstate` to pass the context as user data.
+lua_context_allocator :: proc "c" (ud: rawptr, ptr: rawptr, osize, nsize: c.size_t) -> (buf: rawptr) {
+ old_size := int(osize)
+ new_size := int(nsize)
+ context = (^runtime.Context)(ud)^
+
+ if ptr == nil {
+ data, err := runtime.mem_alloc(new_size)
+ return raw_data(data) if err == .None else nil
+ } else {
+ if nsize > 0 {
+ data, err := runtime.mem_resize(ptr, old_size, new_size)
+ return raw_data(data) if err == .None else nil
+ } else {
+ runtime.mem_free(ptr)
+ return
+ }
+ }
+} \ No newline at end of file
diff --git a/vendor/lua/README.md b/vendor/lua/README.md
index 8f4b0f5a5..4bc7804bb 100644
--- a/vendor/lua/README.md
+++ b/vendor/lua/README.md
@@ -1,12 +1,50 @@
# Lua in Odin
-```odin
-import lua "vendor:lua/5.4" // or whatever version you want
-```
-
Lua packages
* `vendor:lua/5.1` (version 5.1.5)
* `vendor:lua/5.2` (version 5.2.4)
* `vendor:lua/5.3` (version 5.3.6)
-* `vendor:lua/5.4` (version 5.4.2) \ No newline at end of file
+* `vendor:lua/5.4` (version 5.4.2)
+
+With custom context-based allocator:
+
+```odin
+package lua_example_with_context
+
+import "core:fmt"
+import lua "vendor:lua/5.4" // or whatever version you want
+import "core:c"
+import "base:runtime"
+
+state: ^lua.State
+
+lua_allocator :: proc "c" (ud: rawptr, ptr: rawptr, osize, nsize: c.size_t) -> (buf: rawptr) {
+ old_size := int(osize)
+ new_size := int(nsize)
+ context = (^runtime.Context)(ud)^
+
+ if ptr == nil {
+ data, err := runtime.mem_alloc(new_size)
+ return raw_data(data) if err == .None else nil
+ } else {
+ if nsize > 0 {
+ data, err := runtime.mem_resize(ptr, old_size, new_size)
+ return raw_data(data) if err == .None else nil
+ } else {
+ runtime.mem_free(ptr)
+ return
+ }
+ }
+}
+
+main :: proc() {
+ _context := context
+ state = lua.newstate(lua_allocator, &_context)
+ defer lua.close(state)
+
+ lua.L_dostring(state, "return 'somestring'")
+ str := lua.tostring(state, -1)
+ fmt.println(str)
+}
+``` \ No newline at end of file