Skip to content

Commit 5c596cb

Browse files
Add snapshot tests for JS glue for importing TS
1 parent 7309d97 commit 5c596cb

34 files changed

+745
-20
lines changed

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,5 @@ struct ImportedFileSkeleton: Codable {
9292

9393
struct ImportedModuleSkeleton: Codable {
9494
let moduleName: String
95-
let children: [ImportedFileSkeleton]
95+
var children: [ImportedFileSkeleton]
9696
}

Plugins/BridgeJS/Sources/BridgeJSTool/BridgeJSTool.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ import SwiftParser
115115
)
116116
try (outputSwift ?? "").write(to: outputSwiftURL, atomically: true, encoding: .utf8)
117117

118-
let outputSkeletons = ImportedModuleSkeleton(moduleName: importer.moduleName, children: importer.skeletons)
119118
let outputSkeletonsURL = URL(fileURLWithPath: doubleDashOptions["output-skeleton"]!)
120119
try FileManager.default.createDirectory(
121120
at: outputSkeletonsURL.deletingLastPathComponent(),
@@ -124,7 +123,7 @@ import SwiftParser
124123
)
125124
let encoder = JSONEncoder()
126125
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
127-
try encoder.encode(outputSkeletons).write(to: outputSkeletonsURL)
126+
try encoder.encode(importer.skeleton).write(to: outputSkeletonsURL)
128127

129128
progress.print(
130129
"""

Plugins/BridgeJS/Sources/BridgeJSTool/ImportTS.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ import Foundation
1313
/// JavaScript glue code and TypeScript definitions.
1414
struct ImportTS {
1515
let progress: ProgressReporting
16-
let moduleName: String
17-
private(set) var skeletons: [ImportedFileSkeleton] = []
16+
private(set) var skeleton: ImportedModuleSkeleton
17+
private var moduleName: String {
18+
skeleton.moduleName
19+
}
1820

1921
init(progress: ProgressReporting, moduleName: String) {
2022
self.progress = progress
21-
self.moduleName = moduleName
23+
self.skeleton = ImportedModuleSkeleton(moduleName: moduleName, children: [])
2224
}
2325

2426
/// Adds a skeleton to the importer's state
2527
mutating func addSkeleton(_ skeleton: ImportedFileSkeleton) {
26-
self.skeletons.append(skeleton)
28+
self.skeleton.children.append(skeleton)
2729
}
2830

2931
/// Processes a TypeScript definition file and extracts its API information
@@ -69,7 +71,7 @@ struct ImportTS {
6971
/// Finalizes the import process and generates Swift code
7072
func finalize() throws -> String? {
7173
var decls: [DeclSyntax] = []
72-
for skeleton in self.skeletons {
74+
for skeleton in self.skeleton.children {
7375
for function in skeleton.functions {
7476
let thunkDecls = try renderSwiftThunk(function, topLevelDecls: &decls)
7577
decls.append(contentsOf: thunkDecls)

Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSLinkTests.swift

+31-12
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@ import Testing
88

99
@Suite struct BridgeJSLinkTests {
1010
private func snapshot(
11-
swiftAPI: ExportSwift,
11+
bridgeJSLink: BridgeJSLink,
1212
name: String? = nil,
1313
filePath: String = #filePath,
1414
function: String = #function,
1515
sourceLocation: Testing.SourceLocation = #_sourceLocation
1616
) throws {
17-
let (_, outputSkeleton) = try #require(try swiftAPI.finalize())
18-
let encoder = JSONEncoder()
19-
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
20-
let outputSkeletonData = try encoder.encode(outputSkeleton)
21-
var bridgeJSLink = BridgeJSLink()
22-
try bridgeJSLink.addExportedSkeletonFile(data: outputSkeletonData)
2317
let (outputJs, outputDts) = try bridgeJSLink.link()
2418
try assertSnapshot(
2519
name: name,
@@ -43,19 +37,44 @@ import Testing
4337
"Inputs"
4438
)
4539

46-
static func collectInputs() -> [String] {
40+
static func collectInputs(extension: String) -> [String] {
4741
let fileManager = FileManager.default
4842
let inputs = try! fileManager.contentsOfDirectory(atPath: Self.inputsDirectory.path)
49-
return inputs.filter { $0.hasSuffix(".swift") }
43+
return inputs.filter { $0.hasSuffix(`extension`) }
5044
}
5145

52-
@Test(arguments: collectInputs())
53-
func snapshot(input: String) throws {
46+
@Test(arguments: collectInputs(extension: ".swift"))
47+
func snapshotExport(input: String) throws {
5448
let url = Self.inputsDirectory.appendingPathComponent(input)
5549
let sourceFile = Parser.parse(source: try String(contentsOf: url, encoding: .utf8))
5650
let swiftAPI = ExportSwift(progress: .silent)
5751
try swiftAPI.addSourceFile(sourceFile, input)
5852
let name = url.deletingPathExtension().lastPathComponent
59-
try snapshot(swiftAPI: swiftAPI, name: name)
53+
54+
let (_, outputSkeleton) = try #require(try swiftAPI.finalize())
55+
let encoder = JSONEncoder()
56+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
57+
let outputSkeletonData = try encoder.encode(outputSkeleton)
58+
var bridgeJSLink = BridgeJSLink()
59+
try bridgeJSLink.addExportedSkeletonFile(data: outputSkeletonData)
60+
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Export")
61+
}
62+
63+
@Test(arguments: collectInputs(extension: ".d.ts"))
64+
func snapshotImport(input: String) throws {
65+
let url = Self.inputsDirectory.appendingPathComponent(input)
66+
let tsconfigPath = url.deletingLastPathComponent().appendingPathComponent("tsconfig.json")
67+
68+
var importTS = ImportTS(progress: .silent, moduleName: "TestModule")
69+
try importTS.addSourceFile(url.path, tsconfigPath: tsconfigPath.path)
70+
let name = url.deletingPathExtension().deletingPathExtension().lastPathComponent
71+
72+
let encoder = JSONEncoder()
73+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
74+
let outputSkeletonData = try encoder.encode(importTS.skeleton)
75+
76+
var bridgeJSLink = BridgeJSLink()
77+
try bridgeJSLink.addImportedSkeletonFile(data: outputSkeletonData)
78+
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Import")
6079
}
6180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export type Exports = {
8+
}
9+
export type Imports = {
10+
checkArray(a: any): void;
11+
checkArrayWithLength(a: any, b: number): void;
12+
checkArray(a: any): void;
13+
}
14+
export function createInstantiator(options: {
15+
imports: Imports;
16+
}, swift: any): Promise<{
17+
addImports: (importObject: WebAssembly.Imports) => void;
18+
setInstance: (instance: WebAssembly.Instance) => void;
19+
createExports: (instance: WebAssembly.Instance) => Exports;
20+
}>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export async function createInstantiator(options, swift) {
8+
let instance;
9+
let memory;
10+
const textDecoder = new TextDecoder("utf-8");
11+
const textEncoder = new TextEncoder("utf-8");
12+
13+
let tmpRetString;
14+
let tmpRetBytes;
15+
return {
16+
/** @param {WebAssembly.Imports} importObject */
17+
addImports: (importObject) => {
18+
const bjs = {};
19+
importObject["bjs"] = bjs;
20+
bjs["return_string"] = function(ptr, len) {
21+
const bytes = new Uint8Array(memory.buffer, ptr, len);
22+
tmpRetString = textDecoder.decode(bytes);
23+
}
24+
bjs["init_memory"] = function(sourceId, bytesPtr) {
25+
const source = swift.memory.getObject(sourceId);
26+
const bytes = new Uint8Array(memory.buffer, bytesPtr);
27+
bytes.set(source);
28+
}
29+
bjs["make_jsstring"] = function(ptr, len) {
30+
const bytes = new Uint8Array(memory.buffer, ptr, len);
31+
return swift.memory.retain(textDecoder.decode(bytes));
32+
}
33+
bjs["init_memory_with_result"] = function(ptr, len) {
34+
const target = new Uint8Array(memory.buffer, ptr, len);
35+
target.set(tmpRetBytes);
36+
tmpRetBytes = undefined;
37+
}
38+
const TestModule = importObject["TestModule"] = {};
39+
TestModule["bjs_checkArray"] = function bjs_checkArray(a) {
40+
options.imports.checkArray(swift.memory.getObject(a));
41+
}
42+
TestModule["bjs_checkArrayWithLength"] = function bjs_checkArrayWithLength(a, b) {
43+
options.imports.checkArrayWithLength(swift.memory.getObject(a), b);
44+
}
45+
TestModule["bjs_checkArray"] = function bjs_checkArray(a) {
46+
options.imports.checkArray(swift.memory.getObject(a));
47+
}
48+
},
49+
setInstance: (i) => {
50+
instance = i;
51+
memory = instance.exports.memory;
52+
},
53+
/** @param {WebAssembly.Instance} instance */
54+
createExports: (instance) => {
55+
const js = swift.memory.heap;
56+
57+
return {
58+
59+
};
60+
},
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export type Exports = {
8+
}
9+
export type Imports = {
10+
returnAnimatable(): any;
11+
}
12+
export function createInstantiator(options: {
13+
imports: Imports;
14+
}, swift: any): Promise<{
15+
addImports: (importObject: WebAssembly.Imports) => void;
16+
setInstance: (instance: WebAssembly.Instance) => void;
17+
createExports: (instance: WebAssembly.Instance) => Exports;
18+
}>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export async function createInstantiator(options, swift) {
8+
let instance;
9+
let memory;
10+
const textDecoder = new TextDecoder("utf-8");
11+
const textEncoder = new TextEncoder("utf-8");
12+
13+
let tmpRetString;
14+
let tmpRetBytes;
15+
return {
16+
/** @param {WebAssembly.Imports} importObject */
17+
addImports: (importObject) => {
18+
const bjs = {};
19+
importObject["bjs"] = bjs;
20+
bjs["return_string"] = function(ptr, len) {
21+
const bytes = new Uint8Array(memory.buffer, ptr, len);
22+
tmpRetString = textDecoder.decode(bytes);
23+
}
24+
bjs["init_memory"] = function(sourceId, bytesPtr) {
25+
const source = swift.memory.getObject(sourceId);
26+
const bytes = new Uint8Array(memory.buffer, bytesPtr);
27+
bytes.set(source);
28+
}
29+
bjs["make_jsstring"] = function(ptr, len) {
30+
const bytes = new Uint8Array(memory.buffer, ptr, len);
31+
return swift.memory.retain(textDecoder.decode(bytes));
32+
}
33+
bjs["init_memory_with_result"] = function(ptr, len) {
34+
const target = new Uint8Array(memory.buffer, ptr, len);
35+
target.set(tmpRetBytes);
36+
tmpRetBytes = undefined;
37+
}
38+
const TestModule = importObject["TestModule"] = {};
39+
TestModule["bjs_returnAnimatable"] = function bjs_returnAnimatable() {
40+
let ret = options.imports.returnAnimatable();
41+
return swift.memory.retain(ret);
42+
}
43+
TestModule["bjs_Animatable_animate"] = function bjs_Animatable_animate(self, keyframes, options) {
44+
let ret = swift.memory.getObject(self).animate(swift.memory.getObject(keyframes), swift.memory.getObject(options));
45+
return swift.memory.retain(ret);
46+
}
47+
TestModule["bjs_Animatable_getAnimations"] = function bjs_Animatable_getAnimations(self, options) {
48+
let ret = swift.memory.getObject(self).getAnimations(swift.memory.getObject(options));
49+
return swift.memory.retain(ret);
50+
}
51+
},
52+
setInstance: (i) => {
53+
instance = i;
54+
memory = instance.exports.memory;
55+
},
56+
/** @param {WebAssembly.Instance} instance */
57+
createExports: (instance) => {
58+
const js = swift.memory.heap;
59+
60+
return {
61+
62+
};
63+
},
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export type Exports = {
8+
}
9+
export type Imports = {
10+
check(a: number, b: boolean): void;
11+
}
12+
export function createInstantiator(options: {
13+
imports: Imports;
14+
}, swift: any): Promise<{
15+
addImports: (importObject: WebAssembly.Imports) => void;
16+
setInstance: (instance: WebAssembly.Instance) => void;
17+
createExports: (instance: WebAssembly.Instance) => Exports;
18+
}>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export async function createInstantiator(options, swift) {
8+
let instance;
9+
let memory;
10+
const textDecoder = new TextDecoder("utf-8");
11+
const textEncoder = new TextEncoder("utf-8");
12+
13+
let tmpRetString;
14+
let tmpRetBytes;
15+
return {
16+
/** @param {WebAssembly.Imports} importObject */
17+
addImports: (importObject) => {
18+
const bjs = {};
19+
importObject["bjs"] = bjs;
20+
bjs["return_string"] = function(ptr, len) {
21+
const bytes = new Uint8Array(memory.buffer, ptr, len);
22+
tmpRetString = textDecoder.decode(bytes);
23+
}
24+
bjs["init_memory"] = function(sourceId, bytesPtr) {
25+
const source = swift.memory.getObject(sourceId);
26+
const bytes = new Uint8Array(memory.buffer, bytesPtr);
27+
bytes.set(source);
28+
}
29+
bjs["make_jsstring"] = function(ptr, len) {
30+
const bytes = new Uint8Array(memory.buffer, ptr, len);
31+
return swift.memory.retain(textDecoder.decode(bytes));
32+
}
33+
bjs["init_memory_with_result"] = function(ptr, len) {
34+
const target = new Uint8Array(memory.buffer, ptr, len);
35+
target.set(tmpRetBytes);
36+
tmpRetBytes = undefined;
37+
}
38+
const TestModule = importObject["TestModule"] = {};
39+
TestModule["bjs_check"] = function bjs_check(a, b) {
40+
options.imports.check(a, b);
41+
}
42+
},
43+
setInstance: (i) => {
44+
instance = i;
45+
memory = instance.exports.memory;
46+
},
47+
/** @param {WebAssembly.Instance} instance */
48+
createExports: (instance) => {
49+
const js = swift.memory.heap;
50+
51+
return {
52+
53+
};
54+
},
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export type Exports = {
8+
}
9+
export type Imports = {
10+
checkNumber(): number;
11+
checkBoolean(): boolean;
12+
}
13+
export function createInstantiator(options: {
14+
imports: Imports;
15+
}, swift: any): Promise<{
16+
addImports: (importObject: WebAssembly.Imports) => void;
17+
setInstance: (instance: WebAssembly.Instance) => void;
18+
createExports: (instance: WebAssembly.Instance) => Exports;
19+
}>;

0 commit comments

Comments
 (0)