diff --git a/IntegrationTests/TestSuites/Package.swift b/IntegrationTests/TestSuites/Package.swift index 1bb48648b..0344d0499 100644 --- a/IntegrationTests/TestSuites/Package.swift +++ b/IntegrationTests/TestSuites/Package.swift @@ -23,7 +23,11 @@ let package = Package( ], dependencies: [.package(name: "JavaScriptKit", path: "../../")], targets: [ - .target(name: "PrimaryTests", dependencies: ["JavaScriptKit"]), + .target(name: "CHelpers"), + .target(name: "PrimaryTests", dependencies: [ + "JavaScriptKit", + "CHelpers", + ]), .target( name: "ConcurrencyTests", dependencies: [ diff --git a/IntegrationTests/TestSuites/Sources/CHelpers/helpers.c b/IntegrationTests/TestSuites/Sources/CHelpers/helpers.c new file mode 100644 index 000000000..8922cb735 --- /dev/null +++ b/IntegrationTests/TestSuites/Sources/CHelpers/helpers.c @@ -0,0 +1,4 @@ +int growMemory(int pages) { + return __builtin_wasm_memory_grow(0, pages); +} + diff --git a/IntegrationTests/TestSuites/Sources/CHelpers/include/helpers.h b/IntegrationTests/TestSuites/Sources/CHelpers/include/helpers.h new file mode 100644 index 000000000..c5505a5a4 --- /dev/null +++ b/IntegrationTests/TestSuites/Sources/CHelpers/include/helpers.h @@ -0,0 +1,5 @@ +/// Ask host to grow WebAssembly module's allocated memory +/// +/// @param pages Number of memory pages to increase memory by. +int growMemory(int pages); + diff --git a/IntegrationTests/TestSuites/Sources/CHelpers/include/module.modulemap b/IntegrationTests/TestSuites/Sources/CHelpers/include/module.modulemap new file mode 100644 index 000000000..3503a233f --- /dev/null +++ b/IntegrationTests/TestSuites/Sources/CHelpers/include/module.modulemap @@ -0,0 +1,4 @@ +module CHelpers { + header "helpers.h" + export * +} diff --git a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift index 3f7758e16..5110001fe 100644 --- a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift +++ b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift @@ -1,5 +1,5 @@ import JavaScriptKit - +import CHelpers try test("Literal Conversion") { let global = JSObject.global @@ -735,4 +735,15 @@ try test("Exception") { try expectNotNil(errorObject3) } +/// If WebAssembly.Memory is not accessed correctly (i.e. creating a new view each time), +/// this test will fail with `TypeError: Cannot perform Construct on a detached ArrayBuffer`, +/// since asking to grow memory will detach the backing ArrayBuffer. +/// See https://github.com/swiftwasm/JavaScriptKit/pull/153 +try test("Grow Memory") { + let string = "Hello" + let jsString = JSValue.string(string) + growMemory(1) + try expectEqual(string, jsString.description) +} + Expectation.wait(expectations)