MCPcopy Create free account
hub / github.com/anjo76/angelscript / RegisterScriptFunction

Function RegisterScriptFunction

sdk/angelscript/source/as_scriptfunction.cpp:122–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120
121
122void RegisterScriptFunction(asCScriptEngine *engine)
123{
124 // Register the gc behaviours for the script functions
125 int r = 0;
126 UNUSED_VAR(r); // It is only used in debug mode
127 engine->functionBehaviours.engine = engine;
128 engine->functionBehaviours.flags = asOBJ_REF | asOBJ_GC;
129 engine->functionBehaviours.name = "$func";
130#if !defined(AS_MAX_PORTABILITY) && !defined(AS_NO_CLASS_METHODS)
131 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptFunction,AddRef), asCALL_THISCALL, 0); asASSERT( r >= 0 );
132 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptFunction,Release), asCALL_THISCALL, 0); asASSERT( r >= 0 );
133 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptFunction,GetRefCount), asCALL_THISCALL, 0); asASSERT( r >= 0 );
134 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptFunction,SetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
135 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptFunction,GetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
136 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptFunction,EnumReferences), asCALL_THISCALL, 0); asASSERT( r >= 0 );
137 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptFunction,ReleaseAllHandles), asCALL_THISCALL, 0); asASSERT( r >= 0 );
138 // TODO: Need some way to allow the arg type to adapt when the funcdefs are instantiated
139// r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asMETHOD(asCScriptFunction,operator==), asCALL_THISCALL); asASSERT( r >= 0 );
140#else
141 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptFunction_AddRef_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
142 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptFunction_Release_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
143 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptFunction_GetRefCount_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
144 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptFunction_SetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
145 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptFunction_GetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
146 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptFunction_EnumReferences_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
147 r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptFunction_ReleaseAllHandles_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
148// r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asFUNCTION(ScriptFunction_opEquals_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
149#endif
150
151 // Register the builtin function for creating delegates
152 // This function returns a handle to the delegate, but since the type is not known at this time it is
153 // registered to return a void then the return type is changed manually to the builtin function type
154 // The name of the function is an invalid identifier so it cannot be invoked accidentally from the script
155#ifndef AS_MAX_PORTABILITY
156 r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(CreateDelegate), asCALL_CDECL); asASSERT( r >= 0 );
157#else
158 r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(ScriptFunction_CreateDelegate_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
159#endif
160
161 // Rename the function so that it cannot be called manually by the script
162 int idx = engine->registeredGlobalFuncs.GetIndex(engine->scriptFunctions[r]);
163 engine->registeredGlobalFuncs.Erase(idx);
164 engine->scriptFunctions[r]->name = DELEGATE_FACTORY;
165 engine->registeredGlobalFuncs.Put(engine->scriptFunctions[r]);
166
167 // Change the return type so the VM will know the function really returns a handle
168 engine->scriptFunctions[r]->returnType = asCDataType::CreateType(&engine->functionBehaviours, false);
169 engine->scriptFunctions[r]->returnType.MakeHandle(true);
170}
171
172asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj)
173{

Callers 4

RegisterLambdaMethod · 0.85
asCScriptEngineMethod · 0.85

Calls 6

PutMethod · 0.80
MakeHandleMethod · 0.80
GetIndexMethod · 0.45
EraseMethod · 0.45

Tested by

no test coverage detected