Repository navigation
Conversation
|
Review requested:
|
This comment was marked as outdated.
This comment was marked as outdated.
Add the header-only toml++ library, its updater script and license entry, for use by node:formats/toml.
Add a TOML parser and serializer backed by toml++, available behind `--experimental-toml` as the scheme-only `node:formats/toml` builtin. Assisted-by: Devin
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
This can be reverted once at least one serializer it documents is enabled by default. Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Version changes
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #67874 +/- ##
==========================================
- Coverage 90.58% 90.56% -0.03%
==========================================
Files 802 804 +2
Lines 286656 286816 +160
Branches 55369 55378 +9
==========================================
+ Hits 259668 259753 +85
- Misses 17089 17184 +95
+ Partials 9899 9879 -20
🚀 New features to boost your workflow:
|
jsumners-nr
left a comment
There was a problem hiding this comment.
Love seeing this move so quickly. Just a bit concerned that the docs attempt to enforce the same API across all formats/ namespaced modules.
| Every format module exports two functions: | ||
|
|
||
| * `parse(input[, options])` turns a document into JavaScript values. | ||
| * `stringify(value)` turns JavaScript values into a document. |
There was a problem hiding this comment.
I think this may be too restrictive. Each module should strive for such a simple API, but it should also be allowed to export an interface that makes sense for the module.
There was a problem hiding this comment.
The docs are not a spec, they only document the existing behavior. If another format is added, the docs will need to be updated anyway. In any case, happy to update if you have a suggestion
There was a problem hiding this comment.
I think the opening paragraph is sufficient. Lines 11 through 14 can be removed and everything will still make sense.
| An attempt was made to issue Server Name Indication from a TLS server-side | ||
| socket, which is only valid from a client. | ||
|
|
||
| <a id="ERR_TOML_PARSE"></a> |
There was a problem hiding this comment.
Nit... Let's just make sure the err codes with the various formats are consistently named.
| * `input` {string|ArrayBuffer|SharedArrayBuffer|TypedArray|DataView} The TOML | ||
| document. Binary input must be UTF-8. A leading byte order mark is ignored. | ||
| * `options` {Object} | ||
| * `bigint` {boolean} If `true`, every integer is returned as a {bigint}. |
There was a problem hiding this comment.
This should also support a non-throwing parse mode, like the yaml case.
|
|
||
| // Basic document. | ||
| { | ||
| const doc = TOML.parse(` |
There was a problem hiding this comment.
It might be worthwhile using fixtures here to help the tests be more readable
| for (const auto& [key, node] : entries) { | ||
| Local<String> name; | ||
| Local<Value> value; | ||
| if (!String::NewFromUtf8(isolate_, |
| KeyCollectionMode::kOwnOnly, | ||
| static_cast<PropertyFilter>(PropertyFilter::ONLY_ENUMERABLE | | ||
| PropertyFilter::SKIP_SYMBOLS), | ||
| v8::IndexFilter::kIncludeIndices, |
| out->push_back(std::move(*node)); | ||
| } | ||
| Leave(); | ||
| return Just(true); |
There was a problem hiding this comment.
This never returns Just(false)?
Would MaybeVoid be better?
| return std::make_unique<::toml::value<int64_t>>(integer); | ||
| } | ||
| if (value->IsArray()) { | ||
| auto array = std::make_unique<::toml::array>(); |
There was a problem hiding this comment.
Would be nice if this could accept sequences/sync iterators
| Local<Array> info = args[3].As<Array>(); | ||
| Local<String> message; | ||
| if (!String::NewFromUtf8(isolate, | ||
| description.data(), |
|
|
||
| Local<String> result; | ||
| if (!String::NewFromUtf8(isolate, | ||
| output.data(), |
| case kLocalDate: | ||
| return Temporal.PlainDate.from(text); | ||
| default: | ||
| return Temporal.PlainTime.from(text); |
There was a problem hiding this comment.
I'm not familiar yet with the primordial captures for Temporal yet... Are these safe captures?
There was a problem hiding this comment.
It can be mutated by userland so indeed we would need a better solution to remove any temptation of relying on monkey-patching. I should at least add a TODO
|
Note that if #67157 gets merged, it may be possible to use |
|
|
||
| // The JS layer returns an RFC 3339 string. Let toml++ parse it so that it | ||
| // ends up with the right datetime type. | ||
| std::unique_ptr<::toml::node> ParseDatetime(Local<Value> text) { |
There was a problem hiding this comment.
This really has a fair amount of unnecessary overhead. Every datetime in stringify runs a full TOML parse... First it's serialized to an ISO 8601 string. Then we put "v = " in front and call toml::parse() (which builds a whole table), pull v back out and copy it. That's a parser run per timestamp. Could we just pass the numeric fields from JS and build toml::date/time/date_time directly? Or parse the fixed-format string by hand?
| return Number::New(isolate_, node.as_floating_point()->get()); | ||
| case ::toml::node_type::boolean: | ||
| return Boolean::New(isolate_, node.as_boolean()->get()); | ||
| case ::toml::node_type::date: { |
There was a problem hiding this comment.
This snprintfs the fields into a std::string (which allocates once offsets or nanoseconds are present), then make a JS string, add [offset], then have Temporal parse it all again. Pass the numbers and call the Temporal constructors directly. Point is this feels very heavyweight and non-performant for what it's doing.
| } | ||
|
|
||
| // Returns nullptr when a JavaScript exception is pending. | ||
| std::unique_ptr<::toml::node> BuildValue(Local<Value> value) { |
There was a problem hiding this comment.
This appears to allocate everything twice, and strings get copied twice.
| : isolate_(isolate), | ||
| context_(context), | ||
| format_datetime_(format_datetime), | ||
| object_prototype_(Object::New(isolate)->GetPrototype()), |
There was a problem hiding this comment.
This will allocate a throw away object on every parse. Why is this necessary?
| static_cast<unsigned>(date.month), | ||
| static_cast<unsigned>(date.day)); | ||
| out->append(buf); | ||
| } |
There was a problem hiding this comment.
The snprintf and copy here just add unnecessary overhead. I would drop snprintf and write the digits by hand.
Something like... (had an agent whip this up quickly)
inline char* Write2(char* p, unsigned v) {
p[0] = '0' + v / 10;
p[1] = '0' + v % 10;
return p + 2;
}
inline char* Write4(char* p, unsigned v) {
p = Write2(p, v / 100);
return Write2(p, v % 100);
}
char* WriteDate(char* p, const ::toml::date& d) {
DCHECK_LE(d.year, 9999); // TOML only allows 4-digit years
p = Write4(p, d.year);
*p++ = '-';
p = Write2(p, d.month);
*p++ = '-';
return Write2(p, d.day);
}
char* WriteTime(char* p, const ::toml::time& t) {
p = Write2(p, t.hour);
*p++ = ':';
p = Write2(p, t.minute);
*p++ = ':';
p = Write2(p, t.second);
if (uint32_t ns = t.nanosecond) {
*p++ = '.';
int digits = 9;
while (ns % 10 == 0) { ns /= 10; digits--; }
for (int i = digits - 1; i >= 0; i--, ns /= 10) p[i] = '0' + ns % 10;
p += digits;
}
return p;
}But we really don't need the std::string either. Give Convert a char buf[48] on the stack, chain the writers, and hand OneByteString(isolate_, buf, end - buf) to CreateDatetime. No heap allocation, no scratch copy, and no snprintf.
That said, all of this still feels unnecessary. If we passed the numbers to JS and called the Temporal constructors directly, this formatting code and Temporal's re-parse could both go away.
|
Some performance related concerns, but consider them non-blocking for landing this PR. I'd like to see perf improvements and benchmarks landed before it graduates from experimental. |
Related to #67157 //cc @marcopiraccini
Opt-in with a configure flag, so it can be backported. The plan is to make it opt-out in a future semver-major.