Skip to content

Commit

Permalink
Refactor: Introduce a constant for iconv errors
Browse files Browse the repository at this point in the history
There's no universal system for error handling in place yet, but since selene can't parse LuaJIT's ULL syntax extension, might as well move this constant to C++ already.
  • Loading branch information
rdw-software committed Jan 16, 2025
1 parent 70b6dfb commit 1f3c4a2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions Runtime/Bindings/FFI/iconv/iconv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ local ffi_string = ffi.string
local tonumber = tonumber
local tostring = tostring

local iconv = {
ERROR_CONVERSION_FAILED = ffi.cast("size_t", -1ULL),
}
local iconv = {}

iconv.cdefs = [[
typedef void* iconv_t;
Expand All @@ -24,6 +22,9 @@ struct static_iconv_exports_table {
iconv_t (*iconv_open)(const char* input_encoding, const char* output_encoding);
int (*iconv_close)(iconv_t conversion_descriptor);
size_t (*iconv)(iconv_t conversion_descriptor, char** input, size_t* input_size, char** output, size_t* output_size);
// Shared constants
size_t CHARACTER_CONVERSION_FAILED;
};
]]
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Bindings/FFI/iconv/iconv_exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ struct static_iconv_exports_table {
iconv_t (*iconv_open)(const char* input_encoding, const char* output_encoding);
int (*iconv_close)(iconv_t conversion_descriptor);
size_t (*iconv)(iconv_t conversion_descriptor, char** input, size_t* input_size, char** output, size_t* output_size);

// Shared constants
size_t CHARACTER_CONVERSION_FAILED;
};
5 changes: 4 additions & 1 deletion Runtime/Bindings/FFI/iconv/iconv_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ iconv_result_t iconv_convert(char* input, size_t input_length, const char* input
size_t num_input_bytes_left = input_length;

iconv_t conversion_descriptor = iconv_open(output_encoding, input_encoding);
if(conversion_descriptor == (iconv_t)-1) {
if(reinterpret_cast<size_t>(conversion_descriptor) == iconv_ffi::CHARACTER_CONVERSION_FAILED) {
result.message = strerror(errno);
result.status_code = errno;
result.num_bytes_written = 0;
Expand Down Expand Up @@ -53,6 +53,9 @@ namespace iconv_ffi {
.iconv_open = &iconv_open,
.iconv_close = &iconv_close,
.iconv = &iconv,

// Shared constants
.CHARACTER_CONVERSION_FAILED = CHARACTER_CONVERSION_FAILED,
};

return &exports;
Expand Down
2 changes: 2 additions & 0 deletions Runtime/Bindings/FFI/iconv/iconv_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
#include "iconv_exports.h"

namespace iconv_ffi {
constexpr std::size_t CHARACTER_CONVERSION_FAILED = static_cast<size_t>(-1);

void* getExportsTable();
}
8 changes: 4 additions & 4 deletions Tests/BDD/iconv-library.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ describe("iconv", function()

it("should indicate an error if the requested conversion isn't supported", function()
local descriptor = iconv.bindings.iconv_open("Not-a-real-encoding", "UTF-8")
assertEquals(ffi.cast("size_t", descriptor), iconv.ERROR_CONVERSION_FAILED)
assertEquals(ffi.cast("size_t", descriptor), iconv.bindings.CHARACTER_CONVERSION_FAILED)
iconv.bindings.iconv_close(descriptor)
end)

it("should return a valid handle if the conversion is supported", function()
local descriptor = iconv.bindings.iconv_open("CP949", "UTF-8")
assertFalse(ffi.cast("size_t", descriptor) == iconv.ERROR_CONVERSION_FAILED)
assertFalse(ffi.cast("size_t", descriptor) == iconv.bindings.CHARACTER_CONVERSION_FAILED)
iconv.bindings.iconv_close(descriptor)
end)
end)

describe("iconv", function()
it("should be able to convert Windows encodings to UTF-8", function()
local descriptor = iconv.bindings.iconv_open("UTF-8", "CP949")
assert(descriptor ~= iconv.ERROR_CONVERSION_FAILED, "Failed to create conversion descriptor")
assert(descriptor ~= iconv.bindings.CHARACTER_CONVERSION_FAILED, "Failed to create conversion descriptor")

local input = "\192\175\192\250\192\206\197\205\198\228\192\204\189\186"
local inputSize = ffi.new("size_t[1]", #input)
Expand All @@ -114,7 +114,7 @@ describe("iconv", function()
local outputRef = ffi.new("char*[1]", outputBuffer)

local result = iconv.bindings.iconv(descriptor, inputRef, inputSize, outputRef, outputSize)
assert(result ~= iconv.ERROR_CONVERSION_FAILED, "Conversion failed")
assert(result ~= iconv.bindings.CHARACTER_CONVERSION_FAILED, "Conversion failed")

local convertedSize = 256 - outputSize[0]
local converted = ffi.string(outputBuffer, convertedSize)
Expand Down

0 comments on commit 1f3c4a2

Please sign in to comment.