Giter Club home page Giter Club logo

jsexpr.jl's Introduction

JSExpr

Build Coverage
Build Status codecov

This package provides two macros that are used to write JavaScript code inside of Julia programs.

  • The @js macro translates Julia syntax to the equivalent JavaScript
  • The @js"..." macro is used to write code using string literals with smart interpolation of values from Julia

Examples

julia> using JSExpr

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

julia> @js (a, b) -> a + b
JSString("(a, b) => { return a + b; }")

julia> config = Dict("foo" => "bar");
julia> js"initializeProgram($config);"
JSString("initializeProgram({\"foo\":\"bar\"});")

Interpolation

You can interpolate Julia objects or JSStrings (e.g. from other @js or js"..." invocations) as well as values from Julia (such as normal strings, Dicts, etc.).

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

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

Custom Interpolation

By default, values are serialized using the JSON package. This makes sense for Dicts, Arrays, and most other "primitive" types.

3rd-party packages can customize serialization of their own types by defining a method for JSExpr.interpolate. The return value of JSExpr.interpolate should be a JSNode.

julia> struct Link; text::String; href::String; end;
julia> JSExpr.interpolate(link::Link) = JSExpr.JSTerminal(js"<a href=$(link.href)>$(link.text)</a>");
julia> @js @const link = $(Link("Julia", "https://julialang.org/"))
JSString("const link = <a href=\"https://julialang.org/\">\"Julia\"</a>")

Object Literals

Objects are ubiquitous in JavaScript. To create objects using JSExpr, you can use a simple syntax using braces. There are two variants of this syntax (NamedTuple style and Pair style). You can also create objects use normal NamedTuple syntax.

# NamedTuple braces style
julia> @js { foo="foo", bar="bar" }
JSString("{\"foo\": \"foo\", \"bar\": \"bar\"}")

# Pair braces style (similar to Dict constructor)
julia> @js { :foo => "foo", :bar => "bar" }
JSString("{\"foo\": \"foo\", \"bar\": \"bar\"}")

# NamedTuple syntax
julia> @js (foo="foo", bar="bar")
JSString("{\"foo\": \"foo\", \"bar\": \"bar\"}")

Why not Dict?

JSExpr does not attempt to translate semantics between Julia and JavaScript (with a few very minor exceptions covered in Juliaisms below). Since Dict can be a valid function name in JavaScript, we do not translate the Julia Dict constructor to an object creation syntax.

Juliaisms

JSExpr, for the most part, does not attempt to translate semantics between Julia and the resulting JavaScript code. The reason for the decision is that Julia and JavaScript are wildly different languages and we would invariably mess up some edge cases. We do, however, translate a few Julian constructs to a semantically equivalent JavaScript.

Range Syntax (...:...)

JavaScript doesn't have a native Range object and the typical way to repeat a loop body n times is to use a C-style for loop. There is no syntax for this style of for loop in Julia, and : is not a valid JavaScript identifier, so the colon function (:) is translated to JavaScript code that acts like a Range object in Julia.

julia> @js for i in 1:10
         console.log(i)
       end
JSString("for (let i of (new Array(10).fill(undefined).map((_, i) => i + 1))) { console.log(i); }")

The resulting JS is very ugly and will fully materialize the range and so should only be used for relatively small ranges.

Serialization

Serializing a JSString to JSON will result in a normal string containing the JavaScript code.

julia> f = @js array -> array.map($callback);
julia> JSON.print(Dict("foo" => "bar", "bar"=>f))
{"bar":"(array) => { return array.map((a) => { return a + 42; }); }","foo":"bar"}

Supported Expressions

  • Function calls
  • Comparison operators
  • Object and array literals
  • Function creation (named and anonymous functions)
  • If statements
  • For and while loops
  • JavaScript keywords (@new, @var, @let, @const)

Unsupported Expressions

Not Yet Supported

  • Ternary expressions (... ? ... : ...)
  • try / catch

Might Never Be Supported

  • Object destructuring
  • Argument splatting

If you notice anything else that's not supported or doesn't work as intended, please open an issue.

Ternary Expressions

Julia lowers (during parse) if statements and ternary expressions (... ? ... : ...) to the same Expr, so JSExpr cannot distinguish between the two. This poses an issue because JavaScript does not allow non-expression statements (e.g., loops and variable declarations) inside of a ternary expression, but if statements cannot be used in contexts which expect a value (but they can be used in such contexts in Julia).

There are plans to implement a heuristic to emit a ternary expression if appropriate (e.g., if the bodies of the ternary expression contains only one sub-expression) but this is not implemented yet.

jsexpr.jl's People

Contributors

andreasnoack avatar juliatagbot avatar kristofferc avatar mikeinnes avatar rdeits avatar sglyon avatar shashi avatar travigd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.