diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bc07e6a5..8ba892e0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -86,3 +86,19 @@ jobs: - run: swift build env: DEVELOPER_DIR: /Applications/${{ matrix.xcode }}.app/Contents/Developer/ + + embedded-build: + name: Build for embedded target + runs-on: ubuntu-22.04 + strategy: + matrix: + entry: + - os: ubuntu-22.04 + toolchain: DEVELOPMENT-SNAPSHOT-2024-09-25-a + steps: + - uses: actions/checkout@v4 + - uses: ./.github/actions/install-swift + with: + swift-dir: development/ubuntu2204 + swift-version: swift-${{ matrix.entry.toolchain }} + - run: ./Examples/Embedded/build.sh diff --git a/Examples/Embedded/Package.swift b/Examples/Embedded/Package.swift index f0c03bd8..4ebc6e84 100644 --- a/Examples/Embedded/Package.swift +++ b/Examples/Embedded/Package.swift @@ -6,14 +6,31 @@ let package = Package( name: "Embedded", dependencies: [ .package(name: "JavaScriptKit", path: "../../"), - .package(url: "https://github.com/swifweb/EmbeddedFoundation", branch: "0.1.0") + .package(url: "https://github.com/swiftwasm/swift-dlmalloc", branch: "0.1.0") ], targets: [ .executableTarget( name: "EmbeddedApp", dependencies: [ "JavaScriptKit", - .product(name: "Foundation", package: "EmbeddedFoundation") + .product(name: "dlmalloc", package: "swift-dlmalloc") + ], + cSettings: [ + .unsafeFlags(["-fdeclspec"]) + ], + swiftSettings: [ + .enableExperimentalFeature("Embedded"), + .enableExperimentalFeature("Extern"), + .unsafeFlags([ + "-Xfrontend", "-gnone", + "-Xfrontend", "-disable-stack-protector", + ]), + ], + linkerSettings: [ + .unsafeFlags([ + "-Xclang-linker", "-nostdlib", + "-Xlinker", "--no-entry" + ]) ] ) ] diff --git a/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift b/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift index 20a26e08..773f928d 100644 --- a/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift +++ b/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift @@ -17,13 +17,9 @@ func strlen(_ s: UnsafePointer) -> Int { return p - s } -// TODO: why do I need this? and surely this is not ideal... figure this out, or at least have this come from a C lib -@_cdecl("memmove") -func memmove(_ dest: UnsafeMutableRawPointer, _ src: UnsafeRawPointer, _ n: Int) -> UnsafeMutableRawPointer { - let d = dest.assumingMemoryBound(to: UInt8.self) - let s = src.assumingMemoryBound(to: UInt8.self) - for i in 0.. JavaScriptValueKindAndFlags { - #if hasFeature(Embedded) - JavaScriptValueKindAndFlags(bitPattern: bits) - #else - unsafeBitCast(bits, to: JavaScriptValueKindAndFlags.self) - #endif -} \ No newline at end of file diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift b/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift index 95bc2bd9..8b4fc7cd 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift @@ -42,13 +42,14 @@ public class JSThrowingFunction { let argv = bufferPointer.baseAddress let argc = bufferPointer.count - var exceptionKind = JavaScriptValueKindAndFlags() + var exceptionRawKind = JavaScriptRawValueKindAndFlags() var exceptionPayload1 = JavaScriptPayload1() var exceptionPayload2 = JavaScriptPayload2() let resultObj = swjs_call_throwing_new( self.base.id, argv, Int32(argc), - &exceptionKind, &exceptionPayload1, &exceptionPayload2 + &exceptionRawKind, &exceptionPayload1, &exceptionPayload2 ) + let exceptionKind = JavaScriptValueKindAndFlags(bitPattern: exceptionRawKind) if exceptionKind.isException { let exception = RawJSValue(kind: exceptionKind.kind, payload1: exceptionPayload1, payload2: exceptionPayload2) return .failure(exception.jsValue) @@ -70,7 +71,7 @@ private func invokeJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSV rawValues.withUnsafeBufferPointer { bufferPointer -> (JSValue, Bool) in let argv = bufferPointer.baseAddress let argc = bufferPointer.count - var kindAndFlags = JavaScriptValueKindAndFlags() + let kindAndFlags: JavaScriptValueKindAndFlags var payload1 = JavaScriptPayload1() var payload2 = JavaScriptPayload2() if let thisId = this?.id { @@ -78,13 +79,13 @@ private func invokeJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSV thisId, id, argv, Int32(argc), &payload1, &payload2 ) - kindAndFlags = unsafeBitCast(resultBitPattern, to: JavaScriptValueKindAndFlags.self) + kindAndFlags = JavaScriptValueKindAndFlags(bitPattern: resultBitPattern) } else { let resultBitPattern = swjs_call_function( id, argv, Int32(argc), &payload1, &payload2 ) - kindAndFlags = unsafeBitCast(resultBitPattern, to: JavaScriptValueKindAndFlags.self) + kindAndFlags = JavaScriptValueKindAndFlags(bitPattern: resultBitPattern) } let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2) return (result.jsValue, kindAndFlags.isException) @@ -95,4 +96,4 @@ private func invokeJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSV } return result } -#endif \ No newline at end of file +#endif diff --git a/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h b/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h index 8daf7cdc..cac103c3 100644 --- a/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h +++ b/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h @@ -29,15 +29,7 @@ typedef enum __attribute__((enum_extensibility(closed))) { JavaScriptValueKindBigInt = 8, } JavaScriptValueKind; -#if __Embedded -// something about the bit field widths is not working with embedded -typedef unsigned short JavaScriptValueKindAndFlags; -#else -typedef struct { - JavaScriptValueKind kind: 31; - bool isException: 1; -} JavaScriptValueKindAndFlags; -#endif +typedef uint32_t JavaScriptRawValueKindAndFlags; typedef unsigned JavaScriptPayload1; typedef double JavaScriptPayload2; @@ -253,7 +245,7 @@ IMPORT_JS_FUNCTION(swjs_call_new, JavaScriptObjectRef, (const JavaScriptObjectRe IMPORT_JS_FUNCTION(swjs_call_throwing_new, JavaScriptObjectRef, (const JavaScriptObjectRef ref, const RawJSValue *argv, const int argc, - JavaScriptValueKindAndFlags *exception_kind, + JavaScriptRawValueKindAndFlags *exception_kind, JavaScriptPayload1 *exception_payload1, JavaScriptPayload2 *exception_payload2))