Skip to content

Commit c1e8920

Browse files
committed
Initial commit
0 parents  commit c1e8920

20 files changed

+3024
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/build
2+
/src/sandbox.mk
3+
*.o
4+
*.so
5+
*.dll
6+
a.out

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Oliver Schmidt
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# lua-carray
2+
[![Licence](http://img.shields.io/badge/Licence-MIT-brightgreen.svg)](LICENSE)
3+
[![Install](https://img.shields.io/badge/Install-LuaRocks-brightgreen.svg)](https://luarocks.org/modules/osch/carray)
4+
5+
<!-- ---------------------------------------------------------------------------------------- -->
6+
7+
This [Lua] module provides an array data type together with a [C API](./src/carray_capi.h)
8+
for handling arrays of primitive numeric C data types in Lua script code and also in native
9+
C code for enhancing native Lua module interoperability.
10+
11+
[Lua]: https://www.lua.org
12+
13+
<!-- ---------------------------------------------------------------------------------------- -->
14+
15+
## First Example
16+
17+
```lua
18+
local carray = require"carray"
19+
20+
local a = carray.new("int", 10)
21+
22+
for i = 1, a:len() do
23+
assert(a:get(i) == 0)
24+
end
25+
26+
for i = 1, a:len() do
27+
a:set(i, 100 + i)
28+
end
29+
30+
for i = 1, a:len() do
31+
assert(a:get(i) == 100 + i)
32+
end
33+
34+
local x, y, z = a:get(2, 4)
35+
assert(x == 102)
36+
assert(y == 103)
37+
assert(z == 104)
38+
39+
40+
local x, y, z = a:get(-3, -1)
41+
assert(x == 108)
42+
assert(y == 109)
43+
assert(z == 110)
44+
45+
a:set(2, 202, 203, 204)
46+
47+
local x, y, z = a:get(2, 4)
48+
assert(x == 202)
49+
assert(y == 203)
50+
assert(z == 204)
51+
52+
local c = carray.new("char", 10)
53+
c:setstring(1, "1234567890")
54+
55+
assert(c:get(1) == string.byte("1"))
56+
assert(c:tostring() == "1234567890")
57+
assert(c:tostring(1) == "1")
58+
assert(c:tostring(2,4) == "234")
59+
60+
c:setstring(4, "abc")
61+
assert(c:tostring() == "123abc7890")
62+
```
63+
64+
<!-- ---------------------------------------------------------------------------------------- -->

examples/example01.lua

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local carray = require"carray"
2+
3+
local a = carray.new("int", 10)
4+
5+
for i = 1, a:len() do
6+
assert(a:get(i) == 0)
7+
end
8+
9+
for i = 1, a:len() do
10+
a:set(i, 100 + i)
11+
end
12+
13+
for i = 1, a:len() do
14+
assert(a:get(i) == 100 + i)
15+
end
16+
17+
local x, y, z = a:get(2, 4)
18+
assert(x == 102)
19+
assert(y == 103)
20+
assert(z == 104)
21+
22+
23+
local x, y, z = a:get(-3, -1)
24+
assert(x == 108)
25+
assert(y == 109)
26+
assert(z == 110)
27+
28+
a:set(2, 202, 203, 204)
29+
30+
local x, y, z = a:get(2, 4)
31+
assert(x == 202)
32+
assert(y == 203)
33+
assert(z == 204)
34+
35+
local c = carray.new("char", 10)
36+
c:setstring(1, "1234567890")
37+
38+
assert(c:get(1) == string.byte("1"))
39+
assert(c:tostring() == "1234567890")
40+
assert(c:tostring(1) == "1")
41+
assert(c:tostring(2,4) == "234")
42+
43+
c:setstring(4, "abc")
44+
assert(c:tostring() == "123abc7890")
45+

rockspecs/carray-scm-0.rockspec

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package = "carray"
2+
version = "scm-0"
3+
source = {
4+
url = "https://github.com/osch/lua-carray/archive/master.zip",
5+
dir = "lua-carray-master",
6+
}
7+
description = {
8+
summary = "Arrays for primitive numeric C data types",
9+
homepage = "https://github.com/osch/lua-carray",
10+
license = "MIT",
11+
detailed = [[
12+
This Lua module provides an array data type together with a C API for
13+
handling arrays of primitive numeric C data types in Lua script code and
14+
also in native C code for enhancing native Lua module interoperability.
15+
]],
16+
}
17+
dependencies = {
18+
"lua >= 5.1, <= 5.4",
19+
}
20+
build = {
21+
type = "builtin",
22+
modules = {
23+
carray = {
24+
sources = {
25+
"src/main.c",
26+
"src/carray.c",
27+
"src/carray_capi_impl.c",
28+
"src/carray_compat.c",
29+
},
30+
defines = { "CARRAY_VERSION="..version:gsub("^(.*)-.-$", "%1") },
31+
},
32+
}
33+
}

src/Makefile

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.PHONY: default carray
2+
default: carray
3+
4+
BUILD_DATE := $(shell date "+%Y-%m-%dT%H:%M:%S")
5+
6+
LNX_GCC_RUN := gcc -shared -fPIC -O2 -g
7+
WIN_GCC_RUN := gcc -shared -fPIC -O2
8+
MAC_GCC_RUN := MACOSX_DEPLOYMENT_TARGET=10.8 gcc -O2 -bundle -undefined dynamic_lookup -all_load
9+
10+
LNX_COPTS :=
11+
WIN_COPTS := -I/mingw64/include/lua5.1
12+
MAC_COPTS := -I/usr/local/opt/lua/include/lua5.3
13+
14+
LNX_LOPTS :=
15+
WIN_LOPTS :=
16+
MAC_LOPTS :=
17+
18+
LNX_SO_EXT := so
19+
WIN_SO_EXT := dll
20+
MAC_SO_EXT := so
21+
22+
GCC_RUN :=
23+
SO_EXT :=
24+
COPTS :=
25+
LOPTS :=
26+
27+
# platforms: LNX, WIN, MAC
28+
# (may be set in sandbox.mk)
29+
30+
PLATFORM := LNX
31+
LUA_VERSION := 5.4
32+
33+
-include sandbox.mk
34+
35+
GCC_RUN := $(or $(GCC_RUN), $($(PLATFORM)_GCC_RUN))
36+
SO_EXT := $(or $(SO_EXT), $($(PLATFORM)_SO_EXT))
37+
COPTS := $(or $(COPTS), $($(PLATFORM)_COPTS))
38+
LOPTS := $(or $(LOPTS), $($(PLATFORM)_LOPTS))
39+
40+
carray:
41+
@mkdir -p build/lua$(LUA_VERSION)/
42+
$(GCC_RUN) $(COPTS) \
43+
-D CARRAY_VERSION=Makefile"-$(BUILD_DATE)" \
44+
main.c carray.c carray_capi_impl.c \
45+
carray_compat.c \
46+
$(LOPTS) \
47+
-o build/lua$(LUA_VERSION)/carray.$(SO_EXT)
48+
49+

src/activate.sh

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Activations script for add the carray module to the lua path when
2+
# building carray using the Makefile.
3+
# Source this into interactive shell by invoking ". activates.sh" from this directory
4+
# This is not necessary if carray is installed, e.g. via luarocks.
5+
6+
this_dir=$(pwd)
7+
8+
luacarray_dir=$(cd "$this_dir"/..; pwd)
9+
10+
if [ ! -e "$luacarray_dir/src/activate.sh" -o ! -e "$luacarray_dir/src/main.c" ]; then
11+
12+
echo '**** ERROR: ". activate.sh" must be invoked from "src" directory ***'
13+
14+
else
15+
16+
echo "Setting lua paths for: $luacarray_dir"
17+
18+
add_lua_path="$luacarray_dir/src/?.lua;$luacarray_dir/src/?/init.lua"
19+
add_lua_cpath="$luacarray_dir/src/build"
20+
21+
# unset LUA_PATH_5_3 LUA_CPATH_5_3 LUA_PATH_5_2 LUA_CPATH_5_2 LUA_PATH LUA_CPATH
22+
23+
default_version=""
24+
if which lua > /dev/null 2>&1; then
25+
default_version=$(lua -e 'v=_VERSION:gsub("^Lua ","");print(v)')
26+
fi
27+
28+
for vers in 5.4 5.3 5.2 5.1; do
29+
lua_cmd=""
30+
if which lua$vers > /dev/null 2>&1; then
31+
lua_cmd="lua$vers"
32+
elif which lua-$vers > /dev/null 2>&1; then
33+
lua_cmd="lua-$vers"
34+
fi
35+
if [ -n "$lua_cmd" ]; then
36+
lua_version=$($lua_cmd -e 'v=_VERSION:gsub("^Lua ","");print(v)')
37+
if [ "$lua_version" != "$default_version" ]; then
38+
echo "Setting path for $lua_cmd (version=$lua_version)"
39+
if [ "$lua_version" = "5.1" ]; then
40+
export LUA_PATH="$add_lua_path;$($lua_cmd -e 'print(package.path)')"
41+
export LUA_CPATH="$add_lua_cpath/lua5.1/?.so;$($lua_cmd -e 'print(package.cpath)')"
42+
else
43+
lua_path_vers=$(echo $lua_version|sed 's/\./_/')
44+
eval "export LUA_PATH_$lua_path_vers=\"$add_lua_path;$($lua_cmd -e 'print(package.path)')\""
45+
eval "export LUA_CPATH_$lua_path_vers=\"$add_lua_cpath/lua$lua_version/?.so;$($lua_cmd -e 'print(package.cpath)')\""
46+
fi
47+
fi
48+
fi
49+
done
50+
51+
if [ -n "$default_version" ]; then
52+
echo "Setting path for lua (version=$default_version)"
53+
if [ "$default_version" = "5.1" ]; then
54+
export LUA_PATH="$add_lua_path;$(lua -e 'print(package.path)')"
55+
export LUA_CPATH="$add_lua_cpath/lua5.1/?.so;$(lua -e 'print(package.cpath)')"
56+
else
57+
lua_path_vers=$(echo $default_version|sed 's/\./_/')
58+
eval "export LUA_PATH_$lua_path_vers=\"$add_lua_path;$(lua -e 'print(package.path)')\""
59+
eval "export LUA_CPATH_$lua_path_vers=\"$add_lua_cpath/lua$default_version/?.so;$(lua -e 'print(package.cpath)')\""
60+
fi
61+
fi
62+
fi
63+
64+
unset lua_cmd this_dir luacarray_dir add_lua_path add_lua_cpath lua_version lua_path_vers vers default_version
65+

src/async_defines.h

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef LJACK_ASYNC_DEFINES_H
2+
#define LJACK_ASYNC_DEFINES_H
3+
4+
/* -------------------------------------------------------------------------------------------- */
5+
6+
#if defined(LJACK_ASYNC_USE_WIN32) \
7+
&& ( defined(LJACK_ASYNC_USE_STDATOMIC) \
8+
|| defined(LJACK_ASYNC_USE_GNU))
9+
#error "LJACK_ASYNC: Invalid compile flag combination"
10+
#endif
11+
#if defined(LJACK_ASYNC_USE_STDATOMIC) \
12+
&& ( defined(LJACK_ASYNC_USE_WIN32) \
13+
|| defined(LJACK_ASYNC_USE_GNU))
14+
#error "LJACK_ASYNC: Invalid compile flag combination"
15+
#endif
16+
#if defined(LJACK_ASYNC_USE_GNU) \
17+
&& ( defined(LJACK_ASYNC_USE_WIN32) \
18+
|| defined(LJACK_ASYNC_USE_STDATOMIC))
19+
#error "LJACK_ASYNC: Invalid compile flag combination"
20+
#endif
21+
22+
/* -------------------------------------------------------------------------------------------- */
23+
24+
#if !defined(LJACK_ASYNC_USE_WIN32) \
25+
&& !defined(LJACK_ASYNC_USE_STDATOMIC) \
26+
&& !defined(LJACK_ASYNC_USE_GNU)
27+
28+
#if defined(WIN32) || defined(_WIN32)
29+
#define LJACK_ASYNC_USE_WIN32
30+
#elif defined(__GNUC__)
31+
#define LJACK_ASYNC_USE_GNU
32+
#elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
33+
#define LJACK_ASYNC_USE_STDATOMIC
34+
#else
35+
#error "LJACK_ASYNC: unknown platform"
36+
#endif
37+
#endif
38+
39+
/* -------------------------------------------------------------------------------------------- */
40+
41+
#if defined(__unix__) || defined(__unix) || (defined (__APPLE__) && defined (__MACH__))
42+
#include <unistd.h>
43+
#endif
44+
45+
#if !defined(LJACK_ASYNC_USE_WINTHREAD) \
46+
&& !defined(LJACK_ASYNC_USE_PTHREAD) \
47+
&& !defined(LJACK_ASYNC_USE_STDTHREAD)
48+
49+
#ifdef LJACK_ASYNC_USE_WIN32
50+
#define LJACK_ASYNC_USE_WINTHREAD
51+
#elif _XOPEN_VERSION >= 600
52+
#define LJACK_ASYNC_USE_PTHREAD
53+
#elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
54+
#define LJACK_ASYNC_USE_STDTHREAD
55+
#else
56+
#define LJACK_ASYNC_USE_PTHREAD
57+
#endif
58+
#endif
59+
60+
/* -------------------------------------------------------------------------------------------- */
61+
62+
#if defined(LJACK_ASYNC_USE_PTHREAD)
63+
#ifndef _XOPEN_SOURCE
64+
#define _XOPEN_SOURCE 600 /* must be defined before any other include */
65+
#endif
66+
#include <errno.h>
67+
#include <sys/time.h>
68+
#include <pthread.h>
69+
#endif
70+
#if defined(LJACK_ASYNC_USE_WIN32) || defined(LJACK_ASYNC_USE_WINTHREAD)
71+
#include <windows.h>
72+
#endif
73+
#if defined(LJACK_ASYNC_USE_STDATOMIC)
74+
#include <stdint.h>
75+
#include <stdatomic.h>
76+
#endif
77+
#if defined(LJACK_ASYNC_USE_STDTHREAD)
78+
#include <sys/time.h>
79+
#include <threads.h>
80+
#endif
81+
82+
/* -------------------------------------------------------------------------------------------- */
83+
84+
#if __STDC_VERSION__ >= 199901L
85+
#include <stdbool.h>
86+
#else
87+
#if !defined(__GNUC__) || defined(__STRICT_ANSI__)
88+
#define inline
89+
#endif
90+
#define bool int
91+
#define true 1
92+
#define false 0
93+
#endif
94+
95+
/* -------------------------------------------------------------------------------------------- */
96+
97+
98+
#endif /* LJACK_ASYNC_DEFINES_H */

0 commit comments

Comments
 (0)