Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8ab7bc6
document: quote, JSON.stringify's quoting of a string alone
Nov 14, 2026
d6a6d07
document: the page shows quote, writing a JSON text field by field
Nov 14, 2026
1cad77e
document: pin quote's tests on JSON.stringify's bytes, and widen its …
Nov 15, 2026
1875c30
document: the storage's allocator, which holds the image
Nov 15, 2026
6408ab4
document: the allocation tests' places, a failing resource, and membe…
Nov 15, 2026
250264c
document: merge quote's review, its tests pinned on JSON.stringify's …
Nov 15, 2026
cf0da8b
document: nothing, a document that holds nothing and allocates nothing
Nov 15, 2026
9715657
document: every operation's working state in the document's allocator
Nov 15, 2026
8046861
document: the allocation tests run on Emscripten
Nov 15, 2026
cc9c917
document: the page's Allocators section, its program, and the changelog
Nov 15, 2026
070da56
document: an image's blocks 16-aligned or given back, through the all…
Nov 15, 2026
5f5f837
document: Boost.Interprocess's bad_alloc is an allocator's failure too
Nov 15, 2026
23d9660
document: const reads that ask the document's allocator for nothing
Nov 15, 2026
a7bdc88
document: a move assignment that never copies is noexcept, and alloca…
Nov 15, 2026
f1de528
document: set refuses a change it cannot make before it prepares the …
Nov 15, 2026
9be9961
document: the allocation tests catch a leak on a failure path, under …
Nov 15, 2026
4ee1ebd
document: a value's buffers allocated when it needs them, and kept to…
Nov 15, 2026
1bce979
document: the last chunk is finish_with's, finish as it was published
Nov 15, 2026
da46473
document: the page and the comments say exactly what an erasure and a…
Nov 15, 2026
e95b409
document: the lint's findings in the allocation work and in quote's D…
Nov 15, 2026
b6f526c
document: a held callable's place declared with auto, which clang-tid…
Nov 15, 2026
4a211b9
document: builds with MSVC 14.3 and 14.5
Nov 15, 2026
cd1d48d
document: the writer makes the lists of its deeper frames and of a la…
Nov 15, 2026
b584b8f
document: the allocation tests count and fail the document's own bloc…
Nov 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ specific to document.
- No floating point outside `js_traits` and its codec; no hashed container; nodes are built in place
(`emplace`), never copied from a temporary; the allocator zeroes every block it hands out and
frees.
- Every allocation an operation makes outside the image goes through the document's allocator,
rebound (`detail::rebound`, constructed from it, never converted to), and is made before the
operation's first change, so that with exceptions an allocator's failure, which
`detail::unless_allocation_fails` alone catches (`std::bad_alloc`, a type derived from it, or
Boost.Interprocess's `bad_alloc`), is `errc::capacity` with the document's content as it was; an
erasure, a lookup and `==` allocate nothing, and a `const` operation allocates in the document's
allocator only as the page's "Reads from several threads" lists. An image's blocks the allocator
gives at another alignment than 16 are given back, and a change a place cannot take is refused
before its value is prepared. The `allocator*` tests count the global `operator new` and fail an
operation at each of its allocations: a container of the standard allocator in an operation fails
them. Boost.JSON's suspension stack, in a parse fed in chunks, is the one exception.
- The values of `errc` and `kind`, the category's identity `0x646f63756d656e74` ("document") and the
image's header layout never change; a layout change raises the image's format version.
- Boost.MultiIndex erases an element whose `modify` throws: a `modify` only swaps prepared values.
Expand All @@ -44,15 +55,16 @@ specific to document.
- `doc/document.adoc` includes one file per section, in this order: `introduction.adoc`,
`gains.adoc`, `js-module.adoc`, `javascript.adoc`, `handles.adoc`, `numbers.adoc`,
`strings.adoc`, `order.adoc`, `arrays.adoc`, `containers.adoc`, `stringify.adoc`,
`parsing.adoc`, `streams.adoc`, `selectors.adoc`, `traits.adoc`, `images.adoc`, `wire.adoc`,
`sharing.adoc`, `boost-json.adoc`, `exceptions.adoc`, `errors.adoc`, `testing.adoc`,
`benchmarks.adoc`, then the reference and the changelog.
`parsing.adoc`, `streams.adoc`, `selectors.adoc`, `traits.adoc`, `images.adoc`,
`allocation.adoc`, `wire.adoc`, `sharing.adoc`, `boost-json.adoc`, `exceptions.adoc`,
`errors.adoc`, `testing.adoc`, `benchmarks.adoc`, then the reference and the changelog.
`b2 libs/document/doc` builds it with the strict reference.
- Each `@see "<title>"` of a Doc Comment names a section's title: "Introduction", "What C++
gives JavaScript", "Using it from JavaScript", "From JavaScript", "Handles", "Numbers", "Strings,
dates and bytes", "Key order", "Arrays", "Containers", "Writing JSON", "Parsing", "Streams of
documents", "Selectors", "Traits", "Images", "Images on the wire", "Sealed images", "Sharing",
"Compared with Boost.JSON", "The sugar that throws", "Errors", "Testing", "Benchmarks".
documents", "Selectors", "Traits", "Images", "Allocators", "Images on the wire", "Sealed
images", "Sharing", "Compared with Boost.JSON", "The sugar that throws", "Errors", "Testing",
"Benchmarks".
Renaming a section renames its `@see` lines in the same commit; doc-check fails otherwise.
- The guide's programs are `example/document_<topic>.cpp`, each with its `.expected`, whose lines
are what Node prints for the same JavaScript wherever the page does not say otherwise; check a
Expand All @@ -67,5 +79,9 @@ specific to document.
ways.
- `document_image.expected` holds a digest of an image, which is the same on every target: a
change to the image's layout, or to what the example stores, changes it on every target at once.
- `document_allocators.expected` is C++'s alone, which its section says: its JSON lines are what
Node prints for the same steps, and it prints nothing that depends on where a resource's bytes go
(a count or size of allocations), which libstdc++'s and libc++'s growth policies and small-string
buffers make differ between targets.
- The README copies `example/document_quick_start.cpp[tag=program]` and its output; doc-check
compares the copy at every doc build.
337 changes: 337 additions & 0 deletions doc/allocation.adoc

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions doc/changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ of every version are on webcpp's index page.
=== Unreleased

// tag::unreleased[]
* *Breaking change:* Changed `owned` and `external` into aliases of `basic_owned<>` and
`basic_external<>`: a forward declaration `struct owned;` includes `<webcpp/document/storage.hpp>`
instead.
* Added allocators to the storages, `pmr::document` among them, holding the image and every
operation's working state.
* Improved allocation failures: with exceptions, an allocator with no room, Boost.Interprocess's
among them, makes an operation return `errc::capacity`.
* Added `finish_with`, which ends a parser's text with its last chunk or the whole text.
* Added `quote`, which appends a string to a string of the caller's as `JSON.stringify` quotes it.
// end::unreleased[]

[#document-changelog-0-1-0]
Expand Down
2 changes: 2 additions & 0 deletions doc/document.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ include::traits.adoc[]

include::images.adoc[]

include::allocation.adoc[]

include::wire.adoc[]

include::sharing.adoc[]
Expand Down
18 changes: 11 additions & 7 deletions doc/errors.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ unchanged, in its own category.
| `wd::range_error`

| `capacity`, 4
| no room: a full external buffer, an owned document at its `max_bytes`, a parser's limit
| no room: a full external buffer, an owned document at its `max_bytes`, a parser's limit, an
allocator that has none, with exceptions (<<document-allocators-failure>>)
| none; `RangeError` from the module
| `wd::capacity_error`

Expand Down Expand Up @@ -78,7 +79,7 @@ include::{examples}/document_no_exceptions.cpp[tag=errors,indent=0]
include::{examples}/document_no_exceptions.expected[lines=1..8]
----

An operation that fails changes nothing, so the program goes on with the document as it was:
An operation that fails leaves the document's content as it was, so the program goes on with it:

[source]
----
Expand All @@ -90,8 +91,11 @@ include::{examples}/document_no_exceptions.cpp[tag=compare,indent=0]
include::{examples}/document_no_exceptions.expected[lines=9]
----

A failure of the process heap is not an error value. The image's heap is never exhausted by
surprise, since every operation finds its room first, but a few things live on the process heap:
the text `stringify` returns, the key a handle of a missing member holds, and the parser's frames.
The standard library's `std::bad_alloc` there is thrown with exceptions and ends the program
without them, as it does for any {cpp} container.
The image's heap is never exhausted by surprise, since every operation finds its room first, and the
working state of an operation, the records of a change, the parser's frames and the key a handle of
a missing member holds among them, is in the document's allocator. With exceptions, an allocator
that has no room is `errc::capacity`, and the document's content is as it was; without them, an
allocator that fails ends the program, as it does for any {cpp} container
(<<document-allocators-failure>>). What the program gets back in the standard allocator, the text
`stringify` returns among them, fails as any `std::string` does, with `std::bad_alloc`
(<<document-allocators-outside>>).
3 changes: 3 additions & 0 deletions doc/exceptions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ include::{examples}/document_javascript.expected[lines=23..29]
`as<std::string>()`.
* *`wd::document d;` holds `null`,* and the copy constructor gives an equal and independent copy.
Both are for owned documents only: an external document has no buffer to be made or copied into.
The copy is in the allocator that `select_on_container_copy_construction` gives, as a standard
container's copy is: for a `wd::pmr::document`, the default resource, where `doc.clone()` keeps
the document's own (<<document-allocators-moves>>).
* *An external document throws `wd::capacity_error` when it is full,* where `set` would return
`errc::capacity`.

Expand Down
18 changes: 11 additions & 7 deletions doc/images.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,23 @@ changed by accident and is not authentication: *never open an image from a sourc
[#document-images-storage]
=== Owned and external storage

A document's second template parameter is its storage:
A document's second template parameter is its storage, which also carries the allocator the
document allocates through (<<document-allocators>>):

[cols="1,2,3",options="header"]
|===
| Storage | The buffer | When it is full

| `wd::owned`, the default
| the document's own, 16-aligned
| `wd::owned`, the default; `wd::basic_owned<Allocator>`
| the document's own, 16-aligned, from its allocator: for `wd::owned`, the global aligned
`operator new`
| it grows: a buffer twice the size, or what the operation needs if more, the image copied and the
heap grown, up to the `max_bytes` of `create` or `from_image`, 1 GiB unless told otherwise; past
it, `errc::capacity`
it, or when the allocator has no room, `errc::capacity`

| `wd::external`
| a `std::span<std::byte>` its user owns and keeps alive, such as a browser's memory, 16-aligned
| `wd::external`; `wd::basic_external<Allocator>`
| a `std::span<std::byte>` its user owns and keeps alive, such as a browser's memory, 16-aligned;
the allocator holds only the working state of the document's operations
| the operation returns `errc::capacity` and changes nothing
|===

Expand Down Expand Up @@ -235,4 +238,5 @@ The image's size afterwards is `compact_size()`, which is also its `capacity()`,
that needs room grows it.

A moved document takes the image of the one it is moved from, which then holds nothing: its
handles, and those of the document moved into, are invalid.
handles, and those of the document moved into, are invalid. A move assignment between allocators
that are not equal and do not propagate copies the image instead (<<document-allocators-moves>>).
7 changes: 4 additions & 3 deletions doc/introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ handles that read and write a document; <<document-numbers>>, <<document-strings
array methods. <<document-stringify>>, <<document-parsing>>, <<document-streams>> and
<<document-selectors>> write and read JSON text, whole or in pieces, and <<document-containers>>
shows the containers a {cpp} program walks. <<document-traits>> chooses what a document stores;
<<document-images>> shows the binary image, <<document-wire>> sends it, and <<document-sharing>>
shares it. <<document-vs-boost-json>> compares document with Boost.JSON, <<document-exceptions>>
teaches the sugar that throws, and <<document-errors>> lists every error. <<document-testing>> shows
<<document-images>> shows the binary image, <<document-allocators>> places it and every
operation's memory in an allocator, <<document-wire>> sends it, and <<document-sharing>> shares it.
<<document-vs-boost-json>> compares document with Boost.JSON, <<document-exceptions>> teaches the
sugar that throws, and <<document-errors>> lists every error. <<document-testing>> shows
how document is tested, <<document-benchmarks>> what it costs, and the <<reference,reference>>
closes the page.
21 changes: 15 additions & 6 deletions doc/parsing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ document's parser never throws: its input is the network's, and every failure is

`wd::parse<Traits>` parses into a document of other traits, as `wd::parse<wd::js_traits>(text)`
does in <<document-numbers>>; `wd::parse_into(buffer, text)` parses into a buffer the program owns
(<<document-images>>); and `wd::parse_or_throw(text)`, with exceptions only, throws as
`JSON.parse` does (<<document-exceptions>>).
(<<document-images>>); `wd::parse(text, options, alloc)` and
`wd::parse_into(buffer, text, options, alloc)` parse into a document of an allocator's, which holds
the parse's working state too (<<document-allocators>>); and `wd::parse_or_throw(text)`, with
exceptions only, throws as `JSON.parse` does (<<document-exceptions>>).

[#document-parsing-options]
=== Limits and revivers
Expand Down Expand Up @@ -71,8 +73,9 @@ builds itself, with `create`, keeps a limit of 1 GiB (<<document-images>>).
`wd::parser` is the parser itself, `wd::basic_parser<Traits, Storage>` for other traits or a
buffer of the program's: `write(chunk)` takes the text in chunks of any size, split anywhere,
inside a string, a number, an escape or a character's bytes, down to one byte at a time, and
`finish()` ends the text and returns the document. The document a parser builds is the one a
whole parse builds, however the text is split:
`finish()` ends the text and returns the document; `finish_with(last)` takes a last chunk, or the
whole text, and ends it. The document a parser builds is the one a whole parse builds, however the
text is split:

[source]
----
Expand All @@ -86,15 +89,21 @@ include::{examples}/document_streaming.expected[lines=1]

The parser builds the document node by node as each chunk arrives, so the document may grow and
its image move between two chunks; `finish` returns it. `reset()` makes the parser ready for
another text, its selectors kept (<<document-selectors>>).
another text, its selectors kept (<<document-selectors>>). The parser's working state, its frames,
the path and the pending text of a value, and its selectors and their callbacks, is in its
document's allocator, a memory resource's for `wd::pmr::parser`. The stack where Boost.JSON's
parser saves its place when a chunk ends inside a value is in the global heap; a text given whole
to `finish_with(last)`, as `wd::parse` gives it, never needs one (<<document-allocators-outside>>).

[#document-parsing-errors]
=== Errors and offsets

Malformed text is `errc::syntax`, JavaScript's `SyntaxError`. `offset()` is the count of bytes
consumed, which after a syntax error is the offset where it was found, the same however the text
was split, and `cause()` is the code of Boost.JSON's parser behind it. A number the traits refuse
is `errc::range` (<<document-numbers-refused>>), and a limit passed `errc::capacity`.
is `errc::range` (<<document-numbers-refused>>), and a limit passed `errc::capacity`, as is, with
exceptions, an allocator that has no room for the document or for the parse's working state
(<<document-allocators-failure>>).

A parser reads one text: anything but whitespace after it is `errc::syntax`, whose `cause()` is
Boost.JSON's `extra_data`, at the offset where the second text begins, whether it comes in the same
Expand Down
7 changes: 7 additions & 0 deletions doc/sharing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ include::{examples}/document_threads.expected[lines=1..4]
Every document arrived with the image the producer sent: no byte was copied and nothing was parsed
on the consumer's side.

Threads that read one document at once read it through `const` operations. A document of the
standard allocator, `wd::document` and the documents of the pools below among them, may be read by
any number of threads at once with every one of them. A document of a memory resource allocates in
it in a few `const` operations, which <<document-allocators-reads>> lists: threads that use them on
one document at once need a resource that is safe to use from several threads, such as
`std::pmr::synchronized_pool_resource`.

[#document-sharing-pool]
=== Shared documents

Expand Down
41 changes: 39 additions & 2 deletions doc/stringify.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ include::{examples}/document_quick_start.cpp[tag=stringify,indent=0]
The indent is JavaScript's: 0 writes compactly, a larger one is clamped to 10 spaces, and an empty
array or object is written `[]` or `{}` at any indent. A value that is `undefined`, a handle of
nothing or a document that holds `undefined`, writes no text, as `JSON.stringify(undefined)`
returns none. `stringify` returns a `std::string`, so its only failure is the standard library's
`std::bad_alloc`; a program that must not meet even that writes to a sink of its own.
returns none. `stringify` returns a `std::string`, in the standard allocator whatever the
document's, and keeps its stack there too (<<document-allocators-outside>>): it asks the document's
allocator for nothing, and its failure is the standard allocator's `std::bad_alloc`. A program that
must not meet one, or that keeps its text in memory of its own, writes to a sink.

[#document-stringify-to]
=== In pieces, to a sink
Expand All @@ -45,3 +47,38 @@ include::{examples}/document_streaming.expected[lines=12..13]

The first sink writes a bar after each piece of 8 bytes; the second keeps the first piece, then
fails, which stops the writing there.

`stringify_to` keeps its stack, which takes the place of recursion, and the chunk it has not yet
handed to the sink in room of its own, 16 levels of the stack and 4096 bytes of the chunk, and
allocates only past them, in the document's allocator (<<document-allocators>>): a value nested 16
deep or less, written in chunks of at most 4096 bytes, allocates nothing. With exceptions, an
allocator that has no room for them is `errc::capacity`, which stops the writing as a sink's failure
does. A sink that appends to a string of the program's choosing, a
`std::pmr::string` among them, keeps the text in memory the program chooses
(<<document-allocators-resource>>).

[#document-stringify-quote]
=== A string alone, quoted

`wd::quote(text, out)` is `JSON.stringify` of a string alone: it appends the string to `out` as a
JSON string literal, its quotes included, with the bytes `stringify` writes of the same string,
from the same code. `out` is a string of the program's own, a `std::string`, a `std::pmr::string`
or any type with `append(const char*, std::size_t)`, and the only thing that allocates, so a JSON
text of a fixed shape is written field by field into memory the program chooses, without a
document:

[source]
----
include::{examples}/document_streaming.cpp[tag=quote,indent=0]
----

[listing]
----
include::{examples}/document_streaming.expected[lines=14..15]
----

Text in a `std::string_view` is UTF-8, which `quote` checks: text that is not UTF-8, which no
JavaScript string is, returns `wd::errc::type` and leaves `out` as it was, as the last line shows,
which is {cpp}'s alone. Text in a `std::u16string_view` is UTF-16, any sequence of code units, with a
lone surrogate written as `\u` and its four lowercase hex digits, as JavaScript writes it since
ES2019; that overload has no failure of its own, and returns nothing.
16 changes: 16 additions & 0 deletions doc/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ document's tests are lightweight_test programs, one per subject, under `test/`,
operations, no byte left of an erased secret, shrunk, a full external buffer, a buffer that is
not 16-aligned, and a header whose container offset lies.

| `allocator`
| A document's allocator, with the global `operator new` counted: the image created, grown, shrunk,
cloned, moved and move-assigned in a memory resource and between two, with no call of it; the
same image in any allocator, one of the test's own among them; external documents that carry
their allocator; the copy's allocator; and, with exceptions, an allocator with no room,
`errc::capacity` with nothing changed (<<document-allocators-proof>>).

| `allocator_tree`, `allocator_values`, `allocator_parse`, `allocator_stringify`
| The working state of each operation in the document's allocator, with no call of the global
`operator new`: what a change stages and compaction's walk; the handles, missing members' keys,
deep equality, `Object.assign`, the array methods and the containers' views; a whole text parsed,
with selectors and revivers; `stringify_to` into a `std::pmr::string`. A parse in chunks calls it
exactly as often as Boost.JSON's stack grows, and `stringify` as often as its string does. Both
traits, strings and keys longer than any small-string buffer, and, with exceptions, a resource
that fails at each allocation in turn, every failure `errc::capacity` with the document unchanged.

| `stringify`
| `stringify` and `stringify_to` against Node's text: indentation, empty containers, escapes,
numbers, dates, bytes under each policy, a lone surrogate under `js_traits`, an undefined root,
Expand Down
1 change: 1 addition & 0 deletions example/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ webcpp.example document_streaming.cpp ;
webcpp.example document_selectors.cpp ;
webcpp.example document_traits.cpp ;
webcpp.example document_image.cpp ;
webcpp.example document_allocators.cpp ;
# The core in a build without exceptions, whatever the build asks.
webcpp.example document_no_exceptions.cpp : <exception-handling>off ;

Expand Down
Loading
Loading