Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit fe48f0d

Browse files
committed
[[ Bug 20325 ]] Parameterize foreign type methods with descriptor
This patch parameterizes most of the foreign type methods with a pointer to the descriptor as the first argument.
1 parent 6b86e1d commit fe48f0d

File tree

10 files changed

+81
-68
lines changed

10 files changed

+81
-68
lines changed

engine/src/exec-extension.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ bool MCExtensionConvertToScriptType(MCExecContext& ctxt, MCValueRef& x_value)
772772

773773
// Import the type
774774
MCValueRef t_imported;
775-
if (!t_desc->doimport(MCForeignValueGetContentsPtr(x_value), false, t_imported))
775+
if (!t_desc->doimport(t_desc, MCForeignValueGetContentsPtr(x_value), false, t_imported))
776776
return false;
777777

778778
// Recursively convert to a script type

engine/src/module-canvas.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ bool MCProperListGetNumberAtIndex(MCProperListRef p_list, uindex_t p_index, MCNu
232232
if (MCTypeInfoIsForeign(t_typeinfo))
233233
{
234234
const MCForeignTypeDescriptor* t_desc = MCForeignTypeInfoGetDescriptor(t_typeinfo);
235-
if (t_desc->doimport(MCForeignValueGetContentsPtr(t_value), false, (MCValueRef&)r_number))
235+
if (t_desc->doimport(t_desc, MCForeignValueGetContentsPtr(t_value), false, (MCValueRef&)r_number))
236236
return true;
237237
}
238238
}

libfoundation/include/foundation.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,13 +1776,13 @@ struct MCForeignTypeDescriptor
17761776
bool (*initialize)(void *contents);
17771777
void (*finalize)(void *contents);
17781778
bool (*defined)(void *contents);
1779-
bool (*move)(void *source, void *target);
1780-
bool (*copy)(void *source, void *target);
1781-
bool (*equal)(void *left, void *right, bool& r_equal);
1782-
bool (*hash)(void *contents, hash_t& r_hash);
1783-
bool (*doimport)(void *contents, bool release, MCValueRef& r_value);
1784-
bool (*doexport)(MCValueRef value, bool release, void *contents);
1785-
bool (*describe)(void *contents, MCStringRef & r_desc);
1779+
bool (*move)(const MCForeignTypeDescriptor* desc, void *source, void *target);
1780+
bool (*copy)(const MCForeignTypeDescriptor* desc, void *source, void *target);
1781+
bool (*equal)(const MCForeignTypeDescriptor* desc, void *left, void *right, bool& r_equal);
1782+
bool (*hash)(const MCForeignTypeDescriptor* desc, void *contents, hash_t& r_hash);
1783+
bool (*doimport)(const MCForeignTypeDescriptor* desc, void *contents, bool release, MCValueRef& r_value);
1784+
bool (*doexport)(const MCForeignTypeDescriptor* desc, MCValueRef value, bool release, void *contents);
1785+
bool (*describe)(const MCForeignTypeDescriptor* desc, void *contents, MCStringRef & r_desc);
17861786

17871787
/* The promotedtype typeinfo is the type to which this type must be promoted
17881788
* when passed through variadic parameters. The 'promote' method does the

libfoundation/src/foundation-foreign.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ bool MCForeignValueCreate(MCTypeInfoRef p_typeinfo, void *p_contents, MCForeignV
478478
t_value))
479479
return false;
480480

481-
if (!t_resolved_typeinfo -> foreign . descriptor . copy(p_contents, t_value + 1))
481+
if (!t_resolved_typeinfo->foreign.descriptor.copy(&t_resolved_typeinfo->foreign.descriptor, p_contents, t_value + 1))
482482
{
483483
MCMemoryDelete(t_value);
484484
return false;
@@ -505,7 +505,7 @@ bool MCForeignValueCreateAndRelease(MCTypeInfoRef p_typeinfo, void *p_contents,
505505
t_value))
506506
return false;
507507

508-
if (!t_resolved_typeinfo -> foreign . descriptor . move(p_contents, t_value + 1))
508+
if (!t_resolved_typeinfo->foreign.descriptor.move(&t_resolved_typeinfo->foreign.descriptor, p_contents, t_value + 1))
509509
{
510510
MCMemoryDelete(t_value);
511511
return false;
@@ -533,7 +533,7 @@ bool MCForeignValueExport(MCTypeInfoRef p_typeinfo, MCValueRef p_value, MCForeig
533533
return false;
534534

535535
if (t_resolved_typeinfo->foreign.descriptor.doexport == nil ||
536-
!t_resolved_typeinfo->foreign.descriptor.doexport(p_value, false, t_value + 1))
536+
!t_resolved_typeinfo->foreign.descriptor.doexport(&t_resolved_typeinfo->foreign.descriptor, p_value, false, t_value + 1))
537537
{
538538
MCMemoryDelete(t_value);
539539
return false;
@@ -559,7 +559,7 @@ void __MCForeignValueDestroy(__MCForeignValue *self)
559559
MCTypeInfoRef t_resolved_typeinfo;
560560
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
561561

562-
t_resolved_typeinfo -> foreign . descriptor . finalize(self + 1);
562+
t_resolved_typeinfo->foreign.descriptor.finalize(self + 1);
563563

564564
MCValueRelease(self -> typeinfo);
565565
}
@@ -570,7 +570,7 @@ hash_t __MCForeignValueHash(__MCForeignValue *self)
570570
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
571571

572572
hash_t t_hash;
573-
if (!t_resolved_typeinfo -> foreign . descriptor . hash(self + 1, t_hash))
573+
if (!t_resolved_typeinfo->foreign.descriptor.hash(&t_resolved_typeinfo->foreign.descriptor, self + 1, t_hash))
574574
return 0;
575575
return t_hash;
576576
}
@@ -581,7 +581,7 @@ bool __MCForeignValueIsEqualTo(__MCForeignValue *self, __MCForeignValue *other_s
581581
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
582582

583583
bool t_result;
584-
if (!t_resolved_typeinfo -> foreign . descriptor . equal(self + 1, other_self + 1, t_result))
584+
if (!t_resolved_typeinfo->foreign.descriptor.equal(&t_resolved_typeinfo->foreign.descriptor, self + 1, other_self + 1, t_result))
585585
return false;
586586

587587
return t_result;
@@ -592,11 +592,12 @@ bool __MCForeignValueCopyDescription(__MCForeignValue *self, MCStringRef& r_desc
592592
MCTypeInfoRef t_resolved_typeinfo;
593593
t_resolved_typeinfo = __MCTypeInfoResolve(self->typeinfo);
594594

595-
bool (*t_describe_func)(void *, MCStringRef &);
595+
bool (*t_describe_func)(const MCForeignTypeDescriptor*, void *, MCStringRef &);
596596
t_describe_func = t_resolved_typeinfo->foreign.descriptor.describe;
597597

598598
if (NULL != t_describe_func)
599-
return t_describe_func (MCForeignValueGetContentsPtr (self),
599+
return t_describe_func (&t_resolved_typeinfo->foreign.descriptor,
600+
MCForeignValueGetContentsPtr (self),
600601
r_description);
601602
else
602603
return MCStringFormat(r_description, "<foreign: %p>", self);
@@ -691,40 +692,40 @@ bool defined(void *contents)
691692
}
692693

693694
template <typename TypeDesc>
694-
bool equal(void *left, void *right, bool& r_equal)
695+
bool equal(const MCForeignTypeDescriptor* desc, void *left, void *right, bool& r_equal)
695696
{
696697
r_equal = *static_cast<typename TypeDesc::c_type *>(left) == *static_cast<typename TypeDesc::c_type *>(right);
697698
return true;
698699
}
699700

700701
template <typename TypeDesc>
701-
bool copy(void *from, void *to)
702+
bool copy(const MCForeignTypeDescriptor* desc, void *from, void *to)
702703
{
703704
*static_cast<typename TypeDesc::c_type *>(to) = *static_cast<typename TypeDesc::c_type *>(from);
704705
return true;
705706
}
706707

707708
template <typename TypeDesc>
708-
bool move(void *from, void *to)
709+
bool move(const MCForeignTypeDescriptor* desc, void *from, void *to)
709710
{
710-
return copy<TypeDesc>(from, to);
711+
return copy<TypeDesc>(desc, from, to);
711712
}
712713

713714
template <typename TypeDesc>
714-
bool hash(void *value, hash_t& r_hash)
715+
bool hash(const MCForeignTypeDescriptor* desc, void *value, hash_t& r_hash)
715716
{
716717
r_hash = TypeDesc::hash_func(*static_cast<typename TypeDesc::c_type *>(value));
717718
return true;
718719
}
719720

720721
template <typename TypeDesc>
721-
bool describe(void *contents, MCStringRef& r_string)
722+
bool describe(const MCForeignTypeDescriptor* desc, void *contents, MCStringRef& r_string)
722723
{
723724
return Describe<TypeDesc>::describe(*static_cast<typename TypeDesc::c_type *>(contents), r_string);
724725
}
725726

726727
template <typename TypeDesc>
727-
bool doexport(MCValueRef p_value, bool p_release, void *contents)
728+
bool doexport(const MCForeignTypeDescriptor* desc, MCValueRef p_value, bool p_release, void *contents)
728729
{
729730
static_assert(TypeDesc::is_bridgable, "This type is not bridgable");
730731

@@ -743,7 +744,7 @@ bool doexport(MCValueRef p_value, bool p_release, void *contents)
743744
}
744745

745746
template <typename TypeDesc>
746-
bool doimport(void *contents, bool p_release, MCValueRef& r_value)
747+
bool doimport(const MCForeignTypeDescriptor* desc, void *contents, bool p_release, MCValueRef& r_value)
747748
{
748749
static_assert(TypeDesc::is_bridgable, "This type is not bridgable");
749750
return DoImport<TypeDesc>::doimport(*static_cast<typename TypeDesc::c_type *>(contents),

libfoundation/src/foundation-handler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static void __exec_closure(ffi_cif *cif, void *ret, void **args, void *user_data
259259
{
260260
if (t_descriptor -> bridgetype != kMCNullTypeInfo)
261261
{
262-
if (!t_descriptor -> doimport(args[t_arg_index], false, t_value_args[t_arg_index]))
262+
if (!t_descriptor -> doimport(t_descriptor, args[t_arg_index], false, t_value_args[t_arg_index]))
263263
goto cleanup;
264264
}
265265
else
@@ -287,7 +287,7 @@ static void __exec_closure(ffi_cif *cif, void *ret, void **args, void *user_data
287287
{
288288
const MCForeignTypeDescriptor *t_descriptor;
289289
t_descriptor = MCForeignTypeInfoGetDescriptor(t_resolved_return_type . type);
290-
if (!t_descriptor -> doexport(t_value_result, false, ret))
290+
if (!t_descriptor -> doexport(t_descriptor, t_value_result, false, ret))
291291
goto cleanup;
292292
}
293293
else

libfoundation/src/foundation-objc.mm

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,90 +105,90 @@ bool __MCObjcObjectDescribe(MCValueRef p_value, MCStringRef &r_desc)
105105
}
106106

107107
static bool
108-
objc_id_move(void *from, void *to)
108+
objc_id_move(const MCForeignTypeDescriptor* desc, void *from, void *to)
109109
{
110110
*static_cast<void **>(to) = *static_cast<void **>(from);
111111
return true;
112112
}
113113

114114
static bool
115-
objc_id_copy(void *from, void *to)
115+
objc_id_copy(const MCForeignTypeDescriptor* desc, void *from, void *to)
116116
{
117117
*static_cast<void **>(to) = *static_cast<void **>(from);
118118
return true;
119119
}
120120

121121
static bool
122-
objc_id_equal(void *from, void *to, bool& r_equal)
122+
objc_id_equal(const MCForeignTypeDescriptor* desc, void *from, void *to, bool& r_equal)
123123
{
124124
r_equal = *static_cast<void **>(to) == *static_cast<void **>(to);
125125
return true;
126126
}
127127

128128
static bool
129-
objc_id_hash(void *value, hash_t& r_hash)
129+
objc_id_hash(const MCForeignTypeDescriptor* desc, void *value, hash_t& r_hash)
130130
{
131131
r_hash = MCHashPointer(*static_cast<void **>(value));
132132
return true;
133133
}
134134

135135
static bool
136-
objc_id_describe(void *value, MCStringRef& r_string)
136+
objc_id_describe(const MCForeignTypeDescriptor* desc, void *value, MCStringRef& r_string)
137137
{
138138
return MCStringFormat(r_string, "<objc id %p>", *static_cast<void **>(value));
139139
}
140140

141141
static bool
142-
objc_retained_id_describe(void *value, MCStringRef& r_string)
142+
objc_retained_id_describe(const MCForeignTypeDescriptor* desc, void *value, MCStringRef& r_string)
143143
{
144144
return MCStringFormat(r_string, "<objc retained id %p>", *static_cast<void **>(value));
145145
}
146146

147147
static bool
148-
objc_autoreleased_id_describe(void *value, MCStringRef& r_string)
148+
objc_autoreleased_id_describe(const MCForeignTypeDescriptor* desc, void *value, MCStringRef& r_string)
149149
{
150150
return MCStringFormat(r_string, "<objc autoreleased id %p>", *static_cast<void **>(value));
151151
}
152152

153153
static bool
154-
objc_id_import(void *contents, bool p_release, MCValueRef& r_value)
154+
objc_id_import(const MCForeignTypeDescriptor* desc, void *contents, bool p_release, MCValueRef& r_value)
155155
{
156156
MCAssert(!p_release);
157157
return MCObjcObjectCreateWithId(*(void **)contents, (MCObjcObjectRef&)r_value);
158158
}
159159

160160
static bool
161-
objc_id_export(MCValueRef p_value, bool p_release, void *contents)
161+
objc_id_export(const MCForeignTypeDescriptor* desc, MCValueRef p_value, bool p_release, void *contents)
162162
{
163163
MCAssert(!p_release);
164164
*(void **)contents = MCObjcObjectGetId((MCObjcObjectRef)p_value);
165165
return true;
166166
}
167167

168168
static bool
169-
objc_retained_id_import(void *contents, bool p_release, MCValueRef& r_value)
169+
objc_retained_id_import(const MCForeignTypeDescriptor* desc, void *contents, bool p_release, MCValueRef& r_value)
170170
{
171171
MCAssert(!p_release);
172172
return MCObjcObjectCreateWithRetainedId(*(void **)contents, (MCObjcObjectRef&)r_value);
173173
}
174174

175175
static bool
176-
objc_retained_id_export(MCValueRef p_value, bool p_release, void *contents)
176+
objc_retained_id_export(const MCForeignTypeDescriptor* desc, MCValueRef p_value, bool p_release, void *contents)
177177
{
178178
MCAssert(!p_release);
179179
*(void **)contents = MCObjcObjectGetRetainedId((MCObjcObjectRef)p_value);
180180
return true;
181181
}
182182

183183
static bool
184-
objc_autoreleased_id_import(void *contents, bool p_release, MCValueRef& r_value)
184+
objc_autoreleased_id_import(const MCForeignTypeDescriptor* desc, void *contents, bool p_release, MCValueRef& r_value)
185185
{
186186
MCAssert(!p_release);
187187
return MCObjcObjectCreateWithAutoreleasedId(*(void **)contents, (MCObjcObjectRef&)r_value);
188188
}
189189

190190
static bool
191-
objc_autoreleased_id_export(MCValueRef p_value, bool p_release, void *contents)
191+
objc_autoreleased_id_export(const MCForeignTypeDescriptor* desc, MCValueRef p_value, bool p_release, void *contents)
192192
{
193193
MCAssert(!p_release);
194194
*(void **)contents = MCObjcObjectGetAutoreleasedId((MCObjcObjectRef)p_value);

libfoundation/src/foundation-proper-list.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ bool MCProperListCreateWithForeignValues(MCTypeInfoRef p_typeinfo, const void *p
8989
MCAutoValueRef t_value;
9090
if (t_descriptor->doimport != nil)
9191
{
92-
if (!t_descriptor->doimport((void *)p_values,
92+
if (!t_descriptor->doimport(t_descriptor,
93+
(void *)p_values,
9394
false,
9495
&t_value))
9596
{

libfoundation/test/test_foreign.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void check_export_raw(MCTypeInfoRef p_type_info, bool (*p_bridge)(W, U&), T p_va
9595
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_type_info);
9696
EXPECT_TRUE(t_desc->doexport != nullptr);
9797
T t_exported_value;
98-
EXPECT_TRUE(t_desc->doexport(*t_bridge_value, false, &t_exported_value));
98+
EXPECT_TRUE(t_desc->doexport(t_desc, *t_bridge_value, false, &t_exported_value));
9999
EXPECT_EQ(t_exported_value, p_value);
100100
}
101101

@@ -108,7 +108,7 @@ void check_integral_raw_export_overflow(MCTypeInfoRef p_type_info, MCTypeInfoRef
108108
EXPECT_TRUE(t_desc->doexport != nullptr);
109109
MCAutoForeignValueRef t_boxed_value;
110110
T t_exported_value = 0;
111-
EXPECT_FALSE(t_desc->doexport(*t_bridge_value, false, &t_exported_value));
111+
EXPECT_FALSE(t_desc->doexport(t_desc, *t_bridge_value, false, &t_exported_value));
112112
if (t_exported_value != 0)
113113
{
114114
MCAutoErrorRef t_error;
@@ -125,7 +125,7 @@ void check_import_raw(MCTypeInfoRef p_type_info, bool (*p_bridge)(W, U&), T p_va
125125
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_type_info);
126126
EXPECT_TRUE(t_desc->doimport != nullptr);
127127
MCAutoValueRef t_imported_value;
128-
EXPECT_TRUE(t_desc->doimport(&p_value, false, &t_imported_value));
128+
EXPECT_TRUE(t_desc->doimport(t_desc, &p_value, false, &t_imported_value));
129129
if (t_imported_value.IsSet())
130130
EXPECT_TRUE(MCValueIsEqualTo(*t_imported_value, *t_bridge_value));
131131
}
@@ -136,7 +136,7 @@ void check_integral_raw_import_overflow(MCTypeInfoRef p_type_info, MCTypeInfoRef
136136
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_type_info);
137137
EXPECT_TRUE(t_desc->doimport != nullptr);
138138
MCAutoValueRef t_imported_value;
139-
EXPECT_FALSE(t_desc->doimport(&p_value, false, &t_imported_value));
139+
EXPECT_FALSE(t_desc->doimport(t_desc, &p_value, false, &t_imported_value));
140140
if (!t_imported_value.IsSet())
141141
{
142142
MCAutoErrorRef t_error;

0 commit comments

Comments
 (0)