Skip to content

Commit

Permalink
Switched to a zig build which bundles utf8proc with the lua module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehgugs committed Dec 30, 2022
1 parent 35c0067 commit b4881e7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 56 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
*.lib
*.def
.vscode/**
test/**
test/**
zig-cache/**
zig-out/**
57 changes: 57 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const std = @import("std");

// Type helpers
const String = []const u8;
const StringPair = [2]String;
const SourceList = std.ArrayList(String);
const FlagList = std.ArrayList(String);
fn SliceOf(comptime t: type) type {
return []const t;
}

//builder
const Builder = std.build.Builder;
const Step = std.build.Step;
const InstallDir = std.build.InstallDir;
const Artifact = *std.build.LibExeObjStep;

pub fn build(b: *Builder) !void {
const art = b.addSharedLibrary("lunicode", null, .unversioned);
var flags = FlagList.init(b.allocator);

//Target & build mode
const target = b.standardTargetOptions(.{.default_target = std.zig.CrossTarget.fromTarget(b.host.target)});
const mode = std.builtin.Mode.ReleaseSmall; // NB. Hardcoded to ReleaseSmall to prevent errors on windows; investigate.

const luapathopt = b.option(String, "lua", "Path to your lua installation.");
if (luapathopt) |luapath| {
art.addIncludePath(b.pathJoin(&.{luapath, "include"}));
art.addLibraryPath(b.pathJoin(&.{luapath, "lib"}));
art.strip = true;
art.bundle_compiler_rt = false;
} else {
std.log.err("No lua path provided!", .{});
std.os.exit(1);
}

art.setBuildMode(mode);
art.setTarget(target);
art.linkLibC();
art.linkSystemLibrary("lua5.4.4");
art.addIncludePath("utf8proc");

try flags.append("-DUTF8PROC_STATIC");

if(target.os_tag == std.Target.Os.Tag.windows) {
try flags.append("-DLUA_USE_WINDOWS");
}
else if ((target.os_tag == std.Target.Os.Tag.linux) or (target.os_tag == std.Target.Os.Tag.macos)) {
try flags.append("-DLUA_USE_POSIX");
try flags.append("-fPIC");
}

art.addCSourceFiles(&.{"src/lunicode.c", "utf8proc/utf8proc.c"}, flags.toOwnedSlice());

art.install();

}
43 changes: 0 additions & 43 deletions lunicode-dev-0.rockspec

This file was deleted.

27 changes: 15 additions & 12 deletions src/lunicode.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <lua.h>
#include <luaconf.h>
#include <lauxlib.h>
#include <stdbool.h>
#include <limits.h>
#include <utf8proc.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "lua.h"
#include "luaconf.h"
#include "lauxlib.h"
#include "utf8proc.h"

#define all_good(x) ((0 <= (x) && (x) <= 0x10FFFF) && utf8proc_codepoint_valid((x)))

Expand Down Expand Up @@ -93,8 +93,12 @@ static utf8proc_option_t lunicode_check_options(lua_State* L, int arg) {
}

static int lunicode_map2(lua_State* L, utf8proc_option_t options) {
utf8proc_ssize_t size;
const char* str = luaL_checklstring(L, 1, &size);
size_t size;
utf8proc_uint8_t* str = (utf8proc_uint8_t*)luaL_checklstring(L, 1, &size);
utf8proc_ssize_t ssize;

if (size > PTRDIFF_MAX) ssize = PTRDIFF_MAX;
else ssize = size;

utf8proc_ssize_t numwords = utf8proc_decompose(str, size, NULL, 0, options);

Expand All @@ -108,7 +112,7 @@ static int lunicode_map2(lua_State* L, utf8proc_option_t options) {

utf8proc_ssize_t initial_bytes = numwords * 4;

char* space = luaL_prepbuffsize(&b, initial_bytes);
utf8proc_int32_t* space = (utf8proc_int32_t*)luaL_prepbuffsize(&b, initial_bytes);

numwords = utf8proc_decompose(str, size, space, numwords, options);

Expand Down Expand Up @@ -185,7 +189,7 @@ static int lunicode_category_string(lua_State* L) {
static int lunicode_grapheme_break(lua_State* L) {
lua_Integer i = luaL_checkinteger(L, 1);
lua_Integer j = luaL_checkinteger(L, 2);
lua_Integer s = luaL_checkinteger(L, 3, 0);
utf8proc_uint32_t s = (utf8proc_uint32_t)luaL_optinteger(L, 3, 0);
if (all_good(i) && all_good(j)) {

utf8proc_bool out = utf8proc_grapheme_break_stateful(i, j, &s);
Expand All @@ -200,7 +204,7 @@ static int lunicode_grapheme_break(lua_State* L) {
static int lunicode_properties(lua_State* L) {
lua_Integer i = luaL_checkinteger(L, 1);
if (all_good(i)) {
utf8proc_property_t* props = utf8proc_get_property(i);
const utf8proc_property_t* props = utf8proc_get_property(i);

lua_createtable(L, 0, 10);

Expand Down Expand Up @@ -261,9 +265,8 @@ static int lunicode_property(lua_State* L) {
"boundclass",
"_missing", NULL};


if (all_good(i)) {
int idx = luaL_checkoption(L, 2, OPTIONS[9], &OPTIONS);
int idx = luaL_checkoption(L, 2, OPTIONS[9], OPTIONS);
utf8proc_property_t* props = utf8proc_get_property(i);

switch (idx) {
Expand Down Expand Up @@ -314,7 +317,7 @@ static const luaL_Reg lunicode_methods[] = {
{NULL, NULL}
};

LUALIB_API int luaopen_lunicode(lua_State* L) {
LUA_API int luaopen_lunicode(lua_State* L) {
luaL_newlib(L, lunicode_methods);

lua_pushliteral(L, "metadata");
Expand Down

0 comments on commit b4881e7

Please sign in to comment.