9 ms·
ShiftJS – Swift to JavaScript transpiler
- flanbiscuit 10y agoThis is a cool project. I wonder what inspired the developer to make this and what could be some use cases for this? I could see it being used a tool for learning.
- sotojuan 10y agoFirst there was "everything that can be written in JS will be written in JS". Now it's "everything that can be compiled to JS will be compiled to JS".
- oddsignals 10y ago"Everything that has been written in JS will be rewritten in every language that transpiles to JS"
- spicyj 10y agoI love Swift and work on the web, so I would love to see a serious Swift to JS compiler. Unfortunately, this isn't it. One major part of Swift is its value types; many core types, including arrays, are passed by value, like a C struct. So var a = [1, 2] var b = a b.append(3) should make `a` equal to [1, 2] and `b` equal to [1, 2, 3]. ShiftJS eschews these semantics and instead passes arrays (and other value types) by reference, which makes it basically impossible to share code between real Swift and ShiftJS. Also, the demo compiles let a = 3 4 to var a = '3 4'; which doesn't instill confidence.
- Waterluvian 10y agoI'm curious, when are these kinds of compilers truly useful? Maybe its my own ignorance, but language design often doesn't map 1:1, so wouldn't it end up feeling forced?
- elmigranto 10y agoThat's the point, to use a "better" language. Especially if designs aren't similar. If behavior of compiled program matches the original one, that is.
- edgyswingset 10y ago> If behavior of compiled program matches the original one, that is. Unfortunately, that does make it difficult when your target is JavaScript. Every mature transpiler I've used has had some semantic gotchas that aren't present in the "proper" compiler.
- joeyjojo 10y agoI have successfully used Haxe in isolated cases to build games for WebGL and Canvas. I think transpilers are most useful when you rarely want to reach outside of your own code, or you only need to rely on a few key libraries that are known to be well supported within the ecosystem of your transpiler language. I also struggle without a static compiler, so for my case it makes sense to use a transpiler, even with all of its pitfalls. If there is one thing however that I have learned from using transpilers it is that externs as a whole are a huge waste of time, as it can be difficult to determine their quality upfront. If you are building a Node backend or SPA, you are going to need to rely heavily on existing libraries and will probably find yourself constantly hitting walls while you try to swim against the current of the ecosystem if you try to use a transpiler.
- jdonaldson 10y agoI disagree about externs being a waste of time for Haxe. I've written several, and they've been instrumental in helping me understand a codebase, and manage an upgrade process. Haxe has a lot of metadata mechanisms that enable mapping language-specific semantics to Haxe standards (@:selfCall, and @:native for example). Haxe also has abstract types, which enable automatic inline runtime conversions (e.g., converting a native type to a standard Haxe type automatically depending on usage in haxe methods). It's true you can't expect to find a high quality extern for a given version of any given library. However, it's pretty easy to write a small one for the subset of functionality that you require. It always ends up paying off for me.
- hackcasual 10y agoIn the X->JS transpiler space it's really only Emscripten that can take a library not written with JS in mind, and get it working. Scala.js is close, but in my experience the transpilers are really only suited for projects written from scratch. Not to say there's not value in building JS output with semantics very close to to Swift.
- spicyj 10y agojs_of_ocaml and BuckleScript both do a very good job with OCaml.
- hackcasual 10y agoBuckleScript looks very interesting. Semantic mapping and very small potential output are rare. GPLv2 would present some challenges using it commercially though.
- dangoor 10y agoMany compilers are released under the GPL license. Generally speaking[1], the output of the compiler is not subject to the license terms of the GPL, just as a document created by a GPL'ed word processor need not be licensed under the GPL. [1]: There could conceivably be GPL code linked with your code in the final output, but I don't think people would release a compiler like that without big notices unless they're specifically trying to trick people into having to buy commercial licenses or something like that.
- spicyj 10y agoOh, ClojureScript also seems nice.
- kevingadd 10y agoJSIL can run pretty much all modern .NET code with correct semantics in JS (i.e. the standard library + real enterprise apps/games), but the performance cost to match semantics is pretty significant. And then there's stuff you just can't do, like weak references :(
- theodorton 10y agoPassing on type inference can be solved by using Flow as an intermediate before transpiling down to Javascript. From my experience, modules and named arguments are the most important but hardest constructs to take care of when converting to js. I'm currently working on something like this for the Crystal language.
- pbreit 10y agoThe holy grail would still seem to be the opposite: writing native iOS apps in JavaScript.
- sotojuan 10y agoGetting closer with React Native.
- tootie 10y agoThere's already NativeScript and React Native.
- sixstringtheory 10y agoI haven't tried Reactive Native or other options, but my gut tells me it's less safe to write in Javascript than Swift. What would be an advantage that outweighs that?
- AndrewGaspar 10y agoLess safe in what sense?
- danappelxx 10y agoType safety & all that jazz. Swift is a much more powerful language than javascript (and typescript) is.
- hellofunk 10y agoSome are already dong this with Apple's JSCore.
- themihai 10y agoWhy? JS is crap compared to Swift. There are various technologies that allow you to do that anyway(i.e nativeScript)
- rimantas 10y agoThat's a poisonous chalice not the holy grail. How can someone to prefer JS to Swift is beyond my comprehension. Or is that "if the only tool you have is a hammer" situation?
- sixstringtheory 10y agoHow I would love to be able to write front-end code with all the safety checks of Swift, and have it transpiled on a commit hook or something! Wonder how this could work converting to React?
- azdavis 10y agoHave you seen https://www.typescriptlang.org https://www.typescriptlang.org?
- theodorton 10y agoflow (http://www.flowtype.org http://www.flowtype.org) is also an alternative. Developed at Facebook.
- sixstringtheory 10y agoI hadn't! I just started writing React last week, and coming from writing Swift it feels more wild-westy. Of course, that's how I felt going from Java to Objective C long before Swift came around, so I'm sure I'll get used to it. Nice to know about these kinds of things though!
- schrodinger 10y agoIt doesn't seem to handle some pretty basic swift code: var a = 3 let b = { a * 2 }() http://i.imgur.com/D1FxZcN.png http://i.imgur.com/D1FxZcN.png
- expression 10y agoHow about this, too: var a = 1 ^ 2 As it stands, I find the thing entirely useless.
- subliminalpanda 10y agoSame here, tried a basic loop and it broke: for i in 0...10 { print(i) } Then again, the project does say it's in early development. Perhaps a re-visit in a month or two will yield some nice progress.
- expression 10y agoIf the projects GitHub page is anything to go by, I doubt that. The only thing that's been changed in 9 months is the readme, and the change was to remove the note about the project being in "active early development".
- LiweiZ 10y ago8-9 months without any update to codebase is not a good sign for most open-source projects, especially when both JS and Swift are marching quickly. This alone can turn me off on this.
- Slippery_John 10y agoThat's an unfortunate name. Way too close to Shift JIS, an encoding for Japanese characters. IANAL, but it seems close enough to warrant copyright issue.
- coldtea 10y agoFirst of all, if it's the name of a an encoding there's no "copyright issue". Second, nobody (except the Japanese) really gives a duck about Shift JIS (and not many people care about Shift JS either) to warrant anything on either side.
- masters3d 10y agoIt would be interesting if it first converted the swift code to TypeScript then JS
- PeCaN 10y agoWhy would that be interesting? Presumably a Swift compiler handles its own type checking.
- epicureanideal 10y agoTypeScript would be a simpler translation target because it's very similar to Swift, and then TypeScript could handle the translation down to ES6, ES5, ES3, etc. Why reinvent what's already available?
- theodorton 10y agoTotally agree :) I'm working on something similar that uses flow and Crystal (which shares a lot of language features with Swift).
- TazeTSchnitzel 10y agoTypeScript is merely JS with added type annotations, annotations which are stripped off when it compiles to JS. There's no usefulness of TypeScript as a target for a compiler for Swift, because Swift already has static typing.
- closure 10y agoI do wish people would stop using the term transpiler as if there is something novel or different about source-to-source translation. If you're taking a higher level language and compiling it to assembly, it's a compiler. If you're taking a higher level language and compiling it to LLVM IR, it's a compiler. If you're taking a higher level language, and compiling it to another higher level language...it's a compiler. I am not sure where this term was first coined, but we've been doing source-to-source compilation for a very long term and it's just within the last 7-10 years I start hearing people use this term, which IMO adds no actual value. It's perfectly fine to say "Swift to JS compiler".
- michaelmior 10y agoCalling it a compiler is certainly accurate but transpiler seems more appropriate in this context.
- catmanjan 10y agoYep saying transpiler is just being more specific, like saying car instead of automobile - although it is probably superfluous when including the names of both input and output languages.
- tszming 10y agofyi: https://en.wikipedia.org/wiki/Source-to-source_compiler https://en.wikipedia.org/wiki/Source-to-source_compiler
- copperx 10y agoI agree that in "Swift to JavaScript transpiler" the word would be replaced with compiler and nothing would be lost; however, by your own definitions you can see that 'transpiler' is more specific than 'compiler.' Someone out there saw a need for the word, just like someone saw a need for coupe and sedan. You shouldn't worry until academia starts using it. Oops. Here's an article with 'transpiler' on the title: http://dl.acm.org/citation.cfm?id=2173694 http://dl.acm.org/citation.cfm?id=2173694 It's a good thing that English is descriptive and not prescriptive.
- kevindeasis 10y agoAre there other ways to use other languages in the browser asides from transpiler, and asm?
- fleitz 10y agoYes, read the browser docs, many browsers support languages other than JS. However, if the browser only supports a given language than the final target must be that language, similar to how only machine code executes of a CPU, everything else is just a turing machine emulating another turing machine.
- coldtea 10y agoWhere many = IE, and other languages = VBScript (if that's even supported anymore)? Because that's the only actual and in use browser I can think of that supports a language other than JS natively.
- fleitz 10y agoChrome supports ActionScript natively, as well as C++
- coldtea 10y agoNo, it doesn't support either. ActionScript is only supported through the Flash plugin, and inside the Flash environment -- and even that is taken out altogether in the new Chrome release. C++, not at all. Except if you mean Chrome is written in C++ itself, or you can use Native Client, which again is another plugin like environment. So, nothing similar to JS at all. Might as well have mentioned emscripten, which does end up running as a web page.
- fleitz 10y agoIf you install Chrome it supports these things, that's 'native' to me. Does it act like JS? No, but its still native.
- echelon 10y agoThis has a very similar name to the Japanese text encoding, Shift_JIS. I doubt people will be confused since these are two different domains, but they sound very similar to pronounce.
- msoad 10y agoThe true "Swift in web" will happen when WebAssembly is ready and LLVM has a WASM backend. Then I can just compile my Swift to WASM and bypass JavaScript altogether because Swift is already a LLVM front-end.
- Zelmor 10y agoI hope these sort of madness of everything to JS goes away with the advent of webassembly.
- rimantas 10y agoOr just goes away.
- moron4hire 10y agoIt doesn't hurt you.
- deleted 10y ago[deleted]
- leonatan 10y agoFrom their examples. Swift: var n = m--; JS var n = --m; Forget that ++ and -- operators are deprecated and not available in Swift 3.0; simple but profound mistakes like this do not instill confidence at all.
- rahkiin 10y agoAlso, --m and m-- are completely different. One decrements first and returns the new value, the other returns the new value and then decrements.
- zephyz 10y agoWhy didn't they fork the actual swift compiler that does all the lexing, parsing, syntax checking, type checking, SIL generation, LLVMIR generation, and add a final phase that compiles to JS? This whole project seem like a lot of duplicated, wasted (and error prone) work
- bsaul 10y agoWondered the same thing. I thought maybe a sil to js project could be a good target, keeping enough semantics to be able to be smart in the generated js code, rather thab llvmir. I looked for such a project a few days ago but didn't find anything.
- sebastianconcpt 10y agosomeVar++ and -- is deprecated already. I wonder why doing this. What's the applicability of doing this? Will this applicability still remain valid when WebAsembly hits with a real swift binary?
- HemanHeartYou 10y agoFor what purpose?
- joeblau 10y agoI'm not sure if the examples are actual results from the transpiler, but there are semicolons in the Swift code which are not necessary unless you have multiple statements on the same line.
- hongbozhang 10y agoFWIW:BuckleScript(https://github.com/bloomberg/bucklescript https://github.com/bloomberg/bucklescript) is to my knowledge the only compiler which compiles an existing language(not a language designed for compilation to JS) to JS without name mangling. It is a serious piece of work, the compiler is compiled to JS too, so you can play it in the playground(even work offline): https://bloomberg.github.io/bucklescript/js-demo/ https://bloomberg.github.io/bucklescript/js-demo/ It also generates very small code output, for example `hello world` is 20 bytes instead of 100K bytes