Skip to content

Commit 3fa2059

Browse files
committed
[[ Script ]] Added handler querying to module API
[[ Script ]] Made it so that standard 'inout' policy is used on argument list to CallHandlerInInstance.
1 parent b8b3ef2 commit 3fa2059

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

libscript/include/script.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ bool MCScriptLookupModule(MCNameRef name, MCScriptModuleRef& r_module);
142142
// Ensure that the module is valid and has resolved all its dependencies.
143143
bool MCScriptEnsureModuleIsUsable(MCScriptModuleRef module);
144144

145+
// Returns true if the module is a library.
146+
bool MCScriptIsModuleALibrary(MCScriptModuleRef module);
147+
148+
// Returns true if the module is a widget.
149+
bool MCScriptIsModuleAWidget(MCScriptModuleRef module);
150+
145151
// List the module's direct dependencies.
146152
bool MCScriptCopyDependenciesOfModule(MCScriptModuleRef module, /* copy */ MCProperListRef& r_module_names);
147153

@@ -158,6 +164,12 @@ bool MCScriptCopyEventsOfModule(MCScriptModuleRef module, /* copy */ MCProperLis
158164
// Query the signature of the given event.
159165
bool MCScriptQueryEventOfModule(MCScriptModuleRef module, MCNameRef event, /* get */ MCTypeInfoRef& r_signature);
160166

167+
// Returns a list of the handlers declared by the module.
168+
bool MCScriptCopyHandlersOfModule(MCScriptModuleRef module, /* copy */ MCProperListRef& r_handler_names);
169+
170+
// Query the signature of the given handler.
171+
bool MCScriptQueryHandlerOfModule(MCScriptModuleRef module, MCNameRef handler, /* get */ MCTypeInfoRef& r_signature);
172+
161173
// Retain a module.
162174
MCScriptModuleRef MCScriptRetainModule(MCScriptModuleRef module);
163175

libscript/src/script-instance.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,12 @@ bool MCScriptCallHandlerOfInstanceInternal(MCScriptInstanceRef self, MCScriptHan
14881488

14891489
for(uindex_t i = 0; i < MCHandlerTypeInfoGetParameterCount(t_signature); i++)
14901490
if (MCHandlerTypeInfoGetParameterMode(t_signature, i) != kMCHandlerTypeFieldModeIn)
1491-
p_arguments[i] = MCValueRetain(MCScriptFetchFromLocalInFrame(t_frame, i));
1491+
{
1492+
if (p_arguments[i] != nil)
1493+
MCValueAssign(p_arguments[i], MCScriptFetchFromLocalInFrame(t_frame, i));
1494+
else
1495+
p_arguments[i] = MCValueRetain(MCScriptFetchFromLocalInFrame(t_frame, i));
1496+
}
14921497
}
14931498
else
14941499
{

libscript/src/script-module.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ bool MCScriptLookupModule(MCNameRef p_name, MCScriptModuleRef& r_module)
365365
return false;
366366
}
367367

368+
bool MCScriptIsModuleALibrary(MCScriptModuleRef self)
369+
{
370+
return self -> module_kind == kMCScriptModuleKindLibrary;
371+
}
372+
373+
bool MCScriptIsModuleAWidget(MCScriptModuleRef self)
374+
{
375+
return self -> module_kind == kMCScriptModuleKindWidget;
376+
}
377+
368378
bool MCScriptEnsureModuleIsUsable(MCScriptModuleRef self)
369379
{
370380
// If the module has already been ensured as usable, we are done.
@@ -382,8 +392,8 @@ bool MCScriptEnsureModuleIsUsable(MCScriptModuleRef self)
382392
if (!MCScriptLookupModule(self -> dependencies[i] . name, t_module))
383393
return false;
384394

385-
// A used module must be a library.
386-
if (t_module -> module_kind != kMCScriptModuleKindLibrary)
395+
// A used module must not be a widget.
396+
if (t_module -> module_kind == kMCScriptModuleKindWidget)
387397
return false;
388398

389399
// Check all the imported definitions from the module, and compute indicies.
@@ -718,6 +728,39 @@ bool MCScriptQueryEventOfModule(MCScriptModuleRef self, MCNameRef p_event, /* ge
718728
return true;
719729
}
720730

731+
bool MCScriptCopyHandlersOfModule(MCScriptModuleRef self, /* copy */ MCProperListRef& r_handler_names)
732+
{
733+
MCAutoProperListRef t_handlers;
734+
if (!MCProperListCreateMutable(&t_handlers))
735+
return false;
736+
737+
for(uindex_t i = 0; i < self -> exported_definition_count; i++)
738+
if (self -> definitions[self -> exported_definitions[i] . index] -> kind == kMCScriptDefinitionKindHandler ||
739+
self -> definitions[self -> exported_definitions[i] . index] -> kind == kMCScriptDefinitionKindForeignHandler)
740+
if (!MCProperListPushElementOntoBack(*t_handlers, self -> exported_definitions[i] . name))
741+
return false;
742+
743+
if (!MCProperListCopy(*t_handlers, r_handler_names))
744+
return false;
745+
746+
return true;
747+
}
748+
749+
bool MCScriptQueryHandlerOfModule(MCScriptModuleRef self, MCNameRef p_handler, /* get */ MCTypeInfoRef& r_signature)
750+
{
751+
MCScriptHandlerDefinition *t_def;
752+
753+
if (!self -> is_usable)
754+
return false; // TODO - throw error
755+
756+
if (!MCScriptLookupHandlerDefinitionInModule(self, p_handler, t_def))
757+
return false; // TODO - throw error
758+
759+
r_signature = self -> types[t_def -> type] -> typeinfo;
760+
761+
return true;
762+
}
763+
721764
////////////////////////////////////////////////////////////////////////////////
722765

723766
bool MCScriptWriteRawModule(MCStreamRef stream, MCScriptModule *p_module)

0 commit comments

Comments
 (0)