MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / CallMethod

Method CallMethod

src/script/squirrel.cpp:376–408  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

374}
375
376bool Squirrel::CallMethod(HSQOBJECT instance, std::string_view method_name, HSQOBJECT *ret, int suspend)
377{
378 assert(!this->crashed);
379 ScriptAllocatorScope alloc_scope(this);
380 this->allocator->CheckLimit();
381
382 /* Store the stack-location for the return value. We need to
383 * restore this after saving or the stack will be corrupted
384 * if we're in the middle of a DoCommand. */
385 SQInteger last_target = this->vm->_suspended_target;
386 /* Store the current top */
387 int top = sq_gettop(this->vm);
388 /* Go to the instance-root */
389 sq_pushobject(this->vm, instance);
390 /* Find the function-name inside the script */
391 sq_pushstring(this->vm, method_name);
392 if (SQ_FAILED(sq_get(this->vm, -2))) {
393 Debug(misc, 0, "[squirrel] Could not find '{}' in the class", method_name);
394 sq_settop(this->vm, top);
395 return false;
396 }
397 /* Call the method */
398 sq_pushobject(this->vm, instance);
399 if (SQ_FAILED(sq_call(this->vm, 1, ret == nullptr ? SQFalse : SQTrue, SQTrue, suspend))) return false;
400 if (ret != nullptr) sq_getstackobj(vm, -1, ret);
401 /* Reset the top, but don't do so for the script main function, as we need
402 * a correct stack when resuming. */
403 if (suspend == -1 || !this->IsSuspended()) sq_settop(this->vm, top);
404 /* Restore the return-value location. */
405 this->vm->_suspended_target = last_target;
406
407 return true;
408}
409
410bool Squirrel::CallStringMethod(HSQOBJECT instance, std::string_view method_name, std::string *res, int suspend)
411{

Callers 6

CallStringMethodMethod · 0.95
CallIntegerMethodMethod · 0.95
CallBoolMethodMethod · 0.95
GameLoopMethod · 0.45
SaveMethod · 0.45
GetSettingsMethod · 0.45

Calls 9

IsSuspendedMethod · 0.95
sq_gettopFunction · 0.85
sq_pushobjectFunction · 0.85
sq_pushstringFunction · 0.85
sq_getFunction · 0.85
sq_settopFunction · 0.85
sq_callFunction · 0.85
sq_getstackobjFunction · 0.85
CheckLimitMethod · 0.80

Tested by

no test coverage detected