@@ -28,8 +28,9 @@ JSString("initializeProgram({\"foo\":\"bar\"});")
28
28
29
29
## Interpolation
30
30
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.).
33
34
34
35
``` julia
35
36
julia> foo = 42 ;
@@ -60,33 +61,38 @@ JSString("const link = <a href=\"https://julialang.org/\">\"Julia\"</a>")
60
61
Objects are ubiquitous in JavaScript.
61
62
To create objects using JSExpr, you can use a simple syntax using braces.
62
63
There are two variants of this syntax (_ NamedTuple_ style and _ Pair_ style).
64
+ You can also create objects use normal ` NamedTuple ` syntax.
63
65
64
66
``` julia
65
- # NamedTuple style
67
+ # NamedTuple braces style
66
68
julia> @js { foo= " foo" , bar= " bar" }
67
69
JSString (" {\" foo\" : \" foo\" , \" bar\" : \" bar\" }" )
68
70
69
- # Pair style
71
+ # Pair braces style (similar to Dict constructor)
70
72
julia> @js { :foo => " foo" , :bar => " bar" }
71
73
JSString (" {\" foo\" : \" foo\" , \" bar\" : \" bar\" }" )
74
+
75
+ # NamedTuple syntax
76
+ julia> @js (foo= " foo" , bar= " bar" )
77
+ JSString (" {\" foo\" : \" foo\" , \" bar\" : \" bar\" }" )
72
78
```
73
79
74
80
#### Why not ` Dict ` ?
75
81
JSExpr does not attempt to translate _ semantics_ between Julia and JavaScript
76
82
(with a few very minor exceptions covered in _ Juliaisms_ below).
77
83
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.
79
85
80
86
## Juliaisms
81
87
JSExpr, for the most part, does not attempt to translate semantics between
82
88
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.
85
91
We do, however, translate a few Julian constructs to a _ semantically_ equivalent
86
92
JavaScript.
87
93
88
94
#### 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
90
96
loop body ` n ` times is to use a C-style ` for ` loop. There is no syntax for this
91
97
style of for loop in Julia, and ` : ` is not a valid JavaScript identifier, so the
92
98
colon function (` : ` ) is translated to JavaScript code that acts like a ` Range `
@@ -99,7 +105,7 @@ julia> @js for i in 1:10
99
105
JSString (" for (let i of (new Array(10).fill(undefined).map((_, i) => i + 1))) { console.log(i); }" )
100
106
```
101
107
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
103
109
should only be used for relatively small ranges.
104
110
105
111
## Serialization
@@ -123,19 +129,25 @@ julia> JSON.print(Dict("foo" => "bar", "bar"=>f))
123
129
- JavaScript keywords (` @new ` , ` @var ` , ` @let ` , ` @const ` )
124
130
125
131
## Unsupported Expressions
132
+ ### Not Yet Supported
126
133
* Ternary expressions (` ... ? ... : ... ` )
127
134
* ` try ` / ` catch `
135
+
136
+ ### Might Never Be Supported
128
137
* Object destructuring
129
138
* Argument splatting
130
139
131
140
If you notice anything else that's not supported or doesn't work as intended,
132
141
please [ open an issue] ( https://github.com/JuliaGizmos/JSExpr.jl/issues ) .
133
142
134
143
#### 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.
137
146
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
+
139
151
There are plans to implement a heuristic to emit a ternary expression if
140
152
appropriate (e.g., if the bodies of the ternary expression contains only one
141
153
sub-expression) but this is not implemented yet.
0 commit comments