Skip to content

Latest commit

 

History

History
executable file
·
84 lines (58 loc) · 1.67 KB

README.md

File metadata and controls

executable file
·
84 lines (58 loc) · 1.67 KB

QuickJS-Swift

CI Status

Swift bindings to QuickJS. Using NSRunloop to implement asynchronization.

Usage

Execute javascript code

import QuickJS

let runtime = JSRuntime()!
let context = runtime.createContext()!

let jsCode = "var i = 10; i;"
let result = context.eval(jsCode).int
print("Result is \(result!)") //10

Create a native module

import QuickJS

let runtime = JSRuntime()!
let context = runtime.createContext()!

// Create a module named "Magic" with two functions "getMagic" and "getMagic2"
context.module("Magic") {
    JSModuleFunction("getMagic", argc: 0) { context, this, argc, argv in
        return 10
    }
    JSModuleFunction("getMagic2", argc: 0) { context, this, argc, argv in
        return 20
    }
}

let getMagic = """
"use strict";
import { getMagic, getMagic2 } from 'swift'
globalThis.magic = getMagic();
globalThis.magic2 = getMagic2();
"""

context.eval(getMagic, type: .module)

let magic = context.eval("magic;").int
print("Magic is \(magic!)") //10

let magic2 = context.eval("magic2;").int
print("Magic2 is \(magic2!)") //20

Runloop

let runtime = JSRuntime()!
let context = runtime.createContext()!
context.enableRunloop()
  
let jsCode = """
"use strict";
import * as rl from "Runloop";
rl.setTimeout(function(){ console.log("Hello Runloop"); }, 3000);
"""

let _ = context.eval(jsCode, type: .module)
  
// waiting for 3 seconds
// Hello Runloop

Swift Package Manager

Add this dependency to your Package.swift file:

.package(url: "https://github.com/zqqf16/QuickJS-Swift.git", .branch("master")),