• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

JuliaGizmos/JSExpr.jl: Translate Julia to JavaScript

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

JuliaGizmos/JSExpr.jl

开源软件地址:

https://github.com/JuliaGizmos/JSExpr.jl

开源编程语言:

Julia 100.0%

开源软件介绍:

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap