Initial PROTEL 2026 toolchain import
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
/** Java helper invoked from PROTEL via the java_greet JNI embedding shim. */
|
||||
public final class GreetHelper {
|
||||
private GreetHelper() {
|
||||
}
|
||||
|
||||
public static void greet(String name, short value) {
|
||||
System.out.println("Java: java_greet(name=" + name + ", value=" + value + ")");
|
||||
System.out.flush();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/** Java program calling an EXPORTed PROTEL procedure via JNI (C ABI bridge). */
|
||||
public final class JavaCallsProtel {
|
||||
static {
|
||||
System.loadLibrary("javacallsprotel");
|
||||
}
|
||||
|
||||
private static native short protelAdd(short a, short b);
|
||||
|
||||
public static void main(String[] args) {
|
||||
short sum = protelAdd((short) 40, (short) 2);
|
||||
System.out.println("Java: protel_add(40, 2) = " + sum);
|
||||
}
|
||||
}
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build Swift <-> PROTEL interop demos (macOS / Linux with Swift installed).
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
BUILD="$ROOT/build/interop"
|
||||
PROTEL="$ROOT/protel"
|
||||
RUNTIME="$ROOT/src/runtime/protel_io.c"
|
||||
|
||||
mkdir -p "$BUILD"
|
||||
|
||||
if ! command -v swiftc >/dev/null 2>&1; then
|
||||
echo "interop: swiftc not found; install Swift toolchain to build runnable demos" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
CXX="$(command -v clang++ || command -v g++)"
|
||||
|
||||
echo "=== PROTEL calls Swift ==="
|
||||
"$PROTEL" "$ROOT/examples/interop/protel_calls_swift.protel" \
|
||||
--keep -o "$BUILD/protel_calls_swift.cpp"
|
||||
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_swift.cpp" \
|
||||
-o "$BUILD/protel_calls_swift.o"
|
||||
swiftc -parse-as-library -c "$ROOT/examples/interop/swift_greet.swift" \
|
||||
-o "$BUILD/swift_greet.o"
|
||||
"$CXX" "$BUILD/protel_calls_swift.o" "$BUILD/swift_greet.o" "$RUNTIME" \
|
||||
-o "$BUILD/protel_calls_swift"
|
||||
"$BUILD/protel_calls_swift"
|
||||
|
||||
echo "=== Swift calls PROTEL ==="
|
||||
"$PROTEL" "$ROOT/examples/interop/protel_for_swift.protel" \
|
||||
--keep -o "$BUILD/protel_for_swift.cpp"
|
||||
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_for_swift.cpp" \
|
||||
-o "$BUILD/protel_for_swift.o"
|
||||
swiftc "$ROOT/examples/interop/swift_calls_protel.swift" "$BUILD/protel_for_swift.o" \
|
||||
-o "$BUILD/swift_calls_protel"
|
||||
"$BUILD/swift_calls_protel"
|
||||
Executable
+81
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build Java <-> PROTEL interop demos (macOS / Linux with JDK headers).
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
BUILD="$ROOT/build/interop"
|
||||
PROTEL="$ROOT/protel"
|
||||
RUNTIME="$ROOT/src/runtime/protel_io.c"
|
||||
INTEROP="$ROOT/examples/interop"
|
||||
|
||||
mkdir -p "$BUILD"
|
||||
|
||||
if ! command -v javac >/dev/null 2>&1 || ! command -v java >/dev/null 2>&1; then
|
||||
echo "interop: javac/java not found; install a JDK" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! java -version >/dev/null 2>&1; then
|
||||
echo "interop: java runtime not available; set JAVA_HOME to a JDK" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "${JAVA_HOME:-}" ]; then
|
||||
if [ "$(uname -s)" = "Darwin" ] && command -v /usr/libexec/java_home >/dev/null 2>&1; then
|
||||
JAVA_HOME="$(/usr/libexec/java_home 2>/dev/null || true)"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "${JAVA_HOME:-}" ] || [ ! -d "$JAVA_HOME/include" ]; then
|
||||
echo "interop: JAVA_HOME must point to a JDK with JNI headers" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
JNI_OS="linux"
|
||||
if [ "$(uname -s)" = "Darwin" ]; then
|
||||
JNI_OS="darwin"
|
||||
fi
|
||||
|
||||
JNI_CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/$JNI_OS"
|
||||
if [ -d "$JAVA_HOME/lib/server" ]; then
|
||||
JVM_LIBDIR="$JAVA_HOME/lib/server"
|
||||
else
|
||||
JVM_LIBDIR="$JAVA_HOME/jre/lib/server"
|
||||
fi
|
||||
JNI_LDFLAGS="-L$JVM_LIBDIR -ljvm -Wl,-rpath,$JVM_LIBDIR"
|
||||
|
||||
CXX="$(command -v clang++ || command -v g++)"
|
||||
CC="$(command -v clang || command -v gcc)"
|
||||
|
||||
if [ "$(uname -s)" = "Darwin" ]; then
|
||||
SHLIB_EXT="dylib"
|
||||
SHLIB_FLAGS="-dynamiclib"
|
||||
else
|
||||
SHLIB_EXT="so"
|
||||
SHLIB_FLAGS="-shared -fPIC"
|
||||
fi
|
||||
|
||||
javac -d "$BUILD" "$INTEROP/GreetHelper.java" "$INTEROP/JavaCallsProtel.java"
|
||||
|
||||
echo "=== PROTEL calls Java ==="
|
||||
"$PROTEL" "$INTEROP/protel_calls_java.protel" \
|
||||
--keep -o "$BUILD/protel_calls_java.cpp"
|
||||
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_java.cpp" \
|
||||
-o "$BUILD/protel_calls_java.o"
|
||||
# shellcheck disable=SC2086
|
||||
"$CC" -Wall -c "$INTEROP/java_greet.c" -o "$BUILD/java_greet.o" $JNI_CFLAGS
|
||||
# shellcheck disable=SC2086
|
||||
"$CXX" "$BUILD/protel_calls_java.o" "$BUILD/java_greet.o" "$RUNTIME" \
|
||||
$JNI_LDFLAGS -o "$BUILD/protel_calls_java"
|
||||
PROTEL_ROOT="$ROOT" "$BUILD/protel_calls_java"
|
||||
|
||||
echo "=== Java calls PROTEL ==="
|
||||
"$PROTEL" "$INTEROP/protel_for_java.protel" \
|
||||
--keep -o "$BUILD/protel_for_java.cpp"
|
||||
"$CXX" -std=c++20 -Wall -fPIC -I"$ROOT/src/runtime" -c "$BUILD/protel_for_java.cpp" \
|
||||
-o "$BUILD/protel_for_java.o"
|
||||
# shellcheck disable=SC2086
|
||||
"$CC" -Wall -fPIC -c "$INTEROP/java_calls_protel.c" -o "$BUILD/java_calls_protel.o" $JNI_CFLAGS
|
||||
"$CXX" $SHLIB_FLAGS -std=c++20 "$BUILD/protel_for_java.o" "$BUILD/java_calls_protel.o" \
|
||||
-o "$BUILD/libjavacallsprotel.$SHLIB_EXT"
|
||||
java -Djava.library.path="$BUILD" -cp "$BUILD" JavaCallsProtel
|
||||
Executable
+73
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build Python <-> PROTEL interop demos (macOS / Linux with Python 3 dev headers).
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
BUILD="$ROOT/build/interop"
|
||||
PROTEL="$ROOT/protel"
|
||||
RUNTIME="$ROOT/src/runtime/protel_io.c"
|
||||
INTEROP="$ROOT/examples/interop"
|
||||
|
||||
mkdir -p "$BUILD"
|
||||
|
||||
if ! command -v python3 >/dev/null 2>&1; then
|
||||
echo "interop: python3 not found" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PYTHON="${PYTHON:-python3}"
|
||||
if ! "$PYTHON" -c "import sysconfig; sysconfig.get_config_var('LIBDIR')" >/dev/null 2>&1; then
|
||||
echo "interop: Python development headers not available (install python3-dev)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PY_CONFIG=""
|
||||
if command -v python3-config >/dev/null 2>&1; then
|
||||
PY_CONFIG="python3-config"
|
||||
elif command -v "$PYTHON-config" >/dev/null 2>&1; then
|
||||
PY_CONFIG="$PYTHON-config"
|
||||
fi
|
||||
|
||||
if [ -n "$PY_CONFIG" ]; then
|
||||
PY_CFLAGS="$("$PY_CONFIG" --includes)"
|
||||
if "$PY_CONFIG" --embed --ldflags >/dev/null 2>&1; then
|
||||
PY_LDFLAGS="$("$PY_CONFIG" --embed --ldflags)"
|
||||
else
|
||||
PY_LDFLAGS="$("$PY_CONFIG" --ldflags)"
|
||||
fi
|
||||
else
|
||||
PY_CFLAGS=$("$PYTHON" -c "import sysconfig; print('-I' + sysconfig.get_config_var('INCLUDEPY'))")
|
||||
PY_LDFLAGS=$("$PYTHON" -c "import sysconfig; print('-L' + sysconfig.get_config_var('LIBDIR') + ' -lpython' + sysconfig.get_config_var('VERSION') + ' ' + (sysconfig.get_config_var('LIBS') or ''))")
|
||||
fi
|
||||
|
||||
CXX="$(command -v clang++ || command -v g++)"
|
||||
CC="$(command -v clang || command -v gcc)"
|
||||
|
||||
if [ "$(uname -s)" = "Darwin" ]; then
|
||||
SHLIB_EXT="dylib"
|
||||
SHLIB_FLAGS="-dynamiclib -Wl,-undefined,dynamic_lookup"
|
||||
else
|
||||
SHLIB_EXT="so"
|
||||
SHLIB_FLAGS="-shared -fPIC"
|
||||
fi
|
||||
|
||||
echo "=== PROTEL calls Python ==="
|
||||
"$PROTEL" "$INTEROP/protel_calls_python.protel" \
|
||||
--keep -o "$BUILD/protel_calls_python.cpp"
|
||||
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_python.cpp" \
|
||||
-o "$BUILD/protel_calls_python.o"
|
||||
# shellcheck disable=SC2086
|
||||
"$CC" -Wall -c "$INTEROP/python_greet.c" -o "$BUILD/python_greet.o" $PY_CFLAGS
|
||||
# shellcheck disable=SC2086
|
||||
"$CXX" "$BUILD/protel_calls_python.o" "$BUILD/python_greet.o" "$RUNTIME" \
|
||||
$PY_LDFLAGS -o "$BUILD/protel_calls_python"
|
||||
(cd "$ROOT" && "$BUILD/protel_calls_python")
|
||||
|
||||
echo "=== Python calls PROTEL ==="
|
||||
"$PROTEL" "$INTEROP/protel_for_python.protel" \
|
||||
--keep -o "$BUILD/protel_for_python.cpp"
|
||||
"$CXX" -std=c++20 -Wall -fPIC -I"$ROOT/src/runtime" -c "$BUILD/protel_for_python.cpp" \
|
||||
-o "$BUILD/protel_for_python.o"
|
||||
"$CXX" $SHLIB_FLAGS -std=c++20 "$BUILD/protel_for_python.o" \
|
||||
-o "$BUILD/libprotel_for_python.$SHLIB_EXT"
|
||||
"$PYTHON" "$INTEROP/python_calls_protel.py"
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build Rust <-> PROTEL interop demos (macOS / Linux with Rust installed).
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
BUILD="$ROOT/build/interop"
|
||||
PROTEL="$ROOT/protel"
|
||||
RUNTIME="$ROOT/src/runtime/protel_io.c"
|
||||
|
||||
mkdir -p "$BUILD"
|
||||
|
||||
if ! command -v rustc >/dev/null 2>&1; then
|
||||
echo "interop: rustc not found; install Rust (rustup) to build runnable demos" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
CXX="$(command -v clang++ || command -v g++)"
|
||||
|
||||
echo "=== PROTEL calls Rust ==="
|
||||
"$PROTEL" "$ROOT/examples/interop/protel_calls_rust.protel" \
|
||||
--keep -o "$BUILD/protel_calls_rust.cpp"
|
||||
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_rust.cpp" \
|
||||
-o "$BUILD/protel_calls_rust.o"
|
||||
rustc --crate-type staticlib "$ROOT/examples/interop/rust_greet.rs" \
|
||||
-o "$BUILD/librust_greet.a"
|
||||
"$CXX" "$BUILD/protel_calls_rust.o" "$BUILD/librust_greet.a" "$RUNTIME" \
|
||||
-o "$BUILD/protel_calls_rust"
|
||||
"$BUILD/protel_calls_rust"
|
||||
|
||||
echo "=== Rust calls PROTEL ==="
|
||||
"$PROTEL" "$ROOT/examples/interop/protel_for_rust.protel" \
|
||||
--keep -o "$BUILD/protel_for_rust.cpp"
|
||||
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_for_rust.cpp" \
|
||||
-o "$BUILD/protel_for_rust.o"
|
||||
rustc "$ROOT/examples/interop/rust_calls_protel.rs" "$BUILD/protel_for_rust.o" \
|
||||
-o "$BUILD/rust_calls_protel"
|
||||
"$BUILD/rust_calls_protel"
|
||||
@@ -0,0 +1,11 @@
|
||||
/* C function called from PROTEL via EXTERNAL (examples/interop reference). */
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void c_greet(const char* name, int16_t value)
|
||||
{
|
||||
if (name == NULL) {
|
||||
name = "";
|
||||
}
|
||||
printf("C: c_greet(name=%s, value=%d)\n", name, (int)value);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Python helper invoked from PROTEL via the python_greet C embedding shim."""
|
||||
|
||||
|
||||
def greet(name: str, value: int) -> None:
|
||||
print(f"Python: python_greet(name={name}, value={value})", flush=True)
|
||||
@@ -0,0 +1,13 @@
|
||||
/* JNI bridge: JavaCallsProtel.protelAdd -> EXPORTed protel_add (C ABI). */
|
||||
#include <jni.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern int16_t protel_add(int16_t a, int16_t b);
|
||||
|
||||
JNIEXPORT jshort JNICALL
|
||||
Java_JavaCallsProtel_protelAdd(JNIEnv *env, jclass clazz, jshort a, jshort b)
|
||||
{
|
||||
(void)env;
|
||||
(void)clazz;
|
||||
return (jshort)protel_add(a, b);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/* C ABI entry point: embeds the JVM and calls GreetHelper.greet (PROTEL EXTERNAL). */
|
||||
#include <jni.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static JavaVM *jvm;
|
||||
static int jvm_ready;
|
||||
|
||||
static JNIEnv *attach_env(void)
|
||||
{
|
||||
JNIEnv *env = NULL;
|
||||
|
||||
if (jvm == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_10) == JNI_EDETACHED) {
|
||||
(*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
||||
static void ensure_jvm(void)
|
||||
{
|
||||
JavaVMInitArgs vm_args;
|
||||
JavaVMOption options[1];
|
||||
char classpath[1024];
|
||||
const char *root;
|
||||
JNIEnv *env;
|
||||
|
||||
if (jvm_ready) {
|
||||
return;
|
||||
}
|
||||
|
||||
root = getenv("PROTEL_ROOT");
|
||||
if (root == NULL || root[0] == '\0') {
|
||||
root = ".";
|
||||
}
|
||||
snprintf(classpath, sizeof(classpath), "-Djava.class.path=%s/examples/interop", root);
|
||||
options[0].optionString = classpath;
|
||||
vm_args.version = JNI_VERSION_10;
|
||||
vm_args.nOptions = 1;
|
||||
vm_args.options = options;
|
||||
vm_args.ignoreUnrecognized = JNI_TRUE;
|
||||
|
||||
if (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) != 0) {
|
||||
fprintf(stderr, "java_greet: JNI_CreateJavaVM failed\n");
|
||||
return;
|
||||
}
|
||||
jvm_ready = 1;
|
||||
}
|
||||
|
||||
void java_greet(const char *name, int16_t value)
|
||||
{
|
||||
JNIEnv *env;
|
||||
jclass cls;
|
||||
jmethodID mid;
|
||||
jstring jname;
|
||||
|
||||
ensure_jvm();
|
||||
env = attach_env();
|
||||
if (env == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
cls = (*env)->FindClass(env, "GreetHelper");
|
||||
if (cls == NULL) {
|
||||
(*env)->ExceptionDescribe(env);
|
||||
return;
|
||||
}
|
||||
|
||||
mid = (*env)->GetStaticMethodID(env, cls, "greet", "(Ljava/lang/String;S)V");
|
||||
if (mid == NULL) {
|
||||
(*env)->ExceptionDescribe(env);
|
||||
(*env)->DeleteLocalRef(env, cls);
|
||||
return;
|
||||
}
|
||||
|
||||
jname = (*env)->NewStringUTF(env, name != NULL ? name : "");
|
||||
(*env)->CallStaticVoidMethod(env, cls, mid, jname, (jshort)value);
|
||||
if ((*env)->ExceptionCheck(env)) {
|
||||
(*env)->ExceptionDescribe(env);
|
||||
}
|
||||
|
||||
(*env)->DeleteLocalRef(env, jname);
|
||||
(*env)->DeleteLocalRef(env, cls);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
% PROTEL calling Java (Introductory Manual 7.3)
|
||||
% C shim java_greet embeds the JVM and calls GreetHelper.greet.
|
||||
|
||||
SECTION protel_calls_java;
|
||||
|
||||
TYPE char {0 TO 255};
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL java_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
java_greet({'PROTEL', 0}, 42);
|
||||
writeln({'PROTEL called Java', 0});
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,18 @@
|
||||
% PROTEL calling Python (Introductory Manual 7.3)
|
||||
% C shim python_greet embeds Python and calls greet_helper.greet.
|
||||
|
||||
SECTION protel_calls_python;
|
||||
|
||||
TYPE char {0 TO 255};
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL python_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
python_greet({'PROTEL', 0}, 42);
|
||||
writeln({'PROTEL called Python', 0});
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,18 @@
|
||||
% PROTEL calling Rust (Introductory Manual 7.3)
|
||||
% Rust exports rust_greet with #[no_mangle] extern "C"; PROTEL imports EXTERNAL.
|
||||
|
||||
SECTION protel_calls_rust;
|
||||
|
||||
TYPE char {0 TO 255};
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL rust_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
rust_greet({'PROTEL', 0}, 42);
|
||||
writeln({'PROTEL called Rust', 0});
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,18 @@
|
||||
% PROTEL calling Swift (Introductory Manual 7.1 / C interop)
|
||||
% Swift exports swift_greet with @_cdecl; PROTEL imports it as EXTERNAL.
|
||||
|
||||
SECTION protel_calls_swift;
|
||||
|
||||
TYPE char {0 TO 255};
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL swift_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
swift_greet({'PROTEL', 0}, 42);
|
||||
writeln({'PROTEL finished calling Swift', 0});
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,13 @@
|
||||
% PROTEL library exported for Java (EXPORT = extern "C" linkage)
|
||||
% Java loads the shared object via JNI (java_calls_protel.c bridge).
|
||||
|
||||
INTERFACE protel_math;
|
||||
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer EXPORT;
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer IS
|
||||
BLOCK
|
||||
RETURN a + b;
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,13 @@
|
||||
% PROTEL library exported for Python (EXPORT = extern "C" linkage)
|
||||
% Python loads the shared object with ctypes.CDLL and binds protel_add.
|
||||
|
||||
INTERFACE protel_math;
|
||||
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer EXPORT;
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer IS
|
||||
BLOCK
|
||||
RETURN a + b;
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,13 @@
|
||||
% PROTEL library exported for Rust (EXPORT = extern "C" linkage)
|
||||
% Rust imports protel_add with extern "C" { fn protel_add ... }.
|
||||
|
||||
INTERFACE protel_math;
|
||||
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer EXPORT;
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer IS
|
||||
BLOCK
|
||||
RETURN a + b;
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,13 @@
|
||||
% PROTEL library exported for Swift (EXPORT = extern "C" linkage)
|
||||
% Swift imports protel_add with @_silgen_name.
|
||||
|
||||
INTERFACE protel_math;
|
||||
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer EXPORT;
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer IS
|
||||
BLOCK
|
||||
RETURN a + b;
|
||||
ENDBLOCK;
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Python program calling an EXPORTed PROTEL procedure via ctypes (C ABI)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import ctypes
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
BUILD = ROOT / "build" / "interop"
|
||||
|
||||
if sys.platform == "darwin":
|
||||
LIB_NAME = "libprotel_for_python.dylib"
|
||||
elif sys.platform == "win32":
|
||||
LIB_NAME = "protel_for_python.dll"
|
||||
else:
|
||||
LIB_NAME = "libprotel_for_python.so"
|
||||
|
||||
LIB_PATH = BUILD / LIB_NAME
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if not LIB_PATH.is_file():
|
||||
raise SystemExit(f"missing shared library: {LIB_PATH} (run build_interop_python.sh)")
|
||||
|
||||
lib = ctypes.CDLL(str(LIB_PATH))
|
||||
lib.protel_add.argtypes = (ctypes.c_int16, ctypes.c_int16)
|
||||
lib.protel_add.restype = ctypes.c_int16
|
||||
|
||||
total = lib.protel_add(40, 2)
|
||||
print(f"Python: protel_add(40, 2) = {total}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,50 @@
|
||||
/* C ABI entry point: embeds Python and calls greet_helper.greet (PROTEL EXTERNAL). */
|
||||
#include <Python.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static int py_ready;
|
||||
|
||||
static void ensure_python(void)
|
||||
{
|
||||
if (py_ready) {
|
||||
return;
|
||||
}
|
||||
Py_Initialize();
|
||||
if (PyRun_SimpleString("import sys\n"
|
||||
"sys.path.insert(0, 'examples/interop')\n") != 0) {
|
||||
PyErr_Print();
|
||||
}
|
||||
py_ready = 1;
|
||||
}
|
||||
|
||||
void python_greet(const char* name, int16_t value)
|
||||
{
|
||||
PyObject *mod, *func, *args, *result;
|
||||
|
||||
ensure_python();
|
||||
|
||||
mod = PyImport_ImportModule("greet_helper");
|
||||
if (mod == NULL) {
|
||||
PyErr_Print();
|
||||
return;
|
||||
}
|
||||
|
||||
func = PyObject_GetAttrString(mod, "greet");
|
||||
if (func == NULL || !PyCallable_Check(func)) {
|
||||
PyErr_Print();
|
||||
Py_DECREF(mod);
|
||||
return;
|
||||
}
|
||||
|
||||
args = Py_BuildValue("(si)", name != NULL ? name : "", (int)value);
|
||||
result = PyObject_CallObject(func, args);
|
||||
if (result == NULL) {
|
||||
PyErr_Print();
|
||||
} else {
|
||||
Py_DECREF(result);
|
||||
}
|
||||
|
||||
Py_DECREF(args);
|
||||
Py_DECREF(func);
|
||||
Py_DECREF(mod);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// Rust program calling an EXPORTed PROTEL procedure (C linkage).
|
||||
extern "C" {
|
||||
fn protel_add(a: i16, b: i16) -> i16;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sum = unsafe { protel_add(40, 2) };
|
||||
println!("Rust: protel_add(40, 2) = {sum}");
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Rust function called from PROTEL via EXTERNAL / #[no_mangle] extern "C".
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rust_greet(name: *const c_char, value: i16) {
|
||||
let label = unsafe {
|
||||
if name.is_null() {
|
||||
"<null>"
|
||||
} else {
|
||||
CStr::from_ptr(name).to_str().unwrap_or("<invalid utf-8>")
|
||||
}
|
||||
};
|
||||
println!("Rust: rust_greet(name={label}, value={value})");
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Swift program calling an EXPORTed PROTEL procedure (C linkage).
|
||||
import Foundation
|
||||
|
||||
@_silgen_name("protel_add")
|
||||
func protel_add(_ a: Int16, _ b: Int16) -> Int16
|
||||
|
||||
let sum = protel_add(40, 2)
|
||||
print("Swift: protel_add(40, 2) = \(sum)")
|
||||
@@ -0,0 +1,8 @@
|
||||
// Swift function called from PROTEL via EXTERNAL / @_cdecl("swift_greet").
|
||||
import Foundation
|
||||
|
||||
@_cdecl("swift_greet")
|
||||
public func swift_greet(_ name: UnsafePointer<CChar>, _ value: Int32) {
|
||||
let label = String(cString: name)
|
||||
print("Swift: swift_greet(name=\(label), value=\(value))")
|
||||
}
|
||||
Reference in New Issue
Block a user