ScummVM API documentation
as_scriptfunction.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2021 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 
32 //
33 // as_scriptfunction.h
34 //
35 // A container for a compiled script function
36 //
37 
38 
39 
40 #ifndef AS_SCRIPTFUNCTION_H
41 #define AS_SCRIPTFUNCTION_H
42 
43 #include "as_config.h"
44 #include "as_string.h"
45 #include "as_memory.h"
46 #include "as_datatype.h"
47 #include "as_atomic.h"
48 
49 BEGIN_AS_NAMESPACE
50 
51 class asCScriptEngine;
52 class asCModule;
53 class asCConfigGroup;
54 class asCGlobalProperty;
55 class asCScriptNode;
56 class asCFuncdefType;
57 struct asSNameSpace;
58 
60  asCString name;
61  asCDataType type;
62  int stackOffset;
63  asUINT declaredAtProgramPos;
64 };
65 
66 enum asEListPatternNodeType {
67  asLPT_REPEAT,
68  asLPT_REPEAT_SAME,
69  asLPT_START,
70  asLPT_END,
71  asLPT_TYPE
72 };
73 
75  asSListPatternNode(asEListPatternNodeType t) : type(t), next(0) {}
76  virtual ~asSListPatternNode() {};
77  virtual asSListPatternNode *Duplicate() {
78  return asNEW(asSListPatternNode)(type);
79  }
80  asEListPatternNodeType type;
81  asSListPatternNode *next;
82 };
83 
85  asSListPatternDataTypeNode(const asCDataType &dt) : asSListPatternNode(asLPT_TYPE), dataType(dt) {}
86  asSListPatternNode *Duplicate() {
87  return asNEW(asSListPatternDataTypeNode)(dataType);
88  }
89  asCDataType dataType;
90 };
91 
92 enum asEObjVarInfoOption {
93  asOBJ_UNINIT, // object is uninitialized/destroyed
94  asOBJ_INIT, // object is initialized
95  asBLOCK_BEGIN, // scope block begins
96  asBLOCK_END, // scope block ends
97  asOBJ_VARDECL // object variable is declared (but not necessarily initialized)
98 };
99 
100 enum asEFuncTrait {
101  asTRAIT_CONSTRUCTOR = 1,
102  asTRAIT_DESTRUCTOR = 2,
103  asTRAIT_CONST = 4,
104  asTRAIT_PRIVATE = 8,
105  asTRAIT_PROTECTED = 16,
106  asTRAIT_FINAL = 32,
107  asTRAIT_OVERRIDE = 64,
108  asTRAIT_SHARED = 128,
109  asTRAIT_EXTERNAL = 256,
110  asTRAIT_EXPLICIT = 512,
111  asTRAIT_PROPERTY = 1024
112 };
113 
115  asSFunctionTraits() : traits(0) {}
116  void SetTrait(asEFuncTrait trait, bool set) {
117  if (set) traits |= trait;
118  else traits &= ~trait;
119  }
120  bool GetTrait(asEFuncTrait trait) const {
121  return (traits & trait) ? true : false;
122  }
123 protected:
124  asDWORD traits;
125 };
126 
128  asUINT programPos;
129  int variableOffset;
130  asEObjVarInfoOption option;
131 };
132 
134  asUINT tryPos;
135  asUINT catchPos;
136 };
137 
139 
140 // TODO: Might be interesting to allow enumeration of accessed global variables, and
141 // also functions/methods that are being called. This could be used to build a
142 // code database with call graphs, etc.
143 
144 void RegisterScriptFunction(asCScriptEngine *engine);
145 
147 public:
148  // From asIScriptFunction
149  asIScriptEngine *GetEngine() const;
150 
151  // Memory management
152  int AddRef() const;
153  int Release() const;
154 
155  // Miscellaneous
156  int GetId() const;
157  asEFuncType GetFuncType() const;
158  const char *GetModuleName() const;
159  asIScriptModule *GetModule() const;
160  const char *GetScriptSectionName() const;
161  const char *GetConfigGroup() const;
162  asDWORD GetAccessMask() const;
163  void *GetAuxiliary() const;
164 
165  // Function signature
166  asITypeInfo *GetObjectType() const;
167  const char *GetObjectName() const;
168  const char *GetName() const;
169  const char *GetNamespace() const;
170  const char *GetDeclaration(bool includeObjectName = true, bool includeNamespace = false, bool includeParamNames = false) const;
171  bool IsReadOnly() const;
172  bool IsPrivate() const;
173  bool IsProtected() const;
174  bool IsFinal() const;
175  bool IsOverride() const;
176  bool IsShared() const;
177  bool IsExplicit() const;
178  bool IsProperty() const;
179  asUINT GetParamCount() const;
180  int GetParam(asUINT index, int *typeId, asDWORD *flags = 0, const char **name = 0, const char **defaultArg = 0) const;
181  int GetReturnTypeId(asDWORD *flags = 0) const;
182 
183  // Type id for function pointers
184  int GetTypeId() const;
185  bool IsCompatibleWithTypeId(int typeId) const;
186 
187  // Delegates
188  void *GetDelegateObject() const;
189  asITypeInfo *GetDelegateObjectType() const;
190  asIScriptFunction *GetDelegateFunction() const;
191 
192  // Debug information
193  asUINT GetVarCount() const;
194  int GetVar(asUINT index, const char **name, int *typeId = 0) const;
195  const char *GetVarDecl(asUINT index, bool includeNamespace = false) const;
196  int FindNextLineWithCode(int line) const;
197 
198  // For JIT compilation
199  asDWORD *GetByteCode(asUINT *length = 0);
200 
201  // User data
202  void *SetUserData(void *userData, asPWORD type);
203  void *GetUserData(asPWORD type) const;
204 
205 public:
206  //-----------------------------------
207  // Internal methods
208 
209  void SetShared(bool set) {
210  traits.SetTrait(asTRAIT_SHARED, set);
211  }
212  void SetReadOnly(bool set) {
213  traits.SetTrait(asTRAIT_CONST, set);
214  }
215  void SetFinal(bool set) {
216  traits.SetTrait(asTRAIT_FINAL, set);
217  }
218  void SetOverride(bool set) {
219  traits.SetTrait(asTRAIT_OVERRIDE, set);
220  }
221  void SetExplicit(bool set) {
222  traits.SetTrait(asTRAIT_EXPLICIT, set);
223  }
224  void SetProtected(bool set) {
225  traits.SetTrait(asTRAIT_PROTECTED, set);
226  }
227  void SetPrivate(bool set) {
228  traits.SetTrait(asTRAIT_PRIVATE, set);
229  }
230  void SetProperty(bool set) {
231  traits.SetTrait(asTRAIT_PROPERTY, set);
232  }
233  bool IsFactory() const;
234 
235  asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
237 
238  // Keep an internal reference counter to separate references coming from
239  // application or script objects and references coming from the script code
240  int AddRefInternal();
241  int ReleaseInternal();
242 
243  void DestroyHalfCreated();
244 
245  // TODO: operator==
246  // TODO: The asIScriptFunction should provide operator== and operator!= that should do a
247  // a value comparison. Two delegate objects that point to the same object and class method should compare as equal
248  // TODO: The operator== should also be provided in script as opEquals to allow the same comparison in script
249  // To do this we'll need some way to adapt the argtype for opEquals for each funcdef, preferrably without instantiating lots of different methods
250  // Perhaps reusing 'auto' to mean the same type as the object
251  //bool operator==(const asCScriptFunction &other) const;
252 
253  void DestroyInternal();
254 
255  void AddVariable(asCString &name, asCDataType &type, int stackOffset);
256 
257  int GetSpaceNeededForArguments();
258  int GetSpaceNeededForReturnValue();
259  asCString GetDeclarationStr(bool includeObjectName = true, bool includeNamespace = false, bool includeParamNames = false) const;
260  int GetLineNumber(int programPosition, int *sectionIdx);
261  void ComputeSignatureId();
262  bool IsSignatureEqual(const asCScriptFunction *func) const;
263  bool IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
264  bool IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
265  bool IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *fun) const;
266  bool IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
267  bool IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const;
268 
269  asCTypeInfo *GetTypeInfoOfLocalVar(short varOffset);
270 
271  void MakeDelegate(asCScriptFunction *func, void *obj);
272 
273  int RegisterListPattern(const char *decl, asCScriptNode *listPattern);
274  int ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listPattern);
275 
276  bool DoesReturnOnStack() const;
277 
278  void JITCompile();
279 
280  void AddReferences();
281  void ReleaseReferences();
282 
283  void AllocateScriptFunctionData();
284  void DeallocateScriptFunctionData();
285 
286  asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);
287 
288  // GC methods (for delegates)
289  int GetRefCount();
290  void SetFlag();
291  bool GetFlag();
292  void EnumReferences(asIScriptEngine *engine);
293  void ReleaseAllHandles(asIScriptEngine *engine);
294 
295 public:
296  //-----------------------------------
297  // Properties
298 
299  mutable asCAtomic externalRefCount; // Used for external referneces
300  asCAtomic internalRefCount; // Used for internal references
301  mutable bool gcFlag;
302  asCScriptEngine *engine;
303  asCModule *module;
304 
305  asCArray<asPWORD> userData;
306 
307  // Function signature
308  asCString name;
309  asCDataType returnType;
310  asCArray<asCDataType> parameterTypes;
311  asCArray<asCString> parameterNames;
312  asCArray<asETypeModifiers> inOutFlags;
313  asCArray<asCString *> defaultArgs;
314  asSFunctionTraits traits;
315  asCObjectType *objectType;
316  int signatureId;
317 
318  int id;
319 
320  asEFuncType funcType;
321  asDWORD accessMask;
322 
323  // Namespace will be null for funcdefs that are declared as child funcdefs
324  // of a class. In this case the namespace shall be taken from the parentClass
325  // in the funcdefType
326  asSNameSpace *nameSpace;
327 
328  asCFuncdefType *funcdefType; // Doesn't increase refCount
329 
330  // Used by asFUNC_DELEGATE
331  void *objForDelegate;
332  asCScriptFunction *funcForDelegate;
333 
334  // Used by list factory behaviour
335  asSListPatternNode *listPattern;
336 
337  // Used by asFUNC_SCRIPT
339  // Bytecode for the script function
340  asCArray<asDWORD> byteCode;
341 
342  // The stack space needed for the local variables
343  asDWORD variableSpace;
344 
345  // These hold information on objects and function pointers, including temporary
346  // variables used by exception handler and when saving bytecode
347  asCArray<asCTypeInfo *> objVariableTypes;
348  asCArray<int> objVariablePos; // offset on stackframe
349 
350  // The first variables in above array are allocated on the heap, the rest on the stack.
351  // This variable shows how many are on the heap.
352  asUINT objVariablesOnHeap;
353 
354  // Holds information on scope for object variables on the stack
355  asCArray<asSObjectVariableInfo> objVariableInfo;
356 
357  // Holds information on try/catch blocks for exception handling
358  asCArray<asSTryCatchInfo> tryCatchInfo;
359 
360  // The stack needed to execute the function
361  int stackNeeded;
362 
363  // JIT compiled code of this function
364  asJITFunction jitFunction;
365 
366  // Holds debug information on explicitly declared variables
368  // Store position, line number pairs for debug information
369  asCArray<int> lineNumbers;
370  // Store the script section where the code was declared
371  int scriptSectionIdx;
372  // Store the location where the function was declared (row in the lower 20 bits, and column in the upper 12)
373  int declaredAt;
374  // Store position/index pairs if the bytecode is compiled from multiple script sections
375  asCArray<int> sectionIdxs;
376  };
377  ScriptFunctionData *scriptData;
378 
379  // Stub functions and delegates don't own the object and parameters
380  bool dontCleanUpOnException;
381 
382  // Used by asFUNC_VIRTUAL
383  int vfTableIdx;
384 
385  // Used by asFUNC_SYSTEM
386  asSSystemFunctionInterface *sysFuncIntf;
387 };
388 
389 const char *const DELEGATE_FACTORY = "$dlgte";
390 asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj);
391 
392 END_AS_NAMESPACE
393 
394 #endif
Definition: as_module.h:93
Definition: angelscript.h:1083
Definition: as_typeinfo.h:64
Definition: angelscript.h:793
Definition: as_scriptfunction.h:146
Definition: as_objecttype.h:100
Definition: as_namespace.h:39
Definition: as_scriptfunction.h:133
Definition: as_scriptfunction.h:114
Definition: as_configgroup.h:50
Definition: as_scriptfunction.h:59
Definition: as_scriptfunction.h:74
Definition: as_scriptfunction.h:127
Definition: as_atomic.h:49
Definition: as_callfunc.h:99
Definition: as_datatype.h:60
Definition: as_property.h:68
Definition: as_scriptfunction.h:84
Definition: as_scriptfunction.h:338
Definition: as_typeinfo.h:292
Definition: as_scriptengine.h:64
Definition: angelscript.h:639
Definition: angelscript.h:1011
Definition: as_scriptnode.h:104
Definition: as_string.h:41