The Wayback Machine - https://web.archive.org/web/20190328210322/https://github.com/JuliaGizmos/JSExpr.jl
Skip to content
Translate Julia to JavaScript
Branch: master
Clone or download
Latest commit 015f46a Dec 21, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test revert changes for rewrite Dec 21, 2018
.codecov.yml JSExprs.jl generated files. Feb 25, 2018
.gitignore
.travis.yml don't checkout webio master on travis Aug 19, 2018
LICENSE.md
README.md
REQUIRE revert changes for rewrite Dec 21, 2018
appveyor.yml switch to Julia v0.7 Aug 13, 2018

README.md

JSExpr

Build Coverage
Build Status codecov

This package provides the @js macro which translates a Julia expression to JavaScript.

Example

julia> using JSExpr

julia> @js document.querySelector("#root")
WebIO.JSString("document.querySelector(\"#root\")")

julia> @js (a,b) -> a+b
WebIO.JSString("(function (a,b){return (a+b)})")

The JSString object wraps a Julia string. You can access the plain string from the .s field.

Interpolation

You can interpolate Julia objects or JSString expressions (i.e. result of @js macro invocations) in a @js macrocall.

julia> foo = 42
42

julia> callback = @js a -> a + $foo
WebIO.JSString("(function (a){return (a+42)})")

julia> f = @js array -> array.map($callback)
WebIO.JSString("(function (array){return array.map((function (a){return (a+42)}))})")

Converting a JSString or an object containing it to JSON serializes JSString as a string.

julia> JSON.print(Dict("foo" => "bar", "bar"=>f))
{"bar":"(function (array){return array.map((function (a){return (a+42)}))})","foo":"bar"}

This is not ideal when you want to use the serialized output as JavaScript, for example in a <script> tag. In this case, you should use JSExpr.jsexpr

julia> @js $(Dict("foo" => "bar", "bar"=>f))
WebIO.JSString("{\"bar\":(function (array){return array.map((function (a){return (a+42)}))}),\"foo\":\"bar\"}")

Object literals

The @js equivalent of

{foo: 42, bar: "baz"}

is

julia> @js d(foo=42, bar="baz")
WebIO.JSString("{foo:42,bar:\"baz\"}")

or a Dict

julia> @js Dict(:foo=>42, :bar=>"baz")
WebIO.JSString("{foo:42,bar:\"baz\"}")

Supported expressions

  • Function call
  • Comparison operators
  • Dictionary / object literal
  • Anonymous functions (automatically return the result)
  • Function expressions (ditto)
  • Assignment, = and +=, -=, *=, &=, |=
  • If statements (@var expressions are not allowed in if statements yet). Note: if expressions are lowered to the ternary operator and hence return a value - this allows them to be used as the last expression in a function.
  • Array indexing
  • for expression on range literals (i.e. for x in a:b or for x in a:s:b)
  • return statements
  • @new Foo() as the equivalent of new Foo() in JS
  • @var foo = bar as the equivalent of var foo = bar in JS
You can’t perform that action at this time.