Skip to content

Commit

Permalink
[spec/ysh-json] Test nested messages
Browse files Browse the repository at this point in the history
C++ can handle nesting pretty well, even with recursion.  But it would
be nice to get rid of the seg fault.
  • Loading branch information
Andy C committed Jan 13, 2024
1 parent b407fdd commit 422d71b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
36 changes: 36 additions & 0 deletions data_lang/json-survey.sh
Expand Up @@ -192,7 +192,43 @@ decode-utf8-in-surrogate-range() {
# can't decode!
nodejs -e 'var u = new Uint8Array([0xed, 0xa0, 0xbe]); var string = new TextDecoder("utf-8").decode(u); console.log(string);'
echo
}

pairs() {
local nums
nums=$(seq $1)

echo -n '['
for i in $nums; do
echo -n '[42,'
done
echo -n '43]'
for i in $nums; do
echo -n ']'
done
}

decode-deeply-nested() {
local msg
msg=$(pairs 40200)

# RuntimeError
echo "$msg" | python2 -c 'import json, sys; print(repr(json.load(sys.stdin)))' || true

# RecursionError
echo "$msg" | python3 -c 'import json, sys; print(repr(json.load(sys.stdin)))' || true

# Hm node.js handles it fine? Probably doesn't have a stackful parser.
# [ [ [ [Array] ] ] ]
echo "$msg" | nodejs -e 'var fs = require("fs"); var stdin = fs.readFileSync(0, "utf-8"); console.log(JSON.parse(stdin));' || true

echo "$msg" | bin/osh -c 'json read; = _reply' || true

# Hm this works past 40K in C++! Then segmentation fault. We could put an
# artifical limit on it.
local osh=_bin/cxx-opt/osh
ninja $osh
echo "$msg" | $osh -c 'json read; = _reply; echo $[len(_reply)]' || true
}

"$@"
15 changes: 8 additions & 7 deletions doc/j8-notation.md
Expand Up @@ -6,19 +6,17 @@ default_highlighter: oils-sh
J8 Notation
===========

J8 Notation is a set of text interchange formats for **strings**, **bytes**,
tree-shaped **records**, and **tables**. It's built on [JSON]($xref), and
backward compatible with it.
J8 Notation is a set of text interchange formats. It specifies a strict syntax
for **strings** / bytes, tree-shaped **records**, line-based **streams**, and
**tables**.

As part of the Oils project, it was designed to solve the *JSON-Unix Mismatch*.
It's part of the Oils project, and was designed to solve the *JSON-Unix
Mismatch*. It's backward compatible with [JSON]($xref), and built on it.

But J8 notation isn't only for Oils — just like JSON isn't only for
JavaScript. Any language that has a JSON library should also have a J8
library.

(J8 Notation replaces the older [QSN](qsn.html) design, which wasn't compatible
with JSON.)

<!--
it's **not** specific to Oils. This is just like JSON isn't specific to
JavaScript. Today, a Python program and a Go program may communicate with
Expand All @@ -28,6 +26,9 @@ JavaScript. Today, a Python program and a Go program may communicate with
<div id="toc">
</div>

(Historical note: As of January 2024, J8 Notation replaces the [QSN](qsn.html)
design, which wasn't as consistent with both JSON and YSH code.)

## Quick Picture

<style>
Expand Down
29 changes: 29 additions & 0 deletions spec/ysh-json.test.sh
Expand Up @@ -708,3 +708,32 @@ echo status=$?
(Str) "μ"
status=1
## END

#### decode deeply nested structure (stack overflow)

shopt -s ysh:upgrade

proc pairs(n) {
var m = int(n) # TODO: 1 .. n should auto-convert?

for i in (1 .. m) {
write -n -- '['
}
for i in (1 .. m) {
write -n -- ']'
}
}

# This is all Python can handle; C++ can handle more
msg=$(pairs 50)

#echo $msg

echo "$msg" | json read
pp line (_reply)
echo len=$[len(_reply)]

## STDOUT:
(List) [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
len=1
## END

0 comments on commit 422d71b

Please sign in to comment.