KGI (Foreign Function Interface)
The Kernel Gateway Interface (KGI) allows REK to import and use libraries from other languages like Python and Node.js directly.
Prerequisites: To use KGI, you must have the target runtime installed on your system (e.g.,
python for Python modules, node for Node.js modules) and available in your PATH.
Python Integration
Import Python modules using the py: prefix.
import py:math
import py:json
log(math.sqrt(16)) // 4.0
let data = { "name": "REK" }
let str = json.dumps(data)
log(str)
Using Third-Party Libraries
You can use any installed pip package. For example, requests.
import py:requests
let res = requests.get("https://api.github.com")
log("Status: " + res.status_code)
Node.js Integration
Import Node.js modules using the js: prefix.
import js:fs
let content = fs.readFileSync("package.json", "utf8")
log(content)
How it Works
KGI spawns a child process for the target runtime and communicates via a JSON-RPC protocol over standard I/O. This means:
- Isolation: Crashes in the foreign code won't necessarily crash the REK interpreter (though the call will fail).
- Performance: There is a slight overhead for serialization and IPC. Avoid calling foreign functions in tight loops if possible.
- Data Types: Basic types (numbers, strings, lists, maps) are automatically converted between REK and the target language.