Skip to content

Commit e222f1e

Browse files
committed
[[ Script ]] Added module querying APIs for events, properties, and dependencies.
1 parent e1f2eb1 commit e222f1e

File tree

3 files changed

+149
-6
lines changed

3 files changed

+149
-6
lines changed

libscript/include/script.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,38 @@ bool MCScriptLookupModule(MCNameRef name, MCScriptModuleRef& r_module);
143143
bool MCScriptEnsureModuleIsUsable(MCScriptModuleRef module);
144144

145145
// List the module's direct dependencies.
146-
bool MCScriptGetDependenciesOfModule(MCScriptModuleRef module, const MCNameRef*& r_dependencies, uindex_t& r_count);
146+
bool MCScriptCopyDependenciesOfModule(MCScriptModuleRef module, /* copy */ MCProperListRef& r_module_names);
147147

148-
// Create an instance of the given module. If the module is single-instance it
149-
// returns that instance. Otherwise it returns a new instance. If the method
150-
// fails, false is returned. In the case of success, the caller must release the
151-
// instance.
152-
bool MCScriptCreateInstanceOfModule(MCScriptModuleRef module, MCScriptInstanceRef& r_instance);
148+
// Returns a list of properties implemented by the module.
149+
bool MCScriptCopyPropertiesOfModule(MCScriptModuleRef module, /* copy */ MCProperListRef& r_property_names);
150+
151+
// Queries the type of the given property. If the setting type is nil, then the property
152+
// is read-only.
153+
bool MCScriptQueryPropertyOfModule(MCScriptModuleRef module, MCNameRef property, /* get */ MCTypeInfoRef& r_getter, /* get */ MCTypeInfoRef& r_setter);
154+
155+
// Returns a list of the events declared by the module.
156+
bool MCScriptCopyEventsOfModule(MCScriptModuleRef module, /* copy */ MCProperListRef& r_event_names);
157+
158+
// Query the signature of the given event.
159+
bool MCScriptQueryEventOfModule(MCScriptModuleRef module, MCNameRef event, /* get */ MCTypeInfoRef& r_signature);
153160

154161
// Retain a module.
155162
MCScriptModuleRef MCScriptRetainModule(MCScriptModuleRef module);
163+
156164
// Release a module.
157165
void MCScriptReleaseModule(MCScriptModuleRef module);
158166

167+
////////////////////////////////////////////////////////////////////////////////
168+
169+
// Create an instance of the given module. If the module is single-instance it
170+
// returns that instance. Otherwise it returns a new instance. If the method
171+
// fails, false is returned. In the case of success, the caller must release the
172+
// instance.
173+
bool MCScriptCreateInstanceOfModule(MCScriptModuleRef module, MCScriptInstanceRef& r_instance);
174+
159175
// Retain a instance.
160176
MCScriptInstanceRef MCScriptRetainInstance(MCScriptInstanceRef instance);
177+
161178
// Release a instance.
162179
void MCScriptReleaseInstance(MCScriptInstanceRef instance);
163180

libscript/src/script-module.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,112 @@ bool MCScriptEnsureModuleIsUsable(MCScriptModuleRef self)
614614

615615
////////////////////////////////////////////////////////////////////////////////
616616

617+
bool MCScriptCopyDependenciesOfModule(MCScriptModuleRef self, /* copy */ MCProperListRef& r_module_names)
618+
{
619+
MCAutoProperListRef t_modules;
620+
if (!MCProperListCreateMutable(&t_modules))
621+
return false;
622+
623+
for(uindex_t i = 0; i < self -> dependency_count; i++)
624+
if (!MCProperListPushElementOntoBack(*t_modules, self -> dependencies[i] . name))
625+
return false;
626+
627+
if (!MCProperListCopy(*t_modules, r_module_names))
628+
return false;
629+
630+
return true;
631+
}
632+
633+
bool MCScriptCopyPropertiesOfModule(MCScriptModuleRef self, /* copy */ MCProperListRef& r_property_names)
634+
{
635+
MCAutoProperListRef t_props;
636+
if (!MCProperListCreateMutable(&t_props))
637+
return false;
638+
639+
for(uindex_t i = 0; i < self -> exported_definition_count; i++)
640+
if (self -> definitions[self -> exported_definitions[i] . index] -> kind == kMCScriptDefinitionKindProperty)
641+
if (!MCProperListPushElementOntoBack(*t_props, self -> exported_definitions[i] . name))
642+
return false;
643+
644+
if (!MCProperListCopy(*t_props, r_property_names))
645+
return false;
646+
647+
return true;
648+
}
649+
650+
bool MCScriptQueryPropertyOfModule(MCScriptModuleRef self, MCNameRef p_property, /* get */ MCTypeInfoRef& r_getter, /* get */ MCTypeInfoRef& r_setter)
651+
{
652+
MCScriptPropertyDefinition *t_def;
653+
654+
if (!self -> is_usable)
655+
return false; // TODO - throw error
656+
657+
if (!MCScriptLookupPropertyDefinitionInModule(self, p_property, t_def))
658+
return false; // TODO - throw error
659+
660+
MCScriptDefinition *t_getter;
661+
t_getter = t_def -> getter != 0 ? self -> definitions[t_def -> getter - 1] : nil;
662+
663+
if (t_getter != nil)
664+
{
665+
if (t_getter -> kind == kMCScriptDefinitionKindVariable)
666+
r_getter = self -> types[static_cast<MCScriptVariableDefinition *>(t_getter) -> type] -> typeinfo;
667+
else
668+
r_getter = MCHandlerTypeInfoGetReturnType(self -> types[static_cast<MCScriptHandlerDefinition *>(t_getter) -> type] -> typeinfo);
669+
}
670+
else
671+
r_getter = nil;
672+
673+
MCScriptDefinition *t_setter;
674+
t_setter = t_def -> setter != 0 ? self -> definitions[t_def -> setter - 1] : nil;
675+
676+
if (t_setter != nil)
677+
{
678+
if (t_setter -> kind == kMCScriptDefinitionKindVariable)
679+
r_setter = self -> types[static_cast<MCScriptVariableDefinition *>(t_setter) -> type] -> typeinfo;
680+
else
681+
r_setter = MCHandlerTypeInfoGetParameterType(self -> types[static_cast<MCScriptHandlerDefinition *>(t_setter) -> type] -> typeinfo, 0);
682+
}
683+
else
684+
r_setter = nil;
685+
686+
return true;
687+
}
688+
689+
bool MCScriptCopyEventsOfModule(MCScriptModuleRef self, /* copy */ MCProperListRef& r_event_names)
690+
{
691+
MCAutoProperListRef t_events;
692+
if (!MCProperListCreateMutable(&t_events))
693+
return false;
694+
695+
for(uindex_t i = 0; i < self -> exported_definition_count; i++)
696+
if (self -> definitions[self -> exported_definitions[i] . index] -> kind == kMCScriptDefinitionKindEvent)
697+
if (!MCProperListPushElementOntoBack(*t_events, self -> exported_definitions[i] . name))
698+
return false;
699+
700+
if (!MCProperListCopy(*t_events, r_event_names))
701+
return false;
702+
703+
return true;
704+
}
705+
706+
bool MCScriptQueryEventOfModule(MCScriptModuleRef self, MCNameRef p_event, /* get */ MCTypeInfoRef& r_signature)
707+
{
708+
MCScriptEventDefinition *t_def;
709+
710+
if (!self -> is_usable)
711+
return false; // TODO - throw error
712+
713+
if (!MCScriptLookupEventDefinitionInModule(self, p_event, t_def))
714+
return false; // TODO - throw error
715+
716+
r_signature = self -> types[t_def -> type] -> typeinfo;
717+
718+
return true;
719+
}
720+
721+
////////////////////////////////////////////////////////////////////////////////
722+
617723
bool MCScriptWriteRawModule(MCStreamRef stream, MCScriptModule *p_module)
618724
{
619725
return MCPickleWrite(stream, kMCScriptModulePickleInfo, p_module);
@@ -650,6 +756,25 @@ bool MCScriptLookupPropertyDefinitionInModule(MCScriptModuleRef self, MCNameRef
650756
return false;
651757
}
652758

759+
bool MCScriptLookupEventDefinitionInModule(MCScriptModuleRef self, MCNameRef p_property, MCScriptEventDefinition*& r_definition)
760+
{
761+
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindModule);
762+
763+
for(uindex_t i = 0; i < self -> exported_definition_count; i++)
764+
{
765+
if (self -> definitions[self -> exported_definitions[i] . index] -> kind != kMCScriptDefinitionKindEvent)
766+
continue;
767+
768+
if (!MCNameIsEqualTo(p_property, self -> exported_definitions[i] . name))
769+
continue;
770+
771+
r_definition = static_cast<MCScriptEventDefinition *>(self -> definitions[self -> exported_definitions[i] . index]);
772+
return true;
773+
}
774+
775+
return false;
776+
}
777+
653778
bool MCScriptLookupHandlerDefinitionInModule(MCScriptModuleRef self, MCNameRef p_handler, MCScriptHandlerDefinition*& r_definition)
654779
{
655780
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindModule);

libscript/src/script-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ void MCScriptReleaseRawModule(MCScriptModule *module);
342342
void MCScriptDestroyModule(MCScriptModuleRef module);
343343

344344
bool MCScriptLookupPropertyDefinitionInModule(MCScriptModuleRef module, MCNameRef property, MCScriptPropertyDefinition*& r_definition);
345+
bool MCScriptLookupEventDefinitionInModule(MCScriptModuleRef module, MCNameRef property, MCScriptEventDefinition*& r_definition);
345346
bool MCScriptLookupHandlerDefinitionInModule(MCScriptModuleRef module, MCNameRef handler, MCScriptHandlerDefinition*& r_definition);
346347
bool MCScriptLookupDefinitionInModule(MCScriptModuleRef self, MCNameRef p_name, MCScriptDefinition*& r_definition);
347348

0 commit comments

Comments
 (0)