This example demonstrates traversing a tree, where nodes are implemented in different programming languages.
The tree starts with a root node written in Python. The root node:
- Uses MetaCall to import and execute a child node written in JavaScript.
- Passes a
CurrentNode
variable to the JavaScript node to track the node number.
The JavaScript node, in turn:
- Imports and executes a child node written in C.
- Passes the
CurrentNode
variable forward.
This chain demonstrates how different languages interact within a unified workflow.
- Fork this repository.
- Clone your fork to your local machine:
git clone https://github.com/<your-username>/polyglot-tree-traversal.git cd polyglot-tree-traversal
Install the necessary dependencies:
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
ca-certificates \
git \
liburing-dev \
cmake \
curl
Clone and build MetaCall from its repository:
git clone --branch v0.8.7 https://github.com/metacall/core
cd core
./tools/metacall-environment.sh release base nodejs c python
sudo mkdir build && cd build
sudo cmake \
-DOPTION_BUILD_LOADERS_C=On \
-DOPTION_BUILD_LOADERS_NODE=On \
-DOPTION_BUILD_LOADERS_PY=On \
-DOPTION_BUILD_PORTS=On \
-DOPTION_BUILD_PORTS_NODE=On \
-DOPTION_BUILD_PORTS_PY=On \
-DOPTION_BUILD_DETOURS=Off \
-DOPTION_BUILD_SCRIPTS=Off \
-DOPTION_BUILD_TESTS=Off \
-DOPTION_BUILD_EXAMPLES=Off \
..
sudo cmake --build . --target install
sudo ldconfig /usr/local/lib
cd ../..
sudo rm -rf core
Export the required environment variables:
export LOADER_LIBRARY_PATH="/usr/local/lib"
export LOADER_SCRIPT_PATH="$(pwd)" # Path to the scripts in your project
Use the MetaCall CLI to execute the root node:
metacallcli rootNode.py
This will initiate the traversal of the polyglot tree, starting with the Python root node.
rootNode.py
: The Python script that acts as the root node.middleNode.js
: The JavaScript script imported by the root node.leaf_Node.c
: The C code imported by the JavaScript node.
- Ensure that MetaCall loaders for Python, Node.js, and C are enabled during the build.
- Ensure that MetaCall ports for Nodejs and Python are enabled during the build.
- Scripts should be placed in the directory specified by the
LOADER_SCRIPT_PATH
environment variable.