forked from weaselab/weaseljson
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf3f2fe810 | ||
|
|
bd53e57b8e |
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
@@ -29,17 +29,11 @@ WeaselJsonParser_create(int stackSize, const WeaselJsonCallbacks *callbacks,
|
||||
|
||||
__attribute__((visibility("default"))) void
|
||||
WeaselJsonParser_reset(WeaselJsonParser *parser) {
|
||||
if (parser == nullptr) {
|
||||
return;
|
||||
}
|
||||
((Parser3 *)parser)->reset();
|
||||
}
|
||||
|
||||
__attribute__((visibility("default"))) void
|
||||
WeaselJsonParser_destroy(WeaselJsonParser *parser) {
|
||||
if (parser == nullptr) {
|
||||
return;
|
||||
}
|
||||
((Parser3 *)parser)->~Parser3();
|
||||
free(parser);
|
||||
}
|
||||
|
||||
+8
-6
@@ -83,26 +83,28 @@ struct Parser3 {
|
||||
[[nodiscard]] WeaselJsonStatus parse(char *buf, int len);
|
||||
|
||||
void flushNumber(bool done, char *buf) {
|
||||
int len = buf - dataBegin;
|
||||
int len = (intptr_t)buf - (intptr_t)dataBegin;
|
||||
assert(len >= 0);
|
||||
if (done || len > 0) {
|
||||
callbacks->on_number_data(userdata, dataBegin, len, done);
|
||||
callbacks->on_number_data(userdata, dataBegin ? dataBegin : "", len,
|
||||
done);
|
||||
}
|
||||
}
|
||||
|
||||
void flushString(bool done, char *buf) {
|
||||
int len;
|
||||
if (!(flags & WeaselJsonRaw)) {
|
||||
len = writeBuf - dataBegin;
|
||||
len = (intptr_t)writeBuf - (intptr_t)dataBegin;
|
||||
} else {
|
||||
len = buf - dataBegin;
|
||||
len = (intptr_t)buf - (intptr_t)dataBegin;
|
||||
}
|
||||
assert(len >= 0);
|
||||
if (done || len > 0) {
|
||||
const char *data = dataBegin ? dataBegin : "";
|
||||
if (inKey) {
|
||||
callbacks->on_key_data(userdata, dataBegin, len, done);
|
||||
callbacks->on_key_data(userdata, data, len, done);
|
||||
} else {
|
||||
callbacks->on_string_data(userdata, dataBegin, len, done);
|
||||
callbacks->on_string_data(userdata, data, len, done);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
-14
@@ -246,20 +246,6 @@ TEST_CASE("create rejects too-small stack") {
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
TEST_CASE("reset and destroy accept null parser") {
|
||||
// Creation can legitimately fail and return null. The cleanup functions must
|
||||
// tolerate a null pointer the same way free(nullptr) is a no-op.
|
||||
auto c = noopCallbacks();
|
||||
WeaselJsonParser *parser = WeaselJsonParser_create(-1, &c, nullptr, 0);
|
||||
REQUIRE(parser == nullptr);
|
||||
WeaselJsonParser_reset(parser); // must not crash
|
||||
WeaselJsonParser_destroy(parser); // must not crash
|
||||
|
||||
// Calling reset/destroy on literal nullptr directly must also be safe.
|
||||
WeaselJsonParser_reset(nullptr);
|
||||
WeaselJsonParser_destroy(nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("parse rejects negative length") {
|
||||
auto c = noopCallbacks();
|
||||
auto *parser = WeaselJsonParser_create(1024, &c, nullptr, 0);
|
||||
@@ -317,6 +303,24 @@ TEST_CASE("reset clears inKey and transient state") {
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
TEST_CASE("scalar ending at chunk boundary is finalized at EOF") {
|
||||
// A number whose digits exactly fill the first chunk must not invoke
|
||||
// undefined behaviour on the EOF call, and must still signal completion.
|
||||
auto c = serializeCallbacks();
|
||||
SerializeState state;
|
||||
auto *parser = WeaselJsonParser_create(1024, &c, &state, 0);
|
||||
REQUIRE(parser != nullptr);
|
||||
|
||||
std::string chunk = "123";
|
||||
REQUIRE(WeaselJsonParser_parse(parser, chunk.data(), chunk.size()) ==
|
||||
WeaselJson_AGAIN);
|
||||
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) == WeaselJson_OK);
|
||||
|
||||
CHECK(state.result == "(123)");
|
||||
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
void doTestUnescapingUtf8(std::string const &escaped,
|
||||
std::string const &expected, int stride, int flags) {
|
||||
CAPTURE(escaped);
|
||||
|
||||
@@ -95,20 +95,8 @@ def test_create_rejects_too_small_stack():
|
||||
raise AssertionError(f"expected ValueError for stackSize={stack_size}")
|
||||
|
||||
|
||||
def test_missing_library_raises_oserror():
|
||||
try:
|
||||
weaseljson.WeaselJsonParser(
|
||||
weaseljson.WeaselJsonCallbacksBase(),
|
||||
build_dir="/nonexistent",
|
||||
)
|
||||
except OSError:
|
||||
return
|
||||
raise AssertionError("expected OSError when the shared library is missing")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_object_keys_routed_correctly()
|
||||
test_mixed_values()
|
||||
test_create_rejects_too_small_stack()
|
||||
test_missing_library_raises_oserror()
|
||||
print("python bindings ok")
|
||||
|
||||
+7
-1
@@ -84,7 +84,13 @@ class WeaselJsonParser:
|
||||
pass
|
||||
|
||||
if self._lib is None:
|
||||
raise OSError(f"Could not load libweaseljson from {build_dir}")
|
||||
import sys
|
||||
|
||||
print(
|
||||
"Could not find libweaseljson implementation",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
self._lib.WeaselJsonParser_create.argtypes = (
|
||||
ctypes.c_int,
|
||||
|
||||
Reference in New Issue
Block a user