| 1 |
#include "Interpreter.hh" |
| 2 |
|
| 3 |
#include "Command.hh" |
| 4 |
#include "CommandException.hh" |
| 5 |
#include "InterpreterOutput.hh" |
| 6 |
#include "MSXCommandController.hh" |
| 7 |
#include "TclObject.hh" |
| 8 |
|
| 9 |
#include "FileOperations.hh" |
| 10 |
#include "MSXCPUInterface.hh" |
| 11 |
#include "MSXMotherBoard.hh" |
| 12 |
#include "Setting.hh" |
| 13 |
|
| 14 |
#include "narrow.hh" |
| 15 |
#include "ranges.hh" |
| 16 |
#include "stl.hh" |
| 17 |
#include "unreachable.hh" |
| 18 |
|
| 19 |
#include <bit> |
| 20 |
#include <cstdint> |
| 21 |
#include <iostream> |
| 22 |
#include <span> |
| 23 |
#include <utility> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
|
| 27 |
namespace openmsx { |
| 28 |
|
| 29 |
|
| 30 |
namespace { |
| 31 |
struct Trace { |
| 32 |
uintptr_t id; |
| 33 |
BaseSetting* setting; |
| 34 |
}; |
| 35 |
} |
| 36 |
static std::vector<Trace> traces; |
| 37 |
static uintptr_t traceCount = 0; |
| 38 |
|
| 39 |
|
| 40 |
static int dummyClose(ClientData , Tcl_Interp* , int ) |
| 41 |
{ |
| 42 |
return 0; |
| 43 |
} |
| 44 |
static int dummyInput(ClientData , char* , |
| 45 |
int , int* ) |
| 46 |
{ |
| 47 |
return 0; |
| 48 |
} |
| 49 |
static void dummyWatch(ClientData , int ) |
| 50 |
{ |
| 51 |
} |
| 52 |
static int dummyGetHandle(ClientData , int , |
| 53 |
ClientData* ) |
| 54 |
{ |
| 55 |
return TCL_ERROR; |
| 56 |
} |
| 57 |
Tcl_ChannelType Interpreter::channelType = { |
| 58 |
"openMSX console", |
| 59 |
TCL_CHANNEL_VERSION_5, |
| 60 |
TCL_CLOSE2PROC, |
| 61 |
dummyInput, |
| 62 |
Interpreter::outputProc, |
| 63 |
nullptr, |
| 64 |
nullptr, |
| 65 |
nullptr, |
| 66 |
dummyWatch, |
| 67 |
dummyGetHandle, |
| 68 |
dummyClose, |
| 69 |
nullptr, |
| 70 |
nullptr, |
| 71 |
nullptr, |
| 72 |
nullptr, |
| 73 |
nullptr, |
| 74 |
nullptr, |
| 75 |
}; |
| 76 |
|
| 77 |
void Interpreter::init(const char* programName) const |
| 78 |
{ |
| 79 |
Tcl_FindExecutable(programName); |
| 80 |
Tcl_SetSystemEncoding(interp, "utf-8"); |
| 81 |
} |
| 82 |
|
| 83 |
Interpreter::Interpreter() |
| 84 |
: interp(Tcl_CreateInterp()) |
| 85 |
{ |
| 86 |
Tcl_Preserve(interp); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
Tcl_Channel channel = Tcl_CreateChannel(&channelType, |
| 102 |
"openMSX console", this, TCL_WRITABLE); |
| 103 |
if (channel) { |
| 104 |
Tcl_SetChannelOption(interp, channel, "-translation", "binary"); |
| 105 |
Tcl_SetChannelOption(interp, channel, "-buffering", "line"); |
| 106 |
Tcl_SetChannelOption(interp, channel, "-encoding", "utf-8"); |
| 107 |
} |
| 108 |
Tcl_SetStdChannel(channel, TCL_STDOUT); |
| 109 |
|
| 110 |
setVariable(TclObject("env(OPENMSX_USER_DATA)"), |
| 111 |
TclObject(FileOperations::getUserDataDir())); |
| 112 |
setVariable(TclObject("env(OPENMSX_SYSTEM_DATA)"), |
| 113 |
TclObject(FileOperations::getSystemDataDir())); |
| 114 |
} |
| 115 |
|
| 116 |
Interpreter::~Interpreter() |
| 117 |
{ |
| 118 |
|
| 119 |
MSXCPUInterface::cleanup(); |
| 120 |
|
| 121 |
if (!Tcl_InterpDeleted(interp)) { |
| 122 |
Tcl_DeleteInterp(interp); |
| 123 |
} |
| 124 |
Tcl_Release(interp); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
static bool scheduled = false; |
| 134 |
if (!scheduled) { |
| 135 |
scheduled = true; |
| 136 |
atexit(Tcl_Finalize); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
int Interpreter::outputProc(ClientData clientData, const char* buf, |
| 141 |
int toWrite, int* ) |
| 142 |
{ |
| 143 |
try { |
| 144 |
auto* output = static_cast<Interpreter*>(clientData)->output; |
| 145 |
std::string_view text(buf, toWrite); |
| 146 |
if (!text.empty() && output) { |
| 147 |
output->output(text); |
| 148 |
} |
| 149 |
} catch (...) { |
| 150 |
UNREACHABLE; |
| 151 |
} |
| 152 |
return toWrite; |
| 153 |
} |
| 154 |
|
| 155 |
bool Interpreter::hasCommand(zstring_view name) const |
| 156 |
{ |
| 157 |
|
| 158 |
|
| 159 |
return Tcl_FindCommand(interp, name.c_str(), nullptr, 0); |
| 160 |
} |
| 161 |
|
| 162 |
void Interpreter::registerCommand(zstring_view name, Command& command) |
| 163 |
{ |
| 164 |
auto token = Tcl_CreateObjCommand( |
| 165 |
interp, name.c_str(), commandProc, |
| 166 |
static_cast<ClientData>(&command), nullptr); |
| 167 |
command.setToken(token); |
| 168 |
} |
| 169 |
|
| 170 |
void Interpreter::unregisterCommand(Command& command) |
| 171 |
{ |
| 172 |
Tcl_DeleteCommandFromToken(interp, static_cast<Tcl_Command>(command.getToken())); |
| 173 |
} |
| 174 |
|
| 175 |
int Interpreter::commandProc(ClientData clientData, Tcl_Interp* interp, |
| 176 |
int objc, Tcl_Obj* const* objv) |
| 177 |
{ |
| 178 |
try { |
| 179 |
auto& command = *static_cast<Command*>(clientData); |
| 180 |
std::span<const TclObject> tokens( |
| 181 |
std::bit_cast<TclObject*>(const_cast<Tcl_Obj**>(objv)), |
| 182 |
objc); |
| 183 |
int res = TCL_OK; |
| 184 |
TclObject result; |
| 185 |
try { |
| 186 |
if (!command.isAllowedInEmptyMachine()) { |
| 187 |
if (const auto* controller = |
| 188 |
dynamic_cast<const MSXCommandController*>( |
| 189 |
&command.getCommandController())) { |
| 190 |
if (!controller->getMSXMotherBoard().getMachineConfig()) { |
| 191 |
throw CommandException( |
| 192 |
"Can't execute command in empty machine"); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
command.execute(tokens, result); |
| 197 |
} catch (MSXException& e) { |
| 198 |
result = e.getMessage(); |
| 199 |
res = TCL_ERROR; |
| 200 |
} |
| 201 |
Tcl_SetObjResult(interp, result.getTclObject()); |
| 202 |
return res; |
| 203 |
} catch (...) { |
| 204 |
UNREACHABLE; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
TclObject Interpreter::getCommandNames() |
| 213 |
{ |
| 214 |
return execute("openmsx::all_command_names"); |
| 215 |
} |
| 216 |
|
| 217 |
bool Interpreter::isComplete(zstring_view command) const |
| 218 |
{ |
| 219 |
return Tcl_CommandComplete(command.c_str()) != 0; |
| 220 |
} |
| 221 |
|
| 222 |
TclObject Interpreter::execute(zstring_view command) |
| 223 |
{ |
| 224 |
if (Tcl_Eval(interp, command.c_str()) != TCL_OK) { |
| 225 |
throw CommandException(Tcl_GetStringResult(interp)); |
| 226 |
} |
| 227 |
return TclObject(Tcl_GetObjResult(interp)); |
| 228 |
} |
| 229 |
|
| 230 |
TclObject Interpreter::executeFile(zstring_view filename) |
| 231 |
{ |
| 232 |
if (Tcl_EvalFile(interp, filename.c_str()) != TCL_OK) { |
| 233 |
throw CommandException(Tcl_GetStringResult(interp)); |
| 234 |
} |
| 235 |
return TclObject(Tcl_GetObjResult(interp)); |
| 236 |
} |
| 237 |
|
| 238 |
static void setVar(Tcl_Interp* interp, const TclObject& name, const TclObject& value) |
| 239 |
{ |
| 240 |
if (!Tcl_ObjSetVar2(interp, name.getTclObjectNonConst(), nullptr, |
| 241 |
value.getTclObjectNonConst(), |
| 242 |
TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG)) { |
| 243 |
|
| 244 |
std::cerr << Tcl_GetStringResult(interp) << '\n'; |
| 245 |
} |
| 246 |
} |
| 247 |
static Tcl_Obj* getVar(Tcl_Interp* interp, const TclObject& name) |
| 248 |
{ |
| 249 |
return Tcl_ObjGetVar2(interp, name.getTclObjectNonConst(), nullptr, |
| 250 |
TCL_GLOBAL_ONLY); |
| 251 |
} |
| 252 |
|
| 253 |
void Interpreter::setVariable(const TclObject& name, const TclObject& value) |
| 254 |
{ |
| 255 |
if (!Tcl_ObjSetVar2(interp, name.getTclObjectNonConst(), nullptr, |
| 256 |
value.getTclObjectNonConst(), |
| 257 |
TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG)) { |
| 258 |
throw CommandException(Tcl_GetStringResult(interp)); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
void Interpreter::setVariable(const TclObject& arrayName, const TclObject& arrayIndex, const TclObject& value) |
| 263 |
{ |
| 264 |
if (!Tcl_ObjSetVar2(interp, arrayName.getTclObjectNonConst(), arrayIndex.getTclObjectNonConst(), |
| 265 |
value.getTclObjectNonConst(), |
| 266 |
TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG)) { |
| 267 |
throw CommandException(Tcl_GetStringResult(interp)); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void Interpreter::unsetVariable(const char* name) |
| 272 |
{ |
| 273 |
Tcl_UnsetVar(interp, name, TCL_GLOBAL_ONLY); |
| 274 |
} |
| 275 |
|
| 276 |
static TclObject getSafeValue(const BaseSetting& setting) |
| 277 |
{ |
| 278 |
|
| 279 |
if (auto val = setting.getOptionalValue()) { |
| 280 |
return *val; |
| 281 |
} |
| 282 |
return TclObject(0); |
| 283 |
} |
| 284 |
void Interpreter::registerSetting(BaseSetting& variable) |
| 285 |
{ |
| 286 |
const auto& name = variable.getFullNameObj(); |
| 287 |
if (Tcl_Obj* tclVarValue = getVar(interp, name)) { |
| 288 |
|
| 289 |
try { |
| 290 |
variable.setValueDirect(TclObject(tclVarValue)); |
| 291 |
} catch (MSXException&) { |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
} |
| 300 |
} else { |
| 301 |
|
| 302 |
setVariable(name, getSafeValue(variable)); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
uintptr_t traceID = traceCount++; |
| 330 |
traces.emplace_back(Trace{.id = traceID, .setting = &variable}); |
| 331 |
Tcl_TraceVar(interp, name.getString().data(), |
| 332 |
TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS, |
| 333 |
traceProc, std::bit_cast<ClientData>(traceID)); |
| 334 |
} |
| 335 |
|
| 336 |
void Interpreter::unregisterSetting(BaseSetting& variable) |
| 337 |
{ |
| 338 |
auto it = rfind_unguarded(traces, &variable, &Trace::setting); |
| 339 |
uintptr_t traceID = it->id; |
| 340 |
traces.erase(it); |
| 341 |
|
| 342 |
const char* name = variable.getFullName().data(); |
| 343 |
Tcl_UntraceVar(interp, name, |
| 344 |
TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS, |
| 345 |
traceProc, std::bit_cast<ClientData>(traceID)); |
| 346 |
unsetVariable(name); |
| 347 |
} |
| 348 |
|
| 349 |
static BaseSetting* getTraceSetting(uintptr_t traceID) |
| 350 |
{ |
| 351 |
auto t = binary_find(traces, traceID, {}, &Trace::id); |
| 352 |
return t ? t->setting : nullptr; |
| 353 |
} |
| 354 |
|
| 355 |
#ifndef NDEBUG |
| 356 |
static std::string_view removeColonColon(std::string_view s) |
| 357 |
{ |
| 358 |
if (s.starts_with("::")) s.remove_prefix(2); |
| 359 |
return s; |
| 360 |
} |
| 361 |
#endif |
| 362 |
|
| 363 |
char* Interpreter::traceProc(ClientData clientData, Tcl_Interp* interp, |
| 364 |
const char* part1, const char* , int flags) |
| 365 |
{ |
| 366 |
try { |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
auto traceID = std::bit_cast<uintptr_t>(clientData); |
| 398 |
auto* variable = getTraceSetting(traceID); |
| 399 |
if (!variable) return nullptr; |
| 400 |
|
| 401 |
const TclObject& part1Obj = variable->getFullNameObj(); |
| 402 |
assert(removeColonColon(part1) == removeColonColon(part1Obj.getString())); |
| 403 |
|
| 404 |
static std::string static_string; |
| 405 |
if (flags & TCL_TRACE_READS) { |
| 406 |
try { |
| 407 |
setVar(interp, part1Obj, variable->getValue()); |
| 408 |
} catch (MSXException& e) { |
| 409 |
static_string = std::move(e).getMessage(); |
| 410 |
return static_string.data(); |
| 411 |
} |
| 412 |
} |
| 413 |
if (flags & TCL_TRACE_WRITES) { |
| 414 |
try { |
| 415 |
Tcl_Obj* v = getVar(interp, part1Obj); |
| 416 |
TclObject newValue(v ? v : Tcl_NewObj()); |
| 417 |
variable->setValueDirect(newValue); |
| 418 |
const TclObject& newValue2 = variable->getValue(); |
| 419 |
if (newValue != newValue2) { |
| 420 |
setVar(interp, part1Obj, newValue2); |
| 421 |
} |
| 422 |
} catch (MSXException& e) { |
| 423 |
setVar(interp, part1Obj, getSafeValue(*variable)); |
| 424 |
static_string = std::move(e).getMessage(); |
| 425 |
return static_string.data(); |
| 426 |
} |
| 427 |
} |
| 428 |
if (flags & TCL_TRACE_UNSETS) { |
| 429 |
try { |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
variable->setValueDirect(TclObject( |
| 434 |
variable->getDefaultValue())); |
| 435 |
} catch (MSXException&) { |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
} |
| 441 |
setVar(interp, part1Obj, getSafeValue(*variable)); |
| 442 |
Tcl_TraceVar(interp, part1, TCL_TRACE_READS | |
| 443 |
TCL_TRACE_WRITES | TCL_TRACE_UNSETS, |
| 444 |
traceProc, |
| 445 |
std::bit_cast<ClientData>(traceID)); |
| 446 |
} |
| 447 |
} catch (...) { |
| 448 |
UNREACHABLE; |
| 449 |
} |
| 450 |
return nullptr; |
| 451 |
} |
| 452 |
|
| 453 |
void Interpreter::createNamespace(const std::string& name) |
| 454 |
{ |
| 455 |
execute(tmpStrCat("namespace eval ", name, " {}")); |
| 456 |
} |
| 457 |
|
| 458 |
void Interpreter::deleteNamespace(const std::string& name) |
| 459 |
{ |
| 460 |
execute(tmpStrCat("namespace delete ", name)); |
| 461 |
} |
| 462 |
|
| 463 |
void Interpreter::poll() const |
| 464 |
{ |
| 465 |
|
| 466 |
Tcl_DoOneEvent(TCL_DONT_WAIT); |
| 467 |
} |
| 468 |
|
| 469 |
TclParser Interpreter::parse(std::string_view command) |
| 470 |
{ |
| 471 |
return {interp, command}; |
| 472 |
} |
| 473 |
|
| 474 |
bool Interpreter::validExpression(std::string_view expression) |
| 475 |
{ |
| 476 |
Tcl_Parse parseInfo; |
| 477 |
int result = Tcl_ParseExpr(interp, expression.data(), narrow<int>(expression.size()), &parseInfo); |
| 478 |
Tcl_FreeParse(&parseInfo); |
| 479 |
return result == TCL_OK; |
| 480 |
} |
| 481 |
|
| 482 |
std::string Interpreter::parseCommandError(std::string_view command) |
| 483 |
{ |
| 484 |
Tcl_Parse parseInfo; |
| 485 |
int result = Tcl_ParseCommand(interp, command.data(), narrow<int>(command.size()), 0, &parseInfo); |
| 486 |
Tcl_FreeParse(&parseInfo); |
| 487 |
return (result == TCL_OK) ? std::string{} : std::string(Tcl_GetStringResult(interp)); |
| 488 |
} |
| 489 |
|
| 490 |
std::string Interpreter::parseExpressionError(std::string_view expression) |
| 491 |
{ |
| 492 |
Tcl_Parse parseInfo; |
| 493 |
int result = Tcl_ParseExpr(interp, expression.data(), narrow<int>(expression.size()), &parseInfo); |
| 494 |
Tcl_FreeParse(&parseInfo); |
| 495 |
return (result == TCL_OK) ? std::string{} : std::string(Tcl_GetStringResult(interp)); |
| 496 |
} |
| 497 |
|
| 498 |
void Interpreter::wrongNumArgs(unsigned argc, std::span<const TclObject> tokens, const char* message) |
| 499 |
{ |
| 500 |
assert(argc <= tokens.size()); |
| 501 |
Tcl_WrongNumArgs(interp, narrow<int>(argc), std::bit_cast<Tcl_Obj* const*>(tokens.data()), message); |
| 502 |
|
| 503 |
throw CommandException(Tcl_GetStringResult(interp)); |
| 504 |
} |
| 505 |
|
| 506 |
} |