Skip to content

Commit faf397b

Browse files
committed
Add move semantics and better information for exeptions in the node port.
1 parent a16434b commit faf397b

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

source/metacall/include/metacall/metacall_value.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ METACALL_API void metacall_value_own(void * v, void * owner);
333333
/**
334334
* @brief
335335
* Deep copies the value @v, the result copy resets
336-
* the reference counter and ownership
336+
* the reference counter and ownership, including the finalizer
337337
*
338338
* @param[in] v
339339
* Reference to the value to be copied
@@ -343,6 +343,19 @@ METACALL_API void metacall_value_own(void * v, void * owner);
343343
*/
344344
METACALL_API void * metacall_value_copy(void * v);
345345

346+
/**
347+
* @brief
348+
* Copies the ownership from @src to @dst, including the finalizer,
349+
* and resets the owner and finalizer of @src
350+
*
351+
* @param[in] src
352+
* Source value which will lose the ownership
353+
*
354+
* @param[in] dst
355+
* Destination value which will recieve the ownership
356+
*/
357+
METACALL_API void metacall_value_move(void * src, void * dest);
358+
346359
/**
347360
* @brief
348361
* Convert value @v to boolean

source/metacall/source/metacall_value.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ void * metacall_value_copy(void * v)
194194
return value_type_copy(v);
195195
}
196196

197+
void metacall_value_move(void * src, void * dst)
198+
{
199+
value_move(src, dst);
200+
}
201+
197202
boolean metacall_value_to_bool(void * v)
198203
{
199204
assert(value_type_id(v) == TYPE_BOOL);

source/ports/node_port/source/node_port.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ inline void metacall_node_exception(napi_env env, napi_status status)
289289
metacall_node_exception(env, status);
290290

291291
/* TODO: Notify MetaCall error handling system when it is implemented */
292-
/* error_raise(str); */
292+
/* Meanwhile, throw it again */
293+
status = napi_throw_error(env, nullptr, str);
294+
295+
metacall_node_exception(env, status);
293296

294297
free(str);
295298
}
@@ -694,7 +697,7 @@ napi_value metacall_node_value_to_napi(/* loader_impl_node node_impl,*/ napi_env
694697
// Copy value and set the ownership, the old value will be deleted after the call
695698
void * c = metacall_value_copy(arg_value);
696699

697-
metacall_value_own(c, metacall_value_owner(arg_value));
700+
metacall_value_move(arg_value, c);
698701

699702
status = napi_create_external(env, c, nullptr, nullptr, &v);
700703

source/reflect/include/reflect/reflect_value.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ REFLECT_API value value_create(const void * data, size_t bytes);
8686
*/
8787
REFLECT_API value value_copy(value v);
8888

89+
/**
90+
* @brief
91+
* Copies the ownership from @src to @dst, including the finalizer,
92+
* and resets the owner and finalizer of @src
93+
*
94+
* @param[in] src
95+
* Source value which will lose the ownership
96+
*
97+
* @param[in] dst
98+
* Destination value which will recieve the ownership
99+
*/
100+
REFLECT_API void value_move(value src, value dst);
101+
89102
/**
90103
* @brief
91104
* Returns the size of the value

source/reflect/source/reflect_value.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ value value_copy(value v)
116116
return copy;
117117
}
118118

119+
void value_move(value src, value dst)
120+
{
121+
if (src != NULL && dst != NULL)
122+
{
123+
value_impl impl_src = value_descriptor(src);
124+
value_impl impl_dst = value_descriptor(dst);
125+
126+
impl_dst->owner = impl_src->owner;
127+
impl_dst->finalizer = impl_src->finalizer;
128+
129+
impl_src->owner = NULL;
130+
impl_src->finalizer = NULL;
131+
}
132+
}
133+
119134
size_t value_size(value v)
120135
{
121136
value_impl impl = value_descriptor(v);

source/scripts/python/function/source/function.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ def function_capsule_new_class():
4747
def function_capsule_method(klass):
4848
print('Executing class method with instance passed as opaque pointer:', klass.f());
4949
return klass.f();
50+
51+
def function_capsule_cb(cb):
52+
return cb(MyClass());

0 commit comments

Comments
 (0)