summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-03-11 11:23:38 +0100
committerJo-Philipp Wich <jo@mein.io>2021-03-11 11:39:40 +0100
commita4785102dc58ce9c8facb716f18d3aaad0538cba (patch)
treece9a42990050bdc6e44fd751718e21d5878afa4f
parent40fed16a0beb0eb4af251f6375bd9002cf05e8c5 (diff)
object: prevent registering the same ressource type multiple times
When a module registering custom ressource types, such as "fs.so", is required multiple times we need to ensure that only one instance of a given ressource type is registered, otherwise objects created after subsequent requires will cease to function since the internal type prototype mismatches. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--object.c7
-rw-r--r--tests/03_bugs/05_duplicate_ressource_type31
2 files changed, 38 insertions, 0 deletions
diff --git a/object.c b/object.c
index c0a5227..df81fda 100644
--- a/object.c
+++ b/object.c
@@ -357,6 +357,13 @@ static uc_ressource_types res_types;
uc_ressource_type *
uc_ressource_type_add(const char *name, uc_prototype *proto, void (*freefn)(void *))
{
+ uc_ressource_type *existing;
+
+ existing = uc_ressource_type_lookup(name);
+
+ if (existing)
+ return existing;
+
uc_vector_grow(&res_types);
res_types.entries[res_types.count].name = name;
diff --git a/tests/03_bugs/05_duplicate_ressource_type b/tests/03_bugs/05_duplicate_ressource_type
new file mode 100644
index 0000000..21166b2
--- /dev/null
+++ b/tests/03_bugs/05_duplicate_ressource_type
@@ -0,0 +1,31 @@
+When requiring a C module that registers custom ressource types multiple
+times, ressource values instantiated after subsequent requires of the
+same extensions didn't properly function since the internal type prototype
+was resolved to the initial copy and subsequently discarded due to an index
+mismatch.
+
+-- Testcase --
+{%
+ fs = require("fs");
+ fd = fs.open("README.md");
+
+ printf("fd.read() #1: %s\n",
+ fd.read("line") ? "working" : "not working (" + fd.error() + ")");
+
+ fd.close();
+
+
+ fs = require("fs");
+ fd = fs.open("README.md");
+
+ printf("fd.read() #2: %s\n",
+ fd.read("line") ? "working" : "not working (" + fd.error() + ")");
+
+ fd.close();
+%}
+-- End --
+
+-- Expect stdout --
+fd.read() #1: working
+fd.read() #2: working
+-- End --