diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7835cded..554ecb18 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,11 +6,6 @@ jobs: strategy: matrix: os: [macOS-10.15, Ubuntu-18.04] - include: - - os: macOS-10.15 - toolchain: https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.3-SNAPSHOT-2020-08-10-a/swift-wasm-5.3-SNAPSHOT-2020-08-10-a-osx.tar.gz - - os: Ubuntu-18.04 - toolchain: https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.3-SNAPSHOT-2020-08-10-a/swift-wasm-5.3-SNAPSHOT-2020-08-10-a-linux.tar.gz runs-on: ${{ matrix.os }} steps: - name: Checkout @@ -23,8 +18,5 @@ jobs: export SWIFTENV_ROOT="$HOME/.swiftenv" export PATH="$SWIFTENV_ROOT/bin:$PATH" eval "$(swiftenv init -)" - swiftenv install $TOOLCHAIN_DOWNLOAD make bootstrap make test - env: - TOOLCHAIN_DOWNLOAD: ${{ matrix.toolchain }} diff --git a/.swift-version b/.swift-version index 64ccfaef..ff4900d3 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -wasm-5.3-SNAPSHOT-2020-08-10-a +wasm-5.3-SNAPSHOT-2020-10-02-a diff --git a/Makefile b/Makefile index 7f4fe586..45dcc788 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST))) .PHONY: bootstrap bootstrap: + ./scripts/install-toolchain.sh npm install .PHONY: build diff --git a/Package.swift b/Package.swift index 114e2588..813fe7c2 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:5.2 import PackageDescription @@ -10,34 +10,8 @@ let package = Package( targets: [ .target( name: "JavaScriptKit", - dependencies: ["_CJavaScriptKit"], - linkerSettings: [ - .unsafeFlags( - [ - "-Xlinker", - "--export=swjs_call_host_function", - "-Xlinker", - "--export=swjs_prepare_host_function_call", - "-Xlinker", - "--export=swjs_cleanup_host_function_call", - "-Xlinker", - "--export=swjs_library_version", - ], - .when(platforms: [.wasi]) - ), - ] - ), - .target( - name: "_CJavaScriptKit", - linkerSettings: [ - .unsafeFlags( - [ - "-Xlinker", - "--allow-undefined", - ], - .when(platforms: [.wasi]) - ), - ] + dependencies: ["_CJavaScriptKit"] ), + .target(name: "_CJavaScriptKit"), ] ) diff --git a/Package@swift-5.2.swift b/Package@swift-5.2.swift deleted file mode 100644 index f66aaaad..00000000 --- a/Package@swift-5.2.swift +++ /dev/null @@ -1,41 +0,0 @@ -// swift-tools-version:5.2 - -import PackageDescription - -let package = Package( - name: "JavaScriptKit", - products: [ - .library(name: "JavaScriptKit", targets: ["JavaScriptKit"]), - ], - targets: [ - .target( - name: "JavaScriptKit", - dependencies: ["_CJavaScriptKit"], - linkerSettings: [ - .unsafeFlags( - [ - "-Xlinker", - "--export=swjs_call_host_function", - "-Xlinker", - "--export=swjs_prepare_host_function_call", - "-Xlinker", - "--export=swjs_cleanup_host_function_call", - "-Xlinker", - "--export=swjs_library_version", - ] - ), - ] - ), - .target( - name: "_CJavaScriptKit", - linkerSettings: [ - .unsafeFlags( - [ - "-Xlinker", - "--allow-undefined", - ] - ), - ] - ), - ] -) diff --git a/Sources/JavaScriptKit/Compatibility.swift b/Sources/JavaScriptKit/Compatibility.swift deleted file mode 100644 index 979f5dd8..00000000 --- a/Sources/JavaScriptKit/Compatibility.swift +++ /dev/null @@ -1,7 +0,0 @@ -/// The compatibility runtime library version. -/// Notes: If you change any interface of runtime library, please increment -/// this and `SwiftRuntime.version` in `./Runtime/src/index.ts`. -@_cdecl("swjs_library_version") -func _library_version() -> Double { - return 700 -} diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift index 03edf7da..adc4035e 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift @@ -190,19 +190,8 @@ public class JSClosure: JSFunction { // │ │ │ // └─────────────────────┴──────────────────────────┘ -@_cdecl("swjs_prepare_host_function_call") -func _prepare_host_function_call(_ argc: Int32) -> UnsafeMutableRawPointer { - let argumentSize = MemoryLayout.size * Int(argc) - return malloc(Int(argumentSize))! -} - -@_cdecl("swjs_cleanup_host_function_call") -func _cleanup_host_function_call(_ pointer: UnsafeMutableRawPointer) { - free(pointer) -} - -@_cdecl("swjs_call_host_function") -func _call_host_function( +@_cdecl("_call_host_function_impl") +func _call_host_function_impl( _ hostFuncRef: JavaScriptHostFuncRef, _ argv: UnsafePointer, _ argc: Int32, _ callbackFuncRef: JavaScriptObjectRef diff --git a/Sources/_CJavaScriptKit/_CJavaScriptKit.c b/Sources/_CJavaScriptKit/_CJavaScriptKit.c new file mode 100644 index 00000000..a1282b5d --- /dev/null +++ b/Sources/_CJavaScriptKit/_CJavaScriptKit.c @@ -0,0 +1,35 @@ +#include "_CJavaScriptKit.h" +#include + +#if __wasm32__ + +void _call_host_function_impl(const JavaScriptHostFuncRef host_func_ref, + const RawJSValue *argv, const int argc, + const JavaScriptObjectRef callback_func); + +__attribute__((export_name("swjs_call_host_function"))) +void _call_host_function(const JavaScriptHostFuncRef host_func_ref, + const RawJSValue *argv, const int argc, + const JavaScriptObjectRef callback_func) { + _call_host_function_impl(host_func_ref, argv, argc, callback_func); +} + +__attribute__((export_name("swjs_prepare_host_function_call"))) +void *_prepare_host_function_call(const int argc) { + return malloc(argc * sizeof(RawJSValue)); +} + +__attribute__((export_name("swjs_cleanup_host_function_call"))) +void _cleanup_host_function_call(void *argv_buffer) { + free(argv_buffer); +} + +/// The compatibility runtime library version. +/// Notes: If you change any interface of runtime library, please increment +/// this and `SwiftRuntime.version` in `./Runtime/src/index.ts`. +__attribute__((export_name("swjs_library_version"))) +int _library_version() { + return 700; +} + +#endif diff --git a/Sources/_CJavaScriptKit/dummy.c b/Sources/_CJavaScriptKit/dummy.c deleted file mode 100644 index 8b137891..00000000 --- a/Sources/_CJavaScriptKit/dummy.c +++ /dev/null @@ -1 +0,0 @@ - diff --git a/scripts/install-toolchain.sh b/scripts/install-toolchain.sh new file mode 100755 index 00000000..b6a82df2 --- /dev/null +++ b/scripts/install-toolchain.sh @@ -0,0 +1,41 @@ +#!/bin/bash +set -eu + +scripts_dir="$(cd "$(dirname $0)" && pwd)" + +swift_version="$(cat $scripts_dir/../.swift-version)" +swift_tag="swift-$swift_version" + +if [ -z "$(which swiftenv)" ]; then + echo "swiftenv not installed, please install it before this script." + exit 1 +fi + +if [ ! -z "$(swiftenv versions | grep $swift_version)" ]; then + echo "$swift_version is already installed." + exit 0 +fi + +case $(uname -s) in + Darwin) + toolchain_download="$swift_tag-osx.tar.gz" + ;; + Linux) + if [ $(grep RELEASE /etc/lsb-release) == "DISTRIB_RELEASE=18.04" ]; then + toolchain_download="$swift_tag-ubuntu18.04.tar.gz" + elif [ $(grep RELEASE /etc/lsb-release) == "DISTRIB_RELEASE=20.04" ]; then + toolchain_download="$swift_tag-ubuntu20.04.tar.gz" + else + echo "Unknown Ubuntu version" + exit 1 + fi + ;; + *) + echo "Unrecognised platform $(uname -s)" + exit 1 + ;; +esac + +toolchain_download_url="https://github.com/swiftwasm/swift/releases/download/$swift_tag/$toolchain_download" + +swiftenv install "$toolchain_download_url"