From 14fd692b1795461bdb7e05cc2e1d1a655d85ae86 Mon Sep 17 00:00:00 2001 From: Nelson-He Date: Wed, 30 Nov 2022 17:35:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?wat=E5=8A=A0=E8=BD=BD=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0namespace=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wasm/wasm_executor--1.0.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasm/wasm_executor--1.0.sql b/wasm/wasm_executor--1.0.sql index f5fc822..a1d64c1 100644 --- a/wasm/wasm_executor--1.0.sql +++ b/wasm/wasm_executor--1.0.sql @@ -183,7 +183,7 @@ BEGIN -- Insert the wasm information to gloable table INSERT INTO wasm.instances SELECT id, wasm_file FROM wasm_get_instances() WHERE id = current_instance_id; - INSERT INTO wasm.exported_functions SELECT current_instance_id, funcname, inputs, outputs FROM wasm_get_exported_functions(current_instance_id); + INSERT INTO wasm.exported_functions SELECT current_instance_id, namespace, funcname, inputs, outputs FROM wasm_get_exported_functions(current_instance_id); -- Generate functions for each exported functions from the WebAssembly instance. FOR -- Gitee From 673f7f7171e27738b7e6b972d7df0672154bbbef Mon Sep 17 00:00:00 2001 From: Nelson-He Date: Fri, 16 Dec 2022 11:13:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wasm/wasm_executor--1.0.sql | 41 +++++++++++++++++++++++++++++++++++++ wasm/wasm_executor.cpp | 29 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/wasm/wasm_executor--1.0.sql b/wasm/wasm_executor--1.0.sql index a1d64c1..5215aa7 100644 --- a/wasm/wasm_executor--1.0.sql +++ b/wasm/wasm_executor--1.0.sql @@ -47,6 +47,11 @@ RETURNS int8 AS 'MODULE_PATHNAME', 'wasm_create_instance_wat' LANGUAGE C STRICT; +CREATE FUNCTION wasm_drop_instance(int8) +RETURNS text +AS 'MODULE_PATHNAME', 'wasm_drop_instance' +LANGUAGE C STRICT; + CREATE FUNCTION wasm_invoke_function_0(text, text) RETURNS int8 AS 'MODULE_PATHNAME', 'wasm_invoke_function_0' @@ -238,4 +243,40 @@ BEGIN RETURN current_instance_id; END; +$$ LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION wasm_delete_instance(delete_instance int8) RETURNS text AS $$ +DECLARE + instance_module_path text; + exported_function RECORD; +BEGIN + -- Create a new instance, and stores its ID in `current_instance_id`. + SELECT wasm_drop_instance(delete_instance) INTO STRICT instance_module_path; + + -- Generate functions for each exported functions from the WebAssembly instance. + FOR + exported_function + IN + SELECT + namespace, + funcname, + inputs + FROM + wasm.exported_functions WHERE instanceid = delete_instance + LOOP + + EXECUTE format( + 'DROP FUNCTION %I_%I(%3$s)', + exported_function.namespace, -- 1 + exported_function.funcname, -- 2 + exported_function.inputs + ); + END LOOP; + + DELETE FROM wasm.instances WHERE id = delete_instance; + DELETE FROM wasm.exported_functions WHERE instanceid = delete_instance; + + RETURN instance_module_path; +END; $$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/wasm/wasm_executor.cpp b/wasm/wasm_executor.cpp index 73b4126..838e406 100644 --- a/wasm/wasm_executor.cpp +++ b/wasm/wasm_executor.cpp @@ -15,6 +15,7 @@ PG_MODULE_MAGIC; extern "C" Datum wasm_create_instance_wat(PG_FUNCTION_ARGS); extern "C" Datum wasm_create_instance(PG_FUNCTION_ARGS); +extern "C" Datum wasm_drop_instance(PG_FUNCTION_ARGS); extern "C" Datum wasm_get_instances(PG_FUNCTION_ARGS); extern "C" Datum wasm_get_exported_functions(PG_FUNCTION_ARGS); extern "C" Datum wasm_invoke_function_0(PG_FUNCTION_ARGS); @@ -376,6 +377,34 @@ Datum wasm_create_instance(PG_FUNCTION_ARGS) return Int64GetDatum(uuid); } +PG_FUNCTION_INFO_V1(wasm_drop_instance); +Datum wasm_drop_instance(PG_FUNCTION_ARGS) +{ + int64 instanceid = PG_GETARG_INT64(0); + Datum module_path; + + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("wasm_executor: must be system admin to delete wasm instance")))); + + std::map::iterator institor = instances.begin(); + while (institor != instances.end() && institor->first != instanceid) { + institor++; + } + if (institor == instances.end()) { + ereport(ERROR, (errmsg("wasm_executor:instance with id=%ld not exist", instanceid))); + } + module_path = CStringGetTextDatum(institor->second->wasm_file.c_str()); + instances.erase(institor); + + std::map*>::iterator funcitor = exported_functions.begin(); + while (funcitor != exported_functions.end() && funcitor->first == instanceid) { + exported_functions.erase(funcitor++); + } + + return module_path; +} + PG_FUNCTION_INFO_V1(wasm_get_instances); Datum wasm_get_instances(PG_FUNCTION_ARGS) { -- Gitee