Swiftpack.co -  jectivex/JXKit as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
jectivex/JXKit
A pure swift interface to JavaScriptCore
.package(url: "https://github.com/jectivex/JXKit.git", from: "1.0.1")

JXKit

Build Status Swift Package Manager compatible Platform

A pure swift interface the JavaScriptCore C API with support for Encodable & Decodable.

Browse the API Documentation.

This permits JSC to be used on platforms where the Objective-C runtime is unavailable (e.g., Linux).

API

Direct function invocation

Functions can be accessed (and cached) to be invoked directly with codable arguments:

let ctx = JXContext()
let hypot = ctx["Math"]["hypot"]
assert(hypot.isFunction == true)
let result = hypot.call(withArguments: try [ctx.encode(3), ctx.encode(4)])
assert(result.numberValue == 5)

Codable passing

JXKit supports encoding and decoding Swift types directly into the JXValue instances, which enables Codable instances to be passed back and forth to the virtual machine with minimal overhead. Since encoding & decoding doesn't use JSON stringify & parse, this can lead to considerable performance improvements when interfacing between Swift & JS.

The above invocation of Math.hypot can instead be performed by wrapping the arguments in an Encodable struct, and returning a Decodable value.

/// An example of invoking `Math.hypot` in a wrapper function that takes an encodable argument and returns a Decodable retult.
struct AB : Encodable { let a, b: Double }
struct C : Decodable { let c: Double }

let ctx = JXContext()

let hypot = try ctx.eval(script: "(function(args) { return { c: Math.hypot(args.a, args.b) }; })")
assert(hypot.isFunction == true)

let result: C = try hypot.call(withArguments: [ctx.encode(AB(a: 3, b: 4))]).toDecodable(ofType: C.self)
assert(result.c == 5)

JavaScriptCore Compatibility

The JXKit API is a mostly drop-in replacement for the Objective-C JavaScriptCore framework available on most Apple devices. E.g., the following JavaScriptCore code:

import JavaScriptCore

let ctx = JSContext()
let value: JXValue = ctx.evaluateScript("1+2")
assert(value.doubleValue == 3)

becomes:

import JXKit

let ctx = JXContext()
let value: JXValue = ctx.evaluateScript("1+2")
assert(value.doubleValue == 3)

Installation

Note: Requires Swift 5.3+

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code.

  1. Add the following to your Package.swift file:
// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "MyPackage",
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]),
    ],
    dependencies: [
        .package(name: "JXKit", url: "https://github.com/jectivex/JXKit.git", .upToNextMajor(from: "1.0")),
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: ["JXKit"]),
            .testTarget(
                name: "MyPackageTests",
                dependencies: ["MyPackage"]),
        ]
    )
  1. Build your project:
$ swift build

GitHub

link
Stars: 2
Last commit: 2 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API