Skip to content

Commit 6fdf889

Browse files
authored
Add test for detached ArrayBuffer (swiftwasm#154)
1 parent 16eaaa4 commit 6fdf889

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

Diff for: IntegrationTests/TestSuites/Package.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ let package = Package(
2323
],
2424
dependencies: [.package(name: "JavaScriptKit", path: "../../")],
2525
targets: [
26-
.target(name: "PrimaryTests", dependencies: ["JavaScriptKit"]),
26+
.target(name: "CHelpers"),
27+
.target(name: "PrimaryTests", dependencies: [
28+
"JavaScriptKit",
29+
"CHelpers",
30+
]),
2731
.target(
2832
name: "ConcurrencyTests",
2933
dependencies: [
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int growMemory(int pages) {
2+
return __builtin_wasm_memory_grow(0, pages);
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Ask host to grow WebAssembly module's allocated memory
2+
///
3+
/// @param pages Number of memory pages to increase memory by.
4+
int growMemory(int pages);
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CHelpers {
2+
header "helpers.h"
3+
export *
4+
}

Diff for: IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import JavaScriptKit
2-
2+
import CHelpers
33

44
try test("Literal Conversion") {
55
let global = JSObject.global
@@ -735,4 +735,15 @@ try test("Exception") {
735735
try expectNotNil(errorObject3)
736736
}
737737

738+
/// If WebAssembly.Memory is not accessed correctly (i.e. creating a new view each time),
739+
/// this test will fail with `TypeError: Cannot perform Construct on a detached ArrayBuffer`,
740+
/// since asking to grow memory will detach the backing ArrayBuffer.
741+
/// See https://github.com/swiftwasm/JavaScriptKit/pull/153
742+
try test("Grow Memory") {
743+
let string = "Hello"
744+
let jsString = JSValue.string(string)
745+
growMemory(1)
746+
try expectEqual(string, jsString.description)
747+
}
748+
738749
Expectation.wait(expectations)

0 commit comments

Comments
 (0)