I'm looking for a solution to setup a swift SDK (e.g. framework) that access shared business logic from a javascript bundle (shared with for example an android app or SDK). I don't want to use webviews etc...
shared business logic in javascript bundle
I have a Javascript bundle containing the shared logic of a project, available as an api, e.g. myBundle.js which exports several modules/functions/objects.
I want to build a native Swift app, that uses the above mentioned Swift SDK and as such can access the business logic from myBundle.js:
// module "myModule.js"
function cube(x) {
return x * x * x;
}
const foo = Math.PI + 1.0;
export { cube, foo };
Step 1: In my Swift project I would like to
import Foundation
public class MySwiftClass {
func doSomeStuffBasedOnJavascriptCode() {
// access MyBundle.js, and run 'cube' method on 'foo' from MyModule
let x = cube(foo);
NSLog("result: %l", x);
}
}
Step 2: In addition, accessing the Swift code from JS would also be very useful!
I tried: ...using Nativescript with iOS, and was able to setup a Swift app that runs the V8 runtime. This setup allows me to access the native Swift code from the javascript bundle (Nativescript with swift). So step 2 is covered by that. However, Nativescript setups the runtime internally and 'runs the main context of the bundle' (expecting it to contain an application 'run'?). It wraps the logic of the underlying V8 runtime with a very simple Nativescript interface:
Config* config = [[Config alloc] init];
config.IsDebug = isDebug;
config.LogToSystemConsole = isDebug;
config.MetadataPtr = metadataPtr;
config.BaseDir = baseDir;
config.ArgumentsCount = argc;
config.Arguments = argv;
[NativeScript start:config];
Is there an easy way to access the underlying runtime's context and call functions on modules from the bundle explicitly throughout the Swift code? Just like you can add Swift code throughout the JS code in the bundle... So, I'm really looking for a bidirectional solution where javascript uses Swift code and Swift code uses JS.
Other solutions are also welcome!