Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
367 changes: 318 additions & 49 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSCore/TypeDeclResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,26 @@ class TypeDeclResolver {
func lookupType(fullyQualified: QualifiedName) -> TypeDecl? {
return typeDeclByQualifiedName[fullyQualified]
}

/// Resolves a type usage (identifier or member type) to its declaration node
func resolve(_ type: TypeSyntax) -> TypeDecl? {
if let id = type.as(IdentifierTypeSyntax.self) {
return lookupType(for: id)
}
if let components = qualifiedComponents(from: type) {
return lookupType(fullyQualified: components)
}
return nil
}

private func qualifiedComponents(from type: TypeSyntax) -> QualifiedName? {
if let m = type.as(MemberTypeSyntax.self) {
guard let base = qualifiedComponents(from: TypeSyntax(m.baseType)) else { return nil }
return base + [m.name.text]
} else if let id = type.as(IdentifierTypeSyntax.self) {
return [id.name.text]
} else {
return nil
}
}
}
429 changes: 376 additions & 53 deletions Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@JS
enum APIResult {
case success(String)
case failure(Int)
case flag(Bool)
case rate(Float)
case precise(Double)
case info
}

@JS func handle(result: APIResult)
@JS func getResult() -> APIResult

@JS
enum ComplexResult {
case success(String)
case error(String, Int)
case status(Bool, Int, String)
case coordinates(Double, Double, Double)
case comprehensive(Bool, Bool, Int, Int, Double, Double, String, String, String)
case info
}

@JS func handleComplex(result: ComplexResult)
@JS func getComplexResult() -> ComplexResult

@JS
enum Utilities {
@JS enum Result {
case success(String)
case failure(String, Int)
case status(Bool, Int, String)
}
}

@JS(namespace: "API")
@JS enum NetworkingResult {
case success(String)
case failure(String, Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export async function createInstantiator(options, swift) {
let tmpRetString;
let tmpRetBytes;
let tmpRetException;
let tmpRetTag;

let tmpRetStrings = [];
let tmpRetInts = [];
let tmpRetF32s = [];
let tmpRetF64s = [];
let tmpRetBools = [];

return {
/**
* @param {WebAssembly.Imports} importObject
Expand All @@ -24,7 +32,9 @@ export async function createInstantiator(options, swift) {
const imports = options.getImports(importsContext);
bjs["swift_js_return_string"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
tmpRetString = textDecoder.decode(bytes);
const value = textDecoder.decode(bytes);
tmpRetString = value;
tmpRetStrings.push(value);
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
Expand All @@ -49,6 +59,30 @@ export async function createInstantiator(options, swift) {
bjs["swift_js_release"] = function(id) {
swift.memory.release(id);
}
bjs["swift_js_return_tag"] = function(tag) {
tmpRetTag = tag | 0;
tmpRetString = undefined;
tmpRetStrings = [];
tmpRetInts = [];
tmpRetF32s = [];
tmpRetF64s = [];
tmpRetBools = [];
}
bjs["swift_js_return_int"] = function(v) {
const value = v | 0;
tmpRetInts.push(value);
}
bjs["swift_js_return_f32"] = function(v) {
const value = Math.fround(v);
tmpRetF32s.push(value);
}
bjs["swift_js_return_f64"] = function(v) {
tmpRetF64s.push(v);
}
bjs["swift_js_return_bool"] = function(v) {
const value = v !== 0;
tmpRetBools.push(value);
}

const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
TestModule["bjs_checkArray"] = function bjs_checkArray(a) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export async function createInstantiator(options, swift) {
let tmpRetString;
let tmpRetBytes;
let tmpRetException;
let tmpRetTag;

let tmpRetStrings = [];
let tmpRetInts = [];
let tmpRetF32s = [];
let tmpRetF64s = [];
let tmpRetBools = [];

return {
/**
* @param {WebAssembly.Imports} importObject
Expand All @@ -24,7 +32,9 @@ export async function createInstantiator(options, swift) {
const imports = options.getImports(importsContext);
bjs["swift_js_return_string"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
tmpRetString = textDecoder.decode(bytes);
const value = textDecoder.decode(bytes);
tmpRetString = value;
tmpRetStrings.push(value);
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
Expand All @@ -49,6 +59,30 @@ export async function createInstantiator(options, swift) {
bjs["swift_js_release"] = function(id) {
swift.memory.release(id);
}
bjs["swift_js_return_tag"] = function(tag) {
tmpRetTag = tag | 0;
tmpRetString = undefined;
tmpRetStrings = [];
tmpRetInts = [];
tmpRetF32s = [];
tmpRetF64s = [];
tmpRetBools = [];
}
bjs["swift_js_return_int"] = function(v) {
const value = v | 0;
tmpRetInts.push(value);
}
bjs["swift_js_return_f32"] = function(v) {
const value = Math.fround(v);
tmpRetF32s.push(value);
}
bjs["swift_js_return_f64"] = function(v) {
tmpRetF64s.push(v);
}
bjs["swift_js_return_bool"] = function(v) {
const value = v !== 0;
tmpRetBools.push(value);
}


},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export async function createInstantiator(options, swift) {
let tmpRetString;
let tmpRetBytes;
let tmpRetException;
let tmpRetTag;

let tmpRetStrings = [];
let tmpRetInts = [];
let tmpRetF32s = [];
let tmpRetF64s = [];
let tmpRetBools = [];

return {
/**
* @param {WebAssembly.Imports} importObject
Expand All @@ -24,7 +32,9 @@ export async function createInstantiator(options, swift) {
const imports = options.getImports(importsContext);
bjs["swift_js_return_string"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
tmpRetString = textDecoder.decode(bytes);
const value = textDecoder.decode(bytes);
tmpRetString = value;
tmpRetStrings.push(value);
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
Expand All @@ -49,6 +59,30 @@ export async function createInstantiator(options, swift) {
bjs["swift_js_release"] = function(id) {
swift.memory.release(id);
}
bjs["swift_js_return_tag"] = function(tag) {
tmpRetTag = tag | 0;
tmpRetString = undefined;
tmpRetStrings = [];
tmpRetInts = [];
tmpRetF32s = [];
tmpRetF64s = [];
tmpRetBools = [];
}
bjs["swift_js_return_int"] = function(v) {
const value = v | 0;
tmpRetInts.push(value);
}
bjs["swift_js_return_f32"] = function(v) {
const value = Math.fround(v);
tmpRetF32s.push(value);
}
bjs["swift_js_return_f64"] = function(v) {
tmpRetF64s.push(v);
}
bjs["swift_js_return_bool"] = function(v) {
const value = v !== 0;
tmpRetBools.push(value);
}

const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
TestModule["bjs_asyncReturnVoid"] = function bjs_asyncReturnVoid() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
// DO NOT EDIT.
//
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

export const APIResult: {
readonly Tag: {
readonly Success: 0;
readonly Failure: 1;
readonly Flag: 2;
readonly Rate: 3;
readonly Precise: 4;
readonly Info: 5;
};
};

export type APIResult =
{ tag: typeof APIResult.Tag.Success; param0: string } | { tag: typeof APIResult.Tag.Failure; param0: number } | { tag: typeof APIResult.Tag.Flag; param0: boolean } | { tag: typeof APIResult.Tag.Rate; param0: number } | { tag: typeof APIResult.Tag.Precise; param0: number } | { tag: typeof APIResult.Tag.Info }

export const ComplexResult: {
readonly Tag: {
readonly Success: 0;
readonly Error: 1;
readonly Status: 2;
readonly Coordinates: 3;
readonly Comprehensive: 4;
readonly Info: 5;
};
};

export type ComplexResult =
{ tag: typeof ComplexResult.Tag.Success; param0: string } | { tag: typeof ComplexResult.Tag.Error; param0: string; param1: number } | { tag: typeof ComplexResult.Tag.Status; param0: boolean; param1: number; param2: string } | { tag: typeof ComplexResult.Tag.Coordinates; param0: number; param1: number; param2: number } | { tag: typeof ComplexResult.Tag.Comprehensive; param0: boolean; param1: boolean; param2: number; param3: number; param4: number; param5: number; param6: string; param7: string; param8: string } | { tag: typeof ComplexResult.Tag.Info }

export {};

declare global {
namespace API {
const NetworkingResult: {
readonly Tag: {
readonly Success: 0;
readonly Failure: 1;
};
};
type NetworkingResult =
{ tag: typeof NetworkingResult.Tag.Success; param0: string } | { tag: typeof NetworkingResult.Tag.Failure; param0: string; param1: number }
}
namespace Utilities {
const Result: {
readonly Tag: {
readonly Success: 0;
readonly Failure: 1;
readonly Status: 2;
};
};
type Result =
{ tag: typeof Result.Tag.Success; param0: string } | { tag: typeof Result.Tag.Failure; param0: string; param1: number } | { tag: typeof Result.Tag.Status; param0: boolean; param1: number; param2: string }
}
}

export type Exports = {
handle(result: APIResult): void;
getResult(): APIResult;
handleComplex(result: ComplexResult): void;
getComplexResult(): ComplexResult;
}
export type Imports = {
}
export function createInstantiator(options: {
imports: Imports;
}, swift: any): Promise<{
addImports: (importObject: WebAssembly.Imports) => void;
setInstance: (instance: WebAssembly.Instance) => void;
createExports: (instance: WebAssembly.Instance) => Exports;
}>;
Loading