-
-
Notifications
You must be signed in to change notification settings - Fork 56
BridgeJS: Adding support for static / class properties & functions #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BridgeJS: Adding support for static / class properties & functions #448
Conversation
…on of static properties and static functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, looks good to me, modulo the enum static properties and methods APIs. Thanks!
Calculator.square = function(value) { | ||
const ret = instance.exports.bjs_Calculator_static_square(value); | ||
return ret; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think BridgeJS should support multiple instantiations as long as the global namespace feature is not used in principle, so I'd like to keep any APIs accessing an instance in the Exports
set returned by an instantiation.
So I expect the generated TS interface would have something like below (we need a better naming convention for the constant value type and the type holding static methods though 😅):
export const Calculator: {
readonly Scientific: 0;
readonly Basic: 1;
};
export type CalculatorConst = typeof Calculator[keyof typeof Calculator];
export type Exports = {
Calculator: {
square(value: number): number;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable 👌 I’ll update PR after next week though, so hang in there 🙏
What about ‚namespace enums’, are you fine with globalThis namespaces properties and functions as implemented now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
For namespace enums, I have been thinking more about that feature, but we might need to split the "namespacing" feature and the "exposing to global" feature for better consistency.
@JS enum Utils {
@JS enum String {
@JS static func uppercase(_ text: String) -> String {
return text.uppercased()
}
}
}
@JS(namespace: "Networking.APIV2")
enum Internal {
@JS enum SupportedMethod {
case get
case post
static func describe(_ method: SupportedMethod) -> String { ... }
}
}
I expect the above code will generate the following interface by default:
namespace Networking {
namespace APIV2 {
namespace Internal {
type SupportedMethod = typeof Networking.APIV2.Internal.SupportedMethod[keyof typeof Networking.APIV2.Internal.SupportedMethod];
}
}
}
export const Networking = {
APIV2: {
Internal {
SupportedMethod: {
Get: 0;
Post: 1;
}
}
}
}
export type Exports = {
Utils: {
String: {
uppercase(text: string): string;
}
},
Networking: {
APIV2 {
Internal: {
SupportedMethod: {
describe(method: Networking.APIV2.Internal.SupportedMethod): string;
},
}
}
}
}
Then if we set { "exposeToGlobal": true }
in bridge-js.config.json
, then it will add:
+namespace global {
namespace Networking {
namespace APIV2 {
namespace Internal {
type SupportedMethod = typeof Networking.APIV2.Internal.SupportedMethod[keyof typeof Networking.APIV2.Internal.SupportedMethod];
}
}
}
+}
export const Networking = {
APIV2: {
Internal {
SupportedMethod: {
Get: 0;
Post: 1;
+ describe(method: Networking.APIV2.Internal.SupportedMethod): string;
}
}
}
}
But I think that change would be out of scope in this PR, so I'm fine with keeping it as is for now.
7ff3af4
to
0828f80
Compare
@kateinoigakukun addressed feedback, for namespace and global started new issue: https://github.com/swiftwasm/JavaScriptKit/issues As for current changes, as agreed, I went with following approach: export const CalculatorValues: {
readonly Scientific: 0;
readonly Basic: 1;
};
export type CalculatorTag = typeof CalculatorValues[keyof typeof CalculatorValues];
export type CalculatorObject = typeof CalculatorValues & {
square(value: number): number;
};
export type Exports = {
Calculator: CalculatorObject
APIResult: APIResultObject
} I started with applying this pattern only to enums with static properties / functions, but eventually decided to apply this for all enums, to keep everything consistent and avoid API changes if enum would be extended by static content later on 🙏🏻 |
0828f80
to
f286bd8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much!
Introduction
This PR adds support for Swift static / class functions and properties when exporting Swift funcionality to JS / TS using BridgeJS plugin.
Overview
Both static / class functions are translated to
static
in JS.Everything beside willSet / didSet is supported similarly to instance properties.
Static functions in namespaced enums are translated to
globalThis
namespaced functions.Examples can be found in added documentation.
Additional changes
Resolves
Testing
Added tests for different scenarios for all supported optional data types, including parameters, properties and return value usage.
Documentation
Extended current documentation with new