Skip to content

Commit 805c177

Browse files
committed
Add JSON compat entry, update README.
1 parent d4a8bca commit 805c177

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "1.0.0"
77
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
88

99
[compat]
10+
JSON = "0.18"
1011
julia = "0.7, 1"
1112

1213
[extras]

README.md

+24-12
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ JSString("initializeProgram({\"foo\":\"bar\"});")
2828

2929
## Interpolation
3030

31-
You can interpolate Julia objects or `JSString`'s (e.g. from other `@js` or
32-
`js"..."` invocations) as well.
31+
You can interpolate Julia objects or `JSString`s (e.g. from other `@js` or
32+
`js"..."` invocations) as well as values from Julia (such as normal
33+
strings, `Dict`s, etc.).
3334

3435
```julia
3536
julia> foo = 42;
@@ -60,33 +61,38 @@ JSString("const link = <a href=\"https://julialang.org/\">\"Julia\"</a>")
6061
Objects are ubiquitous in JavaScript.
6162
To create objects using JSExpr, you can use a simple syntax using braces.
6263
There are two variants of this syntax (_NamedTuple_ style and _Pair_ style).
64+
You can also create objects use normal `NamedTuple` syntax.
6365

6466
```julia
65-
# NamedTuple style
67+
# NamedTuple braces style
6668
julia> @js { foo="foo", bar="bar" }
6769
JSString("{\"foo\": \"foo\", \"bar\": \"bar\"}")
6870

69-
# Pair style
71+
# Pair braces style (similar to Dict constructor)
7072
julia> @js { :foo => "foo", :bar => "bar" }
7173
JSString("{\"foo\": \"foo\", \"bar\": \"bar\"}")
74+
75+
# NamedTuple syntax
76+
julia> @js (foo="foo", bar="bar")
77+
JSString("{\"foo\": \"foo\", \"bar\": \"bar\"}")
7278
```
7379

7480
#### Why not `Dict`?
7581
JSExpr does not attempt to translate _semantics_ between Julia and JavaScript
7682
(with a few very minor exceptions covered in _Juliaisms_ below).
7783
Since `Dict` can be a valid function name in JavaScript, we do not translate
78-
the Julia `Dict` function to an object creation syntax.
84+
the Julia `Dict` constructor to an object creation syntax.
7985

8086
## Juliaisms
8187
JSExpr, for the most part, does not attempt to translate semantics between
8288
Julia and the resulting JavaScript code.
83-
The reason for the decision is due to the fact that Julia and JavaScript are
84-
wildly different languages and we would invariably screw up some edge cases.
89+
The reason for the decision is that Julia and JavaScript are
90+
wildly different languages and we would invariably mess up some edge cases.
8591
We do, however, translate a few Julian constructs to a _semantically_ equivalent
8692
JavaScript.
8793

8894
#### Range Syntax (`...:...`)
89-
JavaScript doesn't have a native `Range` object and the typical way of repeat a
95+
JavaScript doesn't have a native `Range` object and the typical way to repeat a
9096
loop body `n` times is to use a C-style `for` loop. There is no syntax for this
9197
style of for loop in Julia, and `:` is not a valid JavaScript identifier, so the
9298
colon function (`:`) is translated to JavaScript code that acts like a `Range`
@@ -99,7 +105,7 @@ julia> @js for i in 1:10
99105
JSString("for (let i of (new Array(10).fill(undefined).map((_, i) => i + 1))) { console.log(i); }")
100106
```
101107

102-
The resulting code is very ugly and will fully materialize the range and so
108+
The resulting JS is very ugly and will fully materialize the range and so
103109
should only be used for relatively small ranges.
104110

105111
## Serialization
@@ -123,19 +129,25 @@ julia> JSON.print(Dict("foo" => "bar", "bar"=>f))
123129
- JavaScript keywords (`@new`, `@var`, `@let`, `@const`)
124130

125131
## Unsupported Expressions
132+
### Not Yet Supported
126133
* Ternary expressions (`... ? ... : ...`)
127134
* `try` / `catch`
135+
136+
### Might Never Be Supported
128137
* Object destructuring
129138
* Argument splatting
130139

131140
If you notice anything else that's not supported or doesn't work as intended,
132141
please [open an issue](https://github.com/JuliaGizmos/JSExpr.jl/issues).
133142

134143
#### Ternary Expressions
135-
Julia lowers the `if` statements and ternary expressions (`... ? ... : ...`) to
136-
the same `Expr` value, so JSExpr cannot distinguish between the two.
144+
Julia lowers (during parse) `if` statements and ternary expressions (`... ? ... : ...`)
145+
to the same `Expr`, so JSExpr cannot distinguish between the two.
137146
This poses an issue because JavaScript does not allow non-expression statements
138-
(e.g., loops and variable declarations) inside of a ternary expression.
147+
(e.g., loops and variable declarations) inside of a ternary expression, but if
148+
statements cannot be used in contexts which expect a value (but they _can_ be
149+
used in such contexts in Julia).
150+
139151
There are plans to implement a heuristic to emit a ternary expression if
140152
appropriate (e.g., if the bodies of the ternary expression contains only one
141153
sub-expression) but this is not implemented yet.

0 commit comments

Comments
 (0)