initop.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Definitions for init operations.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <cstdio>
  14. #include <cstdlib>
  15. #include "initop.hpp"
  16. #include "memory.hpp"
  17. KP_DECLS_BEGIN
  18. init_op_list& init_op_list::global_ops ()
  19. {
  20. static init_op_list lst;
  21. return (lst);
  22. }
  23. bool init_op_list::call (interpreter *interp)
  24. {
  25. for (auto it = this->ops.iterator (&init_op::link); it.valid (); ++it)
  26. {
  27. int ret = it->call (interp);
  28. if (ret == init_op::result_circular)
  29. {
  30. fprintf (stderr, "Circular dependency detected "
  31. "in call to %s\n", it->name);
  32. return (false);
  33. }
  34. else if (ret == init_op::result_failed)
  35. {
  36. fprintf (stderr, "Failed to initialize %s\n", it->name);
  37. return (false);
  38. }
  39. }
  40. return (true);
  41. }
  42. interpreter *main_interp;
  43. bool khipu_init (char *base, uint32_t size)
  44. {
  45. static interpreter ip_main;
  46. main_interp = &ip_main;
  47. if (!memory_init ())
  48. {
  49. fputs ("failed to initialize memory subsystem", stderr);
  50. return (false);
  51. }
  52. if (!main_interp->init (base, size))
  53. return (false);
  54. else if (!init_op_list::global_ops().call (main_interp))
  55. {
  56. fputs ("init ops failed: ", stderr);
  57. fputs (init_op_list::global_ops().errmsg, stderr);
  58. return (false);
  59. }
  60. else if (atexit (memory_exit) != 0)
  61. {
  62. fputs ("could not install atexit handler", stderr);
  63. return (false);
  64. }
  65. gc_enable ();
  66. return (true);
  67. }
  68. KP_DECLS_END