Angelscript/docs/manual/doc_adv_reflection.html

132 lines
10 KiB
HTML
Raw Permalink Normal View History

2021-04-12 18:25:02 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: Reflection</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_adv_reflection.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Reflection </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>While the script language doesn't offer built-in functionality for reflection, the application interface do provide all the necessary methods to enumerate everything within the scripts.</p>
<p>The article on <a class="el" href="doc_debug.html">debugging</a> shows a little of these interfaces with the focus on debugging rather than reflection. The article on <a class="el" href="doc_adv_dynamic_build.html">dynamic compilations</a> shows another aspect where scripts may be partially modified at runtime.</p>
<p>The following will do an overview of the available methods for enumerating the various entities in a script.</p>
<h1><a class="anchor" id="doc_adv_reflection_vars"></a>
Enumerating variables and properties</h1>
<p>Global variables in a script module are enumerated with the interface <a class="el" href="classas_i_script_module.html">asIScriptModule</a>. Specifically the methods <a class="el" href="classas_i_script_module.html#a87e29773f7e6f2980d75288faaa74d02">GetGlobalVarCount</a> and <a class="el" href="classas_i_script_module.html#a939e6caf004c6fdae78fe89bb244d962">GetGlobalVar</a>. <a class="el" href="classas_i_script_module.html#a00cff95b43c256cc6b9062e135a473a2">GetGlobalVarIndexByName</a> and <a class="el" href="classas_i_script_module.html#ac3d5dafe8ca92bf618f438dc79ef2906">GetGlobalVarIndexByDecl</a> can be used if the name and/or declaration of the desired variable is known before hand. To inspect or even modify the value of the global variable the method <a class="el" href="classas_i_script_module.html#a0998d375ca1de02bc72f4544806da58c">GetAddressOfGlobalVar</a> should be used.</p>
<p>The engine interface <a class="el" href="classas_i_script_engine.html">asIScriptEngine</a> has a similar set of methods for enumerating application registered global properties, i.e. <a class="el" href="classas_i_script_engine.html#aa69f6b37f9c7bdf9b52b9c1692daf048">GetGlobalPropertyCount</a>, <a class="el" href="classas_i_script_engine.html#a93bd686853a48647d2136792e27380fb">GetGlobalPropertyByIndex</a>, <a class="el" href="classas_i_script_engine.html#a07e85878869e4d0597c1177d767dc717">GetGlobalPropertyIndexByName</a>, and <a class="el" href="classas_i_script_engine.html#a91a4cc8af51ca439ca82b9b6630439b3">GetGlobalPropertyIndexByDecl</a>.</p>
<p>Member properties of classes are accessed through the <a class="el" href="classas_i_script_object.html">asIScriptObject</a> interface for live object instances, and <a class="el" href="classas_i_type_info.html">asITypeInfo</a> interface for inspecting the class declarations without any live object instance.</p>
<p>Local variables within functions can also be enumerated as long as the script has been compiled with debug information. These are then enumerated through the <a class="el" href="classas_i_script_function.html">asIScriptFunction</a> interface for inspecting the declarations, and directly through <a class="el" href="classas_i_script_context.html">asIScriptContext</a> for inspecting and/or modifying them on the stack.</p>
<h1><a class="anchor" id="doc_adv_reflection_funcs"></a>
Enumerating functions and methods</h1>
<p>Global functions in a script are enumerated with the interface <a class="el" href="classas_i_script_module.html">asIScriptModule</a>, using the methods <a class="el" href="classas_i_script_module.html#a373d102f109ae0fa20584f243ead4035">GetFunctionCount</a>, <a class="el" href="classas_i_script_module.html#afd4d9951ee006caa8ec1bab3d3d206fc">GetFunctionByIndex</a>, <a class="el" href="classas_i_script_module.html#a81d727c9677b683942b6087df4ce95ad">GetFunctionByName</a>, and <a class="el" href="classas_i_script_module.html#ab4754d55d8667aefbed135b4794d461b">GetFunctionByDecl</a>.</p>
<p>The engine interface <a class="el" href="classas_i_script_engine.html">asIScriptEngine</a> also exposes methods for enumerating application registered functions in a similar manner.</p>
<p>To enumerate methods of classes the interface <a class="el" href="classas_i_type_info.html">asITypeInfo</a> should be used.</p>
<h1><a class="anchor" id="doc_adv_reflection_types"></a>
Enumerating types</h1>
<p><a class="el" href="classas_i_script_module.html">asIScriptModule</a> is of course used to enumerate the types declared in the scripts too. The methods <a class="el" href="classas_i_script_module.html#a931d0c00cba2df1b4e368e59bac1a207">GetObjectTypeCount</a>, <a class="el" href="classas_i_script_module.html#a49ebfdd345f18d88e489edebd4888af7">GetObjectTypeByIndex</a>, and <a class="el" href="classas_i_script_module.html#ae8460fa05f441072e14f984ccf507396">GetTypeInfoByName</a> are for enumerating classes and interfaces. The methods <a class="el" href="classas_i_script_module.html#a1a241b3dfd47f7ab5bb475c757e38df9">GetEnumCount</a> and <a class="el" href="classas_i_script_module.html#a0ec42bef3874f0fc0e6b929068ae927c">GetEnumByIndex</a> are for enumerating enums.</p>
<p>The <a class="el" href="classas_i_script_engine.html">asIScriptEngine</a> interface has near identical methods for enumerating the application registered types.</p>
<p>Many of the above methods return values called type ids that describes the type of the respective variable, property, or function argument. In many cases the type id can be directly inspected as a bitfield to get necessary information on the what the type is. The lower bits are just a sequence number where the first 12 numbers represents the built-in primitives, and anything higher represents either application registered types or script declared types. The higher bits indicate if the type represents a primitive, object, or handle. Use the flags in <a class="el" href="angelscript_8h.html#ae8c3a67a97321be53181e9ed396ad83a">asETypeIdFlags</a> to do the necessary verifications on the type id.</p>
<p>For type ids that represent object types it may be necessary to obtain the <a class="el" href="classas_i_type_info.html">asITypeInfo</a> instance to get further information on what the type is. The method <a class="el" href="classas_i_script_engine.html#a55cc2d9592651538efcf79eb8106a867">GetTypeInfoById</a> is used to do this translate from type id to <a class="el" href="classas_i_type_info.html">asITypeInfo</a>. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sat Dec 5 2020 23:20:24 for AngelScript by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>