$ ./emsdk-portable/emscripten/incoming/emcc swift/hello.ll -o swift/hello.html warning: Linking two modules of different data layouts: '/Users/hiragram/.emscripten_cache/asmjs/dlmalloc.bc' is 'e-p:32:32-i64:64-v128:32:128-n32-S128' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmp4kw329/hello_0.o' is 'e-m:o-i64:64-f80:128-n8:16:32:64-S128' warning: Linking two modules of different target triples: /Users/hiragram/.emscripten_cache/asmjs/dlmalloc.bc' is 'asmjs-unknown-emscripten' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmp4kw329/hello_0.o' is 'x86_64-apple-macosx10.9' warning: Linking two modules of different data layouts: '/Users/hiragram/.emscripten_cache/asmjs/libc.bc' is 'e-p:32:32-i64:64-v128:32:128-n32-S128' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmp4kw329/hello_0.o' is 'e-m:o-i64:64-f80:128-n8:16:32:64-S128' warning: Linking two modules of different target triples: /Users/hiragram/.emscripten_cache/asmjs/libc.bc' is 'asmjs-unknown-emscripten' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmp4kw329/hello_0.o' is 'x86_64-apple-macosx10.9' error: Appending variables with different alignment need to be linked! ERROR:root:Failed to run llvm optimizations:
print("Hello, Swift!")
これだけ$ swiftc swift/hello.swift -emit-ir -o swift/hello.ll
@convention(c) func a(x: Int) -> Int { return x * 2 }
swift/hello.swift:1:2: error: attribute can only be applied to types, not declarations @convention(c) ^
asmjs-unknown-emscripten
で、swiftcはターゲットが x86_64-apple-macosx10.9
になってるっぽいwarning: Linking two modules of different data layouts: '/Users/hiragram/.emscripten_cache/asmjs/dlmalloc.bc' is 'e-p:32:32-i64:64-v128:32:128-n32-S128' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWhbLkq/hello_0.o' is 'e-m:o-i64:64-f80:128-n8:16:32:64-S128' warning: Linking two modules of different target triples: /Users/hiragram/.emscripten_cache/asmjs/dlmalloc.bc' is 'asmjs-unknown-emscripten' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWhbLkq/hello_0.o' is 'x86_64-apple-macosx10.9' warning: Linking two modules of different data layouts: '/Users/hiragram/.emscripten_cache/asmjs/libc.bc' is 'e-p:32:32-i64:64-v128:32:128-n32-S128' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWhbLkq/hello_0.o' is 'e-m:o-i64:64-f80:128-n8:16:32:64-S128' warning: Linking two modules of different target triples: /Users/hiragram/.emscripten_cache/asmjs/libc.bc' is 'asmjs-unknown-emscripten' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWhbLkq/hello_0.o' is 'x86_64-apple-macosx10.9'
i32
みたいな型で記述されているから; ModuleID = 'swift/hello.ll' source_filename = "swift/hello.ll" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.9" @__swift_reflection_version = linkonce_odr hidden constant i16 3 @llvm.used = appending global [1 x i8*] [i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata", align 8 define i32 @main(i32, i8**) #0 { entry: %2 = bitcast i8** %1 to i8* ret i32 0 } attributes #0 = { "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "target-cpu"="core2" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+ssse3,+x87" } !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !9, !10} !0 = !{i32 1, !"Objective-C Version", i32 2} !1 = !{i32 1, !"Objective-C Image Info Version", i32 0} !2 = !{i32 1, !"Objective-C Image Info Section", !"__DATA, __objc_imageinfo, regular, no_dead_strip"} !3 = !{i32 4, !"Objective-C Garbage Collection", i32 1280} !4 = !{i32 1, !"Objective-C Class Properties", i32 64} !5 = !{i32 6, !"Linker Options", !6} !6 = !{!7, !8} !7 = !{!"-lswiftCore"} !8 = !{!"-lobjc"} !9 = !{i32 1, !"PIC Level", i32 2} !10 = !{i32 1, !"Swift Version", i32 5}
@llvm.used = appending global [1 x i8*] [i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata", align 8
; ModuleID = 'swift/hello.ll' source_filename = "swift/hello.ll" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.9" define i32 @main(i32, i8**) #0 { entry: %2 = bitcast i8** %1 to i8* ret i32 0 }
main
って名前の関数になってくれるかもしれんけど。Hello All, I tried using clang 3.9.0 to emit the llvm ir with -target wasm32-wasm32-wasm32, but end up getting warning for different data layout and target triple. The question is if llvm WebAssembly target is usable as is to generate llvm ir to be fed to emscripten ?
The warning is expected, but should not cause problems in practice. The important things are the same in those two triples. (Except perhaps for the size of long doubles, is that important in Swift?)
warning: Linking two modules of different data layouts: '/Users/hiragram/.emscripten_cache/asmjs/dlmalloc.bc' is 'e-p:32:32-i64:64-v128:32:128-n32-S128' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWrnym7/hello_0.o' is 'e-m:o-i64:64-f80:128-n8:16:32:64-S128' warning: Linking two modules of different target triples: /Users/hiragram/.emscripten_cache/asmjs/dlmalloc.bc' is 'asmjs-unknown-emscripten' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWrnym7/hello_0.o' is 'x86_64-apple-macosx10.9' warning: Linking two modules of different data layouts: '/Users/hiragram/.emscripten_cache/asmjs/libc.bc' is 'e-p:32:32-i64:64-v128:32:128-n32-S128' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWrnym7/hello_0.o' is 'e-m:o-i64:64-f80:128-n8:16:32:64-S128' warning: Linking two modules of different target triples: /Users/hiragram/.emscripten_cache/asmjs/libc.bc' is 'asmjs-unknown-emscripten' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWrnym7/hello_0.o' is 'x86_64-apple-macosx10.9' warning: Linking two modules of different data layouts: '/Users/hiragram/.emscripten_cache/asmjs/wasm-libc.bc' is 'e-p:32:32-i64:64-v128:32:128-n32-S128' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWrnym7/hello_0.o' is 'e-m:o-i64:64-f80:128-n8:16:32:64-S128' warning: Linking two modules of different target triples: /Users/hiragram/.emscripten_cache/asmjs/wasm-libc.bc' is 'asmjs-unknown-emscripten' whereas '/var/folders/b6/shjk0l7j0mb0j9__n3w7z92w0000gn/T/tmpWrnym7/hello_0.o' is 'x86_64-apple-macosx10.9' warning: incorrect target triple 'x86_64-apple-macosx10.9' (did you use emcc/em++ on all source files and not clang directly?)
$ swiftc -emit-ir -Xcc --target=wasm32 first.swift -o first.ll
$ swiftc -emit-ir -Xcc --target=wasm32aaa first.swift -o first.ll <unknown>:0: warning: argument unused during compilation: '-mmacosx-version-min=10.9.0' <unknown>:0: error: -fobjc-arc is not supported on platforms using the legacy runtime <unknown>:0: error: unknown target triple 'wasm32aaa', please use -triple or -arch <unknown>:0: error: clang importer creation failed
define hidden swiftcc i64 @_T01b3addS2i_SitF(i64, i64) #0 { entry: %2 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %0, i64 %1) %3 = extractvalue { i64, i1 } %2, 0 %4 = extractvalue { i64, i1 } %2, 1 br i1 %4, label %6, label %5 ; <label>:5: ; preds = %entry ret i64 %3 ; <label>:6: ; preds = %entry call void @llvm.trap() unreachable }
swiftcc
ってコンベンションになってて、 llc
の wasm モードがそれをサポートしてないからダメっぽいswiftcc
のサポートがバックポートされてるっぽくて.... + [origin/swift-4.1-branch~577] Canonicalize the spelling of RefDie (NFC) + [origin/swift-4.1-branch~578] dsymutil: strip unused types from imported DW_TAG_modules + [origin/swift-4.1-branch~579] Debug Info: Don't strip clang module skeleton CUs. ++ [llvm/master~5237] Recommit r274270, [CMake] Module builds depend on target intrinsics_gen to be built first.
commit 18e8f9ef0645b15c777547d8655a17a315b0815d Author: NAKAMURA Takumi <geek4civic@gmail.com> Date: Wed Jul 19 11:27:31 2017 +0000 Recommit r274270, [CMake] Module builds depend on target intrinsics_gen to be built first. The builder clang-x86_64-linux-selfhost-modules-2 complains. Investigating. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308439 91177308-0d34-0410-b5e6-96231b3b80d8
xcrun clang
で動くclangは-target=wasm32使えたけどそういうもん?.s
を吐ければ、あとは Wasmのツールの管轄swiftcc
などのSwift仕様が含まれていて、 llc が通らない。swiftc -emit-ir b.swift -o b.ll llc b.ll -o b.s libs=/Users/omochi/work/swift-source/build/Ninja-ReleaseAssert/swift-macosx-x86_64/lib/swift/macosx gcc -L$libs b.s -o b.out LD_LIBRARY_PATH=$libs ./b.out
$ cat b.swift print("hello swift")
_swift_retain
とかも外部リンクされているので-static-stdlib
の挙動について調べたけど、 /usr/bin/ld に -force_load_swift_libs を渡すようになっているだけで、/usr/bin/ld
についてはオープンソースになってないから、何もわからんという結論になったupstream-with-swift
っていうブランチがあるけど、CIは通ってないぽい。 https://ci.swift.org/view/swift-master-next/$ kotlinc -target wasm32 hello.kt
で hello.wasm
が直接吐かれて超楽っぽい。The calling conventions are mostly for the backends, so it should be possible to just ignore it. That is, if swift didn't emit it, it should be ok, and for now you can strip it from the IR manually for testing. (There are exceptions like tail call optimizations that are done on IR and depend on the convention, but I don't think that would be an issue here.)
avoid up-front load time costs
がよくわからないな〜Relative..Pointer
的なもの持つことをを検討する価値があるかも知れない」。uintptr_t
とかタグポインタとか挟んで取り回してたら検出できないっしょ。~/Library/Developer/Toolchains
に手でインストールすればXcodeからも切り替えられますexport TOOLCHAINS="org.swift.51220191107a"
で$ swift
が切り替わってれば他はいろいろと連動するはずってことね、OKEXAMPLES To check whether Mail.app is allowed to run on the local system: spctl -a /Applications/Mail.app To allow Frobozz.app to run on the local system: spctl --add --label "My Stuff" /Applications/Frobozz.app To forbid all code obtained from the Mac App Store from running: spctl --disable --label "Mac App Store"
[omochi@omochi-iMacPro bin]$ sudo spctl --add --label swiftwasm $(pwd)/swift-demangle Password: [omochi@omochi-iMacPro bin]$ ./swift-demangle Killed: 9 [omochi@omochi-iMacPro bin]$ sudo spctl -a swift-demangle swift-demangle: rejected (the code is valid but does not seem to be an app)
--master-disable
でセキュリティ全解除する情報ばっかり出てくる/usr/local
は聖域?$ codesign -dvvv /usr/local/bin/swiftlint /usr/local/bin/swiftlint: code object is not signed at all
/usr/local/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/bin
に動かしたけど駄目だなあ。 フォルダコピーするときにFinderが謎のダイアログを出して確認してきたからなんかいけそうな気がしたけど。[omochi@omochi-iMacPro wasm]$ spctl -a /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/bin/swift-frontend /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/bin/swift-frontend: rejected (the code is valid but does not seem to be an app)
[omochi@omochi-iMacPro wasm]$ sil-opt Killed: 9 [omochi@omochi-iMacPro wasm]$ xattr sil-opt xattr: No such file: sil-opt [omochi@omochi-iMacPro wasm]$ which sil-opt /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/bin/sil-opt [omochi@omochi-iMacPro wasm]$ xattr $(which sil-opt) com.apple.quarantine [omochi@omochi-iMacPro wasm]$ sudo xattr -r -d com.apple.quarantine $(which sil-opt) Password: [omochi@omochi-iMacPro wasm]$ sil-opt --version LLVM (http://llvm.org/): LLVM version 10.0.0 Optimized build. Default target: x86_64-apple-darwin19.6.0 Host CPU: skylake-avx512
find . -perm +111 -type f
と xattr
を組み合わせたらガッと浄化できそう。xattr -r -d com.apple.quarantine ディレクトリパス
でディレクトリ内一気に消せますよ[omochi@omochi-iMacPro wasm]$ ls -alh total 20432 drwxr-xr-x 4 omochi staff 128B 10 7 01:32 . drwxr-xr-x 14 omochi staff 448B 10 7 01:07 .. -rw-r--r--@ 1 omochi staff 21B 10 7 01:08 a.swift -rwxr-xr-x 1 omochi staff 10M 10 7 01:32 a.wasm [omochi@omochi-iMacPro wasm]$ wasmer a.wasm hello world
$ sudo codesign --force --deep --sign "Developer ID Application: Neko Chan (XXXXXXXXX)" --options runtime --deep-verify swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a.xctoolchain/ $ codesign -display swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a.xctoolchain/usr/bin/swift-package swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a.xctoolchain/usr/bin/swift-package: code object is not signed at all
(edited)[omochi@omochi-iMacPro usr]$ tree | grep -i xctest │ │ │ ├── XCTest.swiftmodule │ │ │ │ ├── XCTest.opt.bitstream │ │ │ ├── libswiftXCTest.dylib │ │ │ ├── XCTestOverlayShims.h │ │ ├── XCTest.swiftdoc │ │ ├── XCTest.swiftmodule │ │ ├── XCTestOverlayShims.h │ ├── libXCTest.a │ └── swiftpm-xctest-helper
[omochi@omochi-iMacPro SwiftDeclWeb]$ swift build --triple wasm32-unknown-wasi --sdk $TOOLCHAIN_PATH/share/wasi-sysroot <module-includes>:1:10: note: in file included from <module-includes>:1: #include "LibcOverlayShims.h" ^ /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift/shims/LibcOverlayShims.h:31:10: error: 'semaphore.h' file not found #include <semaphore.h> ^ <unknown>:0: error: could not build C module 'SwiftOverlayShims' <module-includes>:1:10: note: in file included from <module-includes>:1: #include "../../../../share/wasi-sysroot/include/ctype.h" ^ <module-includes>:1:10: note: in file included from <module-includes>:1: #include "LibcOverlayShims.h" ^
[omochi@omochi-iMacPro SwiftDeclWeb]$ swift test --triple wasm32-unknown-wasi --sdk $TOOLCHAIN_PATH/share/wasi-sysroot /Users/omochi/github/omochi/SwiftDeclWeb/Tests/SDWVDOMTests/VDOMTests.swift:1:8: error: could not find module 'XCTest' for target 'wasm32-unknown-wasi'; found: arm64e-apple-ios-macabi, x86_64-apple-ios-macabi, arm64, arm64-apple-macos, x86_64, arm64e-apple-macos, x86_64-apple-macos, arm64-apple-ios-macabi, arm64e import XCTest ^ /Users/omochi/github/omochi/SwiftDeclWeb/Tests/SDWVDOMTests/VDOMTests.swift:1:8: error: could not find module 'XCTest' for target 'wasm32-unknown-wasi'; found: arm64e-apple-ios-macabi, x86_64-apple-ios-macabi, arm64, arm64-apple-macos, x86_64, arm64e-apple-macos, x86_64-apple-macos, arm64-apple-ios-macabi, arm64e import XCTest ^ error: fatalError
swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a
なんだけど。[omochi@omochi-iMacPro SwiftDeclWeb]$ swift build --triple wasm32-unknown-wasi --sdk $TOOLCHAIN_PATH/share/wasi-sysroot wasm-ld: error: /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift_static/wasi/libFoundation.a(NSTimeZone.swift.obj): undefined symbol: CFHash wasm-ld: error: /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift_static/wasi/libFoundation.a(NSDate.swift.obj): undefined symbol: _CFDeinit wasm-ld: error: /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift_static/wasi/libFoundation.a(NSDate.swift.obj): undefined symbol: CFDateGetTypeID wasm-ld: error: /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift_static/wasi/libFoundation.a(NSDate.swift.obj): undefined symbol: CFDateGetTypeID wasm-ld: error: /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift_static/wasi/libFoundation.a(NSRegularExpression.swift.obj): undefined symbol: kCFAllocatorSystemDefault
[omochi@omochi-iMacPro SwiftDeclWeb]$ tree -L 2 -d $TOOLCHAIN_PATH/share/wasi-sysroot /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/bin/../share/wasi-sysroot ├── include │ ├── arpa │ ├── bits │ ├── c++ │ ├── net │ ├── netinet │ ├── netpacket │ ├── scsi │ ├── sys │ └── wasi ├── lib │ ├── wasm32-wasi │ └── wasm32-wasi-unknown ├── share │ └── wasm32-wasi └── usr └── include 17 directories
{ "extra-cc-flags" : [ "-I", "TOOLCHAIN_DIR/usr/lib/swift/wasi/wasm32" ], "extra-cpp-flags" : [ ], "target" : "wasm32-unknown-wasi", "version" : 1, "extra-swiftc-flags" : [ "-I", "TOOLCHAIN_DIR/usr/lib/swift/wasi/wasm32", "-Xlinker", "-lFoundation", "-Xlinker", "-lCoreFoundation", "-Xlinker", "-lBlocksRuntime", "-Xlinker", "-licui18n", "-Xlinker", "-luuid" ], "sdk" : "TOOLCHAIN_DIR/usr/share/wasi-syroot", "toolchain-bin-dir" : "TOOLCHAIN_DIR/usr/bin" }
swift build -c debug --build-tests --destination wasm32-unknown-wasi.json
--destination
オプションに渡す[omochi@omochi-iMacPro SwiftDeclWeb]$ swift build -c debug --build-tests --destination /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/wasm32-unknown-wasi.json wasm-ld: error: /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift_static/wasi/libFoundation.a(Data.swift.obj): undefined symbol: _withStackOrHeapBuffer clang-10: error: linker command failed with exit code 1 (use -v to see invocation) error: link command failed with exit code 1 (use -v to see invocation) [16/18] Linking SwiftDeclWebPackageTests.xctest
[omochi@omochi-iMacPro wpkg]$ swift package init --type executable Creating executable package: wpkg Creating Package.swift Creating README.md Creating .gitignore Creating Sources/ Creating Sources/wpkg/main.swift Creating Tests/ Creating Tests/LinuxMain.swift Creating Tests/wpkgTests/ Creating Tests/wpkgTests/wpkgTests.swift Creating Tests/wpkgTests/XCTestManifests.swift [omochi@omochi-iMacPro wpkg]$ open . [omochi@omochi-iMacPro wpkg]$ swift build -c debug --build-tests --destination /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/wasm32-unknown-wasi.json /Users/omochi/temp/wpkg/Tests/wpkgTests/wpkgTests.swift:20:23: error: cannot find 'Process' in scope let process = Process() ^~~~~~~ /Users/omochi/temp/wpkg/Tests/wpkgTests/wpkgTests.swift:23:20: error: cannot find 'Pipe' in scope let pipe = Pipe() ^~~~ /Users/omochi/temp/wpkg/Tests/wpkgTests/wpkgTests.swift:44:28: error: value of type 'Bundle' has no member 'bundleURL' return Bundle.main.bundleURL ~~~~~~~~~~~ ^~~~~~~~~ [6/9] Compiling wpkgTests XCTestManifests.swift
import XCTest final class wpkgTests: XCTestCase { func testExample() throws { XCTAssertTrue(true) } static var allTests = [ ("testExample", testExample), ] }
[omochi@omochi-iMacPro wpkg]$ swift build -c debug --build-tests --destination /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/wasm32-unknown-wasi.json wasm-ld: error: /Users/omochi/swift/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a/usr/lib/swift_static/wasi/libFoundation.a(Data.swift.obj): undefined symbol: _withStackOrHeapBuffer clang-10: error: linker command failed with exit code 1 (use -v to see invocation) <unknown>:0: error: link command failed with exit code 1 (use -v to see invocation) [7/8] Linking wpkgPackageTests.xctest
$ cat Tests/helloTests/helloTests.swift import XCTest final class helloTests: XCTestCase { func testExample() throws { XCTAssertTrue(true) } static var allTests = [ ("testExample", testExample), ] } $ ~/Downloads/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-10-05-a.xctoolchain/usr/bin/swift build -c debug --build-tests --destination wasm-5.3-SNAPSHOT-2020-09-08-a.json [12/12] Linking helloPackageTests.xctest /var/folders/br/7sr_p7cj7v549x_gtkb33df00000gp/T/tmp.BSZU6PLa $ wasmer ./.build/debug/helloPackageTests.xctest Test Suite 'All tests' started at 2020-10-06 17:24:16.346 Test Suite 'testBundle.xctest' started at 2020-10-06 17:24:16.346 Test Suite 'helloTests' started at 2020-10-06 17:24:16.346 Test Case 'helloTests.testExample' started at 2020-10-06 17:24:16.346 Test Case 'helloTests.testExample' passed (0.0 seconds) Test Suite 'helloTests' passed at 2020-10-06 17:24:16.346 Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds Test Suite 'testBundle.xctest' passed at 2020-10-06 17:24:16.347 Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds Test Suite 'All tests' passed at 2020-10-06 17:24:16.347 Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds
.xctoolchain
?[omochi@omochi-iMacPro wpkg]$ swift build -c debug --build-tests --destination /Users/omochi/swift/swift-wasm-5.3-SNAPSHOT-2020-09-08-a/usr/wasm32-unknown-wasi.json wasm-ld: error: /Users/omochi/swift/swift-wasm-5.3-SNAPSHOT-2020-09-08-a/usr/share/wasi-sysroot/lib/wasm32-wasi/libc.a(__original_main.o): undefined symbol: main clang-10: error: linker command failed with exit code 1 (use -v to see invocation) <unknown>:0: error: link command failed with exit code 1 (use -v to see invocation) [8/9] Linking wpkgPackageTests.xctest
[omochi@omochi-iMacPro wpkg]$ swift build -c debug --build-tests --destination /Users/omochi/swift/swift-wasm-5.3-SNAPSHOT-2020-10-02-a/usr/wasm32-unknown-wasi.json [12/12] Linking wpkgPackageTests.xctest
(edited)[omochi@omochi-iMacPro wpkg]$ wasmer .build/debug/wpkgPackageTests.xctest Test Suite 'All tests' started at 2020-10-06 18:03:44.789 Test Suite 'testBundle.xctest' started at 2020-10-06 18:03:44.790 Test Suite 'wpkgTests' started at 2020-10-06 18:03:44.790 Test Case 'wpkgTests.testExample' started at 2020-10-06 18:03:44.790 Test Case 'wpkgTests.testExample' passed (0.0 seconds) Test Suite 'wpkgTests' passed at 2020-10-06 18:03:44.790 Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds Test Suite 'testBundle.xctest' passed at 2020-10-06 18:03:44.790 Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds Test Suite 'All tests' passed at 2020-10-06 18:03:44.790 Executed 1 test, with 0 failures (0 unexpected) in 0.0 (0.0) seconds
.xctoolchain
にしてexport TOOLCHAINS="org.swift.…"
でツールチェインを切り替える仕組みは、SwiftPMがツールチェイン添付のXCTest.swiftmodule
を使ってくれないから、SwiftWasmには使えないんじゃないかな。 (edited)WebAssembly.instantiate
でJS側の関数をWASM環境にいれるやつ// Output stdout and stderr to console const originalWriteSync = wasmFs.fs.writeSync; wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => { const text = new TextDecoder("utf-8").decode(buffer); // Filter out standalone "\n" added by every `print`, `console.log` // always adds its own "\n" on top. if (text !== "\n") { switch (fd) { case 1: console.log(text); break; case 2: console.error(text); break; } } return originalWriteSync(fd, buffer, offset, length, position); };
print
って Swift.print
のこと?Swift.print
の末尾の改行が不要なのであれば、 print(str, terminator: "")
って呼び出せばよくない?wasmFS.fs.writeSync
の先に繋がってる console.log
で勝手に改行されて改行が2つになることを気にしてそうなSwift.print("str", terminator: "")
ってやっていたとしてもfflush()
が呼ばれたら詰む。 (edited)/dev/stdout
にファイルとして書き溜めてるだけなのね<pre>
タグの中に流し込んでいくとかのほうがマシな挙動だと思う。 f2 = fopen(filename, mode); if (!f2) goto fail; if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */ #ifdef __wasilibc_unmodified_upstream // WASI has no dup else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2; #else // WASI doesn't have dup3, but does have a way to renumber // an existing file descriptor. else { if (__wasilibc_fd_renumber(f2->fd, f->fd)<0) goto fail2; f2->fd = -1; /* avoid closing in fclose */ } #endif
open
の世界だったら pipe
がそうfopen
が返す FILE*
は 生fd のラッパーpoll_oneoff(in: ConstPointer<subscription>, out: Pointer<event>, nsubscriptions: size) -> (errno, size) Concurrently poll for the occurrence of a set of events.
void __wasi_fd_write(...) { // do redirect return original_fd_write(...); } void original_fd_write(...) __attribute__(( __import_module__("wasi_snapshot_preview1"), __import_name__("fd_write") ))
(edited)> void __wasi_fd_write(...) { > // do redirect > return original_fd_write(...); > } > void original_fd_write(...) __attribute__(( > __import_module__("wasi_snapshot_preview1"), > __import_name__("fd_write") > ))
(edited)__attribute__(( __import_module__("wasi_snapshot_preview1"), __import_name__("fd_write") )) __wasi_fd_write(...)
↑すでにこう書かれたヘッダーがビルドに含まれてるわけでしょ?original_fd_write
と、 wasi関数を受け取ろうとしてるやつが2つできちゃうし__wasi_fd_write
と 新たに作ろうとしてる、 もうひとつの fd_writeを本当につなぎたい original_fd_write
と、__import_name__
は見ていなくて、C的なシンボルでルックアップしています__wasi_fd_write
が別途存在すればそれはそこにつながるのか。__wasi_fd_write
は実装が無いがユーザーが定義した __wasi_fd_write
には実装があるので、この時点で __wasi_fd_write
はundefinedなシンボルではなくなります__import_name__
は無視されますoriginal_fd_write
は実装が無いのでundefinedなので、__import_name__
に基づいてimportエントリを発行します/dev/stdout
を wasi で fopen して poll_oneoff 回せば良いんだ。 this.view.setUint32(nevents, eventc, true); while (bindings.hrtime() < waitEnd) { // nothing } return WASI_ESUCCESS;
try
とか await
は前に書くし、前に書いちゃえば良さそう。 let x = try await jsTry(jsAwait(foo.bar.baz))
jsTryAwait
があってもいいか。let result = await jsAsync(fetch)("...")
fetch
は Promise
返すんだから、 await jsAsync(fetch("..."))
でも良さそうな気も?JSValue
に case error(JSValue)
を追加すれば良い?indirect