1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
|
/* Copyright (C) 2005 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "MGRT.h"
#include "myx_grt_builtin_module_public_interface.h"
#include "myx_grt_private.h"
#include <gtkmm/main.h>
#include "MGShellView.h"
/**
* @file MGRT.cc
* @brief
*/
static Glib::ustring make_error_message_from_result(const MGRTValue &result)
{
if (result.isValid())
{
const char *error= result["error"].isValid() ? result["error"].asString() : 0;
const char *detail= result["detail"].isValid() ? result["detail"].asString() : 0;
if (detail)
return ufmt("%s (%s)", error, detail);
else
return error;
}
return "";
}
void MGRT::Request::set_output_handler(const OutputHandlerSlot &handler)
{
_outputHandler= handler;
}
void MGRT::Request::set_message_handler(const MessageHandlerSlot &handler)
{
_messageHandler= handler;
}
void MGRT::Request::set_input_handler(const InputHandlerSlot &handler)
{
_inputHandler= handler;
}
class BlockingRequest : public MGRT::Request {
Glib::Cond _finished;
Glib::Mutex _finished_mx;
bool _ready;
public:
BlockingRequest(const Glib::ustring &module,
const Glib::ustring &function,
const MGRTValue &arguments)
{
_module= module;
_function= function;
_arguments= arguments;
_ready= false;
}
virtual bool is_async()
{
return false;
}
virtual void finish(MGRT *grt)
{
_finished_mx.lock();
_ready= true;
_finished.signal();
_finished_mx.unlock();
}
virtual bool wait()
{
_finished_mx.lock();
while (!_ready)
_finished.wait(_finished_mx);
_finished_mx.unlock();
return true;
}
virtual void execute(MGRT *grt)
{
_result= MGRTValue(myx_grt_function_get_and_call(grt->grt(),
_module.c_str(),
_function.c_str(),
0,
_arguments.grtValue(),
&_error));
}
};
MGRT::AsyncRequest::AsyncRequest(const Glib::ustring &module,
const Glib::ustring &function,
const MGRTValue &arguments,
void *user_data,
sigc::slot<void,const MGRTValue&,bool,void*> finished,
bool null_ok)
: _data(user_data), _finished(finished)
{
_module= module;
_function= function;
_arguments= arguments;
_null_ok= null_ok;
}
bool MGRT::AsyncRequest::is_async()
{
return true;
}
void MGRT::AsyncRequest::finish(MGRT *grt)
{
bool error= false;
if ((!_result.isValid() || !grt->result_is_error(_result)) && _error == MYX_GRT_NO_ERROR)
{
if (!_result.isValid() && !_null_ok)
{
grt->out_text(ufmt("Error: function %s.%s returned unexpected NULL result",
_module.c_str(), _function.c_str()));
error= true;
}
}
else
{
if (_result.isValid())
grt->out_text(ufmt("%s calling %s.%s: %s",
error == MYX_GRT_NO_ERROR ? "Error" : grt->error_text(_error).c_str(),
_module.c_str(), _function.c_str(),
myx_grt_dict_item_get_as_string(_result, "error")?:""));
else
grt->out_text(ufmt("%s calling %s.%s",
error == MYX_GRT_NO_ERROR ? "Error" : grt->error_text(_error).c_str(),
_module.c_str(), _function.c_str()));
error= true;
}
if (_finished)
_finished(error || !_result.isValid() ? _result : _result["value"], error, _data);
}
void MGRT::AsyncRequest::execute(MGRT *grt)
{
_result= MGRTValue(myx_grt_function_get_and_call(grt->grt(),
_module.c_str(),
_function.c_str(),
0,
_arguments.grtValue(),
&_error));
}
bool MGRT::AsyncRequest::wait()
{
return false;
}
MGRT::MGRT()
{
_grt= myx_grt_initialize(0);
_finishedRequestSignal.connect(sigc::mem_fun(*this,&MGRT::handle_finished_async_requests));
}
void MGRT::initialize_grt_thread(const Glib::ustring &resourcePath, const Glib::ustring &binResourcePath)
{
_thread_ready.lock();
Glib::Thread::create(sigc::bind<std::string,std::string>(sigc::mem_fun(*this, &MGRT::request_thread), resourcePath, binResourcePath),
false);
// wait until thread ready
_thread_ready.lock();
// cleanup
_thread_ready.unlock();
}
void MGRT::dump(const MGRTValue &value)
{
myx_grt_value_print(_grt, value.grtValue());
}
void MGRT::init_thread(const std::string &resourcePath, const std::string &binResourcePath)
{
MYX_GRT_MODULE_LOADER *loader;
MYX_GRT_ERROR error;
myx_grt_set_input_callback(_grt, this, process_grt_input);
myx_grt_set_output_callback(_grt, this, process_grt_output);
myx_grt_set_message_callback(_grt, this, process_grt_message);
myx_grt_set_status_query_callback(_grt, this, process_grt_status_query);
if (myx_grt_setup_shell(_grt, MYX_GRT_SHELL_LUA) != MYX_GRT_NO_ERROR)
{
out_text("could not initialize shell");
_thread_ready.unlock();
return;
}
myx_grt_shell_print_welcome(_grt);
scan_structs_in_path(resourcePath+"/grt");
// loaders
myx_register_builtin_grt_module_base(_grt);
myx_register_builtin_grt_module_reverse_engineer_mysql(_grt);
myx_register_builtin_grt_module_transformation_mysql(_grt);
#ifdef ENABLE_JAVA_MODULES
// java modules
out_text("Loading Java modules...");
loader= myx_java_init_loader(_grt,
binResourcePath.c_str(), &error, NULL,
resourcePath.c_str());
if (loader)
{
if (myx_grt_register_module_loader(_grt, loader) < 0)
out_text("Error registering Java module loader\n");
else
{
int i= myx_grt_scan_for_modules(_grt, std::string(resourcePath+"/java").c_str(), &error);
if (error != MYX_GRT_NO_ERROR)
out_text(ufmt("Error scanning Java modules (%i)\n", error));
else
out_text(ufmt("Registered %i Java modules\n", i));
}
}
else
out_text(ufmt("Could not initialize Java module loader (%i)\n",error));
flush_messages();
#endif
#ifdef ENABLE_PHP_MODULES
// php modules
out_text("Loading PHP modules...");
loader= myx_php_init_loader(_grt, &error);
if (loader)
{
if (myx_grt_register_module_loader(_grt, loader) < 0)
out_text("Error registering PHP module loader\n");
else
{
i= myx_grt_scan_for_modules(_grt,
std::string(resourcePath+"/php/modules").c_str());
if (error != MYX_GRT_NO_ERROR)
out_text(ufmt("Error scanning PHP modules (%i)\n", error));
else
out_text("Registered %i PHP modules\n", i);
}
}
else
out_text("Error initializing PHP module loader (%i)\n",error);
flush_messages();
#endif
// lua modules
out_text("Loading Lua modules...");
loader= myx_lua_init_loader(_grt, &error, std::string(resourcePath+"/lua").c_str());
if (loader)
{
if (myx_grt_register_module_loader(_grt, loader) < 0)
out_text("Error registering Lua module loader\n");
else
{
int i= myx_grt_scan_for_modules(_grt, std::string(resourcePath+"/lua").c_str(), &error);
if (error != MYX_GRT_NO_ERROR)
out_text(ufmt("Error scanning Lua modules (%i)\n", error));
else
out_text(ufmt("Registered %i Lua modules\n", i));
}
}
else
out_text(ufmt("Error initializing Lua module loader (%i)\n",error));
flush_messages();
// builtin c++
out_text("Initializing C++ loader...");
setup_loader();
flush_messages();
myx_grt_shell_init(_grt);
// this will "signal" the main thread that init is done.
// must be the last thing done in this function
_thread_ready.unlock();
}
void MGRT::flush_messages()
{
myx_grt_messages_stack_flush(_grt, 0);
}
MGRT::~MGRT()
{
queue_request(0);
myx_grt_finalize(_grt);
}
void MGRT::set_console(MGShellView *shell)
{
_console= shell;
//_console->make_ready();
}
int MGRT::process_grt_input(const char *caption, MYX_GRT_INPUT_OPTIONS options, const char **text, void *user_data)
{
MGRT *me= (MGRT*)user_data;
Glib::ustring out;
if (!me->_inputHandler(caption, options & MYX_GR_IO_PASSWORD, &out))
return -1;
*text= g_strdup(out.c_str());
return 0;
}
void MGRT::process_grt_output(const char *text, void *data)
{
MGRT *me= (MGRT*)data;
me->out_text(text);
}
void MGRT::process_grt_message(MYX_GRT_MSGS *msgs, void *data)
{
MGRT *me= (MGRT*)data;
if (msgs)
{
Glib::ustring type;
for (unsigned int i= 0; i < msgs->msgs_num; i++)
{
switch (msgs->msgs[i].msg_type)
{
case 0: type= "ERROR"; break;
case 1: type= "WARNING"; break;
default: type= "MESSAGE"; break;
}
if (msgs->msgs[i].msg_detail)
{
Glib::ustring msg= msgs->msgs[i].msg;
for (unsigned int j= 0; j < msgs->msgs[i].msg_detail->strings_num; j++)
msg+= ufmt(" %s\n", msgs->msgs[i].msg_detail->strings[j]);
me->out_message(msg, type);
}
else
me->out_message(msgs->msgs[i].msg, type);
}
}
}
int MGRT::process_grt_status_query(void *data)
{
MGRT *me= (MGRT*)data;
if (me->_statusQueryHandler)
return me->_statusQueryHandler();
return 0;
}
void MGRT::out_text(const Glib::ustring &text)
{
if (!_outputHandler.empty())
_outputHandler(text);
else if (_console)
_console->out_text(text);
if (getenv("DEBUG"))
g_message("%s", text.c_str());
}
void MGRT::out_message(const Glib::ustring &text,
const Glib::ustring &type)
{
if (getenv("DEBUG"))
g_message("%s: %s", type.c_str(), text.c_str());
if (!_messageHandler.empty())
_messageHandler(text, type);
else if (_console)
_console->out_text(type+": "+text);
}
void MGRT::perform_shell_command(const Glib::ustring &command)
{
MYX_GRT_SHELL_COMMAND error;
error= myx_grt_shell_execute(_grt, command.c_str());
myx_grt_messages_stack_flush(_grt, 0);
}
Glib::ustring MGRT::shell_prompt()
{
return myx_grt_shell_get_prompt(_grt);
}
bool MGRT::save_subtree(const char *subtree_path, const std::string &path)
{
MGRTValue subtree(global_value(subtree_path));
if (!subtree.isValid())
return true;
if (myx_grt_store_to_file(_grt,
subtree.grtValue(),
path.c_str()) == MYX_GRT_NO_ERROR)
return true;
return false;
}
bool MGRT::load_subtree(const char *subtree_path, const std::string &path)
{
MYX_GRT_VALUE *dict= 0;
if (access(path.c_str(), F_OK)==0)
dict= myx_grt_retrieve_from_file(_grt, path.c_str());
if (dict)
{
set_global_value(subtree_path, dict);
myx_grt_value_release(dict);
return true;
}
return false;
}
MGRTValue MGRT::global_app_dict()
{
MGRTValue result(global_value("/app"));
if (!result.isValid())
{
result= myx_grt_dict_new(_grt, "base.ApplicationData");
set_global_value("/app", result.grtValue());
}
return result;
}
void MGRT::load_common_options(const std::string &path)
{
if (!load_subtree("/app/commonOptions", path))
{
MGRTValue options(MGRTValue::createTypedDict());
set_global_value("/app/commonOptions", options);
}
}
bool MGRT::scan_structs_in_path(const std::string &path)
{
int i;
MYX_GRT_ERROR error;
i= myx_grt_scan_for_structs(_grt, path.c_str(), &error);
if (error != MYX_GRT_NO_ERROR)
{
out_text(ufmt("Error loading struct definition files from %s (%i)\n",path.c_str(),error));
return false;
}
else
{
if (i == 1)
out_text("Registered one struct definition\n");
else
out_text(ufmt("Registered %i struct definitions\n",i));
return true;
}
}
bool MGRT::scan_modules_in_path(const std::string &path)
{
MYX_GRT_ERROR error;
int i;
i= myx_grt_scan_for_modules(_grt, path.c_str(), &error);
if (error != MYX_GRT_NO_ERROR)
{
out_text(ufmt("Error scanning for modules in %s (%i)\n", path.c_str(), error));
return false;
}
else
{
out_text(ufmt("Registered %i modules\n", i));
return true;
}
}
void MGRT::request_thread(const std::string &resourcePath, const std::string &binResourcePath)
{
init_thread(resourcePath, binResourcePath);
for (;;)
{
Request *req;
OutputHandlerSlot outputSlot;
InputHandlerSlot inputSlot;
MessageHandlerSlot messageSlot;
_requestQueueLock.lock();
while (_requestQueue.empty())
_requestReady.wait(_requestQueueLock);
req= *_requestQueue.begin();
_requestQueue.erase(_requestQueue.begin());
_requestQueueLock.unlock();
if (!req)
break;
if (req->_inputHandler)
inputSlot= set_input_handler(req->_inputHandler);
if (req->_outputHandler)
outputSlot= set_output_handler(req->_outputHandler);
if (req->_messageHandler)
messageSlot= set_message_handler(req->_messageHandler);
req->execute(this);
if (outputSlot)
set_output_handler(outputSlot);
if (inputSlot)
set_input_handler(inputSlot);
if (messageSlot)
set_message_handler(messageSlot);
if (req->is_async())
{
_finishedRequestLock.lock();
_finishedRequestQueue.push_back(req);
_finishedRequestLock.unlock();
_finishedRequestSignal.emit();
}
else
req->finish(this);
}
}
void MGRT::queue_request(Request *req)
{
_requestQueueLock.lock();
_requestQueue.push_front(req);
_requestReady.signal();
_requestQueueLock.unlock();
}
void MGRT::handle_finished_async_requests()
{
Request *req;
_finishedRequestLock.lock();
req= *_finishedRequestQueue.begin();
_finishedRequestQueue.erase(_finishedRequestQueue.begin());
_finishedRequestLock.unlock();
req->finish(this);
delete req;
}
MGRT::AsyncRequest *MGRT::prepare_async_call(const std::string &module,
const std::string &function,
const MGRTValue &arguments,
void *data,
sigc::slot<void,const MGRTValue&,bool,void*> finished,
bool null_ok)
{
AsyncRequest *request;
_lastError= MYX_GRT_NO_ERROR;
_lastErrorMessage= "";
request= new AsyncRequest(module, function, arguments, data, finished, null_ok);
return request;
}
bool MGRT::call_async_function(const std::string &module,
const std::string &function,
const MGRTValue &arguments,
void *data,
sigc::slot<void,const MGRTValue&,bool,void*> finished,
bool null_ok)
{
AsyncRequest *request;
_lastError= MYX_GRT_NO_ERROR;
_lastErrorMessage= "";
request= new AsyncRequest(module, function, arguments, data, finished, null_ok);
queue_request(request);
return true;
}
bool MGRT::call_procedure(const std::string &module,
const std::string &procedure,
const MGRTValue &arguments)
{
MGRTValue result(call_function(module, procedure, arguments));
return result.isValid();
}
MGRTValue MGRT::call_function(const std::string &module,
const std::string &function,
const MGRTValue &arguments)
{
BlockingRequest *request;
_lastError= MYX_GRT_NO_ERROR;
_lastErrorMessage= "";
request= new BlockingRequest(module, function, arguments);
queue_request(request);
request->wait();
MGRTValue result(request->result());
MYX_GRT_ERROR error= request->error();
delete request;
myx_grt_messages_stack_flush(_grt, 0);
if ((!result.isValid() || !result_is_error(result)) && error == MYX_GRT_NO_ERROR)
{
if (result.isValid())
return result["value"];
else
return MGRTValue();
}
else
{
_lastError= error;
_lastErrorMessage= make_error_message_from_result(result);
if (result.isValid())
out_text(ufmt("%s calling %s.%s: %s",
error == MYX_GRT_NO_ERROR ? "Error" : error_text(error).c_str(),
module.c_str(), function.c_str(),
myx_grt_dict_item_get_as_string(result, "error")?:""));
else
out_text(ufmt("%s calling %s.%s",
error == MYX_GRT_NO_ERROR ? "Error" : error_text(error).c_str(),
module.c_str(), function.c_str()));
return MGRTValue();
}
}
int MGRT::call_int_function(const std::string &module,
const std::string &function,
const MGRTValue &arguments)
{
MGRTValue result(call_function(module, function, arguments));
if (result.isValid())
return result.asInt();
return 0;
}
Glib::ustring MGRT::call_string_function(const std::string &module,
const std::string &function,
const MGRTValue &arguments)
{
MGRTValue result(call_function(module, function, arguments));
if (result.isValid())
return result.asString();
return "";
}
MGRT::OutputHandlerSlot MGRT::set_output_handler(const OutputHandlerSlot &handler)
{
_handlerMutex.lock();
OutputHandlerSlot old= _outputHandler;
_outputHandler= handler;
_handlerMutex.unlock();
return old;
}
MGRT::InputHandlerSlot MGRT::set_input_handler(const InputHandlerSlot &handler)
{
_handlerMutex.lock();
InputHandlerSlot old= _inputHandler;
_inputHandler= handler;
_handlerMutex.unlock();
return old;
}
void MGRT::reset_output_handler()
{
_handlerMutex.lock();
_outputHandler.disconnect();
_handlerMutex.unlock();
}
void MGRT::set_status_query_handler(sigc::slot<int> handler)
{
_statusQueryHandler= handler;
}
MGRT::MessageHandlerSlot MGRT::set_message_handler(const MessageHandlerSlot &handler)
{
_handlerMutex.lock();
MessageHandlerSlot old= _messageHandler;
_messageHandler= handler;
_handlerMutex.unlock();
return old;
}
void MGRT::reset_message_handler()
{
_handlerMutex.lock();
_messageHandler.disconnect();
_handlerMutex.unlock();
}
void MGRT::report_error(MYX_GRT_ERROR error)
{
out_text(error_text(error));
}
MYX_GRT_ERROR MGRT::last_error()
{
return _lastError;
}
Glib::ustring MGRT::last_error_description()
{
return _lastErrorMessage;
}
bool MGRT::result_is_error(const MGRTValue &value)
{
if (value.isValid() && value["error"].isValid())
return true;
return false;
}
MGRTValue MGRT::result_value(const MGRTValue &value)
{
return value["value"];
}
void MGRT::report_error_result(const MGRTValue &result)
{
if (result.isValid() && result["error"].isValid())
myx_grt_value_print(_grt, result["error"].grtValue());
else
myx_grt_value_print(_grt, MGRTValue("no error").grtValue());
}
Glib::ustring MGRT::error_text(MYX_GRT_ERROR error)
{
switch (error)
{
case MYX_GRT_NO_ERROR: return "Success";
case MYX_GRT_INTERNAL_ERROR: return "Internal error";
case MYX_GRT_BAD_PATH: return "Invalid path";
case MYX_GRT_CANT_OPEN_FILE: return "Cannot open file";
case MYX_GRT_BAD_FUNCTION: return "Invalid function";
case MYX_GRT_DUPLICATE_ENTRY: return "Duplicate entry";
case MYX_GRT_BAD_VALUE: return "Bad value";
case MYX_GRT_BAD_DATA: return "Bad data";
case MYX_GRT_VALIDATION_ERROR: return "Validation error";
case MYX_GRT_FUNCTION_CALL_ERROR: return "Function call error";
case MYX_GRT_MODULE_INIT_ERROR: return "Module init error";
case MYX_GRT_BAD_MODULE: return "Bad module";
case MYX_GRT_UNKNOWN_MODULE_TYPE: return "Unknown module type";
case MYX_GRT_SHELL_UNSUPPORTED: return "Unsupported shell language";
case MYX_GRT_SHELL_ALREADY_LOADED: return "Internal error: shell already loaded";
case MYX_GRT_SHELL_INIT_ERROR: return "Error initializing GRT shell";
case MYX_GRT_JAVA_NOT_FOUND: return "Java Runtime not found";
case MYX_GRT_JAVA_REGISTRY_CORRUPTED: return "Java registry corrupted";
case MYX_GRT_JAVA_JRE_CANNOT_BE_LOADED: return "JRE cannot be loaded";
}
return "";
}
MGRTValue MGRT::global_value(const char *path)
{
return MGRTValue::fromGlobal(_grt, path);
}
MGRTValue MGRT::global_ref_value(const char *path)
{
return MGRTValue::refObject(_grt, MGRTValue::fromGlobal(_grt, path).asString());
}
void MGRT::set_global_value(const char *path, const MGRTValue &value)
{
myx_grt_dict_item_set_by_path(myx_grt_get_root(_grt), path, value.grtValue());
}
void MGRT::unset_global_value(const char *path)
{
char *end= strrchr(path, '/');
std::string subpath(path, end-path);
MYX_GRT_VALUE *dict;
dict= myx_grt_dict_item_get_by_path(_grt, myx_grt_get_root(_grt), subpath.c_str());
myx_grt_dict_item_del(dict, end+1);
}
//----------------------------------------------------------------------
class MGRTModuleFunctionWrapper {
public:
sigc::slot<MGRTValue,MGRTValue> function;
static MYX_GRT_ERROR call_function(MYX_GRT_FUNCTION *function, MYX_GRT_VALUE *value, MYX_GRT_VALUE **retval);
};
MYX_GRT_ERROR MGRTModuleFunctionWrapper::call_function(MYX_GRT_FUNCTION *function, MYX_GRT_VALUE *value, MYX_GRT_VALUE **retval)
{
MGRTModuleFunctionWrapper *wrapper= (MGRTModuleFunctionWrapper*)function->priv;
MGRTValue ret;
ret= wrapper->function(MGRTValue(value));
if (ret.isValid())
*retval= ret.grtValue();
else
*retval= NULL;
return MYX_GRT_NO_ERROR;
}
void MGRT::setup_loader()
{
MYX_GRT_MODULE_LOADER *loader= g_new0(MYX_GRT_MODULE_LOADER, 1);
loader->grt= _grt;
loader->loader_type= MYX_CPP_MODULE_TYPE;
loader->priv= NULL;
loader->init_module= NULL;
loader->call_function= MGRTModuleFunctionWrapper::call_function;
loader->extensions_num= 0;
loader->extensions= NULL;
myx_grt_register_module_loader(_grt, loader);
}
void MGRT::register_builtin_module(const Glib::ustring &module_name,
std::map<Glib::ustring,sigc::slot<MGRTValue,MGRTValue> > &functions)
{
MYX_GRT_MODULE *module;
MYX_GRT_ERROR error;
int count= 0;
GRT_ENTER(_grt);
// init internal module descriptor
module= g_new0(MYX_GRT_MODULE, 1);
module->loader= myx_grt_get_loader_of_type(_grt, MYX_CPP_MODULE_TYPE);
module->priv= NULL;
module->name= g_strdup(module_name.c_str());
module->path= NULL;
module->functions_num= 0;
module->functions= NULL;
for (std::map<Glib::ustring,sigc::slot<MGRTValue,MGRTValue> >::const_iterator iter= functions.begin();
iter != functions.end(); ++iter)
{
MGRTModuleFunctionWrapper *wrapper= new MGRTModuleFunctionWrapper();
wrapper->function= iter->second;
count= count+1;
module->functions= (MYX_GRT_FUNCTION*)g_realloc(module->functions, sizeof(MYX_GRT_FUNCTION)*count);
memset(module->functions+count-1, 0, sizeof(MYX_GRT_FUNCTION));
module->functions[count-1].module= module;
module->functions[count-1].name= g_strdup(iter->first.c_str());
module->functions[count-1].priv= (MYX_GRT_FUNCTION_PRIVATE*)wrapper;
}
module->functions_num= count;
module->extends= NULL;
error= myx_grt_add_module(_grt, module);
if (error == MYX_GRT_NO_ERROR)
out_text("Registration ok");
else
out_text(ufmt("C++ loader registration failed (%i)", error));
GRT_LEAVE(_grt);
}
|