2009-09-17 16:53:58 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sci/sci.h"
|
|
|
|
#include "sci/engine/state.h"
|
|
|
|
|
|
|
|
namespace Sci {
|
|
|
|
|
2009-12-26 14:58:23 +00:00
|
|
|
reg_t read_selector(SegManager *segMan, reg_t object, Selector selector_id) {
|
2009-09-17 16:53:58 +00:00
|
|
|
ObjVarRef address;
|
|
|
|
|
|
|
|
if (lookup_selector(segMan, object, selector_id, &address, NULL) != kSelectorVariable)
|
|
|
|
return NULL_REG;
|
|
|
|
else
|
|
|
|
return *address.getPointer(segMan);
|
|
|
|
}
|
|
|
|
|
2009-12-26 14:58:23 +00:00
|
|
|
void write_selector(SegManager *segMan, reg_t object, Selector selector_id, reg_t value) {
|
2009-09-17 16:53:58 +00:00
|
|
|
ObjVarRef address;
|
|
|
|
|
|
|
|
if ((selector_id < 0) || (selector_id > (int)((SciEngine*)g_engine)->getKernel()->getSelectorNamesSize())) {
|
|
|
|
warning("Attempt to write to invalid selector %d of"
|
2009-12-26 14:58:23 +00:00
|
|
|
" object at %04x:%04x.", selector_id, PRINT_REG(object));
|
2009-09-17 16:53:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lookup_selector(segMan, object, selector_id, &address, NULL) != kSelectorVariable)
|
|
|
|
warning("Selector '%s' of object at %04x:%04x could not be"
|
2009-12-26 14:58:23 +00:00
|
|
|
" written to", ((SciEngine*)g_engine)->getKernel()->getSelectorName(selector_id).c_str(), PRINT_REG(object));
|
2009-09-17 16:53:58 +00:00
|
|
|
else
|
|
|
|
*address.getPointer(segMan) = value;
|
|
|
|
}
|
|
|
|
|
- Changed the unimplemented debug SCI kernel functions (InspectObj, ShowSends, ShowObjs, ShowFree, StackUsage and Profiler) to be dummy functions - we have our own debugger, and don't use these functions for debugging
- Removed the function number parameter from all kernel functions, as it's no longer needed, and removed the FAKE_FUNCT_NR hack
- Removed kUnknown() and kStub()
- Dummy/unknown kernel functions are no longer invoked, and a warning is shown instead, with the paremeters passed to them
Note: there is an evil hack used for debugging scripts in invoke_selector(), which probably no longer works now
svn-id: r44461
2009-09-29 14:24:07 +00:00
|
|
|
int invoke_selector(EngineState *s, reg_t object, int selector_id, SelectorInvocation noinvalid,
|
2009-12-26 14:58:23 +00:00
|
|
|
StackPtr k_argp, int k_argc, int argc, ...) {
|
2009-09-17 16:53:58 +00:00
|
|
|
va_list argp;
|
|
|
|
int i;
|
|
|
|
int framesize = 2 + 1 * argc;
|
|
|
|
reg_t address;
|
|
|
|
int slc_type;
|
|
|
|
StackPtr stackframe = k_argp + k_argc;
|
|
|
|
|
|
|
|
stackframe[0] = make_reg(0, selector_id); // The selector we want to call
|
|
|
|
stackframe[1] = make_reg(0, argc); // Argument count
|
|
|
|
|
2009-10-04 18:38:18 +00:00
|
|
|
slc_type = lookup_selector(s->_segMan, object, selector_id, NULL, &address);
|
2009-09-17 16:53:58 +00:00
|
|
|
|
|
|
|
if (slc_type == kSelectorNone) {
|
2009-12-26 14:58:23 +00:00
|
|
|
warning("Selector '%s' of object at %04x:%04x could not be invoked",
|
|
|
|
s->_kernel->getSelectorName(selector_id).c_str(), PRINT_REG(object));
|
2009-09-17 16:53:58 +00:00
|
|
|
if (noinvalid == kStopOnInvalidSelector)
|
|
|
|
error("[Kernel] Not recoverable: VM was halted");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (slc_type == kSelectorVariable) // Swallow silently
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(argp, argc);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
reg_t arg = va_arg(argp, reg_t);
|
|
|
|
stackframe[2 + i] = arg; // Write each argument
|
|
|
|
}
|
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
ExecStack *xstack;
|
|
|
|
|
|
|
|
// Now commit the actual function:
|
|
|
|
xstack = send_selector(s, object, object, stackframe, framesize, stackframe);
|
|
|
|
|
|
|
|
xstack->sp += argc + 2;
|
|
|
|
xstack->fp += argc + 2;
|
|
|
|
|
|
|
|
run_vm(s, 0); // Start a new vm
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _obj_locate_varselector(SegManager *segMan, Object *obj, Selector slc) {
|
|
|
|
// Determines if obj explicitly defines slc as a varselector
|
|
|
|
// Returns -1 if not found
|
|
|
|
byte *buf;
|
|
|
|
uint varnum;
|
|
|
|
|
|
|
|
if (getSciVersion() < SCI_VERSION_1_1) {
|
2009-10-10 15:58:51 +00:00
|
|
|
varnum = obj->getVarCount();
|
2009-09-17 16:53:58 +00:00
|
|
|
int selector_name_offset = varnum * 2 + SCRIPT_SELECTOR_OFFSET;
|
2009-10-11 13:46:58 +00:00
|
|
|
buf = obj->_baseObj + selector_name_offset;
|
2009-09-17 16:53:58 +00:00
|
|
|
} else {
|
|
|
|
if (!(obj->getInfoSelector().offset & SCRIPT_INFO_CLASS))
|
|
|
|
obj = segMan->getObject(obj->getSuperClassSelector());
|
|
|
|
|
2009-10-11 13:46:58 +00:00
|
|
|
buf = (byte *)obj->_baseVars;
|
2009-10-10 15:58:51 +00:00
|
|
|
varnum = obj->getVariable(1).toUint16();
|
2009-09-17 16:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < varnum; i++)
|
|
|
|
if (READ_LE_UINT16(buf + (i << 1)) == slc) // Found it?
|
|
|
|
return i; // report success
|
|
|
|
|
|
|
|
return -1; // Failed
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectorType lookup_selector(SegManager *segMan, reg_t obj_location, Selector selector_id, ObjVarRef *varp, reg_t *fptr) {
|
|
|
|
Object *obj = segMan->getObject(obj_location);
|
|
|
|
int index;
|
|
|
|
bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY);
|
|
|
|
|
|
|
|
// Early SCI versions used the LSB in the selector ID as a read/write
|
|
|
|
// toggle, meaning that we must remove it for selector lookup.
|
|
|
|
if (oldScriptHeader)
|
|
|
|
selector_id &= ~1;
|
|
|
|
|
|
|
|
if (!obj) {
|
|
|
|
error("lookup_selector(): Attempt to send to non-object or invalid script. Address was %04x:%04x",
|
|
|
|
PRINT_REG(obj_location));
|
|
|
|
}
|
|
|
|
|
|
|
|
index = _obj_locate_varselector(segMan, obj, selector_id);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
// Found it as a variable
|
|
|
|
if (varp) {
|
|
|
|
varp->obj = obj_location;
|
|
|
|
varp->varindex = index;
|
|
|
|
}
|
|
|
|
return kSelectorVariable;
|
2009-10-10 15:58:51 +00:00
|
|
|
} else {
|
|
|
|
// Check if it's a method, with recursive lookup in superclasses
|
|
|
|
while (obj) {
|
|
|
|
index = obj->funcSelectorPosition(selector_id);
|
|
|
|
if (index >= 0) {
|
|
|
|
if (fptr)
|
|
|
|
*fptr = obj->getFunction(index);
|
|
|
|
|
|
|
|
return kSelectorMethod;
|
|
|
|
} else {
|
|
|
|
obj = segMan->getObject(obj->getSuperClassSelector());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kSelectorNone;
|
2009-09-17 16:53:58 +00:00
|
|
|
}
|
|
|
|
|
2009-10-10 15:58:51 +00:00
|
|
|
|
|
|
|
// return _lookup_selector_function(segMan, obj, selector_id, fptr);
|
2009-09-17 16:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Sci
|