How to get Hello Word compilation from Swift to JavaScript using Emscripten

Given the simplest of the fast files:

println("lol") 

It is trivial to run this on the command line using xcrun swift -i lol.swift or to compile the executable using xcrun swift lol.swift -o lol , but what about a simple proof of concept for emscripten?

I haven't used emscripten before, but got a welcome example using C ++ working from http://kripken.imtqy.com/emscripten-site/docs/getting_started/Tutorial.html , and wanted to compile my Swift code too.

I tried

 xcrun swift lol.swift -emit-bc -o lol.bc emcc lol.bc 

But we get

 Value: %1 = call { i8*, i64, i64 } @_TFSS37_convertFromBuiltinUTF16StringLiteralfMSSFTBp17numberOfCodeUnitsBw_SS(i8* bitcast ([4 x i16]* @0 to i8*), i64 3) LLVM ERROR: Unrecognized struct value Traceback (most recent call last): File "/Users/glen/Downloads/emsdk_portable/emscripten/1.16.0/emcc", line 1540, in <module> shared.Building.llvm_opt(final, link_opts) File "/Users/glen/Downloads/emsdk_portable/emscripten/1.16.0/tools/shared.py", line 1267, in llvm_opt assert os.path.exists(target), 'Failed to run llvm optimizations: ' + output AssertionError: Failed to run llvm optimizations: 

Thoughts?

+7
javascript swift llvm emscripten
source share
2 answers

The problem is that LLVM cannot find the types / characters used in this call during the binding process. These characters are most likely specific to the quick structure. If you run emcc with the -v , you can get additional debugging information. You might also consider providing links --llvm-opts where this information can be found.

I ran xcrun swift -v test.swift to see which command actually executed.

 Swift version 1.0 (swift-600.0.34.4.5) Target: x86_64-apple-darwin13.2.0 /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -enable-objc-attr-requires-objc-module -target x86_64-apple-darwin13.2.0 -module-name test -color-diagnostics -o /var/folders/69/l9w0zkqn38s1td4_gm5c__km0000gn/T/test-d800d3.o /usr/bin/ld /var/folders/69/l9w0zkqn38s1td4_gm5c__km0000gn/T/test-d800d3.o -force_load /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -lSystem -arch x86_64 -L /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -rpath /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -macosx_version_min 10.9.0 -no_objc_category_merging -o test 

You might think about how to apply these binding options to emscripten to get what you want. There will be no documentation on this, because I do not think that they sought to use it that way.

+3
source share

According to this GitHub question that you submitted to the Emscripten repo, what you are trying to do is not possible:

Unfortunately, it is not possible to link .dylib files to Emscripten. The reason for this is that .dylib already contains native machine code for x86 / x64, and Emscripten cannot "go back" and get it again in the form of LLVM IR.

- juj (user GitHub), 2014-06-14

+2
source share

All Articles