Skip to content

zqqf16/QuickJS-Swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 'Magic'
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")),

About

Swift bindings to QuickJS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published