From 261ab204ea9e132426a7ce42549a4c6ca883ca66 Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Thu, 17 Feb 2022 14:00:14 +0300
Subject: [PATCH 3/3] picodata: add support for static stored procedures using
 ".foo" syntax

box.schema.func.create(".func_name", {language = "C"}) will create a
stored procedure which calls the function `func_name` from the current
executable
---
 src/box/module_cache.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/box/module_cache.c b/src/box/module_cache.c
index 7f81902bd..a48c18ea0 100644
--- a/src/box/module_cache.c
+++ b/src/box/module_cache.c
@@ -387,9 +387,37 @@ error:
 	return NULL;
 }
 
+struct module *
+module_current_exe()
+{
+	struct module *m = cache_find(NULL, 0);
+	if (m != NULL)
+		return m;
+
+	size_t size = sizeof(struct module) + 1;
+	m = malloc(size);
+	if (m == NULL) {
+		diag_set(OutOfMemory, size, "malloc", "module");
+		return NULL;
+	}
+	memset(m, 0, size);
+	m->handle = dlopen(0, RTLD_NOW);
+	module_ref(m);
+
+	if (cache_put(m) != 0) {
+		module_unload(m);
+		return NULL;
+	}
+
+	return m;
+}
+
 struct module *
 module_load_force(const char *package, size_t package_len)
 {
+	if (package_len == 0)
+		return module_current_exe();
+
 	char path[PATH_MAX];
 	size_t size = sizeof(path);
 
@@ -416,6 +444,9 @@ module_load_force(const char *package, size_t package_len)
 struct module *
 module_load(const char *package, size_t package_len)
 {
+	if (package_len == 0)
+		return module_current_exe();
+
 	char path[PATH_MAX];
 
 	if (find_package(package, package_len, path, sizeof(path)) != 0)
-- 
2.25.1